The following structures are available globally.

  • A type-erased derivative value.

    The AnyDerivative type forwards its operations to an arbitrary underlying base derivative value conforming to Differentiable and AdditiveArithmetic, hiding the specifics of the underlying value.

    Declaration

    public struct AnyDerivative : Differentiable & AdditiveArithmetic
  • A context that stores thread-local contextual information used by deep learning APIs such as layers.

    Use Context.local to retrieve the current thread-local context.

    Examples:

    • Set the current learning phase to training so that layers like BatchNorm will compute mean and variance when applied to inputs.
      Context.local.learningPhase = .training
    
    • Set the current learning phase to inference so that layers like Dropout will not drop out units when applied to inputs.
      Context.local.learningPhase = .inference
    

    Declaration

    public struct Context
  • A TensorFlow dynamic type value that can be created from types that conform to TensorFlowScalar.

    Declaration

    public struct TensorDataType
  • Declaration

    public struct BFloat16
  • Represents a potentially large set of elements.

    A Dataset can be used to represent an input pipeline as a collection of element tensors.

    Declaration

    public struct Dataset<Element> where Element : TensorGroup
  • The type that allows iteration over a dataset’s elements.

    Declaration

    public struct DatasetIterator<Element> where Element : TensorGroup
  • A 2-tuple-like struct that conforms to TensorGroup that represents a tuple of 2 types conforming to TensorGroup.

    Declaration

    public struct Zip2TensorGroup<T, U> : TensorGroup where T : TensorGroup, U : TensorGroup
  • 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:

    var 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(repeating: 0, shape: [3, 2])
    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
  • StringTensor is a multi-dimensional array whose elements are Strings.

    Declaration

    public struct StringTensor