<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -",
    "tvOS: 18.0.0 -",
    "visionOS: 2.0.0 -",
    "watchOS: 11.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreML",
  "identifier" : "/documentation/CoreML/MLTensor",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Core ML"
    ],
    "preciseIdentifier" : "s:6CoreML8MLTensorV"
  },
  "title" : "MLTensor"
}
-->

# MLTensor

A 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.

```
struct MLTensor
```

## Topics

### Creating a tensor

[`init ( _ :)`](/documentation/CoreML/MLTensor/init(_:))

Creates a one-dimensional tensor from scalars.

[`init(_:alongAxis:)`](/documentation/CoreML/MLTensor/init(_:alongAxis:))

Creates a tensor by stacking the given tensors along the specified axis.

[`init ( _ : scalarType :)`](/documentation/CoreML/MLTensor/init(_:scalarType:))

Creates a one-dimensional tensor from scalars.

[`init(bytesNoCopy:shape:scalarType:deallocator:)`](/documentation/CoreML/MLTensor/init(bytesNoCopy:shape:scalarType:deallocator:))

Creates a tensor with memory content without copying the bytes.

[`init(concatenating:alongAxis:)`](/documentation/CoreML/MLTensor/init(concatenating:alongAxis:))

Concatenates `tensors` along the `axis` dimension.

[`init(linearSpaceFrom:through:count:)`](/documentation/CoreML/MLTensor/init(linearSpaceFrom:through:count:))

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.

[`init(linearSpaceFrom:through:count:scalarType:)`](/documentation/CoreML/MLTensor/init(linearSpaceFrom:through:count:scalarType:))

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.

[`init ( ones : scalarType :)`](/documentation/CoreML/MLTensor/init(ones:scalarType:))

Creates a tensor with all scalars set to ones.

[`init(randomNormal:mean:standardDeviation:seed:scalarType:)`](/documentation/CoreML/MLTensor/init(randomNormal:mean:standardDeviation:seed:scalarType:))

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

[`init ( randomUniform : in : seed : scalarType :)`](/documentation/CoreML/MLTensor/init(randomUniform:in:seed:scalarType:))

Creates a tensor with the specified shape, randomly sampling scalar values from a uniform distribution in `bounds`.

[`init(rangeFrom:to:by:)`](/documentation/CoreML/MLTensor/init(rangeFrom:to:by:))

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

[`init(rangeFrom:to:by:scalarType:)`](/documentation/CoreML/MLTensor/init(rangeFrom:to:by:scalarType:))

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

[`init(repeating:shape:)`](/documentation/CoreML/MLTensor/init(repeating:shape:))

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

[`init(repeating:shape:scalarType:)`](/documentation/CoreML/MLTensor/init(repeating:shape:scalarType:))

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

[`init(shape:data:scalarType:)`](/documentation/CoreML/MLTensor/init(shape:data:scalarType:))

Creates a tensor by copying the given block of data.

[`init(shape:scalars:)`](/documentation/CoreML/MLTensor/init(shape:scalars:))

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

[`init(shape:scalars:scalarType:)`](/documentation/CoreML/MLTensor/init(shape:scalars:scalarType:))

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

[`init(stacking:alongAxis:)`](/documentation/CoreML/MLTensor/init(stacking:alongAxis:))

Stacks the given tensors along the `axis` dimension into a new tensor with rank one higher than the current tensor and
each tensor.

[`init(unsafeUninitializedShape:scalarType:initializingWith:)`](/documentation/CoreML/MLTensor/init(unsafeUninitializedShape:scalarType:initializingWith:))

Creates a tensor with the specified shape, then calls the given closure with a buffer covering the tensor’s uninitialized memory.

[`init ( zeros : scalarType :)`](/documentation/CoreML/MLTensor/init(zeros:scalarType:))

Creates a tensor with all scalars set to zero.

### Accessing tensor properties

[`isScalar`](/documentation/CoreML/MLTensor/isScalar)

A Boolean value indicating whether the tensor is a scalar (when the `rank` is equal to `0`) or not.

[`rank`](/documentation/CoreML/MLTensor/rank)

The number of dimensions of the tensor.

[`scalarCount`](/documentation/CoreML/MLTensor/scalarCount)

The number of scalar elements in the tensor.

