reciprocal
TypeScriptreciprocal(): MLTensorSwift declarationfunc reciprocal() -> MLTensorComputes the reciprocal of the tensor's elements.
struct · iOS 18.0+ · SDK 27.1
struct MLTensorA multi-dimensional array of numerical or Boolean scalars tailored to ML use cases, containing methods to perform transformations and mathematical operations efficiently using a ML compute device.
reciprocal(): MLTensorSwift declarationfunc reciprocal() -> MLTensorComputes the reciprocal of the tensor's elements.
squareRoot(): MLTensorSwift declarationfunc squareRoot() -> MLTensorComputes square root of the tensor's elements.
transposedPermutation(permutation: Array<number>): MLTensorSwift declarationfunc transposed(permutation: [Int]) -> MLTensorPermutes the dimensions of the tensor in the specified order. For example: ```swift let x = MLTensor(shape: [1, 2, 3], scalars: [1, 2, 3, 4, 5, 6], scalarType: Float.self) let y = x.transposed(permutation: [1, 0, 2]) y.shape // is [2, 1, 3] ``` - Parameter permutation: An array of integers defining the permutation, must be of length `rank` and define a valid permutation. - Returns: A permuted tensor.
transposed(): MLTensorSwift declarationfunc transposed() -> MLTensorPermutes the tensor with dimensions permuted in reverse order. For example: ```swift let x = MLTensor(shape: [1, 2, 3], scalars: [1, 2, 3, 4, 5, 6], scalarType: Float.self) let y = x.transposed() y.shape // is [3, 2, 1] ``` - Returns: A permuted tensor.
get description(): stringSwift declarationvar description: String { get }A textual representation of this instance. Calling this property directly is discouraged. Instead, convert an instance of any type to a string by using the `String(describing:)` initializer. This initializer works with any type, and uses the custom `description` property for types that conform to `CustomStringConvertible`: struct Point: CustomStringConvertible { let x: Int, y: Int var description: String { return "(\(x), \(y))" } } let p = Point(x: 21, y: 30) let s = String(describing: p) print(s) // Prints "(21, 30)" The conversion of `p` to a string in the assignment to `s` uses the `Point` type's `description` property.
get scalarCount(): numberSwift declarationvar scalarCount: Int { get }The number of scalar elements in the tensor.
concatenated(other: MLTensor, axis?: number): MLTensorSwift declarationfunc concatenated(with other: MLTensor, alongAxis axis: Int = 0) -> MLTensorReturns a concatenated tensor along the specified axis. - Parameters: - other: The tensor to concatenate. The tensors must have the same dimensions, except for the specified axis. - axis: The axis along which to concatenate. Negative values wrap around but must be in the range `[-rank, rank)`.
get customMirror(): SDKHandleSwift declarationvar customMirror: Mirror { get }The custom mirror for `MLTensor`.
static initFloatLiteral(floatLiteral: number): MLTensorSwift declarationinit(floatLiteral: Float)Creates an instance initialized to the specified floating-point value. Do not call this initializer directly. Instead, initialize a variable or constant using a floating-point literal. For example: let x = 21.5 In this example, the assignment to the `x` constant calls this floating-point literal initializer behind the scenes. - Parameter value: The value to create.
cumulativeSum(axis?: number): MLTensorSwift declarationfunc cumulativeSum(alongAxis axis: Int = 0) -> MLTensorComputes the cumulative sum along the specified axis. The scalar type of the tensor must be numeric. For example: ```swift MLTensor([1, 2, 3]).cumulativeSum() = [1, 1 + 2, 1 + 2 + 3] ``` - Parameter axis: The axis along which to perform the cumulative sum. The default value is `0`. Must be in the range `[-rank, rank)` and have a rank greater than zero. - Returns: The result of the cumulative sum operation.
static initBooleanLiteral(booleanLiteral: boolean): MLTensorSwift declarationinit(booleanLiteral: Bool)Creates an instance initialized to the given Boolean value. Do not call this initializer directly. Instead, initialize a variable or constant using one of the Boolean literals `true` and `false`. For example: let twasBrillig = true In this example, the assignment to the `twasBrillig` constant calls this Boolean literal initializer behind the scenes. - Parameter value: The value of the new instance.
expandingShape(axes: Array<number>): MLTensorSwift declarationfunc expandingShape(at axes: [Int]) -> MLTensorReturns a shape-expanded tensor with a dimension of 1 inserted at the specified shape indices. For example: ```swift let x = MLTensor(shape: [2], scalars: [1, 2], scalarType: Float.self) let y = x.expandingShape(at: [0]) y.shape // is [1, 2] ``` - Parameter axes: The axes at which to expand the shape.
static initIntegerLiteral(integerLiteral: number): MLTensorSwift declarationinit(integerLiteral: Int32)Creates an instance initialized to the specified integer value. Do not call this initializer directly. Instead, initialize a variable or constant using an integer literal. For example: let x = 23 In this example, the assignment to the `x` constant calls this integer literal initializer behind the scenes. - Parameter value: The value to create.
squeezingShapeAt(axes: Array<number>): MLTensorSwift declarationfunc squeezingShape(at axes: [Int]) -> MLTensorRemoves the specified dimensions of size 1 from the shape of the tensor. For example: ```swift let x = MLTensor(shape: [1, 2, 1], scalars: [1, 2], scalarType: Float.self) let y = x.squeezingShape(at: [0]) y.shape // is [2, 1] ``` - Parameter axes: The axes to remove if the size is `1`.
squeezingShape(): MLTensorSwift declarationfunc squeezingShape() -> MLTensorRemoves all dimensions of size 1 from the shape of the tensor. For example: ```swift let x = MLTensor(shape: [1, 2, 1], scalars: [1, 2], scalarType: Float.self) let y = x.squeezingShape() y.shape // is [2] ```
static initLinearSpaceFromThroughCount(start: number, end: number, count: number): MLTensorSwift declarationinit(linearSpaceFrom start: Float, through end: Float, count: Int)Creates a one-dimensional tensor representing a sequence from a starting value, up to and including an end value, spaced evenly to generate the number of values specified. - 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 the last element of the resulting sequence. - count: The number of values in the resulting sequence. `count` must be positive and greater than `1`.
cumulativeProduct(axis?: number): MLTensorSwift declarationfunc cumulativeProduct(alongAxis axis: Int = 0) -> MLTensorComputes the cumulative product along the specified axis. The scalar type of the tensor must be numeric. For example: ```swift MLTensor([1, 2, 3]).cumulativeProduct() = [1, 1 * 2, 1 * 2 * 3] ``` - Parameter axis: The axis along which to perform the cumulative product. The default value is `0`. Must be in the range `[-rank, rank)` and have a rank greater than zero. - Returns: The result of the cumulative product operation.
abs(): MLTensorSwift declarationfunc abs() -> MLTensorComputes the absolute of the tensor's elements.
allKeepRank(keepRank?: boolean): MLTensorSwift declarationfunc all(keepRank: Bool = false) -> MLTensorComputes logical AND on elements across all axes of a tensor where the scalar type of the tensor is expected to be Boolean. - Parameter keepRank: A Boolean indicating whether to keep the reduced axes or not. The default value is `false`. - Returns: The reduced Boolean tensor.
allAlongAxesKeepRank(axes: Array<number>, keepRank?: boolean): MLTensorSwift declarationfunc all(alongAxes axes: [Int], keepRank: Bool = false) -> MLTensorComputes logical AND on elements across the specified axes of a tensor where the scalar type of the tensor is expected to be Boolean. - Parameters: - axes: The axes to reduce. - keepRank: A Boolean indicating whether to keep the reduced axes or not. The default value is `false`. - Returns: The reduced Boolean tensor.
anyKeepRank(keepRank?: boolean): MLTensorSwift declarationfunc any(keepRank: Bool = false) -> MLTensorComputes logical OR on elements across all dimensions of a tensor where the scalar type of the tensor is expected to be Boolean. - Parameter keepRank: A Boolean indicating whether to keep the reduced axes or not. The default value is `false`. - Returns: The reduced tensor.
anyAlongAxesKeepRank(axes: Array<number>, keepRank?: boolean): MLTensorSwift declarationfunc any(alongAxes axes: [Int], keepRank: Bool = false) -> MLTensorComputes logical OR on elements across the specified axes of a tensor where the scalar type of the tensor is expected to be Boolean. - Parameters: - axes: The axes to reduce. - keepRank: A Boolean indicating whether to keep the reduced axes or not. The default value is `false`. - Returns: The reduced Boolean tensor.
cos(): MLTensorSwift declarationfunc cos() -> MLTensorComputes the cosine of the tensor's elements.
exp(): MLTensorSwift declarationfunc exp() -> MLTensorComputes the natural exponent of the tensor's elements.
log(): MLTensorSwift declarationfunc log() -> MLTensorComputes the natural logarithm of the tensor's elements.
maxKeepRank(keepRank?: boolean): MLTensorSwift declarationfunc max(keepRank: Bool = false) -> MLTensorReturns the maximum value in the array. ```swift let x = MLTensor(shape: [3, 2], scalars: [2, 3, 4, 5, 6, 7], scalarType: Float.self) let y = x.max() y.shape // is [] await y.shapedArray(of: Float.self) // is [7.0] ``` - Parameter keepRank: A Boolean indicating whether to keep the reduced axes or not. The default value is `false`. - Returns: The reduced tensor.
maxAlongAxesKeepRank(axes: Array<number>, keepRank?: boolean): MLTensorSwift declarationfunc max(alongAxes axes: [Int], keepRank: Bool = false) -> MLTensorReturns the maximum values along the specified axes. ```swift let x = MLTensor(shape: [3, 2], scalars: [2, 3, 4, 5, 6, 7], scalarType: Float.self) let y = x.max(alongAxes: [0], keepRank: true) y.shape // is [1, 2] await y.shapedArray(of: Float.self) // is [6.0 7.0] ``` - Parameters: - axes: The axes to reduce. - keepRank: A Boolean indicating whether to keep the reduced axes or not. The default value is `false`. - Returns: The reduced tensor.
minKeepRank(keepRank?: boolean): MLTensorSwift declarationfunc min(keepRank: Bool = false) -> MLTensorReturns the minimum value in the array. ```swift let x = MLTensor(shape: [3, 2], scalars: [2, 3, 4, 5, 6, 7], scalarType: Float.self) let y = x.min() y.shape // is [] await y.shapedArray(of: Float.self) // is [2.0] ``` - Parameter keepRank: A Boolean indicating whether to keep the reduced axes or not. The default value is `false`. - Returns: The reduced tensor.
minAlongAxesKeepRank(axes: Array<number>, keepRank?: boolean): MLTensorSwift declarationfunc min(alongAxes axes: [Int], keepRank: Bool = false) -> MLTensorReturns the minimum values along the specified axes. ```swift let x = MLTensor(shape: [3, 2], scalars: [2, 3, 4, 5, 6, 7], scalarType: Float.self) let y = x.min(alongAxes: [0]) y.shape // is [1, 2] await y.shapedArray(of: Float.self) // is [2.0 3.0] ``` - Parameters: - axes: The axes to reduce. - keepRank: A Boolean indicating whether to keep the reduced axes or not. The default value is `false`. - Returns: The reduced tensor.
pow(exponent: MLTensor): MLTensorSwift declarationfunc pow(_ exponent: MLTensor) -> MLTensorComputes element-wise power of each element with `exponent`. - Parameter exponent: The exponent.
sin(): MLTensorSwift declarationfunc sin() -> MLTensorComputes sine of the tensor's elements.
sumKeepRank(keepRank?: boolean): MLTensorSwift declarationfunc sum(keepRank: Bool = false) -> MLTensorReturns the sum along all axes. For example: ```swift let x = MLTensor(shape: [3, 2], scalars: [2, 3, 4, 5, 6, 7], scalarType: Float.self) let y = x.sum() y.shape // is [] await y.shapedArray(of: Float.self) // is [27] ``` - Parameter keepRank: A Boolean indicating whether to keep the reduced axes or not. The default value is `false`. - Returns: The reduced tensor.
sumAlongAxesKeepRank(axes: Array<number>, keepRank?: boolean): MLTensorSwift declarationfunc sum(alongAxes axes: [Int], keepRank: Bool = false) -> MLTensorReturns the sum along the specified axes. For example: ```swift let x = MLTensor(shape: [3, 2], scalars: [2, 3, 4, 5, 6, 7], scalarType: Float.self) let y = x.sum(alongAxes: [0], keepRank: true) y.shape // is [1, 2] await y.shapedArray(of: Float.self) // is [[12, 15]] ``` - Parameters: - axes: The axes to reduce. - keepRank: A Boolean indicating whether to keep the reduced axes or not. The default value is `false`. - Returns: The reduced tensor.
tan(): MLTensorSwift declarationfunc tan() -> MLTensorComputes tangent of the tensor's elements.
acos(): MLTensorSwift declarationfunc acos() -> MLTensorComputes the inverse cosine of the tensor's elements.
asin(): MLTensorSwift declarationfunc asin() -> MLTensorComputes the inverse sine of the tensor's elements.
atan(): MLTensorSwift declarationfunc atan() -> MLTensorComputes the inverse tangent of the tensor's elements.
castTo(scalarType: "Bool" | "Float"): MLTensorSwift declarationfunc cast<Scalar>(to scalarType: Scalar.Type) -> MLTensor where Scalar : MLTensorScalarCasts the elements of the tensor to the given scalar type. For example: ```swift let x = MLTensor([1, 2, 3], scalarType: Int32.self) let y = x.cast(to: Float.self) y.scalarType // is Float ``` - Parameter scalarType: The destination scalar type. - Returns: A new tensor with its contents cast to the given scalar type.
castLike(other: MLTensor): MLTensorSwift declarationfunc cast(like other: MLTensor) -> MLTensorCasts the elements of the tensor to the scalar type of the given array. For example: ```swift let x = MLTensor([1, 2, 3], scalarType: Float.self) let y = MLTensor([1, 2, 3], scalarType: Int32.self) let z = y.cast(like: x) z.scalarType // is Float ``` - Parameter other: The other tensor whose scalar type is used for the cast. - Returns: A new tensor with its contents cast to the scalar type of `other`.
ceil(): MLTensorSwift declarationfunc ceil() -> MLTensorComputes the ceiling of the tensor's elements.
cosh(): MLTensorSwift declarationfunc cosh() -> MLTensorComputes the hyperbolic cosine of the tensor's elements.
exp2(): MLTensorSwift declarationfunc exp2() -> MLTensorComputes the exponent with base two of the tensor's elements.
meanKeepRank(keepRank?: boolean): MLTensorSwift declarationfunc mean(keepRank: Bool = false) -> MLTensorReturns the mean along all axes. For example: ```swift let x = MLTensor(shape: [3, 2], scalars: [2, 3, 4, 5, 6, 7], scalarType: Float.self) let y = x.mean() y.shape // is [] await y.shapedArray(of: Float.self) // is [4.5] ``` - Parameter keepRank: A Boolean indicating whether to keep the reduced axes or not. The default value is `false`. - Returns: The reduced tensor.
meanAlongAxesKeepRank(axes: Array<number>, keepRank?: boolean): MLTensorSwift declarationfunc mean(alongAxes axes: [Int], keepRank: Bool = false) -> MLTensorReturns the mean along the specified axes. For example: ```swift let x = MLTensor(shape: [3, 2], scalars: [2, 3, 4, 5, 6, 7], scalarType: Float.self) let y = x.mean(alongAxes: [0], keepRank: true) y.shape // is [1, 2] await y.shapedArray(of: Float.self) // is [4.0 5.0] ``` - Parameters: - axes: The axes to reduce. - keepRank: A Boolean indicating whether to keep the reduced axes or not. The default value is `false`. - Returns: The reduced tensor.
get rank(): numberSwift declarationvar rank: Int { get }The number of dimensions of the tensor. Rank is equal to the number of dimensions of the shape, i.e., `tensor.rank == tensor.shape.count`.
sign(): MLTensorSwift declarationfunc sign() -> MLTensorReturns the sign of the tensor's elements. This operation returns `1.0` if the correspnding element is greater than `0`, `-1.0` if it is less than `0`, `-0.0` if it is equal to `-0.0`, and `+0.0` if it is equal to `+0.0`.
sinh(): MLTensorSwift declarationfunc sinh() -> MLTensorComputes hyperbolic sine of the tensor's elements.
tanh(): MLTensorSwift declarationfunc tanh() -> MLTensorComputes hyperbolic tangent of the tensor's elements.
topK(k: number): { "values": MLTensor; "indices": MLTensor }Swift declarationfunc topK(_ k: Int) -> (values: MLTensor, indices: MLTensor)Returns the *k* largest values along the last axis of the tensor. - Note: The tensor must have at least k elements along its last axis and order returned by largest values which are equal is not deterministic. For example: ```swift let x = MLTensor(shape: [1, 5], scalars: [1.0, 2.0, 3.0, 4.0, 5.0]) let (values, indices) = x.topK(3) // values = [[5.0, 4.0, 3.0]] // indices = [[4, 3, 2]] ``` - Parameter k: The number of largest values to return. - Returns: A tuple of (values, indices) with values containing the top k values along the last axis and indices, a `Int32` tensor, containing the indices to the corresponding values.
acosh(): MLTensorSwift declarationfunc acosh() -> MLTensorComputes the inverse hyperbolic cosine of the tensor's elements.
asinh(): MLTensorSwift declarationfunc asinh() -> MLTensorComputes the inverse hyperbolic sine of the tensor's elements.
atanh(): MLTensorSwift declarationfunc atanh() -> MLTensorComputes the inverse hyperbolic tangent of the tensor's elements.
floor(): MLTensorSwift declarationfunc floor() -> MLTensorComputes the floor of the tensor's elements.
round(): MLTensorSwift declarationfunc round() -> MLTensorRounds the tensor's elements.
rsqrt(): MLTensorSwift declarationfunc rsqrt() -> MLTensorComputes reverse square root of the tensor's elements. The reverse square root operation is the reciprocal of the square root.
get shape(): Array<number>Swift declarationvar shape: [Int] { get }The shape of the tensor. For example, 2 x 3 matrix may be represented as a tensor with the shape of `[2, 3]`.
splitCountAlongAxis(count: number, axis?: number): Array<MLTensor>Swift declarationfunc split(count: Int, alongAxis axis: Int = 0) -> [MLTensor]Splits a tensor into multiple tensors. The tensor is split along dimension `axis` into `count` smaller tensors. For example: ``` // 'value' is a tensor with shape [5, 30] // Split 'value' into 3 tensors along dimension 1: let parts = value.split(count: 3, alongAxis: 1) parts[0] // has shape [5, 10] parts[1] // has shape [5, 10] parts[2] // has shape [5, 10] ``` - Parameters: - count: The number of splits to create, must divide the size of dimension `axis` evenly. - axis: The dimension along which to split this tensor. The `axis` must be in the range `[-rank, rank)`. - Returns: An array containing the tensor parts.
splitSizesAlongAxis(sizes: Array<number>, axis?: number): Array<MLTensor>Swift declarationfunc split(sizes: [Int], alongAxis axis: Int = 0) -> [MLTensor]Splits a tensor into multiple tensors. The tensor is split into `sizes.shape[0]` parts. For example: ``` // 'value' is a tensor with shape [5, 30] // Split 'value' into 3 tensors with sizes [4, 15, 11] along dimension 1: let parts = value.split(sizes: [4, 15, 11], alongAxis: 1) parts[0] // has shape [5, 4] parts[1] // has shape [5, 15] parts[2] // has shape [5, 11] ``` - Parameters: - sizes: A one-dimensional tensor containing the size of each split, must add up to the size of dimension `axis`. - axis: The dimension along which to split this tensor. Must be in the range `[-rank, rank)`. - Returns: Array containing the tensors parts.
tiled(multiples: Array<number>): MLTensorSwift declarationfunc tiled(multiples: [Int]) -> MLTensorReturns a tensor by replicating its elements multiple times. The `multiples` argument specifies the multiplier across each dimensions, i.e., the output dimension at index `i` is equal to `shape[i] * multiples[i]`. For example: ```swift let x = MLTensor(shape: [2, 2], scalars: [1, 2, 3, 4], scalarType: Float.self) let y = x.tiled(multiples: [1, 2]) y.shape // is [2, 4] ``` If `multiples` specifies fewer dimensions than the tensor, then ones are prepended to `multiples` until all the dimensions are specified. If tensor has fewer dimensions than `multiples`, then the tensor is reshaped by prepending ones to the dimensions until all the dimensions are specified. - Parameter multiples: The multiplier across each dimension. All values must be greater than zero. - Returns: The replicated tensor.
argmaxAlongAxisKeepRank(axis: number, keepRank?: boolean): MLTensorSwift declarationfunc argmax(alongAxis axis: Int, keepRank: Bool = false) -> MLTensorReturns the indices of the maximum values along the specified axis. ```swift let x = MLTensor(shape: [3, 2], scalars: [2, 3, 4, 5, 6, 7], scalarType: Float.self) let y = x.argmax(alongAxis: 0) y.shape // is [2] y.scalarType // is Int32 await y.shapedArray(of: Int32.self) // is 2 2 ``` - Parameters: - axis: The axis to reduce. - keepRank: A Boolean indicating whether to keep the reduced axis or not. The default value is `false`. - Returns: The reduced tensor.
argmax(): MLTensorSwift declarationfunc argmax() -> MLTensorReturns the index of the maximum value of the flattened scalars. ```swift let x = MLTensor(shape: [3, 2], scalars: [2, 3, 4, 5, 6, 7], scalarType: Float.self) let y = x.argmax() y.shape // is [] y.scalarType // is Int32 await y.shapedArray(of: Int32.self) // is 5 ```
argminAlongAxisKeepRank(axis: number, keepRank?: boolean): MLTensorSwift declarationfunc argmin(alongAxis axis: Int, keepRank: Bool = false) -> MLTensorReturns the indices of the minimum values along the specified axis. ```swift let x = MLTensor(shape: [3, 2], scalars: [2, 3, 4, 5, 6, 7], scalarType: Float.self) let y = x.argmin(alongAxis: 0) y.shape // is [2] y.scalarType // is Int32 await y.shapedArray(of: Int32.self) // is 0 0 ``` - Parameters: - axis: The axis to reduce. - keepRank: A Boolean indicating whether to keep the reduced axis or not. The default value is `false`. - Returns: The reduced tensor.
argmin(): MLTensorSwift declarationfunc argmin() -> MLTensorReturns the index of the minimum value of the flattened scalars. ```swift let x = MLTensor(shape: [3, 2], scalars: [2, 3, 4, 5, 6, 7], scalarType: Float.self) let y = x.argmin() y.shape // is [] y.scalarType // is Int32 await y.shapedArray(of: Int32.self) // is 0 ```
matmul(other: MLTensor): MLTensorSwift declarationfunc matmul(_ other: MLTensor) -> MLTensorMultiplies two tensors together using matrix multiplication. The specific behaviour is determined by the dimensionality of the inputs, as described below: - If both tensors are 1-dimensional, the dot-product is returned. - If the first is 2-dimensional and other 1-dimensional, the matrix-vector product is returned. - If both have a rank of 2, the matrix-matrix product is returned and when the rank is greater than 2, the batched matrix multiply is returned For example: ```swift let v1 = MLTensor([1.0, 2.0, 3.0, 4.0]) let v2 = MLTensor([5.0, 6.0, 7.0, 8.0]) let v3 = v1.matmul(v2) v3.shape // is [] await v3.shapedArray(of: Float.self) // is 70.0 let m1 = MLTensor(shape: [2, 3], scalars: [ 1, 2, 3, 4, 5, 6 ], scalarType: Float.self) let m2 = MLTensor(shape: [3, 2], scalars: [ 7, 8, 9, 10, 11, 12 ], scalarType: Float.self) let m3 = t1.matmul(r2) m3.shape // is [2, 2] await m3.shapedArray(of: Float.self) // is [[58, 64], [139, 154]] // Supports broadcasting let m4 = MLTensor(randomNormal: [3, 1, 1, 4], scalarType: Float.self) let m5 = MLTensor(randomNormal: [4, 2], scalarType: Float.self) let m6 = t4.matmul(t5) m6.shape // is [3, 1, 1, 2] ``` - Parameter other: The right-hand side tensor to be multiplied. - Returns: The result of the matrix multiplication.
paddedForSizesMode(sizes: Array<{ "before": number; "after": number }>, mode: MLTensor_PaddingMode): MLTensorSwift declarationfunc padded(forSizes sizes: [(before: Int, after: Int)], mode: MLTensor.PaddingMode) -> MLTensorReturns a padded tensor according to the specified padding sizes and mode. For example: ```swift let x = MLTensor(shape: [2, 3], scalars: [ 1, 2, 3, 4, 5, 6 ], scalarType: Float32.self) let constantPadding = x.padded(forSizes: [(0, 0), (2, 2)], mode: .constant(Float(0))) // [[0, 0, 1, 2, 3, 0, 0], // [0, 0, 4, 5, 6, 0, 0]] let reflectionPadding = x.padded(forSizes: [(0, 0), (2, 2)], mode: .reflection) // [[3, 2, 1, 2, 3, 2, 1], // [6, 5, 4, 5, 6, 5, 4]] let symmetricPadding = x.padded(forSizes: [(0, 0), (2, 2)], mode: .symmetric) // [[2, 1, 1, 2, 3, 3, 2], // [5, 4, 4, 5, 6, 6, 5]] ``` - Parameters: - sizes: An array of tuples describing the size to be inserted before and after each dimension. - mode: The mode of padding, etiher constant, reflection, or symmetric. - Returns: The padded tensor.
paddedForSizesWith(sizes: Array<{ "before": number; "after": number }>, value: number): MLTensorSwift declarationfunc padded(forSizes sizes: [(before: Int, after: Int)], with value: Float) -> MLTensorReturns a tensor padded with the given constant according to the specified padding sizes. For example: ```swift let x = MLTensor(shape: [2, 3], scalars: [ 1, 2, 3, 4, 5, 6 ], scalarType: Float32.self) let y = x.padded(forSizes: [(0, 0), (2, 2)], with: 0.0) // [[0, 0, 1, 2, 3, 0, 0], // [0, 0, 4, 5, 6, 0, 0]] ``` - Parameters: - sizes: An array of tuples describing the size to be inserted before and after each dimension. - value: The constant value used for padding. - Returns: The padded tensor.
argsort(axis?: number, descendingOrder?: boolean): MLTensorSwift declarationfunc argsort(alongAxis axis: Int = -1, descendingOrder: Bool = false) -> MLTensorReturns the indices (or arguments) of a tensor that give its sorted order along the specified axis. For example: ```swift let x = MLTensor([1.0, 3.0, 2.0]) let y = x.argSort() await y.shapedArray(of: Int32.self) // is [0, 2, 1] ``` - Parameters: - axis: The axis along which to sort. The default is `-1`, which sorts the last axis. - descendingOrder: A Boolean value that determines the sort order. The default is `false` which sorts from largest to least. - Returns: A `Int32` tensor of sorted indices.
clampedTo(bounds: SDKHandle): MLTensorSwift declarationfunc clamped(to bounds: ClosedRange<Float>) -> MLTensorClamps all elements to the given lower and upper bounds, inclusively. For example: ```swift let x = MLTensor([-1.0, 1.0, 2.0]) let y = x.clamped(to: 0...1) await y.shapedArray(of: Float.self) // is [0.0, 1.0, 1.0] ```
clampedTo84394(bounds: SDKHandle): MLTensorSwift declarationfunc clamped(to bounds: PartialRangeFrom<Float>) -> MLTensorClamps all elements to the given lower bound, inclusively. For example: ```swift let x = MLTensor([-1.0, 1.0, 2.0]) let y = x.clamped(to: 0...) await y.shapedArray(of: Float.self) // is [0.0, 1.0, 2.0] ```
clampedTo17076(bounds: SDKHandle): MLTensorSwift declarationfunc clamped(to bounds: PartialRangeThrough<Float>) -> MLTensorClamps all elements to the given upper bound, inclusively. For example: ```swift let x = MLTensor([-1.0, 1.0, 2.0]) let y = x.clamped(to: ...1) await y.shapedArray(of: Float.self) // is [-1.0, 1.0, 1.0] ```
productKeepRank(keepRank?: boolean): MLTensorSwift declarationfunc product(keepRank: Bool = false) -> MLTensorReturns the product along all axes. ```swift let x = MLTensor(shape: [3, 2], scalars: [2, 3, 4, 5, 6, 7], scalarType: Float.self) let y = x.product() y.shape // is [] await y.shapedArray(of: Float.self) // is [5040.0] ``` - Parameter keepRank: A Boolean indicating whether to keep the reduced axes or not. The default value is `false`. - Returns: The reduced tensor.
productAlongAxesKeepRank(axes: Array<number>, keepRank?: boolean): MLTensorSwift declarationfunc product(alongAxes axes: [Int], keepRank: Bool = false) -> MLTensorReturns the product along the specified axes. ```swift let x = MLTensor(shape: [3, 2], scalars: [2, 3, 4, 5, 6, 7], scalarType: Float.self) let y = x.product(alongAxes: [0], keepRank: true) y.shape // is [1, 2] await y.shapedArray(of: Float.self) // is [48.0 105.0] ``` - Parameters: - axes: The axes to reduce. - keepRank: A Boolean indicating whether to keep the reduced axes or not. The default value is `false`. - Returns: The reduced tensor.
resized(size: { "newHeight": number; "newWidth": number }, method?: MLTensor_ResizeMethod): MLTensorSwift declarationfunc resized(to size: (newHeight: Int, newWidth: Int), method: MLTensor.ResizeMethod = .nearestNeighbor) -> MLTensorResize the tensor's spatial dimensions to size using the specified method. For example: ```swift let image = MLTensor(shape: [1, 1, 2, 2], scalars: [ 1, 0, 0, 1 ], scalarType: Float.self) let resizedImage = image.resized(to: (4, 4), method: .nearest) // [[[[1, 1, 0, 0], // [1, 1, 0, 0], // [0, 0, 1, 1], // [0, 0, 1, 1]]]] ``` The tensor must be either a 4-dimensional float tensor of shape `[batch, channels, height, width]` or 3-dimensional float tensor of shape `[channel, height, width]`. - Parameters: - size: The new size for the spatial dimensions of the tensor. The size must be positive. - method: The resize method. The default value is `.nearest`.
softmax(axis?: number): MLTensorSwift declarationfunc softmax(alongAxis axis: Int = -1) -> MLTensorComputes the softmax of the specified tensor along the specified axis. - Parameter axis: The axis along which softmax will be computed. - Returns: A new tensor with the same shape and scalar type.
squared(): MLTensorSwift declarationfunc squared() -> MLTensorComputes square of the tensor's elements.
bandPart(lowerBandCount: number, upperBandCount: number): MLTensorSwift declarationfunc bandPart(lowerBandCount: Int, upperBandCount: Int) -> MLTensorReturns a new tensor with the same shape where everything outside a central band in each innermost matrix is set to zero. The framework copies a diagonal band of values from the tensor to the result tensor of the same size. A coordinate `[..., i, j]` is considered in band if ```md (lowerBandCount < 0 || (i-j) <= lowerBandCount) && (upperBandCount < 0 || (j-i) <= upperBandCount) ``` Values outside of the band are set to `0`. For example: ```swift let t = Tensor([ [ 5, 1, 2, 3], [-1, 5, 1, 2], [-2, -1, 5, 1], [-3, -2, -1, 5] ], scalarType: Float.self) t.bandPart(lowerBandCount: 0, upperBandCount: 0) // [[ 5, 0, 0, 0] // [ 0, 5, 0, 0] // [ 0, 0, 5, 0] // [ 0, 0, 0, 5]] t.bandPart(lowerBandCount: 0, upperBandCount: -1) // [[ 5, 1, 2, 3] // [ 0, 5, 1, 2] // [ 0, 0, 5, 1] // [ 0, 0, 0, 5]] t.bandPart(lowerBandCount: -1, upperBandCount: 0) // [[ 5, 0, 0, 0] // [-1, 5, 0, 0] // [-2, -1, 5, 0] // [-3, -2, -1, 5]] ``` - Parameters: - lowerBandCount: The number of diagonals in the lower triangle to keep. If `-1`, keep the entire lower triangle. - upperBandCount: The number of diagonals in the upper triangle to keep. If `-1`, keep the entire upper triangle. - Returns: The band part of the tensor.
get isScalar(): booleanSwift declarationvar isScalar: Bool { get }A Boolean value indicating whether the tensor is a scalar (when the `rank` is equal to `0`) or not.
reshaped(newShape: Array<number>): MLTensorSwift declarationfunc reshaped(to newShape: [Int]) -> MLTensorReshape to the specified shape. For example: ```swift let x = MLTensor(shape: [2], scalars: [1, 2], scalarType: Float.self) let y = x.reshaped(at: [1, 2, 1]) y.shape // is [1, 2, 1] ``` - Parameter newShape: The new shape of the array. The number of scalars matches the new shape.
reversed(axes: Array<number>): MLTensorSwift declarationfunc reversed(alongAxes axes: [Int]) -> MLTensorReturns a new tensor with the specified dimensions reversed. For example: ```swift let x = MLTensor(shape: [4], scalars: [0, 1, 2, 3], scalarType: Float.self) let y = x.reversed(alongAxes: [0]) // [3, 2, 1, 0] ``` - Parameter axes: Axis or axes to reverse the elements of the tensor. Must be in the range `[-rank, rank)`. If no axis is specified, the tensor will be reversed along all axes. - Returns: A new tensor with the same shape and scalar type with the specified dimensions reversed.
flattened(): MLTensorSwift declarationfunc flattened() -> MLTensorReshape to a one-dimensional tensor. - Note: Flattening a zero-dimensional tensor will return a one-dimensional tensor. For example: ```swift let x = MLTensor(shape: [2, 2], scalars: [1, 2, 3, 4], scalarType: Float.self) let y = x.flattened() y.shape // is [4] ```
gatheringAtIndicesAlongAxis(indices: MLTensor, axis: number): MLTensorSwift declarationfunc gathering(atIndices indices: MLTensor, alongAxis axis: Int) -> MLTensorReturns a tensor by gathering slices along the given axis at the specified indices. For example: ```swift let x = MLTensor(shape: [3, 3], scalars: [ 0, 1, 2, 10, 11, 12, 20, 21, 22 ], scalarType: Float.self) let i = MLTensor([2, 1], scalarType: Int32.self) let y0 = x.gathering(atIndices: i) // [[20, 21, 22], // [10, 11, 12]] let y1 = x.gathering(atIndices: i, alongAxis: 1) // [[ 2, 1], // [12, 11], // [22, 21]] ``` - Parameters: - indices: A 32-bit integer tensor containing indices to gather at. - axis: The dimension to gather along. Must be in the range `[-rank, rank)`. - Returns: The gathered tensor.
gatheringAtIndices(indices: MLTensor): MLTensorSwift declarationfunc gathering(atIndices indices: MLTensor) -> MLTensorReturns a tensor by gathering slices at the specified indices. The `indices` tensor is interpreted as a `(rank-1)` dimensional set of one-dimensional lookup vectors, for example: ```swift let x = MLTensor(shape: [2, 2], scalars: [ 0, 1, 10, 11 ], scalarType: Float.self) let i = MLTensor(shape: [2, 2], scalars: [ 0, 0, 1, 1 ], scalarType: Int32.self) let y = x.gathering(atIndices: i) // [ 0, 11] ``` If the one-dimensional lookup vectors do not give a full set of indices, the remaining indices are treated as a slice, for example: ```swift let x = MLTensor(shape: [3, 3], scalars: [ 0, 1, 2, 10, 11, 12, 20, 21, 22 ], scalarType: Float.self) let i = MLTensor(shape: [3, 1], scalars: [ 2, 1 ], scalarType: Int32.self) let y = x.gathering(atIndices: i) // [[20 21 22] // [10 11 12]] ``` - Parameter indices: A 32-bit integer tensor containing indices to gather at. - Returns: The gathered tensor.
static initRangeFromToBy(start: number, end: number, stride: SDKHandle): MLTensorSwift declarationinit(rangeFrom start: Float, to end: Float, by stride: Float.Stride)Creates a one-dimensional tensor representing a sequence from a starting value to, but not including, an end value, stepping by the specified amount. - 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. - scalarType: The scalar type.
static initRepeatingShape(repeatedValue: number, shape: Array<number>): MLTensorSwift declarationinit<Scalar>(repeating repeatedValue: Scalar, shape: [Int], scalarType: Scalar.Type = Scalar.self) where Scalar : MLTensorScalarCreates a tensor with the specified shape and a single, repeated scalar value. - Parameters: - repeatedValue: The scalar value to repeat. - shape: The shape of the tensor. - scalarType: The scalar type.
static initRepeatingShape31494(repeatedValue: number, shape: Array<number>): MLTensorSwift declarationinit(repeating repeatedValue: Float, shape: [Int])Creates a tensor with the specified shape and a single, repeated scalar value. - Parameters: - repeatedValue: The float value to repeat. - shape: The shape of the tensor.
replacingWithWhere(replacement: MLTensor, mask: MLTensor): MLTensorSwift declarationfunc replacing(with replacement: MLTensor, where mask: MLTensor) -> MLTensorReturns a new tensor replacing values from `other` with the corresponding element in `self` where the associated element in `mask` is `true`. For example: ```swift let x = MLTensor([1, 2, 3], scalarType: Float.self) let y = MLTensor([4, 5, 6], scalarType: Float.self) let mask = MLTensor([false, true, false]) let z = x.replacing(with: y, where: mask) await z.shapedArray(of: Float.self) // is [1, 5, 3] ``` - Parameters: - replacement: The replacement values where `mask` is `true`. - mask: The Boolean mask that determines whether the corresponding element / row should be taken from `self` (if the element in `mask` is `false`) or `other` (if `true`). - Returns: A new tensor of the same shape and type as `self`.
replacingWithWhere75710(replacement: Implements<MLTensorScalar> | SDKHandle, mask: MLTensor): MLTensorSwift declarationfunc replacing(with replacement: some MLTensorScalar, where mask: MLTensor) -> MLTensorReturns a new tensor replacing the value of `other` with the corresponding element in `self` where the associated element in `mask` is `true`. For example: ```swift let x = MLTensor([1, 2, 3], scalarType: Float.self) let mask = MLTensor([false, true, false]) let z = x.replacing(with: 5, where: mask) await z.shapedArray(of: Float.self) // is [1, 5, 3] ``` - Parameters: - replacement: The replacement value where `mask` is `true`. - mask: The Boolean mask that determines whether the corresponding element / row should be taken from `self` (if the element in `mask` is `false`) or `other` (if `true`). - Returns: A new tensor of the same shape and type as `self`.
replacingWithAtIndicesAlongAxis(replacement: MLTensor, indices: MLTensor, axis: number): MLTensorSwift declarationfunc replacing(with replacement: MLTensor, atIndices indices: MLTensor, alongAxis axis: Int) -> MLTensorReplaces slices along the specified indices with the given replacement values. For example: ```swift let x = MLTensor(shape: [2, 3], scalars: [ 10, 30, 20, 60, 40, 50 ], scalarType: Float.self) let i = MLTensor(shape: [2, 1], scalars: [ 1, 0 ], scalarType: Int32.self) let updates = MLTensor(shape: [2, 1], scalars: [ 99, 99 ], scalarType: Float.self) let y = x.replacing(atIndices: i, with: updates, alongAxis: 1) // [[10, 99, 20], // [99, 40, 50]] ``` - Parameters: - indices: A 32-bit integer tensor containing indices to scatter values from `replacement`. Must have the same shape as `replacement`. Must have the same shape as `self` except at `axis`. - replacement: The replacement values. - axis: The axis to scatter to. Must be in the range `[-rank, rank)`. - Returns: The updated tensor.
replacingAtIndicesWithAlongAxis(indices: MLTensor, replacement: Implements<MLTensorScalar> | SDKHandle, axis: number): MLTensorSwift declarationfunc replacing(atIndices indices: MLTensor, with replacement: some MLTensorScalar, alongAxis axis: Int) -> MLTensorReplaces slices along the specified indices with the given replacement values. For example: ```swift let x = MLTensor(shape: [2, 3], scalars: [ 10, 30, 20, 60, 40, 50 ], scalarType: Float.self) let i = MLTensor(shape: [2, 1], scalars: [ 1, 0 ], scalarType: Int32.self) let y = x.replacing(with: 99, atIndices: i, alongAxis: 1) // [[10, 99, 20], // [99, 40, 50]] ``` - Parameters: - indices: A 32-bit integer tensor containing indices to scatter values from `replacement`. `indices` must have the same shape as `self` except at `axis`. Must be in the range `[-rank, rank)`. - replacement: The replacement value. - axis: The axis to scatter to. - Returns: The updated tensor.
unstacked(axis?: number): Array<MLTensor>Swift declarationfunc unstacked(alongAxis axis: Int = 0) -> [MLTensor]Unpacks the given dimension of a rank-`R` tensor into multiple rank-`(R-1)` tensors. Unpacks `N` tensors from this tensor by chipping it along the `axis` dimension, where `N` is inferred from this tensor's shape. For example, given a tensor with shape `[A, B, C, D]`: - If `axis == 0` then the `i`-th tensor in the returned array is the slice `self[i, nil, nil, nil]` and each tensor in that array will have shape `[B, C, D]`. - If `axis == 1` then the `i`-th tensor in the returned array is the slice `value[:, i, :, :]` and each tensor in that array will have shape `[A, C, D]`. - Etc. This is the opposite of `MLTensor.init(stacking:alongAxis:)`. - Parameter axis: The dimension along which to unstack. The `axis` must be in the range `[-rank, rank)`. - Returns: An array containing the unstacked tensors.
static init(value: number): MLTensorSwift declarationinit<Scalar>(_ value: Scalar, scalarType: Scalar.Type = Scalar.self) where Scalar : MLTensorScalarCreates a zero-dimensional tensor from a scalar value. - Parameters: - value: The value. - scalarType: The scalar type.
41 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.
MLTensor.all(alongAxes:keepRank:)
A variable number of Swift arguments is not supported.
MLTensor.any(alongAxes:keepRank:)
A variable number of Swift arguments is not supported.
MLTensor.squeezingShape(at:)
A variable number of Swift arguments is not supported.
MLTensor.expandingShape(at:)
A variable number of Swift arguments is not supported.
MLTensor.transposed(permutation:)
A variable number of Swift arguments is not supported.
MLTensor.init(concatenating:alongAxis:)
This parameter hides a concrete Swift type.
MLTensor.init(stacking:alongAxis:)
This parameter hides a concrete Swift type.
MLTensor.init(arrayLiteral:)
A variable number of Swift arguments is not supported.
MLTensor.init(_:alongAxis:)
This parameter hides a concrete Swift type.
MLTensor.reversed(alongAxes:)
A variable number of Swift arguments is not supported.
MLTensor.pow(_:)
This parameter hides a concrete Swift type.
MLTensor.subscript(_:_:)
A variable number of Swift arguments is not supported.
MLTensor.subscript(_:_:_:)
A variable number of Swift arguments is not supported.
MLTensor.subscript(_:_:_:_:)
A variable number of Swift arguments is not supported.
MLTensor.subscript(_:_:_:_:_:)
A variable number of Swift arguments is not supported.
MLTensor.subscript(_:)
A variable number of Swift arguments is not supported.
MLTensor.sum(alongAxes:keepRank:)
A variable number of Swift arguments is not supported.
MLTensor.mean(alongAxes:keepRank:)
A variable number of Swift arguments is not supported.
MLTensor.min(alongAxes:keepRank:)
A variable number of Swift arguments is not supported.
MLTensor.max(alongAxes:keepRank:)
A variable number of Swift arguments is not supported.
MLTensor.product(alongAxes:keepRank:)
A variable number of Swift arguments is not supported.
MLTensor.init(randomNormal:mean:standardDeviation:seed:scalarType:)
This declaration needs Swift generic type arguments. (no canonical witness for Scalar)
MLTensor.init(randomUniform:in:seed:scalarType:)
Passing a Swift type itself is not supported.
MLTensor.init(randomUniform:in:seed:scalarType:)
This declaration needs Swift generic type arguments. (no canonical witness for Scalar)
MLTensor.init(unsafeUninitializedShape:scalarType:initializingWith:)
Passing a Swift type itself is not supported.
MLTensor.init(shape:scalars:)
This parameter hides a concrete Swift type.
MLTensor.init(shape:scalars:scalarType:)
This declaration needs Swift generic type arguments. (no canonical witness for Scalar)
MLTensor.init(_:scalarType:)
This declaration needs Swift generic type arguments. (no canonical witness for Scalar)
MLTensor.init(_:)
This parameter hides a concrete Swift type.
MLTensor.init(_:)
This parameter hides a concrete Swift type.