Tensor

public struct Tensor<Element> : AnyTensor where Element : TensorNumeric
extension Tensor: ExpressibleByArrayLiteral
extension Tensor: CustomStringConvertible
extension Tensor: CustomDebugStringConvertible
extension Tensor: PythonConvertible where Element: NumpyScalarCompatible

Basic tensor type.

  • Declaration

    Swift

    public var storage: AnyTensorStorage { get }
  • Declaration

    Swift

    public var cTensor: UnsafeMutablePointer<ccv_nnc_tensor_t> { get }
  • Create a typed tensor from a type-erased tensor.

    Declaration

    Swift

    public init(_ tensor: AnyTensor)

    Parameters

    tensor

    A type-erased tensor.

  • Convert from a different type tensor to this tensor.

    Declaration

    Swift

    public init(from tensor: AnyTensor)

    Parameters

    tensor

    A type-erased tensor.

  • Declaration

    Swift

    public init(_ kind: DeviceKind, format: TensorFormat, shape: TensorShape)
  • Create a new uninitialized tensor.

    Declaration

    Swift

    public init(_ kind: DeviceKind, _ dimensionFormat: TensorShapeFormat)

    Parameters

    kind

    Which device this new tensor is on.

    dimensionFormat

    The format and shape of the new tensor.

  • Declaration

    Swift

    public init<S: Sequence>(
      _ sequence: S, kind: DeviceKind, format: TensorFormat, shape: TensorShape
    )
    where S.Element == Element
  • Create a new tensor and initialize with content from a sequence.

    Declaration

    Swift

    public init<S: Sequence>(
      _ sequence: S, _ kind: DeviceKind, _ dimensionFormat: TensorShapeFormat
    )
    where S.Element == Element

    Parameters

    sequence

    The sequence to initialize the new tensor with.

    dimensionFormat

    The format and shape of the new tensor.

  • Declaration

    Swift

    public init(
      _ kind: DeviceKind, format: TensorFormat, shape: TensorShape,
      unsafeMutablePointer: UnsafeMutablePointer<Element>, bindLifetimeOf: Any
    )
  • Declaration

    Swift

    @inlinable
    public subscript(indices: Int...) -> Element { get set }
  • Declaration

    Swift

    @inlinable
    public subscript(ranges: Range<Int>...) -> Tensor<Element> { get set }
  • Initialize a tensor from MjArray. This doesn’t copy the data over, rather, we simply keep the original MjArray alive. That’s also why we don’t support any data conversion. If you want to keep the resulting Tensor for later usage (rather than do the computation right now), you need to make your own copies because MjArray CANNOT be immutable and can tied to underlying MjData updates (through MjModel.forward or MjModel.step).

    Declaration

    Swift

    @inlinable
    public init(mjArray: MjArray<Element>)
  • Declaration

    Swift

    @inlinable
    public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R
  • Declaration

    Swift

    public init(_ elements: [Element])
  • Declaration

    Swift

    public init(arrayLiteral elements: Element...)
  • Declaration

    Swift

    @inlinable
    public subscript(range: Range<Int>) -> Tensor<Element> { get set }
  • Declaration

    Swift

    @inlinable
    public subscript(i0: Int, range: Range<Int>) -> Tensor<Element> { get set }
  • Declaration

    Swift

    @inlinable
    public subscript(i0: Int, i1: Int, range: Range<Int>) -> Tensor<Element> { get set }
  • Declaration

    Swift

    @inlinable
    public subscript(i0: Int, i1: Int, i2: Int, range: Range<Int>) -> Tensor<Element> { get set }
  • Declaration

    Swift

    @inlinable
    public subscript(i0: Int, i1: Int, i2: Int, i3: Int, range: Range<Int>) -> Tensor<Element> { get set }
  • Declaration

    Swift

    @inlinable
    public subscript(i0: Int, i1: Int, i2: Int, i3: Int, i4: Int, range: Range<Int>) -> Tensor<
      Element
    >
  • Declaration

    Swift

    @inlinable
    public subscript(i0: Int, i1: Int, i2: Int, i3: Int, i4: Int, i5: Int, range: Range<Int>)
      -> Tensor<Element>
  • Declaration

    Swift

    @inlinable
    public subscript(i0: Int, i1: Int, i2: Int, i3: Int, i4: Int, i5: Int, i6: Int, range: Range<Int>)
      -> Tensor<Element>
  • Declaration

    Swift

    @inlinable
    public subscript(range: UnboundedRange) -> Tensor<Element> { get set }
  • Declaration

    Swift

    @inlinable
    public subscript(i0: Int, range: UnboundedRange) -> Tensor<Element> { get set }
  • Declaration

    Swift

    @inlinable
    public subscript(i0: Int, i1: Int, range: UnboundedRange) -> Tensor<Element> { get set }
  • Declaration

    Swift

    @inlinable
    public subscript(i0: Int, i1: Int, i2: Int, range: UnboundedRange) -> Tensor<Element> { get set }
  • Declaration

    Swift

    @inlinable
    public subscript(i0: Int, i1: Int, i2: Int, i3: Int, range: UnboundedRange) -> Tensor<Element> { get set }
  • Declaration

    Swift

    @inlinable
    public subscript(i0: Int, i1: Int, i2: Int, i3: Int, i4: Int, range: UnboundedRange) -> Tensor<
      Element
    >
  • Declaration

    Swift

    @inlinable
    public subscript(i0: Int, i1: Int, i2: Int, i3: Int, i4: Int, i5: Int, range: UnboundedRange)
      -> Tensor<Element>
  • Declaration

    Swift

    @inlinable
    public subscript(i0: Int, i1: Int, i2: Int, i3: Int, i4: Int, i5: Int, i6: Int,
      range: UnboundedRange
    )
      -> Tensor<Element>
  • Move this tensor from CPU to GPU.

    Declaration

    Swift

    public func toGPU(_ ordinal: Int = 0, streamContext: StreamContext? = nil) -> Tensor<Element>

    Parameters

    ordinal

    Which GPU the new tensor will reside.

    streamContext

    Run the operation on the given stream context.

    Return Value

    A new tensor on GPU.

  • Move this tensor from GPU to CPU.

    Declaration

    Swift

    public func toCPU(streamContext: StreamContext? = nil) -> Tensor<Element>

    Parameters

    streamContext

    Run the operation on the given stream context.

    Return Value

    A new tensor on CPU.

  • Make an explicit copy of the existing tensor. Raw tensors conforms to copy-on-write semantics. But sometimes, it is better to have explicit copy for better memory management hygiene. For example, if the tensor is obtained with .rawValue from a DynamicGraph variable, keeping that tensor alive would keep the whole computation graph available. Making an explicit copy would break that chain.

    Declaration

    Swift

    public func copied() -> Tensor<Element>

    Return Value

    A new tensor copied from the existing one.

  • Declaration

    Swift

    public func reshaped(
      format: TensorFormat, shape: TensorShape, offset: TensorShape? = nil,
      strides: TensorShape? = nil
    ) -> Self
  • Create a new tensor pointing to the same memory region but with different sizes.

    Declaration

    Swift

    public func reshaped(
      _ shapeFormat: TensorShapeFormat, offset: TensorShape? = nil, strides: TensorShape? = nil
    ) -> Self

    Parameters

    shapeFormat

    New format and shape for the tensor.

    offset

    Whether offset on each shape.

    strides

    The strides on each shape.

    Return Value

    The new tensor with different format but the same memory content.

  • Create a new tensor with dimensions permuted.

    Declaration

    Swift

    public func permuted(_ indices: Int...) -> Tensor<Element>

    Parameters

    indices

    The indices for dimensions from the original tensor. For example, a [2, 3, 4] tensor with [2, 0, 1] indices will permute to a [4, 2, 3] tensor.

    Return Value

    The new tensor with dimensions permuted.

  • Declaration

    Swift

    public var description: String { get }
  • Declaration

    Swift

    public var debugDescription: String { get }

Available where Element: NumpyScalarCompatible

  • Cannot create a tensor from numpy array.

    See more

    Declaration

    Swift

    public enum NumpyScalarCompatibleError : Error
  • Initialize a tensor from numpy object. This doesn’t copy the data over, rather, we simply keep the original numpyArray alive. That’s also why we don’t support any data conversion.

    Declaration

    Swift

    public init(numpy numpyArray: PythonObject) throws
  • Make a numpy object from a typed tensor.

    Declaration

    Swift

    public func makeNumpyArray() -> PythonObject
  • Declaration

    Swift

    public var pythonObject: PythonObject { get }