[`scalarType`](/documentation/CoreML/MLTensor/scalarType)

The type of scalars in the tensor.

[`shape`](/documentation/CoreML/MLTensor/shape)

The shape of the tensor.

### Getting the sum

[`func   sum ( alongAxes : keepRank :)`](/documentation/CoreML/MLTensor/sum(alongAxes:keepRank:))

Returns the sum along the specified axes.

[`sum(keepRank:)`](/documentation/CoreML/MLTensor/sum(keepRank:))

Returns the sum along all axes.

### Performing a logical AND operation

[`func   all ( alongAxes : keepRank :)`](/documentation/CoreML/MLTensor/all(alongAxes:keepRank:))

Computes logical AND on elements across the specified axes of a tensor where the scalar type of the tensor is expected
to be Boolean.

[`all(keepRank:)`](/documentation/CoreML/MLTensor/all(keepRank:))

Computes logical AND on elements across all axes of a tensor where the scalar type of the tensor is expected to be Boolean.

### Performing a logical OR operation

[`func   any ( alongAxes : keepRank :)`](/documentation/CoreML/MLTensor/any(alongAxes:keepRank:))

Computes logical OR on elements across the specified axes of a tensor where the scalar type of the tensor is expected
to be Boolean.

[`any(keepRank:)`](/documentation/CoreML/MLTensor/any(keepRank:))

Computes logical OR on elements across all dimensions of a tensor where the scalar type of the tensor is expected to be Boolean.

### Accessing the indicies

[`argmax()`](/documentation/CoreML/MLTensor/argmax())

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

[`argmax(alongAxis:keepRank:)`](/documentation/CoreML/MLTensor/argmax(alongAxis:keepRank:))

Returns the indices of the maximum values along the specified axis.

[`argmin()`](/documentation/CoreML/MLTensor/argmin())

Returns the index of the minimum value of the flattened scalars.

[`argmin(alongAxis:keepRank:)`](/documentation/CoreML/MLTensor/argmin(alongAxis:keepRank:))

Returns the indices of the minimum values along the specified axis.

[`argsort(alongAxis:descendingOrder:)`](/documentation/CoreML/MLTensor/argsort(alongAxis:descendingOrder:))

Returns the indices (or arguments) of a tensor that give its sorted order along the specified axis.

### Casting the elements

[`cast(like:)`](/documentation/CoreML/MLTensor/cast(like:))

Casts the elements of the tensor to the scalar type of the given array.

[`cast(to:)`](/documentation/CoreML/MLTensor/cast(to:))

Casts the elements of the tensor to the given scalar type.

### Computing the absolute, ceiling and floor

[`abs()`](/documentation/CoreML/MLTensor/abs())

Computes the absolute of the tensor’s elements.

[`ceil()`](/documentation/CoreML/MLTensor/ceil())

Computes the ceiling of the tensor’s elements.

[`floor()`](/documentation/CoreML/MLTensor/floor())

Computes the floor of the tensor’s elements.

### Performing arithmetic operations

