tensor
Multi dimensional arrays and numeric operations for machine learning.
import tensor
A tensor is a multi dimensional array of numbers: a vector is one dimensional, a matrix two, and a batch of images four. The data is 32 bit floats in row order. The +, -, * and / operators work element by element and take tensors of the same shape, or shapes that broadcast.
This package is arithmetic, with no gradients. To train a network, use autograd and nn. Matrix multiplication and convolution go multi threaded with SetThreads.
Types
Tensor
struct Tensor| Name | Type | Description |
|---|---|---|
data | Vector | The elements in row order. |
shape | Vector | The dimensions. |
import tensor
import compress
func main:
mut shape := tensor.Shape2(2, 3)
mut a := tensor.Arange(6)
mut b := tensor.Reshape(a, shape)
println tensor.ShapeString(b), b.rank(), b.numel(), b.dim(0), b.dim(1), b.is_empty()
println tensor.ToString(b)
println b.at2(1, 2), b.at(4)
b.set_at2(0, 0, 9.0)
b.set_at(1, 8.0)
b.add_at(2, 0.5)
tensor.Print(b)
mut z := tensor.Zeros(shape)
println tensor.ShapeString(z), z.same_shape(b), tensor.SameShape(z, b)
mut z1 := tensor.Zeros1(3)
mut z2 := tensor.Zeros2(2, 2)
mut z3 := tensor.Zeros3(2, 2, 2)
mut z4 := tensor.Zeros4(1, 2, 2, 2)
println z1.numel(), z2.numel(), z3.numel(), z4.rank()
mut ones := tensor.Ones(tensor.Shape1(3))
mut full := tensor.Full(tensor.Shape3(1, 1, 2), 7.0)
mut scalar := tensor.Scalar(2.5)
println tensor.ToString(ones), tensor.ToString(full), scalar.rank(), scalar.at(0)
println tensor.Shape4(1, 2, 3, 4).len(), tensor.Empty().is_empty()
mut payload := Vector {} as Vector<f32>
payload.push(1.0 as f32)
payload.push(2.0 as f32)
mut from_array := tensor.FromArray(tensor.Shape1(2), payload)
mut copy := tensor.Clone(from_array)
tensor.SetAll(copy, 5.0)
tensor.CopyInto(z1, ones)
println tensor.ToString(from_array), tensor.ToString(copy), tensor.ToString(z1)
mut shape2 := tensor.Shape3(3, 1, 2)
println b.reshape_(shape2), tensor.ShapeString(b)
tensor.SqueezeInPlace(b)
println tensor.ShapeString(b)
tensor.UnsqueezeInPlace(b, 0)
println tensor.ShapeString(b)
mut order := Vector {} as Vector<int>
order.push(2)
order.push(1)
order.push(0)
mut permuted := tensor.Permute(b, order)
println tensor.ShapeString(permuted)
mut m := tensor.Arange(6)
mut m2 := tensor.Reshape(m, shape)
println tensor.ToString(tensor.SliceAxis0(m2, 1, 1)), tensor.ToString(tensor.Narrow(m2, 1, 1, 2))
println tensor.ShapeString(tensor.Concat2(m2, m2, 0)), tensor.ShapeString(tensor.Stack2(m2, m2))
println tensor.ShapeString(tensor.Expand(tensor.Scalar(1.0), shape))
as_matrix := tensor.ToMatrix(m2)
println as_matrix.rows, as_matrix.cols, tensor.ShapeString(tensor.FromMatrix(as_matrix))
mut buffer := compress.NewByteBuf()
println tensor.WriteTensor(buffer, "w", m2)
mut reader := compress.ReaderFromBuf(buffer)
mut name := ""
mut read := tensor.Empty()
println tensor.ReadTensor(reader, name, read), name, tensor.Equal(read, m2)
compress.BufFree(buffer)
println tensor.TSR_TOL
tensor.Free(a)
b.free()
endrank
func rank(): intThe example for this item is shown with the ones above: tensor-Create.salam
numel
func numel(): intThe example for this item is shown with the ones above: tensor-Create.salam
dim
func dim(i: int): intThe example for this item is shown with the ones above: tensor-Create.salam
is_empty
func is_empty(): boolThe example for this item is shown with the ones above: tensor-Create.salam
same_shape
func same_shape(o &: Tensor): boolThe example for this item is shown with the ones above: tensor-Create.salam
at
func at(i: int): f64The example for this item is shown with the ones above: tensor-Create.salam
set_at
func set_at(i: int, v: f64)The example for this item is shown with the ones above: tensor-Create.salam
add_at
func add_at(i: int, v: f64)The example for this item is shown with the ones above: tensor-Create.salam
at2
func at2(i: int, j: int): f64The example for this item is shown with the ones above: tensor-Create.salam
set_at2
func set_at2(i: int, j: int, v: f64)The example for this item is shown with the ones above: tensor-Create.salam
reshape_
func reshape_(shape &: Vector<int>): boolThe example for this item is shown with the ones above: tensor-Create.salam
operator_add
func operator_add(other: Tensor): Tensor+ operator, element by element with broadcasting.The example for this item is shown with the ones above: tensor-Create.salam
operator_sub
func operator_sub(other: Tensor): Tensor- operator.The example for this item is shown with the ones above: tensor-Create.salam
operator_mul
func operator_mul(other: Tensor): Tensor* operator, elementwise multiplication.The example for this item is shown with the ones above: tensor-Create.salam
operator_div
func operator_div(other: Tensor): Tensor/ operator.The example for this item is shown with the ones above: tensor-Create.salam
free
func free()The example for this item is shown with the ones above: tensor-Create.salam
Rng
struct Rng| Name | Type | Description |
|---|---|---|
s | u64 = 0 | The internal state. |
import tensor
func rounded(t &: tensor.Tensor): str:
mut text := ""
repeat t.numel() in i:
if i > 0:
text += " "
end
text += "" + ((t.at(i) * 100.0 + 10000.5) as int - 10000)
end
ret text
end
func main:
mut rng := tensor.NewRng(42)
println rng.next_bits() > 0, rng.next_float() < 1.0, rng.next_normal() < 10.0
println tensor.DeriveSeed(42, 1) != tensor.DeriveSeed(42, 2)
mut v := tensor.Zeros1(5)
tensor.FillUniform(v, 0.0, 1.0, rng)
println tensor.MinAll(v) >= 0.0, tensor.MaxAll(v) < 1.0
tensor.FillNormal(v, 0.0, 1.0, rng)
tensor.FillBernoulli(v, 0.5, rng)
println tensor.SumAll(v) <= 5.0
mut t1 := tensor.RandUniform(tensor.Shape1(3), -1.0, 1.0, 7)
mut t2 := tensor.RandUniform(tensor.Shape1(3), -1.0, 1.0, 7)
println tensor.Equal(t1, t2), rounded(t1)
mut n := tensor.RandNormal(tensor.Shape1(2), 7)
println n.numel()
tensor.SetThreads(2)
println tensor.Threads()
println tensor.ConvOutSize(5, 3, 1, 1), tensor.ConvOutSize(5, 3, 2, 0)
mut image := tensor.Reshape(tensor.Arange(16), tensor.Shape4(1, 1, 4, 4))
mut kernel := tensor.Ones(tensor.Shape4(1, 1, 2, 2))
mut bias := tensor.Zeros1(1)
mut conv := tensor.Conv2DForward(image, kernel, bias, 2, 2, 0, 0)
println tensor.ShapeString(conv), rounded(conv)
mut argmax := tensor.Empty()
mut pooled := tensor.MaxPool2DForward(image, 2, 2, 2, 2, argmax)
println rounded(pooled), rounded(argmax)
println rounded(tensor.AvgPool2DForward(image, 2, 2, 2, 2))
mut cols := tensor.Im2Col(image, 0, 2, 2, 2, 2, 0, 0)
println tensor.ShapeString(cols)
mut grad := tensor.Zeros4(1, 1, 4, 4)
println tensor.Col2ImAcc(cols, grad, 0, 2, 2, 2, 2, 0, 0), tensor.Equal(grad, image)
mut m := tensor.Reshape(tensor.Arange(6), tensor.Shape2(2, 3))
mut mt := tensor.TransposeRaw(m.data.ref(0), 2, 3)
println tensor.ShapeString(mt), rounded(mt)
mut product := tensor.Zeros2(2, 2)
tensor.GemmAcc(m.data.ref(0), mt.data.ref(0), product.data.ref(0), 2, 3, 2)
println rounded(product)
endnext_bits
func next_bits(): u64The example for this item is shown with the ones above: tensor-Random.salam
next_float
func next_float(): f64The example for this item is shown with the ones above: tensor-Random.salam
next_normal
func next_normal(): f64The example for this item is shown with the ones above: tensor-Random.salam
Constants and variables
TSR_TOL
const TSR_TOL: f64 = 1e-05The example for this item is shown with the ones above: tensor-Create.salam
Functions
Transpose2D
func Transpose2D(x &: Tensor): Tensorimport tensor
func show(name: str, t &: tensor.Tensor):
println name, tensor.ToString(t)
end
func show_rounded(name: str, t &: tensor.Tensor):
mut text := ""
repeat t.numel() in i:
rounded := (t.at(i) * 1000.0 + 0.5) as int
text += " " + rounded
end
println name + " x1000:" + text
end
func main:
mut a := tensor.Arange(4)
mut b := tensor.Full(tensor.Shape1(4), 2.0)
mut sum := a + b
show("+", sum)
mut mixed := a * b - b / b
show("*-/", mixed)
mut added := tensor.Add(a, b)
mut subtracted := tensor.Sub(a, b)
mut multiplied := tensor.Mul(a, b)
mut divided := tensor.Div(a, b)
show("Add", added)
show("Sub", subtracted)
show("Mul", multiplied)
show("Div", divided)
mut out := tensor.Zeros1(4)
println tensor.AddInto(a, b, out), tensor.SubInto(a, b, out), tensor.MulInto(a, b, out), tensor.DivInto(a, b, out)
show("DivInto", out)
show("+1", tensor.AddScalar(a, 1.0))
show("-1", tensor.SubScalar(a, 1.0))
show("x3", tensor.Scale(a, 3.0))
show("/2", tensor.DivScalar(a, 2.0))
show("Neg", tensor.Neg(a))
mut d := tensor.Clone(a)
tensor.AddInPlace(d, b)
tensor.SubInPlace(d, a)
tensor.MulInPlace(d, b)
tensor.ScaleInPlace(d, 0.5)
tensor.AddScalarInPlace(d, 1.0)
tensor.AddScaled(d, a, 2.0)
show("in place", d)
show_rounded("Exp", tensor.Clip(tensor.Exp(a), 0.0, 10.0))
show_rounded("Log", tensor.Log(b))
show("Tanh", tensor.Tanh(tensor.Zeros1(2)))
show_rounded("Sigmoid", tensor.Sigmoid(tensor.Zeros1(2)))
show("Relu", tensor.Relu(tensor.Neg(a)))
show_rounded("Gelu", tensor.Gelu(tensor.Zeros1(2)))
show_rounded("Silu", tensor.Silu(tensor.Zeros1(2)))
show("Sqrt", tensor.Sqrt(tensor.Mul(b, b)))
show("Abs", tensor.Abs(tensor.Neg(a)))
show("Square", tensor.Square(a))
show("PowScalar", tensor.PowScalar(a, 3.0))
println tensor.SumAll(a), tensor.MeanAll(a), tensor.MaxAll(a), tensor.MinAll(a), tensor.ArgMaxAll(a)
mut m := tensor.Reshape(tensor.Arange(6), tensor.Shape2(2, 3))
show("SumAxis", tensor.SumAxis(m, 0, false))
show("MeanAxis", tensor.MeanAxis(m, 1, true))
show("MaxAxis", tensor.MaxAxis(m, 1, false))
show("ArgMaxAxis", tensor.ArgMaxAxis(m, 1, false))
show("Cumsum", tensor.Cumsum(m, 1))
println tensor.SumAll(tensor.Softmax(m)) > 1.99, tensor.MaxAll(tensor.LogSoftmax(a)) < 0.0
mut t := tensor.Transpose2D(m)
show("Transpose2D", t)
show("MatMul", tensor.MatMul(m, t))
show("TransposeMul", tensor.TransposeMul(m, m))
show("MulTranspose", tensor.MulTranspose(m, m))
mut product := tensor.Zeros2(2, 2)
println tensor.MatMulInto(m, t, product), tensor.MatMulAcc(m, t, product)
show("MatMulAcc", product)
show("MatVec", tensor.MatVec(m, tensor.Ones(tensor.Shape1(3))))
mut batch := tensor.Stack2(m, m)
mut batch_t := tensor.Stack2(t, t)
println tensor.ShapeString(tensor.BatchMatMul(batch, batch_t)), tensor.Dot(a, b)
println tensor.MaxAbsDiff(a, added), tensor.AllClose(added, sum, 0.001)
endMatMul
func MatMul(x &: Tensor, y &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
MatMulInto
func MatMulInto(x &: Tensor, y &: Tensor, out &: Tensor): boolThe example for this item is shown with the ones above: tensor-Math.salam
MatMulAcc
func MatMulAcc(x &: Tensor, y &: Tensor, out &: Tensor): boolThe example for this item is shown with the ones above: tensor-Math.salam
TransposeMul
func TransposeMul(x &: Tensor, y &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
MulTranspose
func MulTranspose(x &: Tensor, y &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
BatchMatMul
func BatchMatMul(x &: Tensor, y &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
MatVec
func MatVec(a &: Tensor, x &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
TransposeRaw
func TransposeRaw(px: f32*, n: int, m: int): TensorThe example for this item is shown with the ones above: tensor-Random.salam
GemmAcc
func GemmAcc(px: f32*, py: f32*, pr: f32*, n: int, k: int, m: int)The example for this item is shown with the ones above: tensor-Random.salam
Dot
func Dot(x &: Tensor, y &: Tensor): f64The example for this item is shown with the ones above: tensor-Math.salam
SetThreads
func SetThreads(n: int)The example for this item is shown with the ones above: tensor-Random.salam
Threads
func Threads(): intThe example for this item is shown with the ones above: tensor-Random.salam
Add
func Add(x &: Tensor, y &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
Sub
func Sub(x &: Tensor, y &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
Mul
func Mul(x &: Tensor, y &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
Div
func Div(x &: Tensor, y &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
AddInto
func AddInto(x &: Tensor, y &: Tensor, out &: Tensor): boolThe example for this item is shown with the ones above: tensor-Math.salam
SubInto
func SubInto(x &: Tensor, y &: Tensor, out &: Tensor): boolThe example for this item is shown with the ones above: tensor-Math.salam
MulInto
func MulInto(x &: Tensor, y &: Tensor, out &: Tensor): boolThe example for this item is shown with the ones above: tensor-Math.salam
DivInto
func DivInto(x &: Tensor, y &: Tensor, out &: Tensor): boolThe example for this item is shown with the ones above: tensor-Math.salam
AddScalar
func AddScalar(x &: Tensor, s: f64): TensorThe example for this item is shown with the ones above: tensor-Math.salam
SubScalar
func SubScalar(x &: Tensor, s: f64): TensorThe example for this item is shown with the ones above: tensor-Math.salam
Scale
func Scale(x &: Tensor, s: f64): TensorThe example for this item is shown with the ones above: tensor-Math.salam
DivScalar
func DivScalar(x &: Tensor, s: f64): TensorThe example for this item is shown with the ones above: tensor-Math.salam
Neg
func Neg(x &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
AddInPlace
func AddInPlace(dst &: Tensor, src &: Tensor)The example for this item is shown with the ones above: tensor-Math.salam
SubInPlace
func SubInPlace(dst &: Tensor, src &: Tensor)The example for this item is shown with the ones above: tensor-Math.salam
MulInPlace
func MulInPlace(dst &: Tensor, src &: Tensor)The example for this item is shown with the ones above: tensor-Math.salam
ScaleInPlace
func ScaleInPlace(dst &: Tensor, s: f64)The example for this item is shown with the ones above: tensor-Math.salam
AddScalarInPlace
func AddScalarInPlace(dst &: Tensor, s: f64)The example for this item is shown with the ones above: tensor-Math.salam
AddScaled
func AddScaled(dst &: Tensor, src &: Tensor, alpha: f64)The example for this item is shown with the ones above: tensor-Math.salam
Exp
func Exp(x &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
Log
func Log(x &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
Tanh
func Tanh(x &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
Sigmoid
func Sigmoid(x &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
Relu
func Relu(x &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
Gelu
func Gelu(x &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
Sqrt
func Sqrt(x &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
Abs
func Abs(x &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
Square
func Square(x &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
Silu
func Silu(x &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
PowScalar
func PowScalar(x &: Tensor, p: f64): TensorThe example for this item is shown with the ones above: tensor-Math.salam
Clip
func Clip(x &: Tensor, lo: f64, hi: f64): TensorThe example for this item is shown with the ones above: tensor-Create.salam
BroadcastShape
func BroadcastShape(a &: Tensor, b &: Tensor, out &: Vector<int>): boolimport tensor
func show(name: str, t &: tensor.Tensor):
println name, tensor.ToString(t)
end
func main:
mut a := tensor.Arange(4)
mut b := tensor.Full(tensor.Shape1(4), 1.5)
show("Greater", tensor.Greater(a, b))
show("GreaterEqual", tensor.GreaterEqual(a, tensor.Clone(a)))
show("Less", tensor.Less(a, b))
show("LessEqual", tensor.LessEqual(a, b))
show("EqualMask", tensor.EqualMask(a, a))
show("NotEqualMask", tensor.NotEqualMask(a, b))
show("GreaterScalar", tensor.GreaterScalar(a, 1.0))
show("LessScalar", tensor.LessScalar(a, 1.0))
show("EqualScalar", tensor.EqualScalar(a, 2.0))
mut mask := tensor.Greater(a, b)
show("Where", tensor.Where(mask, a, tensor.Neg(a)))
show("MaskedFill", tensor.MaskedFill(a, mask, -1.0))
mut m := tensor.Reshape(tensor.Arange(6), tensor.Shape2(3, 2))
mut indices := Vector {} as Vector<int>
indices.push(2)
indices.push(0)
show("IndexSelect", tensor.IndexSelect(m, 0, indices))
show("TakeRows", tensor.TakeRows(m, indices))
mut places := tensor.Zeros2(3, 1)
places.set_at(0, 1.0)
show("Gather", tensor.Gather(m, 1, places))
mut target := tensor.Zeros2(3, 2)
println tensor.ScatterAddRows(target, indices, tensor.Ones(tensor.Shape2(2, 2)))
show("ScatterAddRows", target)
show("OneHot", tensor.OneHot(indices, 3))
mut scores := tensor.Reshape(tensor.Arange(5), tensor.Shape1(5))
mut top_places := tensor.Empty()
show("TopK", tensor.TopK(scores, 2, true, top_places))
show("TopK places", top_places)
mut square := tensor.Ones(tensor.Shape2(3, 3))
show("Tril", tensor.Tril(square, 0))
show("Triu", tensor.Triu(square, 1))
mut broadcast := Vector {} as Vector<int>
println tensor.BroadcastShape(m, tensor.Ones(tensor.Shape1(2)), broadcast), broadcast.len(), broadcast.get(0)
show("Pad2D", tensor.Pad2D(tensor.Ones(tensor.Shape4(1, 1, 1, 1)), 1, 0, 0, 1))
endExpand
func Expand(x &: Tensor, shape &: Vector<int>): TensorThe example for this item is shown with the ones above: tensor-Create.salam
Greater
func Greater(a &: Tensor, b &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Index.salam
GreaterEqual
func GreaterEqual(a &: Tensor, b &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Index.salam
Less
func Less(a &: Tensor, b &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Index.salam
LessEqual
func LessEqual(a &: Tensor, b &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Index.salam
EqualMask
func EqualMask(a &: Tensor, b &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Index.salam
NotEqualMask
func NotEqualMask(a &: Tensor, b &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Index.salam
GreaterScalar
func GreaterScalar(x &: Tensor, s: f64): TensorThe example for this item is shown with the ones above: tensor-Index.salam
LessScalar
func LessScalar(x &: Tensor, s: f64): TensorThe example for this item is shown with the ones above: tensor-Index.salam
EqualScalar
func EqualScalar(x &: Tensor, s: f64): TensorThe example for this item is shown with the ones above: tensor-Index.salam
Where
func Where(cond &: Tensor, a &: Tensor, b &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Index.salam
MaskedFill
func MaskedFill(x &: Tensor, mask &: Tensor, value: f64): TensorThe example for this item is shown with the ones above: tensor-Index.salam
IndexSelect
func IndexSelect(x &: Tensor, axis: int, ids &: Vector<int>): TensorThe example for this item is shown with the ones above: tensor-Index.salam
TakeRows
func TakeRows(x &: Tensor, ids &: Vector<int>): TensorThe example for this item is shown with the ones above: tensor-Index.salam
Gather
func Gather(x &: Tensor, axis: int, idx &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Index.salam
ScatterAddRows
func ScatterAddRows(dst &: Tensor, ids &: Vector<int>, src &: Tensor): boolThe example for this item is shown with the ones above: tensor-Index.salam
OneHot
func OneHot(ids &: Vector<int>, classes: int): TensorThe example for this item is shown with the ones above: tensor-Index.salam
TopK
func TopK(x &: Tensor, k: int, largest: bool, idx_out &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Index.salam
Tril
func Tril(x &: Tensor, diagonal: int): TensorThe example for this item is shown with the ones above: tensor-Index.salam
Triu
func Triu(x &: Tensor, diagonal: int): TensorThe example for this item is shown with the ones above: tensor-Index.salam
Cumsum
func Cumsum(x &: Tensor, axis: int): TensorThe example for this item is shown with the ones above: tensor-Math.salam
ConvOutSize
func ConvOutSize(size: int, k: int, s: int, p: int): intThe example for this item is shown with the ones above: tensor-Random.salam
Im2Col
func Im2Col(x &: Tensor, sample: int, kh: int, kw: int, sh: int, sw: int, ph: int, pw: int): TensorThe example for this item is shown with the ones above: tensor-Random.salam
Col2ImAcc
func Col2ImAcc(cols &: Tensor, dx &: Tensor, sample: int, kh: int, kw: int, sh: int, sw: int, ph: int, pw: int): boolThe example for this item is shown with the ones above: tensor-Random.salam
Conv2DForward
func Conv2DForward(x &: Tensor, w &: Tensor, bias &: Tensor, sh: int, sw: int, ph: int, pw: int): TensorThe example for this item is shown with the ones above: tensor-Random.salam
MaxPool2DForward
func MaxPool2DForward(x &: Tensor, kh: int, kw: int, sh: int, sw: int, argmax_out &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Random.salam
AvgPool2DForward
func AvgPool2DForward(x &: Tensor, kh: int, kw: int, sh: int, sw: int): TensorThe example for this item is shown with the ones above: tensor-Random.salam
SumAll
func SumAll(x &: Tensor): f64The example for this item is shown with the ones above: tensor-Math.salam
MeanAll
func MeanAll(x &: Tensor): f64The example for this item is shown with the ones above: tensor-Math.salam
MaxAll
func MaxAll(x &: Tensor): f64The example for this item is shown with the ones above: tensor-Math.salam
MinAll
func MinAll(x &: Tensor): f64The example for this item is shown with the ones above: tensor-Math.salam
ArgMaxAll
func ArgMaxAll(x &: Tensor): intThe example for this item is shown with the ones above: tensor-Math.salam
SumAxis
func SumAxis(x &: Tensor, axis: int, keepdim: bool): TensorThe example for this item is shown with the ones above: tensor-Math.salam
MeanAxis
func MeanAxis(x &: Tensor, axis: int, keepdim: bool): TensorThe example for this item is shown with the ones above: tensor-Math.salam
MaxAxis
func MaxAxis(x &: Tensor, axis: int, keepdim: bool): TensorThe example for this item is shown with the ones above: tensor-Math.salam
ArgMaxAxis
func ArgMaxAxis(x &: Tensor, axis: int, keepdim: bool): TensorThe example for this item is shown with the ones above: tensor-Math.salam
Softmax
func Softmax(x &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
LogSoftmax
func LogSoftmax(x &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Math.salam
Reshape
func Reshape(x &: Tensor, shape &: Vector<int>): TensorThe example for this item is shown with the ones above: tensor-Create.salam
SqueezeInPlace
func SqueezeInPlace(x &: Tensor)The example for this item is shown with the ones above: tensor-Create.salam
UnsqueezeInPlace
func UnsqueezeInPlace(x &: Tensor, axis: int)The example for this item is shown with the ones above: tensor-Create.salam
Permute
func Permute(x &: Tensor, perm &: Vector<int>): TensorThe example for this item is shown with the ones above: tensor-Create.salam
SliceAxis0
func SliceAxis0(x &: Tensor, start: int, len: int): TensorThe example for this item is shown with the ones above: tensor-Create.salam
Narrow
func Narrow(x &: Tensor, axis: int, start: int, len: int): TensorThe example for this item is shown with the ones above: tensor-Create.salam
Concat2
func Concat2(a &: Tensor, b &: Tensor, axis: int): TensorThe example for this item is shown with the ones above: tensor-Create.salam
Stack2
func Stack2(a &: Tensor, b &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Create.salam
Pad2D
func Pad2D(x &: Tensor, pt: int, pb: int, pl: int, pr: int): TensorThe example for this item is shown with the ones above: tensor-Index.salam
ShapeString
func ShapeString(x &: Tensor): str2x3.The example for this item is shown with the ones above: tensor-Create.salam
ToString
func ToString(x &: Tensor): strThe example for this item is shown with the ones above: tensor-Create.salam
Print
func Print(x &: Tensor)The example for this item is shown with the ones above: tensor-Create.salam
MaxAbsDiff
func MaxAbsDiff(x &: Tensor, y &: Tensor): f64The example for this item is shown with the ones above: tensor-Math.salam
AllClose
func AllClose(x &: Tensor, y &: Tensor, tol: f64): boolThe example for this item is shown with the ones above: tensor-Math.salam
Equal
func Equal(x &: Tensor, y &: Tensor): boolThe example for this item is shown with the ones above: tensor-Create.salam
Empty
func Empty(): TensorThe example for this item is shown with the ones above: tensor-Create.salam
Shape1
func Shape1(n: int): Vector<int>The example for this item is shown with the ones above: tensor-Create.salam
Shape2
func Shape2(a: int, b: int): Vector<int>The example for this item is shown with the ones above: tensor-Create.salam
Shape3
func Shape3(a: int, b: int, c: int): Vector<int>The example for this item is shown with the ones above: tensor-Create.salam
Shape4
func Shape4(a: int, b: int, c: int, d: int): Vector<int>The example for this item is shown with the ones above: tensor-Create.salam
Zeros
func Zeros(shape &: Vector<int>): TensorThe example for this item is shown with the ones above: tensor-Create.salam
Zeros1
func Zeros1(n: int): TensorThe example for this item is shown with the ones above: tensor-Create.salam
Zeros2
func Zeros2(r: int, c: int): TensorThe example for this item is shown with the ones above: tensor-Create.salam
Zeros3
func Zeros3(a: int, b: int, c: int): TensorThe example for this item is shown with the ones above: tensor-Create.salam
Zeros4
func Zeros4(a: int, b: int, c: int, d: int): TensorThe example for this item is shown with the ones above: tensor-Create.salam
Full
func Full(shape &: Vector<int>, x: f64): TensorThe example for this item is shown with the ones above: tensor-Create.salam
Ones
func Ones(shape &: Vector<int>): TensorThe example for this item is shown with the ones above: tensor-Create.salam
Scalar
func Scalar(x: f64): TensorThe example for this item is shown with the ones above: tensor-Create.salam
FromArray
func FromArray(shape &: Vector<int>, data &: Vector<f32>): TensorThe example for this item is shown with the ones above: tensor-Create.salam
Arange
func Arange(n: int): TensorThe example for this item is shown with the ones above: tensor-Create.salam
Clone
func Clone(x &: Tensor): TensorThe example for this item is shown with the ones above: tensor-Create.salam
CopyInto
func CopyInto(dst &: Tensor, src &: Tensor)The example for this item is shown with the ones above: tensor-Create.salam
Free
func Free(x &: Tensor)The example for this item is shown with the ones above: tensor-Create.salam
SetAll
func SetAll(x &: Tensor, v: f64)The example for this item is shown with the ones above: tensor-Create.salam
SameShape
func SameShape(x &: Tensor, y &: Tensor): boolThe example for this item is shown with the ones above: tensor-Create.salam
ToMatrix
func ToMatrix(x &: Tensor): matrix.MatrixThe example for this item is shown with the ones above: tensor-Create.salam
FromMatrix
func FromMatrix(m &: matrix.Matrix): TensorThe example for this item is shown with the ones above: tensor-Create.salam
WriteTensor
func WriteTensor(buf &: compress.ByteBuf, name: str, t &: Tensor): boolThe example for this item is shown with the ones above: tensor-Create.salam
ReadTensor
func ReadTensor(rd &: compress.ByteReader, name_out &: str, out &: Tensor): boolThe example for this item is shown with the ones above: tensor-Create.salam
NewRng
func NewRng(seed: i64): RngThe example for this item is shown with the ones above: tensor-Random.salam
DeriveSeed
func DeriveSeed(seed: i64, index: i64): i64The example for this item is shown with the ones above: tensor-Random.salam
FillUniform
func FillUniform(t &: Tensor, lo: f64, hi: f64, g &: Rng)The example for this item is shown with the ones above: tensor-Random.salam
FillNormal
func FillNormal(t &: Tensor, mean: f64, std: f64, g &: Rng)The example for this item is shown with the ones above: tensor-Random.salam
FillBernoulli
func FillBernoulli(t &: Tensor, p: f64, g &: Rng)The example for this item is shown with the ones above: tensor-Random.salam
RandUniform
func RandUniform(shape &: Vector<int>, lo: f64, hi: f64, seed: i64): TensorThe example for this item is shown with the ones above: tensor-Random.salam
RandNormal
func RandNormal(shape &: Vector<int>, seed: i64): TensorThe example for this item is shown with the ones above: tensor-Random.salam