TensorJavaOps

Java-friendly static utility class wrapping the most common tensor operations.

All methods are static and accept/return Tensor<*, *> to avoid requiring Java developers to deal with Kotlin generic type parameters.

Example usage from Java:

var c = TensorJavaOps.matmul(a, b);
var d = TensorJavaOps.relu(c);
var e = TensorJavaOps.softmax(d, -1);

Functions

Link copied to clipboard
fun abs(a: Tensor<*, *>): Tensor<*, *>

Element-wise absolute value.

Link copied to clipboard
fun add(a: Tensor<*, *>, b: Tensor<*, *>): Tensor<*, *>

Element-wise addition of two tensors.

Link copied to clipboard
fun addScalar(a: Tensor<*, *>, value: Number): Tensor<*, *>

Add a scalar value to every element.

Link copied to clipboard
fun clamp(a: Tensor<*, *>, minVal: Float, maxVal: Float): Tensor<*, *>

Clamp values to minVal, maxVal.

Link copied to clipboard
fun divide(a: Tensor<*, *>, b: Tensor<*, *>): Tensor<*, *>

Element-wise division: a / b.

Link copied to clipboard
fun divScalar(a: Tensor<*, *>, value: Number): Tensor<*, *>

Divide every element by a scalar value.

Link copied to clipboard
fun elu(a: Tensor<*, *>, alpha: Float = 1.0f): Tensor<*, *>

ELU activation.

Link copied to clipboard
fun flatten(a: Tensor<*, *>, startDim: Int = 0, endDim: Int = -1): Tensor<*, *>

Flatten the tensor between startDim and endDim.

Link copied to clipboard
fun ge(a: Tensor<*, *>, value: Float): Tensor<*, *>

Element-wise greater-than-or-equal comparison.

Link copied to clipboard
fun gelu(a: Tensor<*, *>): Tensor<*, *>

GELU activation (Gaussian Error Linear Unit).

Link copied to clipboard
fun leakyRelu(a: Tensor<*, *>, negativeSlope: Float = 0.01f): Tensor<*, *>

Leaky ReLU activation.

Link copied to clipboard
fun logSoftmax(a: Tensor<*, *>, dim: Int = -1): Tensor<*, *>

Log-softmax along the given dimension.

Link copied to clipboard
fun lt(a: Tensor<*, *>, value: Float): Tensor<*, *>

Element-wise less-than comparison.

Link copied to clipboard
fun matmul(a: Tensor<*, *>, b: Tensor<*, *>): Tensor<*, *>

Matrix multiplication of two tensors.

Link copied to clipboard
fun mean(a: Tensor<*, *>, dim: Int? = null): Tensor<*, *>

Mean of all elements, or mean along a dimension.

Link copied to clipboard
fun mulScalar(a: Tensor<*, *>, value: Number): Tensor<*, *>

Multiply every element by a scalar value.

Link copied to clipboard
fun multiply(a: Tensor<*, *>, b: Tensor<*, *>): Tensor<*, *>

Element-wise multiplication of two tensors.

Link copied to clipboard
fun narrow(a: Tensor<*, *>, dim: Int, start: Int, length: Int): Tensor<*, *>

Extract a narrow slice along a dimension.

Link copied to clipboard
fun relu(a: Tensor<*, *>): Tensor<*, *>

ReLU activation: max(0, x).

Link copied to clipboard
fun reshape(a: Tensor<*, *>, newShape: IntArray): Tensor<*, *>

Reshape the tensor to the given dimensions.

Link copied to clipboard
fun sigmoid(a: Tensor<*, *>): Tensor<*, *>

Sigmoid activation: 1 / (1 + exp(-x)).

Link copied to clipboard
fun sign(a: Tensor<*, *>): Tensor<*, *>

Element-wise sign (-1, 0, or 1).

Link copied to clipboard
fun silu(a: Tensor<*, *>): Tensor<*, *>

SiLU (Swish) activation: x * sigmoid(x).

Link copied to clipboard
fun softmax(a: Tensor<*, *>, dim: Int = -1): Tensor<*, *>

Softmax along the given dimension.

Link copied to clipboard
fun sqrt(a: Tensor<*, *>): Tensor<*, *>

Element-wise square root.

Link copied to clipboard
fun squeeze(a: Tensor<*, *>, dim: Int? = null): Tensor<*, *>

Remove dimensions of size 1.

Link copied to clipboard
fun subScalar(a: Tensor<*, *>, value: Number): Tensor<*, *>

Subtract a scalar value from every element.

Link copied to clipboard
fun subtract(a: Tensor<*, *>, b: Tensor<*, *>): Tensor<*, *>

Element-wise subtraction: a - b.

Link copied to clipboard
fun sum(a: Tensor<*, *>, dim: Int? = null): Tensor<*, *>

Sum all elements, or sum along a dimension if dim is specified.

Link copied to clipboard
fun transpose(a: Tensor<*, *>): Tensor<*, *>

Transpose a 2D tensor.

Link copied to clipboard
fun tril(a: Tensor<*, *>, k: Int = 0): Tensor<*, *>

Returns the lower triangular part of a matrix.

Link copied to clipboard
fun unsqueeze(a: Tensor<*, *>, dim: Int): Tensor<*, *>

Add a dimension of size 1 at the given position.

Link copied to clipboard
fun variance(a: Tensor<*, *>, dim: Int? = null): Tensor<*, *>

Variance of all elements, or variance along a dimension.