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 : EuclideanDifferentiable & AdditiveArithmetic
  • A multidimensional array of elements that is a generalization of vectors and matrices to potentially higher dimensions.

    The generic parameter Scalar describes the type of scalars in the tensor (such as Int32, Float, etc).

    Declaration

    @frozen
    public struct Tensor<Scalar> : TensorProtocol where Scalar : TensorFlowScalar
  • StringTensor is a multi-dimensional array whose elements are Strings.

    Declaration

    @frozen
    public struct StringTensor
  • 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 1-D convolution layer (e.g. temporal convolution over a time-series).

    This layer creates a convolution filter that is convolved with the layer input to produce a tensor of outputs.

    Declaration

    @frozen
    public struct Conv1D<Scalar> : Layer where Scalar : TensorFlowFloatingPoint
  • A 2-D convolution layer (e.g. spatial convolution over images).

    This layer creates a convolution filter that is convolved with the layer input to produce a tensor of outputs.

    Declaration

    @frozen
    public struct Conv2D<Scalar> : Layer where Scalar : TensorFlowFloatingPoint
  • A 3-D convolution layer for spatial/spatio-temporal convolution over images.

    This layer creates a convolution filter that is convolved with the layer input to produce a tensor of outputs.

    Declaration

    @frozen
    public struct Conv3D<Scalar> : Layer where Scalar : TensorFlowFloatingPoint
  • A 2-D transposed convolution layer (e.g. spatial transposed convolution over images).

    This layer creates a convolution filter that is transpose-convolved with the layer input to produce a tensor of outputs.

    Declaration

    @frozen
    public struct TransposedConv2D<Scalar> : Layer where Scalar : TensorFlowFloatingPoint
  • A 2-D depthwise convolution layer.

    This layer creates seperable convolution filters that are convolved with the layer input to produce a tensor of outputs.

    Declaration

    @frozen
    public struct DepthwiseConv2D<Scalar> : Layer where Scalar : TensorFlowFloatingPoint
  • A layer for adding zero-padding in the temporal dimension.

    Declaration

    public struct ZeroPadding1D<Scalar> : ParameterlessLayer where Scalar : TensorFlowFloatingPoint
  • A layer for adding zero-padding in the spatial dimensions.

    Declaration

    public struct ZeroPadding2D<Scalar> : ParameterlessLayer where Scalar : TensorFlowFloatingPoint
  • A layer for adding zero-padding in the spatial/spatio-temporal dimensions.

    Declaration

    public struct ZeroPadding3D<Scalar> : ParameterlessLayer where Scalar : TensorFlowFloatingPoint
  • A 1-D separable convolution layer.

    This layer performs a depthwise convolution that acts separately on channels followed by a pointwise convolution that mixes channels.

    Declaration

    @frozen
    public struct SeparableConv1D<Scalar> : Layer where Scalar : TensorFlowFloatingPoint
  • A 2-D Separable convolution layer.

    This layer performs a depthwise convolution that acts separately on channels followed by a pointwise convolution that mixes channels.

    Declaration

    @frozen
    public struct SeparableConv2D<Scalar> : Layer where Scalar : TensorFlowFloatingPoint
  • A dropout layer.

    Dropout consists in randomly setting a fraction of input units to 0 at each update during training time, which helps prevent overfitting.

    Declaration

    @frozen
    public struct Dropout<Scalar> : ParameterlessLayer where Scalar : TensorFlowFloatingPoint
  • A flatten layer.

    A flatten layer flattens the input when applied without affecting the batch size.

    Declaration

    @frozen
    public struct Flatten<Scalar> : ParameterlessLayer where Scalar : TensorFlowFloatingPoint
  • A reshape layer.

    Declaration

    @frozen
    public struct Reshape<Scalar> : ParameterlessLayer where Scalar : TensorFlowFloatingPoint
  • A densely-connected neural network layer.

    Dense implements the operation activation(matmul(input, weight) + bias), where weight is a weight matrix, bias is a bias vector, and activation is an element-wise activation function.

    This layer also supports 3-D weight tensors with 2-D bias matrices. In this case the first dimension of both is treated as the batch size that is aligned with the first dimension of input and the batch variant of the matmul(_:_:) operation is used, thus using a different weight and bias for each element in input batch.

    Declaration

    @frozen
    public struct Dense<Scalar> : Layer where Scalar : TensorFlowFloatingPoint
  • A layer that encloses a custom differentiable function.

    Declaration

    public struct Function<Input, Output> : ParameterlessLayer where Input : Differentiable, Output : Differentiable
  • A TensorFlow dynamic type value that can be created from types that conform to TensorFlowScalar.

    Declaration

    public struct TensorDataType : Equatable
  • Declaration

    @frozen
    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

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

    Declaration

    @frozen
    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

    @frozen
    public struct Zip2TensorGroup<T, U> : TensorGroup where T : TensorGroup, U : TensorGroup
  • An embedding layer.

    Embedding is effectively a lookup table that maps indices from a fixed vocabulary to fixed-size (dense) vector representations, e.g. [[0], [3]] -> [[0.25, 0.1], [0.6, -0.2]].

    Declaration

    public struct Embedding<Scalar> : Module where Scalar : TensorFlowFloatingPoint
  • Pair of first and second moments (i.e., mean and variance).

    Note

    This is needed because tuple types are not differentiable.

    Declaration

    public struct Moments<Scalar> : Differentiable where Scalar : TensorFlowFloatingPoint
  • 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

    @dynamicCallable
    @dynamicMemberLookup
    @frozen
    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

    @frozen
    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
    @frozen
    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

    @frozen
    @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

    @frozen
    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

    @frozen
    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

    @frozen
    public struct TensorShape : ExpressibleByArrayLiteral
  • A layer that sequentially composes two other layers.

    Declaration

    public struct Sequential<Layer1: Module, Layer2: Layer>: Module
        where Layer1.Output == Layer2.Input,
              Layer1.TangentVector.VectorSpaceScalar == Layer2.TangentVector.VectorSpaceScalar
  • Declaration

    @_functionBuilder
    public struct LayerBuilder
  • TensorHandle is the type used by ops. It includes a Scalar type, which compiler internals can use to determine the datatypes of parameters when they are extracted into a tensor program.

    Declaration

    public struct TensorHandle<Scalar> where Scalar : _TensorFlowDataTypeCompatible
  • Declaration

    public struct ResourceHandle
  • Declaration

    public struct VariantHandle