[`static   * ( _ : _ :)`](/documentation/CoreML/MLTensor/*(_:_:))

Computes element-wise multiplication.

[`*=(_:_:)`](/documentation/CoreML/MLTensor/*=(_:_:))

Computes element-wise multiplication.

[`static   + ( _ : _ :)`](/documentation/CoreML/MLTensor/+(_:_:))

Computes element-wise addition.

[`+=(_:_:)`](/documentation/CoreML/MLTensor/+=(_:_:))

Computes element-wise addition.

[`-(_:)`](/documentation/CoreML/MLTensor/-(_:))

Returns the negation of the tensor.

[`static   - ( _ : _ :)`](/documentation/CoreML/MLTensor/-(_:_:))

Computes element-wise subtraction.

[`-=(_:_:)`](/documentation/CoreML/MLTensor/-=(_:_:))

Computes element-wise subtraction.

[`.!(_:)`](/documentation/CoreML/MLTensor/.!(_:))

Computes logical not on the tensor’s elements.

[`static   .!= ( _ : _ :)`](/documentation/CoreML/MLTensor/.!=(_:_:))

Computes element-wise inequality between two tensors.

[`.&(_:_:)`](/documentation/CoreML/MLTensor/.&(_:_:))

Computes element-wise logical AND where both operands are expected contain Boolean scalar elements.

[`static   .== ( _ : _ :)`](/documentation/CoreML/MLTensor/.==(_:_:))

Computes element-wise equality between two tensors.

[`static   .> ( _ : _ :)`](/documentation/CoreML/MLTensor/._(_:_:)-3m3ro)

Computes element-wise greater comparison between two tensors.

[`static   .< ( _ : _ :)`](/documentation/CoreML/MLTensor/._(_:_:)-6kfoh)

Computes element-wise less comparison between two tensors.

[`.|(_:_:)`](/documentation/CoreML/MLTensor/._(_:_:)-7z7ks)

Computes element-wise logical OR where both operands are expected contain Boolean scalar elements.

[`.^(_:_:)`](/documentation/CoreML/MLTensor/._(_:_:)-8il7w)

Computes element-wise logical XOR where both operands are expected contain Boolean scalar elements.

[`static   .>= ( _ : _ :)`](/documentation/CoreML/MLTensor/._=(_:_:)-5qa2u)

Computes element-wise greater than or equal to comparison between two tensors.

[`static   .<= ( _ : _ :)`](/documentation/CoreML/MLTensor/._=(_:_:)-otif)

Computes element-wise less than or equal to comparison between two tensors.

[`static   / ( _ : _ :)`](/documentation/CoreML/MLTensor/_(_:_:)-5hhe4)

Computes element-wise division.

[`static   % ( _ : _ :)`](/documentation/CoreML/MLTensor/_(_:_:)-7cfjs)

Computes element-wise remainder of division.

[`%=(_:_:)`](/documentation/CoreML/MLTensor/_=(_:_:)-3xllm)

Computes element-wise remainder of division.

[`/=(_:_:)`](/documentation/CoreML/MLTensor/_=(_:_:)-53k1k)

Computes element-wise multiplication.

### Applying trigonometric functions

[`cos()`](/documentation/CoreML/MLTensor/cos())

Computes the cosine of the tensor’s elements.

[`cosh()`](/documentation/CoreML/MLTensor/cosh())

Computes the hyperbolic cosine of the tensor’s elements.

[`acos()`](/documentation/CoreML/MLTensor/acos())

Computes the inverse cosine of the tensor’s elements.

[`acosh()`](/documentation/CoreML/MLTensor/acosh())

Computes the inverse hyperbolic cosine of the tensor’s elements.

[`sin()`](/documentation/CoreML/MLTensor/sin())

Computes sine of the tensor’s elements.

[`sinh()`](/documentation/CoreML/MLTensor/sinh())

Computes hyperbolic sine of the tensor’s elements.

[`asin()`](/documentation/CoreML/MLTensor/asin())

Computes the inverse sine of the tensor’s elements.

[`asinh()`](/documentation/CoreML/MLTensor/asinh())

Computes the inverse hyperbolic sine of the tensor’s elements.

[`tan()`](/documentation/CoreML/MLTensor/tan())

Computes tangent of the tensor’s elements.

[`tanh()`](/documentation/CoreML/MLTensor/tanh())

Computes hyperbolic tangent of the tensor’s elements.

[`atan()`](/documentation/CoreML/MLTensor/atan())

Computes the inverse tangent of the tensor’s elements.

[`atanh()`](/documentation/CoreML/MLTensor/atanh())

Computes the inverse hyperbolic tangent of the tensor’s elements.

### Accessing the minimum, maximum and mean

[`func   min ( alongAxes : keepRank :)`](/documentation/CoreML/MLTensor/min(alongAxes:keepRank:))

Returns the minimum values along the specified axes.

[`min(keepRank:)`](/documentation/CoreML/MLTensor/min(keepRank:))

Returns the minimum value in the array.

[`func   max ( alongAxes : keepRank :)`](/documentation/CoreML/MLTensor/max(alongAxes:keepRank:))

Returns the maximum values along the specified axes.

[`max(keepRank:)`](/documentation/CoreML/MLTensor/max(keepRank:))

Returns the maximum value in the array.

[`func   mean ( alongAxes : keepRank :)`](/documentation/CoreML/MLTensor/mean(alongAxes:keepRank:))

Returns the mean along the specified axes.

[`mean(keepRank:)`](/documentation/CoreML/MLTensor/mean(keepRank:))

Returns the mean along all axes.

### Splitting the tensor

[`split(count:alongAxis:)`](/documentation/CoreML/MLTensor/split(count:alongAxis:))

Splits a tensor into multiple tensors. The tensor is split along dimension `axis` into `count` smaller tensors.

[`split(sizes:alongAxis:)`](/documentation/CoreML/MLTensor/split(sizes:alongAxis:))

Splits a tensor into multiple tensors. The tensor is split  into `sizes.shape[0]` parts.

### Resizing the tensor

[`resized(to:method:)`](/documentation/CoreML/MLTensor/resized(to:method:))

Resize the tensor’s spatial dimensions to size using the specified method.

[`ResizeMethod`](/documentation/CoreML/MLTensor/ResizeMethod)

A resize algorithm.

### Padding the tensor

[`padded(forSizes:mode:)`](/documentation/CoreML/MLTensor/padded(forSizes:mode:))

Returns a padded tensor according to the specified padding sizes and mode.

[`padded(forSizes:with:)`](/documentation/CoreML/MLTensor/padded(forSizes:with:))

Returns a tensor padded with the given constant according to the specified padding sizes.

[`PaddingMode`](/documentation/CoreML/MLTensor/PaddingMode)

A mode that dictates how a tensor is padded.

### Replacing the tensor values

[`replacing(atIndices:with:alongAxis:)`](/documentation/CoreML/MLTensor/replacing(atIndices:with:alongAxis:))

Replaces slices along the specified indices with the given replacement values.

[`replacing(with:atIndices:alongAxis:)`](/documentation/CoreML/MLTensor/replacing(with:atIndices:alongAxis:))

Replaces slices along the specified indices with the given replacement values.

[`func   replacing ( with : where :)`](/documentation/CoreML/MLTensor/replacing(with:where:))

Returns a new tensor replacing values from `other` with the corresponding element in `self` where the
associated element in `mask` is `true`.

### Gathering slices

[`gathering(atIndices:)`](/documentation/CoreML/MLTensor/gathering(atIndices:))

Returns a tensor by gathering slices at the specified indices.

[`gathering(atIndices:alongAxis:)`](/documentation/CoreML/MLTensor/gathering(atIndices:alongAxis:))

Returns a tensor by gathering slices along the given axis at the specified indices.

### Transposing the tensor

[`transposed()`](/documentation/CoreML/MLTensor/transposed())

Permutes the tensor with dimensions permuted in reverse order.

[`func   transposed ( permutation :)`](/documentation/CoreML/MLTensor/transposed(permutation:))

Permutes the dimensions of the tensor in the specified order.

### Unpacking the tensor

[`unstacked(alongAxis:)`](/documentation/CoreML/MLTensor/unstacked(alongAxis:))

Unpacks the given dimension of a rank-`R` tensor into multiple rank-`(R-1)` tensors.

### Getting the shaped representation of the tensor

[`shapedArray(of:)`](/documentation/CoreML/MLTensor/shapedArray(of:))

Returns a materialized representation of the tensor.

### Removing dimensions from the shape of the tensor

[`squeezingShape()`](/documentation/CoreML/MLTensor/squeezingShape())

Removes all dimensions of size 1 from the shape of the tensor.

[`func   squeezingShape ( at :)`](/documentation/CoreML/MLTensor/squeezingShape(at:))

Removes the specified dimensions of size 1 from the shape of the tensor.

### Accessing the product along an axes

[`func   product ( alongAxes : keepRank :)`](/documentation/CoreML/MLTensor/product(alongAxes:keepRank:))

Returns the product along the specified axes.

[`product(keepRank:)`](/documentation/CoreML/MLTensor/product(keepRank:))

Returns the product along all axes.

### Getting the largest values

[`topK(_:)`](/documentation/CoreML/MLTensor/topK(_:))

Returns the *k* largest values along the last axis of the tensor.

### Clamping and concatenating

[`func   clamped ( to :)`](/documentation/CoreML/MLTensor/clamped(to:))

Clamps all elements to the given lower and upper bounds, inclusively.

[`concatenated(with:alongAxis:)`](/documentation/CoreML/MLTensor/concatenated(with:alongAxis:))

Returns a concatenated tensor along the specified axis.

### Computing the cumulative value

[`cumulativeProduct(alongAxis:)`](/documentation/CoreML/MLTensor/cumulativeProduct(alongAxis:))

Computes the cumulative product along the specified axis.

[`cumulativeSum(alongAxis:)`](/documentation/CoreML/MLTensor/cumulativeSum(alongAxis:))

Computes the cumulative sum along the specified axis.

### Computing the exponent, pow and square root

[`exp()`](/documentation/CoreML/MLTensor/exp())

Computes the natural exponent of the tensor’s elements.

[`exp2()`](/documentation/CoreML/MLTensor/exp2())

Computes the exponent with base two of the tensor’s elements.

[`func   pow ( _ :)`](/documentation/CoreML/MLTensor/pow(_:))

Computes element-wise power of each element with `exponent`.

[`rsqrt()`](/documentation/CoreML/MLTensor/rsqrt())

Computes reverse square root of the tensor’s elements.

[`squared()`](/documentation/CoreML/MLTensor/squared())

Computes square of the tensor’s elements.

[`squareRoot()`](/documentation/CoreML/MLTensor/squareRoot())

Computes square root of the tensor’s elements.

[`log()`](/documentation/CoreML/MLTensor/log())

Computes the natural logarithm of the tensor’s elements.

[`round()`](/documentation/CoreML/MLTensor/round())

Rounds the tensor’s elements.

[`matmul(_:)`](/documentation/CoreML/MLTensor/matmul(_:))

Multiplies two tensors together using matrix multiplication.

### Accessing the extended tensor, sign and reciprocal

[`func   expandingShape ( at :)`](/documentation/CoreML/MLTensor/expandingShape(at:))

Returns a shape-expanded tensor with a dimension of 1 inserted at the specified shape indices.

[`bandPart(lowerBandCount:upperBandCount:)`](/documentation/CoreML/MLTensor/bandPart(lowerBandCount:upperBandCount:))

Returns a new tensor with the same shape where everything outside a central band in each innermost matrix is set to zero.

[`tiled(multiples:)`](/documentation/CoreML/MLTensor/tiled(multiples:))

Returns a tensor by replicating its elements multiple times.

[`sign()`](/documentation/CoreML/MLTensor/sign())

Returns the sign of the tensor’s elements.

[`reciprocal()`](/documentation/CoreML/MLTensor/reciprocal())

Computes the reciprocal of the tensor’s elements.

### Reshaping the tensor

[`flattened()`](/documentation/CoreML/MLTensor/flattened())

Reshape to a one-dimensional tensor.

[`reshaped(to:)`](/documentation/CoreML/MLTensor/reshaped(to:))

Reshape to the specified shape.

### Computing the softmax

[`softmax(alongAxis:)`](/documentation/CoreML/MLTensor/softmax(alongAxis:))

Computes the softmax of the specified tensor along the specified axis.

### Reversing the tensor

[`func   reversed ( alongAxes :)`](/documentation/CoreML/MLTensor/reversed(alongAxes:))

Returns a new tensor with the specified dimensions reversed.

### Accessing a multiarray’s elements

[`subscript(_:)`](/documentation/CoreML/MLTensor/subscript(_:))

[`subscript(_:_:)`](/documentation/CoreML/MLTensor/subscript(_:_:))

[`subscript(_:_:_:)`](/documentation/CoreML/MLTensor/subscript(_:_:_:))

[`subscript(_:_:_:_:)`](/documentation/CoreML/MLTensor/subscript(_:_:_:_:))

[`subscript(_:_:_:_:_:)`](/documentation/CoreML/MLTensor/subscript(_:_:_:_:_:))

## Relationships

### Conforms To

[`CustomReflectable`](/documentation/Swift/CustomReflectable)

[`Copyable`](/documentation/Swift/Copyable)

[`ExpressibleByIntegerLiteral`](/documentation/Swift/ExpressibleByIntegerLiteral)

[`ExpressibleByFloatLiteral`](/documentation/Swift/ExpressibleByFloatLiteral)

[`ExpressibleByArrayLiteral`](/documentation/Swift/ExpressibleByArrayLiteral)

[`Escapable`](/documentation/Swift/Escapable)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`Sendable`](/documentation/Swift/Sendable)

[`ExpressibleByBooleanLiteral`](/documentation/Swift/ExpressibleByBooleanLiteral)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)