Node
#struct NodeOne node on the tape.
| Name | Type | Description |
|---|---|---|
op | int = 0 | The operation. |
a | int = -1 | The first input. |
b | int = -1 | The second input. |
out | int = -1 | The output. |
aux | int = -1 | The auxiliary data. |
s0 | int = 0 | The first integer parameter. |
s1 | int = 0 | The second integer parameter. |
f0 | f64 = 0 | The float parameter. |
import ai.autograd
import ai.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 tape := autograd.NewTape(1)
mut x_value := tensor.Arange(3)
mut three_value := tensor.Full(tensor.Shape1(3), 3.0)
x := autograd.Watch(tape, x_value)
three := autograd.Constant(tape, three_value)
squared := autograd.Square(tape, x)
linear := autograd.Mul(tape, three, x)
loss := autograd.Sum(tape, autograd.Add(tape, squared, linear))
autograd.Backward(tape, loss)
println rounded(autograd.Value(tape, loss)), rounded(autograd.Grad(tape, x)), tape.Len()
tape.Reset()
a := autograd.Input(tape, x_value)
one := autograd.AddScalar(tape, a, 1.0)
every := [
autograd.Sub(
tape, a, one
), autograd.Div(
tape, a, one
), autograd.Neg(tape, a), autograd.Scale(
tape, a, 2.0
), autograd.Exp(
tape, a
), autograd.Log(
tape, one
), autograd.Tanh(
tape, a
), autograd.Sigmoid(
tape, a
), autograd.Relu(
tape, a
), autograd.Gelu(
tape, a
), autograd.Silu(tape, a), autograd.Sqrt(tape, one), autograd.PowScalar(
tape, a, 3.0
), autograd.Softmax(tape, a), autograd.LogSoftmax(tape, a), autograd.Mean(tape, a)
]
each node in every:
println rounded(autograd.Value(tape, node))
end
tape.Reset()
mut m_value := tensor.Reshape(tensor.Arange(6), tensor.Shape2(2, 3))
mut m_grad := tensor.Zeros2(2, 3)
m := autograd.Param(tape, m_value, m_grad)
t := autograd.Transpose2D(tape, m)
product := autograd.MatMul(tape, m, t)
println rounded(autograd.Value(tape, product))
println rounded(autograd.Value(tape, autograd.SumAxis(tape, m, 0, false)))
println rounded(autograd.Value(tape, autograd.MeanAxis(tape, m, 1, false)))
mut shape := tensor.Shape1(6)
println tensor.ShapeString(autograd.Value(tape, autograd.Reshape(tape, m, shape)))
autograd.Backward(tape, autograd.Sum(tape, product))
println rounded(m_grad)
tape.Free()
end1400 300 500 700 6 -100 -100 -100 0 50 67 0 -100 -200 0 200 400 100 272 739 0 69 110 0 76 96 50 73 88 0 100 200 0 84 195 0 73 176 100 141 173 0 100 800 9 24 67 -241 -141 -41 100 500 1400 1400 5000 300 500 700 100 400 6 600 1000 1400 600 1000 1400