public struct Tensor<Scalar> : TensorProtocol where Scalar : TensorFlowScalar

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

  • Creates a tensor with the specified shape, randomly sampling scalar values from a discrete uniform distribution.

    Declaration

    init<G: RandomNumberGenerator>(randomStandardUniform shape: TensorShape,
                                   generator: inout G)

    Parameters

    shape

    The dimensions of the tensor.

    generator

    Random number generator to use.

  • Creates a tensor with the specified shape, randomly sampling scalar values from a discrete uniform distribution, using the default random number generator.

    Declaration

    init(randomStandardUniform shape: TensorShape)

    Parameters

    shape

    The dimensions of the tensor.

  • Creates a tensor with the specified shape, randomly sampling scalar values from a uniform distribution between 0 and 1, using the default random number generator.

    Declaration

    init(
        randomUniform shape: TensorShape,
        seed: (Int64, Int64) = (Int64.random(in: Int64.min..<Int64.max),
                                Int64.random(in: Int64.min..<Int64.max))
    )

    Parameters

    shape

    The dimensions of the tensor.

    seed

    The seed value.

  • Creates a tensor with the specified shape, randomly sampling scalar values from a normal distribution, using the default random number generator.

    Declaration

    init(
        randomNormal shape: TensorShape,
        seed: (Int64, Int64) = (Int64.random(in: Int64.min..<Int64.max),
                                Int64.random(in: Int64.min..<Int64.max))
    )

    Parameters

    shape

    The dimensions of the tensor.

    seed

    The seed value.

  • Creates a tensor with the specified shape, randomly sampling scalar values from a uniform distribution between 0 and 1.

    Declaration

    init<G: RandomNumberGenerator>(randomUniform shape: TensorShape,
                                   generator: inout G)

    Parameters

    shape

    The dimensions of the tensor.

    generator

    Random number generator to use.

  • Creates a tensor with the specified shape, randomly sampling scalar values from a normal distribution.

    Declaration

    init<G: RandomNumberGenerator>(randomNormal shape: TensorShape,
                                   mean: Scalar = 0,
                                   stddev: Scalar = 1,
                                   generator: inout G)

    Parameters

    shape

    The dimensions of the tensor.

    mean

    The mean of the distribution.

    stddev

    The standard deviation of the distribution.

    generator

    Random number generator to use.

  • Creates a tensor by performing Glorot uniform initialization for the specified shape, randomly sampling scalar values from a uniform distribution between -limit and limit, generated by the default random number generator, where limit is sqrt(6 / (fanIn + fanOut)) and fanIn/fanOut represent the number of input and output features multiplied by the receptive field if present.

    Declaration

    init(glorotUniform shape: TensorShape,
         seed: (Int64, Int64) = (Int64.random(in: Int64.min..<Int64.max),
                                 Int64.random(in: Int64.min..<Int64.max)))

    Parameters

    shape

    The dimensions of the tensor.

  • Performs Glorot uniform initialization for the specified shape, creating a tensor by randomly sampling scalar values from a uniform distribution between -limit and limit, where limit is sqrt(6 / (fanIn + fanOut)) and fanIn/fanOut represent the number of input and output features multiplied by the receptive field if present.

    Declaration

    init<G>(glorotUniform shape: TensorShape, generator: inout G) where G : RandomNumberGenerator

    Parameters

    shape

    The dimensions of the tensor.

    generator

    Random number generator to use.

  • Computes dropout given a probability.

    Declaration

    func droppingOut(probability: Double) -> Tensor
  • Computes the batch normalized tensor along the specified axis.

    Specifically, returns (self - mu)/(var + epsilon) * gamma + beta where mu and var are respectively the mean and variance of self along axis.

    Declaration

    func batchNormalized(
        alongAxis axis: Int,
        offset: Tensor = Tensor(0),
        scale: Tensor = Tensor(1),
        epsilon: Scalar = 0.001
    ) -> Tensor

    Parameters

    axis

    The batch dimension.

    offset

    The offset, also known as beta.

    scale

    The scale, also known as gamma.

    epsilon

    A small value added to the denominator for numerical stability.

  • Computes a 2-D convolution using self as input, with the specified filter, strides, and padding.

    Precondition

    self must have rank 4.

    Precondition

    filter must have rank 4.

    Declaration

    func convolved2D(
        withFilter filter: Tensor,
        strides: (Int, Int, Int, Int),
        padding: Padding
    ) -> Tensor

    Parameters

    filter

    The convolution filter.

    strides

    The strides of the sliding filter for each dimension of the input.

    padding

    The padding for the operation.

  • Computes a 2-D max pooling, with the specified kernel sizes, strides, and padding.

    Declaration

    func maxPooled(
        kernelSize: (Int, Int, Int, Int),
        strides: (Int, Int, Int, Int),
        padding: Padding
    ) -> Tensor

    Parameters

    kernelSize

    The dimensions of the pooling kernel.

    strides

    The strides of the sliding filter for each dimension of the input.

    padding

    The padding for the operation.

  • Computes a 2-D average pooling, with the specified kernel sizes, strides, and padding.

    Declaration

    func averagePooled(
        kernelSize: (Int, Int, Int, Int),
        strides: (Int, Int, Int, Int),
        padding: Padding
    ) -> Tensor

    Parameters

    kernelSize

    The dimensions of the pooling kernel.

    strides

    The strides of the sliding filter for each dimension of the input.

    padding

    The padding for the operation.

  • Perform an element-wise type conversion from a Bool tensor.

    Declaration

    init(_ other: Tensor<Bool>)
  • A scalar zero tensor.

    Declaration

    public static var zero: Tensor { get }
  • Adds two tensors and produces their sum.

    Note

    + supports broadcasting.

    Declaration

    public static func + (lhs: Tensor, rhs: Tensor) -> Tensor
  • Subtracts one tensor from another and produces their difference.

    Note

    - supports broadcasting.

    Declaration

    public static func - (lhs: Tensor, rhs: Tensor) -> Tensor
  • Multiplies the scalar with every scalar of the tensor and produces the product.

    Declaration

    public static func * (lhs: Scalar, rhs: Tensor) -> Tensor
  • Adds the scalar to every scalar of the tensor and produces the sum.

    Declaration

    static func + (lhs: Scalar, rhs: Tensor) -> Tensor
  • Adds the scalar to every scalar of the tensor and produces the sum.

    Declaration

    static func + (lhs: Tensor, rhs: Scalar) -> Tensor
  • Subtracts the scalar from every scalar of the tensor and produces the difference.

    Declaration

    static func - (lhs: Scalar, rhs: Tensor) -> Tensor
  • Subtracts the scalar from every scalar of the tensor and produces the difference.

    Declaration

    static func - (lhs: Tensor, rhs: Scalar) -> Tensor
  • Adds two tensors and stores the result in the left-hand-side variable.

    Note

    += supports broadcasting.

    Declaration

    static func += (lhs: inout Tensor, rhs: Tensor)
  • Adds the scalar to every scalar of the tensor and stores the result in the left-hand-side variable.

    Declaration

    static func += (lhs: inout Tensor, rhs: Scalar)
  • Subtracts the second tensor from the first and stores the result in the left-hand-side variable.

    Note

    -= supports broadcasting.

    Declaration

    static func -= (lhs: inout Tensor, rhs: Tensor)
  • Subtracts the scalar from every scalar of the tensor and stores the result in the left-hand-side variable.

    Declaration

    static func -= (lhs: inout Tensor, rhs: Scalar)
  • Multiplies two tensors and produces their product.

    Note

    * supports broadcasting.

    Declaration

    static func * (lhs: Tensor, rhs: Tensor) -> Tensor
  • Multiplies the scalar with every scalar of the tensor and produces the product.

    Declaration

    static func * (lhs: Tensor, rhs: Scalar) -> Tensor
  • Multiplies two tensors and stores the result in the left-hand-side variable.

    Note

    *= supports broadcasting.

    Declaration

    static func *= (lhs: inout Tensor, rhs: Tensor)
  • Declaration

    static func *= (lhs: inout Tensor, rhs: Scalar)
  • Returns the quotient of dividing the first tensor by the second.

    Note

    / supports broadcasting.

    Declaration

    static func / (lhs: Tensor, rhs: Tensor) -> Tensor
  • Returns the quotient of dividing the scalar by the tensor, broadcasting the scalar.

    Declaration

    static func / (lhs: Scalar, rhs: Tensor) -> Tensor
  • Returns the quotient of dividing the tensor by the scalar, broadcasting the scalar.

    Declaration

    static func / (lhs: Tensor, rhs: Scalar) -> Tensor
  • Divides the first tensor by the second and stores the quotient in the left-hand-side variable.

    Declaration

    static func /= (lhs: inout Tensor, rhs: Tensor)
  • Divides the tensor by the scalar, broadcasting the scalar, and stores the quotient in the left-hand-side variable.

    Declaration

    static func /= (lhs: inout Tensor, rhs: Scalar)
  • Returns the remainder of dividing the first tensor by the second.

    Note

    % supports broadcasting.

    Declaration

    static func % (lhs: Tensor, rhs: Tensor) -> Tensor
  • Returns the remainder of dividing the tensor by the scalar, broadcasting the scalar.

    Declaration

    static func % (lhs: Tensor, rhs: Scalar) -> Tensor
  • Returns the remainder of dividing the scalar by the tensor, broadcasting the scalar.

    Declaration

    static func % (lhs: Scalar, rhs: Tensor) -> Tensor
  • Divides the first tensor by the second and stores the remainder in the left-hand-side variable.

    Declaration

    static func %= (lhs: inout Tensor, rhs: Tensor)
  • Divides the tensor by the scalar and stores the remainder in the left-hand-side variable.

    Declaration

    static func %= (lhs: inout Tensor, rhs: Scalar)
  • Performs matrix multiplication between two tensors and produces the result.

    Declaration

    static func  (lhs: Tensor, rhs: Tensor) -> Tensor
  • Computes lhs < rhs element-wise and returns a Tensor of Boolean scalars.

    Declaration

    static func .< (lhs: Tensor, rhs: Tensor) -> Tensor<Bool>
  • Computes lhs <= rhs element-wise and returns a Tensor of Boolean scalars.

    Declaration

    static func .<= (lhs: Tensor, rhs: Tensor) -> Tensor<Bool>
  • Computes lhs > rhs element-wise and returns a Tensor of Boolean scalars.

    Declaration

    static func .> (lhs: Tensor, rhs: Tensor) -> Tensor<Bool>
  • Computes lhs >= rhs element-wise and returns a Tensor of Boolean scalars.

    Declaration

    static func .>= (lhs: Tensor, rhs: Tensor) -> Tensor<Bool>
  • Computes lhs < rhs element-wise and returns a Tensor of Boolean scalars.

    Note

    .< supports broadcasting.

    Declaration

    static func .< (lhs: Scalar, rhs: Tensor) -> Tensor<Bool>
  • Computes lhs <= rhs element-wise and returns a Tensor of Boolean scalars.

    Note

    .<= supports broadcasting.

    Declaration

    static func .<= (lhs: Scalar, rhs: Tensor) -> Tensor<Bool>
  • Computes lhs > rhs element-wise and returns a Tensor of Boolean scalars.

    Note

    .> supports broadcasting.

    Declaration

    static func .> (lhs: Scalar, rhs: Tensor) -> Tensor<Bool>
  • Computes lhs >= rhs element-wise and returns a Tensor of Boolean scalars.

    Note

    .>= supports broadcasting.

    Declaration

    static func .>= (lhs: Scalar, rhs: Tensor) -> Tensor<Bool>
  • Computes lhs < rhs element-wise and returns a Tensor of Boolean scalars.

    Note

    .< supports broadcasting.

    Declaration

    static func .< (lhs: Tensor, rhs: Scalar) -> Tensor<Bool>
  • Computes lhs <= rhs element-wise and returns a Tensor of Boolean scalars.

    Note

    .<= supports broadcasting.

    Declaration

    static func .<= (lhs: Tensor, rhs: Scalar) -> Tensor<Bool>
  • Computes lhs > rhs element-wise and returns a Tensor of Boolean scalars.

    Note

    .> supports broadcasting.

    Declaration

    static func .> (lhs: Tensor, rhs: Scalar) -> Tensor<Bool>
  • Computes lhs >= rhs element-wise and returns a Tensor of Boolean scalars.

    Note

    .>= supports broadcasting.

    Declaration

    static func .>= (lhs: Tensor, rhs: Scalar) -> Tensor<Bool>
  • Returns a Boolean value indicating whether the value of the first argument is lexicographically less than that of the second argument.

    Declaration

    public static func < (lhs: Tensor, rhs: Tensor) -> Bool
  • Returns a Boolean value indicating whether the value of the first argument is lexicographically less than or equal to that of the second argument.

    Declaration

    public static func <= (lhs: Tensor, rhs: Tensor) -> Bool
  • Returns a Boolean value indicating whether the value of the first argument is lexicographically greater than that of the second argument.

    Declaration

    public static func > (lhs: Tensor, rhs: Tensor) -> Bool
  • Returns a Boolean value indicating whether the value of the first argument is lexicographically greater than or equal to that of the second argument.

    Declaration

    public static func >= (lhs: Tensor, rhs: Tensor) -> Bool
  • Returns a Boolean value indicating whether the value of the first argument is lexicographically less than that of the second argument.

    Declaration

    static func < (lhs: Tensor, rhs: Scalar) -> Bool
  • Returns a Boolean value indicating whether the value of the first argument is lexicographically less than or equal to that of the second argument.

    Declaration

    static func <= (lhs: Tensor, rhs: Scalar) -> Bool
  • Returns a Boolean value indicating whether the value of the first argument is lexicographically greater than that of the second argument.

    Declaration

    static func > (lhs: Tensor, rhs: Scalar) -> Bool
  • Returns a Boolean value indicating whether the value of the first argument is lexicographically greater than or equal to that of the second argument.

    Declaration

    static func >= (lhs: Tensor, rhs: Scalar) -> Bool
  • Computes lhs != rhs element-wise and returns a Tensor of Boolean scalars.

    Note

    .== supports broadcasting.

    Declaration

    static func .== (lhs: Tensor, rhs: Tensor) -> Tensor<Bool>
  • Computes lhs != rhs element-wise and returns a Tensor of Boolean scalars.

    Note

    .!= supports broadcasting.

    Declaration

    static func .!= (lhs: Tensor, rhs: Tensor) -> Tensor<Bool>
  • Computes lhs == rhs element-wise and returns a Tensor of Boolean scalars.

    Note

    .== supports broadcasting.

    Declaration

    static func .== (lhs: Scalar, rhs: Tensor) -> Tensor<Bool>
  • Computes lhs != rhs element-wise and returns a Tensor of Boolean scalars.

    Note

    .!= supports broadcasting.

    Declaration

    static func .!= (lhs: Scalar, rhs: Tensor) -> Tensor<Bool>
  • Computes lhs == rhs element-wise and returns a Tensor of Boolean scalars.

    Note

    .== supports broadcasting.

    Declaration

    static func .== (lhs: Tensor, rhs: Scalar) -> Tensor<Bool>
  • Computes lhs != rhs element-wise and returns a Tensor of Boolean scalars.

    Note

    .!= supports broadcasting.

    Declaration

    static func .!= (lhs: Tensor, rhs: Scalar) -> Tensor<Bool>
  • Returns a Tensor of Boolean values indicating whether the elements of self are approximately equal to those of other.

    Declaration

    func elementsApproximatelyEqual(_ other: Tensor,
                                    tolerance: Double = 0.00001) -> Tensor<Bool>
  • Computes !self element-wise.

    Declaration

    func elementsLogicalNot() -> Tensor
  • Computes self && other element-wise.

    Note

    && supports broadcasting.

    Declaration

    func elementsLogicalAnd(_ other: Tensor) -> Tensor
  • Computes self && other element-wise, broadcasting other.

    Declaration

    func elementsLogicalAnd(_ other: Scalar) -> Tensor
  • Computes self || other element-wise.

    Declaration

    func elementsLogicalOr(_ other: Tensor) -> Tensor
  • Computes self || other element-wise, broadcasting other.

    Declaration

    func elementsLogicalOr(_ other: Scalar) -> Tensor
  • Returns a transposed tensor, with dimensions permuted in the specified order.

    Declaration

    func transposed(
      withPermutations permutations: Tensor<Int32>
    ) -> Tensor
  • Returns a transposed tensor, with dimensions permuted in the specified order.

    Declaration

    func transposed(withPermutations permutations: [Int]) -> Tensor
  • Returns a transposed tensor, with dimensions permuted in the specified order.

    Declaration

    func transposed(withPermutations permutations: Int...) -> Tensor
  • Returns a transposed tensor, with dimensions permuted in reverse order.

    Declaration

    func transposed() -> Tensor
  • Returns a concatenated tensor of the given tensors.

    Precondition

    The tensors must have the same dimensions, except for the specified axis.

    Precondition

    The axis must be in the range -rank..<rank.

    Declaration

    init(concatenating tensors: [Tensor<Scalar>], alongAxis axis: Int = 0)
  • Concatenates tensors along the specified axis.

    Precondition

    The tensors must have the same dimensions, except for the specified axis.

    Precondition

    The axis must be in the range -rank..<rank.

    Declaration

    func concatenated(with other: Tensor, alongAxis axis: Int = 0) -> Tensor
  • Concatenation operator.

    Note

    ++ is a custom operator that does not exist in Swift, but does in Haskell/Scala. Its addition is not an insignificant language change and may be controversial. The existence/naming of ++ will be discussed during a later API design phase.

    Declaration

    static func ++ (lhs: Tensor, rhs: Tensor) -> Tensor
  • Computes the negation of the specified tensor element-wise.

    Declaration

    prefix static func - (rhs: Tensor) -> Tensor
  • Declaration

    func squared() -> Tensor
  • Returns a new tensor containing elements from either left or right, depending on the elements of self.

    self acts as a mask that chooses, based on the value at each scalar, whether the corresponding scalar in the output should be taken from left (if true) or right (if false).

    Precondition

    left and right must have the same shape. If left and right are scalar, then self must also be scalar. If left and right have rank greater than or equal to 1, then self must be either have the same shape as left or be a 1-D Tensor such that self.scalarCount == left[0].

    Declaration

    @available(*, deprecated, message: "Use '.replacing(with:mask:﹚' instead")
    func selecting<T>(_ left: Tensor<T>, _ right: Tensor<T>) -> Tensor<T> where T : TensorFlowScalar
  • Replaces elements of this tensor with other in the lanes where mask is true.

    Precondition

    self and other must have the same shape. If self and other are scalar, then mask must also be scalar. If self and other have rank greater than or equal to 1, then mask must be either have the same shape as self or be a 1-D Tensor such that mask.scalarCount == self.shape[0].

    Declaration

    func replacing(with other: Tensor,
                   where mask: Tensor<Bool>) -> Tensor
  • Returns true if all scalars are equal to true. Otherwise, returns false.

    Declaration

    func all() -> Bool
  • Returns true if any scalars are equal to true. Otherwise, returns false.

    Declaration

    func any() -> Bool
  • Performs a logical AND operation along the specified axes. The reduced dimensions are removed.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func all(squeezingAxes axes: Int...) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Performs a logical AND operation along the specified axes. The reduced dimensions are removed.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func any(squeezingAxes axes: Int...) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Performs a logical AND operation along the specified axes. The reduced dimensions are retained with value 1.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func all(alongAxes axes: Int...) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Performs a logical OR operation along the specified axes. The reduced dimensions are retained with value 1.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func any(alongAxes axes: Int...) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Declaration

    func min() -> Tensor
  • Declaration

    func max() -> Tensor
  • Returns the maximum values along the specified axes. The reduced dimensions are removed.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func max(squeezingAxes axes: [Int]) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the maximum values along the specified axes. The reduced dimensions are removed.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func max(squeezingAxes axes: Int...) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the minimum values along the specified axes. The reduced dimensions are removed.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func min(squeezingAxes axes: [Int]) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the minimum values along the specified axes. The reduced dimensions are removed.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func min(squeezingAxes axes: Int...) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the indices of the maximum values along the specified axes. The reduced dimensions are removed.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func argmax(squeezingAxis axis: Int) -> Tensor<Int32>

    Parameters

    axes

    The dimensions to reduce.

  • Returns the indices of the minimum values along the specified axes. The reduced dimensions are removed.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func argmin(squeezingAxis axis: Int) -> Tensor<Int32>

    Parameters

    axes

    The dimensions to reduce.

  • Returns the minimum along the specified axes. The reduced dimensions are retained with value 1.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func min(alongAxes axes: [Int]) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the minimum along the specified axes. The reduced dimensions are retained with value 1.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func min(alongAxes axes: Int...) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the minimum along the specified axes. The reduced dimensions are retained with value 1.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func max(alongAxes axes: [Int]) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the minimum along the specified axes. The reduced dimensions are retained with value 1.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func max(alongAxes axes: Int...) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the index of the maximum value of the flattened scalars.

    Declaration

    func argmax() -> Tensor<Int32>
  • Returns the index of the minimum value of the flattened scalars.

    Declaration

    func argmin() -> Tensor<Int32>
  • Returns the sum along the specified axes. The reduced dimensions are removed.

    Precondition

    Each value in axes must be in the range -rank...rank.

    Declaration

    func sum(squeezingAxes axes: Tensor<Int32>) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the sum along the specified axes. The reduced dimensions are removed.

    Precondition

    Each value in axes must be in the range -rank...rank.

    Declaration

    func sum(squeezingAxes axes: [Int]) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the sum along the specified axes. The reduced dimensions are removed.

    Precondition

    Each value in axes must be in the range -rank...rank.

    Declaration

    func sum(squeezingAxes axes: Int...) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Declaration

    func sum() -> Tensor
  • Returns the sum along the specified axes. The reduced dimensions are retained with value 1.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func sum(alongAxes axes: Tensor<Int32>) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the sum along the specified axes. The reduced dimensions are retained with value 1.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func sum(alongAxes axes: [Int]) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the sum along the specified axes. The reduced dimensions are retained with value 1.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func sum(alongAxes axes: Int...) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the product along the specified axes. The reduced dimensions are removed.

    Precondition

    Each value in axes must be in the range -rank...rank.

    Declaration

    func product(squeezingAxes axes: Tensor<Int32>) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the product along the specified axes. The reduced dimensions are removed.

    Precondition

    Each value in axes must be in the range -rank...rank.

    Declaration

    func product(squeezingAxes axes: [Int]) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the product along the specified axes. The reduced dimensions are removed.

    Precondition

    Each value in axes must be in the range -rank...rank.

    Declaration

    func product(squeezingAxes axes: Int...) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Declaration

    func product() -> Tensor
  • Returns the product along the specified axes. The reduced dimensions are retained with value 1.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func product(alongAxes axes: Tensor<Int32>) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the product along the specified axes. The reduced dimensions are retained with value 1.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func product(alongAxes axes: [Int]) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the product along the specified axes. The reduced dimensions are retained with value 1.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func product(alongAxes axes: Int...) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the arithmetic mean along the specified axes. The reduced dimensions are removed.

    Precondition

    Each value in axes must be in the range -rank...rank.

    Declaration

    func mean(squeezingAxes axes: Tensor<Int32>) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the arithmetic mean along the specified axes. The reduced dimensions are removed.

    Precondition

    Each value in axes must be in the range -rank...rank.

    Declaration

    func mean(squeezingAxes axes: [Int]) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the arithmetic mean along the specified axes. The reduced dimensions are removed.

    Precondition

    Each value in axes must be in the range -rank...rank.

    Declaration

    func mean(squeezingAxes axes: Int...) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Declaration

    func mean() -> Tensor
  • Returns the arithmetic mean along the specified axes. The reduced dimensions are retained with value 1.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func mean(alongAxes axes: Tensor<Int32>) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the arithmetic mean along the specified axes. The reduced dimensions are retained with value 1.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func mean(alongAxes axes: [Int]) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the arithmetic mean along the specified axes. The reduced dimensions are retained with value 1.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func mean(alongAxes axes: Int...) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the variance along the specified axes. The reduced dimensions are removed. Does not apply Bessel’s correction.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func variance(squeezingAxes axes: Tensor<Int32>) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the variance along the specified axes. The reduced dimensions are removed. Does not apply Bessel’s correction.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func variance(squeezingAxes axes: [Int]) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the variance along the specified axes. The reduced dimensions are retained with value 1. Does not apply Bessel’s correction.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func variance(squeezingAxes axes: Int...) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Declaration

    func variance() -> Tensor
  • Returns the variance along the specified axes. The reduced dimensions are retained with value 1. Does not apply Bessel’s correction.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func variance(alongAxes axes: Tensor<Int32>) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the variance along the specified axes. The reduced dimensions are retained with value 1. Does not apply Bessel’s correction.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func variance(alongAxes axes: [Int]) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the variance along the specified axes. The reduced dimensions are retained with value 1. Does not apply Bessel’s correction.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func variance(alongAxes axes: Int...) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the standard deviation of the elements along the specified axes. The reduced dimensions are retained with value 1. Does not apply Bessel’s correction.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func standardDeviation(squeezingAxes axes: Tensor<Int32>) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the standard deviation of the elements along the specified axes. The reduced dimensions are retained with value 1. Does not apply Bessel’s correction.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func standardDeviation(squeezingAxes axes: [Int]) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the standard deviation of the elements along the specified axes. The reduced dimensions are retained with value 1. Does not apply Bessel’s correction.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func standardDeviation(squeezingAxes axes: Int...) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the standard deviation of the elements along the specified axes. The reduced dimensions are retained with value 1. Does not apply Bessel’s correction.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func standardDeviation() -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the standard deviation of the elements along the specified axes. The reduced dimensions are retained with value 1. Does not apply Bessel’s correction.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func standardDeviation(alongAxes axes: Tensor<Int32>) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the standard deviation of the elements along the specified axes. The reduced dimensions are retained with value 1. Does not apply Bessel’s correction.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func standardDeviation(alongAxes axes: [Int]) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • Returns the standard deviation of the elements along the specified axes. The reduced dimensions are retained with value 1. Does not apply Bessel’s correction.

    Precondition

    Each value in axes must be in the range -rank..<rank.

    Declaration

    func standardDeviation(alongAxes axes: Int...) -> Tensor

    Parameters

    axes

    The dimensions to reduce.

  • The rank of the tensor, represented as a Tensor<Int32>.

    Declaration

    var rankTensor: Tensor<Int32> { get }
  • The dimensions of the tensor, represented as a Tensor<Int32>.

    Declaration

    var shapeTensor: Tensor<Int32> { get }
  • The number of scalars in the tensor, represented as a Tensor<Int32>.

    Declaration

    var scalarCountTensor: Tensor<Int32> { get }
  • Declaration

    func broadcast(toShape shape: Tensor<Int32>) -> Tensor
  • Declaration

    func broadcast(to shape: TensorShape) -> Tensor
  • Broadcast to the same shape as the specified Tensor.

    Precondition

    The specified shape must be compatible for broadcasting.

    Declaration

    func broadcast<OtherScalar>(like other: Tensor<OtherScalar>) -> Tensor where OtherScalar : TensorFlowScalar
  • Returns a padded tensor according to the specified padding sizes.

    Declaration

    func padded(
      forSizes sizes: [(before: Int, after: Int)],
      with value: Scalar = 0
    ) -> Tensor
  • Extracts a slice from the tensor defined by lower and upper bounds for each dimension.

    Declaration

    func slice(lowerBounds: [Int], upperBounds: [Int]) -> Tensor

    Parameters

    lowerBounds

    The lower bounds at each dimension.

    upperBounds

    The upper bounds at each dimension.

  • Declaration

    func slice(lowerBounds: Tensor<Int32>, sizes: Tensor<Int32>) -> Tensor
  • Creates a tensor with the same shape and scalars as the specified numpy.ndarray instance.

    Precondition

    The numpy Python package must be installed.

    Declaration

    public init?(numpy numpyArray: PythonObject)

    Parameters

    numpyArray

    The numpy.ndarray instance to convert.

    Return Value

    numpyArray converted to an Array. Returns nil if numpyArray does not have a compatible scalar dtype.

  • Creates a numpy.ndarray instance with the same shape and scalars as this tensor.

    Precondition

    The numpy Python package must be installed.

    Declaration

    public func makeNumpyArray() -> PythonObject
  • Mark memory transfer to accelerator.

    Declaration

    func toAccelerator(shape: TensorShape) -> Tensor

    Parameters

    shape

    When sending the tensor to a TF XLA device (including TPU), must specify the tensor shape as required by XLA compilation.

  • Mark memory transfer to accelerator.

    Declaration

    func toAccelerator() -> Tensor
  • Mark memory transfer to host.

    Declaration

    func toHost(shape: TensorShape) -> Tensor

    Parameters

    shape

    When sending the tensor to a TF XLA device (including TPU), must specify the tensor shape as required by XLA compilation.

  • Mark memory transfer to host.

    Declaration

    func toHost() -> Tensor
  • Perform an element-wise conversion from another Tensor.

    Declaration

    init<OtherScalar>(_ other: Tensor<OtherScalar>) where OtherScalar : Numeric, OtherScalar : TensorFlowScalar
  • Creates a tensor from a scalar value.

    Declaration

    init(_ value: Scalar)
  • Creates a tensor from an array of tensors (which may themselves be scalars).

    Declaration

    init(_ elements: [Tensor])
  • Creates a 1D tensor from contiguous scalars.

    Declaration

    init(_ vector: [Scalar])

    Parameters

    vector

    The scalar contents of the tensor.

  • Creates a 1D tensor from contiguous scalars.

    Declaration

    init<C>(_ vector: C) where Scalar == C.Element, C : RandomAccessCollection

    Parameters

    vector

    The scalar contents of the tensor.

  • Creates a tensor with the specified shape and contiguous scalars in row-major order.

    Precondition

    The number of scalars must equal the product of the dimensions of the shape.

    Declaration

    init(shape: TensorShape, scalars: [Scalar])

    Parameters

    shape

    The shape of the tensor.

    scalars

    The scalar contents of the tensor.

  • Creates a tensor with the specified shape and contiguous scalars in row-major order.

    Precondition

    The number of scalars must equal the product of the dimensions of the shape.

    Declaration

    init(shape: TensorShape, scalars: UnsafeBufferPointer<Scalar>)

    Parameters

    shape

    The shape of the tensor.

    scalars

    The scalar contents of the tensor.

  • Creates a tensor with the specified shape and contiguous scalars in row-major order.

    Precondition

    The number of scalars must equal the product of the dimensions of the shape.

    Declaration

    init<C : RandomAccessCollection>(shape: TensorShape, scalars: C)
      where C.Element == Scalar

    Parameters

    shape

    The shape of the tensor.

    scalars

    The scalar contents of the tensor.

  • Creates a tensor with the specified shape and a single, repeated scalar value.

    Declaration

    @available(*, deprecated, renamed: "init(repeating:shape:﹚")
    init(shape: TensorShape, repeating repeatedValue: Scalar)

    Parameters

    shape

    The dimensions of the tensor.

    repeatedValue

    The scalar value to repeat.

  • Creates a tensor with the specified shape and a single, repeated scalar value.

    Declaration

    init(repeating repeatedValue: Scalar, shape: TensorShape)

    Parameters

    repeatedValue

    The scalar value to repeat.

    shape

    The dimensions of the tensor.

  • Creates a tensor by broadcasting the given scalar to a given rank with all dimensions being 1.

    Declaration

    init(broadcasting scalar: Scalar, rank: Int)
  • The type of the elements of an array literal.

    Declaration

    public typealias ArrayLiteralElement = _TensorElementLiteral<Scalar>
  • Creates a tensor initialized with the given elements.

    Declaration

    public init(arrayLiteral elements: _TensorElementLiteral<Scalar>...)
  • The number of dimensions of the Tensor.

    Declaration

    var rank: Int { get }
  • The dimensions of the Tensor.

    Declaration

    var shape: TensorShape { get }
  • The number of scalars in the Tensor.

    Declaration

    var scalarCount: Int { get }
  • Creates a tensor with all scalars set to zero.

    Declaration

    init(zeros shape: TensorShape)

    Parameters

    shape

    The dimensions of the tensor.

  • Creates a tensor with all scalars set to one.

    Declaration

    init(ones shape: TensorShape)

    Parameters

    shape

    The dimensions of the tensor.

  • Creates a 1-D tensor representing a sequence from a starting value to, but not including, an end value, stepping by the specified amount.

    Declaration

    init(rangeFrom start: Scalar, to end: Scalar, stride: Scalar)

    Parameters

    start

    The starting value to use for the sequence. If the sequence contains any values, the first one is start.

    end

    An end value to limit the sequence. end is never an element of the resulting sequence.

    stride

    The amount to step by with each iteration. stride must be positive.

  • Creates a one-hot tensor at given indices. The locations represented by indices take value onValue (1 by default), while all other locations take value offValue (0 by default). If the input indices is rank n, the new tensor will have rank n+1. The new axis is created at dimension axis (by default, the new axis is appended at the end).

    If indices is a scalar, the new tensor’s shape will be a vector of length depth.

    If indices is a vector of length features, the output shape will be: features x depth, if axis == -1 depth x features, if axis == 0

    If indices is a matrix (batch) with shape [batch, features], the output shape will be: batch x features x depth, if axis == -1 batch x depth x features, if axis == 1 depth x batch x features, if axis == 0

    Declaration

    init(oneHotAtIndices indices: Tensor<Int32>, depth: Int,
         onValue: Scalar = 1, offValue: Scalar = 0, axis: Int = -1)

    Parameters

    indices

    A Tensor of indices.

    depth

    A scalar defining the depth of the one hot dimension.

    onValue

    A scalar defining the value at the location referred to by some index in indices.

    offValue

    A scalar defining the value at a location that is not referred to by any index in indices.

    axis

    The axis to fill. The default is -1, a new inner-most axis.

  • Reshape to the shape of the specified Tensor.

    Precondition

    The number of scalars matches the new shape.

    Declaration

    func reshaped<T>(like other: Tensor<T>) -> Tensor where T : TensorFlowScalar
  • Reshape to the specified shape.

    Precondition

    The number of scalars matches the new shape.

    Declaration

    func reshaped(to newShape: TensorShape) -> Tensor
  • Reshape to the specified Tensor representing a shape.

    Precondition

    The number of scalars matches the new shape.

    Declaration

    func reshaped(toShape newShape: Tensor<Int32>) -> Tensor
  • Return a copy of the tensor collapsed into a 1-D Tensor, in row-major order.

    Declaration

    func flattened() -> Tensor
  • Returns a rank-lifted Tensor with a leading dimension of 1.

    Declaration

    func rankLifted() -> Tensor
  • Returns a shape-expanded Tensor, with a dimension of 1 inserted at the specified shape index.

    Declaration

    func expandingShape(at shapeIndex: Int) -> Tensor
  • Remove the specified dimensions of size 1 from the shape of a tensor. If no dimensions are specified, then all dimensions of size 1 will be removed.

    Declaration

    func squeezingShape(at axes: Int...) -> Tensor
  • Remove the specified dimensions of size 1 from the shape of a tensor. If no dimensions are specified, then all dimensions of size 1 will be removed.

    Declaration

    func squeezingShape(at axes: [Int]) -> Tensor
  • Reshape to scalar.

    Precondition

    The tensor has exactly one scalar.

    Declaration

    func scalarized() -> Scalar
  • Returns true if rank is equal to 0 and false otherwise.

    Declaration

    var isScalar: Bool { get }
  • Returns the single scalar element if rank is equal to 0 and nil otherwise.

    Declaration

    var scalar: Scalar? { get }
  • Declaration

    public static func == (lhs: Tensor, rhs: Tensor) -> Bool
  • Declaration

    public static func != (lhs: Tensor, rhs: Tensor) -> Bool
  • A textual representation of the tensor.

    Note

    use fullDescription for a non-pretty-printed description showing all scalars.

    Declaration

    public var description: String { get }
  • A textual representation of the tensor. Returns a summarized description if summarize is true and the element count exceeds twice the edgeElementCount.

    Declaration

    func description(
      lineWidth: Int = 80, edgeElementCount: Int = 3, summarizing: Bool = false
    ) -> String

    Parameters

    lineWidth

    The max line width for printing. Used to determine number of scalars to print per line.

    edgeElementCount

    The maximum number of elements to print before and after summarization via ellipses (...).

    summarizing

    If true, summarize description if element count exceeds twice edgeElementCount.

  • A full, non-pretty-printed textual representation of the tensor, showing all scalars.

    Declaration

    var fullDescription: String { get }
  • Declaration

    public var customMirror: Mirror { get }
  • Declaration

    public func encode(to encoder: Encoder) throws
  • Declaration

    public init(from decoder: Decoder) throws
  • Declaration

    public init(_owning tensorHandles: UnsafePointer<CTensorHandle>?)