The following structures are available globally.

  • Tensor is a multi-dimensional array used for computation. It is a wrapper around a TensorHandle.

    Declaration

    public struct Tensor<Scalar> : TensorProtocol where Scalar : AccelerableByTensorFlow
  • Represents a literal element for conversion to a Tensor.

    Note

    Do not use this API directly. This is implicitly created during the conversion from an array literal to a Tensor.

    Declaration

    public struct TensorElementLiteral<Scalar> : TensorProtocol
      where Scalar : AccelerableByTensorFlow
  • PythonObject represents an object in Python and supports dynamic member lookup. Any member access like object.foo will dynamically request the Python runtime for a member with the specified name in this object.

    PythonObject is passed to and returned from all Python function calls and member references. It supports standard Python arithmetic and comparison operators.

    Internally, PythonObject is implemented as a reference-counted pointer to a Python C API PyObject.

    Declaration

    @dynamicMemberLookup
    public struct PythonObject
  • A PythonObject wrapper that enables throwing method calls. Exceptions produced by Python functions are reflected as Swift errors and thrown.

    Note

    It is intentional that ThrowingPythonObject does not have the @dynamicCallable attribute because the call syntax is unintuitive: x.throwing(arg1, arg2, ...). The methods will still be named dynamicallyCall until further discussion/design.

    Declaration

    public struct ThrowingPythonObject
  • A PythonObject wrapper that enables member accesses. Member access operations return an Optional result. When member access fails, nil is returned.

    Declaration

    @dynamicMemberLookup
    public struct CheckingPythonObject
  • An interface for Python.

    PythonInterface allows interaction with Python. It can be used to import modules and dynamically access Python builtin types and functions.

    Note

    It is not intended for PythonInterface to be initialized directly. Instead, please use the global instance of PythonInterface called Python.

    Declaration

    @dynamicMemberLookup
    public struct PythonInterface
  • ShapedArray is a multi-dimensional array. It has a shape, which has type [Int] and defines the array dimensions, and uses a TensorBuffer internally as storage.

    Declaration

    public struct ShapedArray<Scalar> : _ShapedArrayProtocol
  • A contiguous slice of a ShapedArray or ShapedArraySlice instance.

    ShapedArraySlice enables fast, efficient operations on contiguous slices of ShapedArray instances. ShapedArraySlice instances do not have their own storage. Instead, they provides a view onto the storage of their base ShapedArray. ShapedArraySlice can represent two different kinds of slices: element arrays and subarrays.

    Element arrays are subdimensional elements of a ShapedArray: their rank is one less than that of their base. Element array slices are obtained by indexing a ShapedArray instance with a singular Int32 index.

    For example:

    let matrix = ShapedArray(shape: [2, 2], scalars: [0, 1, 2, 3])
    // `matrix` represents [[0, 1], [2, 3]].
    
    let element = matrix[0]
    // `element` is a `ShapedArraySlice` with shape [2]. It is an element
    // array, specifically the first element in `matrix`: [0, 1].
    
    matrix[1] = ShapedArraySlice(shape: [2], scalars: [4, 8])
    // The second element in `matrix` has been mutated.
    // `matrix` now represents [[0, 1, 4, 8]].
    

    Subarrays are a contiguous range of the elements in a ShapedArray. The rank of a subarray is the same as that of its base, but its leading dimension is the count of the slice range. Subarray slices are obtained by indexing a ShapedArray with a Range<Int32> that represents a range of elements (in the leading dimension). Methods like prefix(:) and suffix(:) that internally index with a range also produce subarray.

    For example:

    let zeros = ShapedArray(shape: [3, 2], repeating: 0)
    var matrix = ShapedArray(shape: [3, 2], scalars: Array(0..<6))
    // `zeros` represents [[0, 0], [0, 0], [0, 0]].
    // `matrix` represents [[0, 1], [2, 3], [4, 5]].
    
    let subarray = matrix.prefix(2)
    // `subarray` is a `ShapedArraySlice` with shape [2, 2]. It is a slice
    // of the first 2 elements in `matrix` and represents [[0, 1], [2, 3]].
    
    matrix[0..<2] = zeros.prefix(2)
    // The first 2 elements in `matrix` have been mutated.
    // `matrix` now represents [[0, 0], [0, 0], [4, 5]].
    

    Declaration

    public struct ShapedArraySlice<Scalar> : _ShapedArrayProtocol
  • A struct representing the shape of a tensor.

    TensorShape is a thin wrapper around an array of integers that represent shape dimensions. All tensor types use TensorShape to represent their shape.

    Declaration

    public struct TensorShape : ExpressibleByArrayLiteral
  • A sequence of pseudorandom numbers.

    Declaration

    public struct RandomNumberSequence : Sequence