~/bend-docscommunity

tensor_training.bend checks

raw source on the hub · import stelliferous@0.0.4.0/tensor_training.bend as Tensor_training

Stelliferous training: layers, residual networks, losses and SGD. The layers are convolution, batch norm, layer norm, activation and pool. A layer is a value. forward returns the output, what backward reads and the layer, backward the input gradient and the layer holding its parameter gradients, evaluate the output in inference mode, and sgd_step the layer with its gradients applied. A network runs layers and residual blocks in order through the same steps. Every step is a public operation in a fixed FP32 order, so results do not depend on the worker count.

9 imports
import Base
import ./math_activation.bend as Activations
import ./tensor_f32.bend as F
import ./tensor_tiled.bend as Tiled
import ./tensor_layer.bend as ConvLayer
import ./tensor_batch_norm.bend as Norm
import ./tensor_layer_norm.bend as LineNorm
import ./tensor_loss.bend as Loss
import ./tensor_optimizer.bend as Optimizer

Types

type Gradient source · line 23 · raw

Type

The gradient of one parameter: none before backward and after sgd_step.

type Layer source · line 31 · raw

Type

A convolution of weights [O,C,k,k] and bias [O]; a batch normalization of gamma, beta and the running statistics, each [C]; a layer normalization of gamma and beta, each [D]; an activation; the global average pool of [N,C,H,W] to [N,C,1,1]. A parameter has its gradient and its velocity.

type Saved source · line 43 · raw

Type

What one backward call of a layer reads.

type Descent source · line 232 · raw

Data

What one step does to every parameter that has a gradient.

type Network source · line 286 · raw

Type

Layers in order. A residual block adds the output of its branch to the output of its shortcut, both on the block's input, and gives the sum to rest; an empty shortcut passes the input on.

type Tape source · line 292 · raw

Type

What backward_network reads, the last layer first.

type Numbered source · line 490 · raw

Type

The layers in order, each under its number.

Definitions

def Tensor source · line 19 · raw

Type

def conv2d_layer source · line 52 · raw

@weights:Tensor -> @bias:Tensor -> @stride:U32 -> @padding:U32 -> Layer

def batch_norm_layer_with source · line 56 · raw

@+channels:U32 -> @epsilon:F32 -> @momentum:F32 -> Layer

gamma 1, beta 0, running mean 0 and running variance 1, as in PyTorch.

def batch_norm_layer source · line 60 · raw

@+channels:U32 -> Layer

epsilon 1e-5 and momentum 0.1, as in PyTorch.

def batch_norm_layer_from source · line 64 · raw

@gamma:Tensor -> @beta:Tensor -> @running_mean:Tensor -> @running_variance:Tensor -> @epsilon:F32 -> @momentum:F32 -> Layer

A batch normalization with the given parameters and running statistics.

def layer_norm_layer_with source · line 68 · raw

@+features:U32 -> @epsilon:F32 -> Layer

gamma 1 and beta 0 for D features over the last axis, as in PyTorch.

def layer_norm_layer source · line 72 · raw

@+features:U32 -> Layer

epsilon 1e-5, as in PyTorch.

def layer_norm_layer_from source · line 76 · raw

@gamma:Tensor -> @beta:Tensor -> @epsilon:F32 -> Layer

A layer normalization with the given parameters.

def activation_layer source · line 79 · raw

@activation:0xa2af805348d03b11eb853850859654f6/math_activation.Activation -> Layer

def average_pool_layer source · line 82 · raw

Layer

def convolved source · line 87 · raw

@+stride:U32 -> @+padding:U32 -> @weight_gradient:Gradient -> @bias_gradient:Gradient -> @weight_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @bias_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @result:Pair(Tensor, Pair(0xa2af805348d03b11eb853850859654f6/tensor_layer.Saved, Pair(Tensor, Tensor))) -> Pair(Tensor, Pair(Saved, Layer))

def normalized source · line 92 · raw

@+epsilon:F32 -> @+momentum:F32 -> @gamma_gradient:Gradient -> @beta_gradient:Gradient -> @gamma_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @beta_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @result:0xa2af805348d03b11eb853850859654f6/tensor_batch_norm.Forward -> Pair(Tensor, Pair(Saved, Layer))

def line_normalized source · line 96 · raw

@+epsilon:F32 -> @gamma_gradient:Gradient -> @beta_gradient:Gradient -> @gamma_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @beta_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @result:0xa2af805348d03b11eb853850859654f6/tensor_layer_norm.Forward -> Pair(Tensor, Pair(Saved, Layer))

def activated source · line 100 · raw

@+activation:0xa2af805348d03b11eb853850859654f6/math_activation.Activation -> @pair:Pair(Tensor, Tensor) -> Pair(Tensor, Pair(Saved, Layer))

def pooled_extents source · line 105 · raw

@shape:List<&2, U32> -> List<&2, U32>

[N,C,H,W] to [N,C,1,1]; another rank gives the extents unchanged.

def pooled_forward source · line 110 · raw

@measured:Pair(Tensor, List<&2, U32>) -> Pair(Tensor, Pair(Saved, Layer))

def forward source · line 118 · raw

@x:Tensor -> @layer:Layer -> Pair(Tensor, Pair(Saved, Layer))

(the output, (what backward reads, the layer)). A convolution keeps its input and output; a batch normalization normalizes with the statistics of the batch and moves its running statistics; a layer normalization normalizes each line of the last axis; the pool averages each channel of each image.

def convolution_back source · line 131 · raw

@+stride:U32 -> @+padding:U32 -> @bias:Tensor -> @weight_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @bias_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @result:Pair(Tensor, Pair(Tensor, Pair(Tensor, Tensor))) -> Pair(Tensor, Layer)

def norm_back source · line 135 · raw

@beta:Tensor -> @running_mean:Tensor -> @running_variance:Tensor -> @+epsilon:F32 -> @+momentum:F32 -> @gamma_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @beta_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @result:0xa2af805348d03b11eb853850859654f6/tensor_batch_norm.Backward -> Pair(Tensor, Layer)

def line_norm_back source · line 139 · raw

@beta:Tensor -> @+epsilon:F32 -> @gamma_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @beta_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @result:0xa2af805348d03b11eb853850859654f6/tensor_layer_norm.Backward -> Pair(Tensor, Layer)

def pooled_backward source · line 144 · raw

@dy:Tensor -> @shape:List<&2, U32> -> Tensor

The average of H * W positions spreads its gradient over them.

def unmatched source · line 151 · raw

@dy:Tensor -> @saved:Saved -> @layer:Layer -> Pair(Tensor, Layer)

A saved value of another kind of layer.

def convolution_backward source · line 154 · raw

@dy:Tensor -> @saved:Saved -> @weights:Tensor -> @bias:Tensor -> @+stride:U32 -> @+padding:U32 -> @weight_gradient:Gradient -> @bias_gradient:Gradient -> @weight_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @bias_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> Pair(Tensor, Layer)

def norm_backward source · line 160 · raw

@dy:Tensor -> @saved:Saved -> @gamma:Tensor -> @beta:Tensor -> @running_mean:Tensor -> @running_variance:Tensor -> @+epsilon:F32 -> @+momentum:F32 -> @gamma_gradient:Gradient -> @beta_gradient:Gradient -> @gamma_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @beta_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> Pair(Tensor, Layer)

def line_norm_backward source · line 166 · raw

@dy:Tensor -> @saved:Saved -> @gamma:Tensor -> @beta:Tensor -> @+epsilon:F32 -> @gamma_gradient:Gradient -> @beta_gradient:Gradient -> @gamma_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @beta_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> Pair(Tensor, Layer)

def activation_backward source · line 171 · raw

@dy:Tensor -> @saved:Saved -> @+activation:0xa2af805348d03b11eb853850859654f6/math_activation.Activation -> Pair(Tensor, Layer)

def pool_backward source · line 176 · raw

@dy:Tensor -> @saved:Saved -> Pair(Tensor, Layer)

def backward source · line 185 · raw

@dy:Tensor -> @saved:Saved -> @layer:Layer -> Pair(Tensor, Layer)

dy, the gradient of the loss with respect to the layer output, and what forward saved: (the gradient of the layer input, the layer holding the gradients of its parameters). A saved value of another kind of layer gives a failed input gradient.

def convolution_evaluated source · line 198 · raw

@x:Tensor -> @+stride:U32 -> @+padding:U32 -> @weight_gradient:Gradient -> @bias_gradient:Gradient -> @weight_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @bias_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @weight_copies:Pair(Tensor, Tensor) -> @bias_copies:Pair(Tensor, Tensor) -> Pair(Tensor, Layer)

def norm_evaluated source · line 204 · raw

@+epsilon:F32 -> @+momentum:F32 -> @gamma_gradient:Gradient -> @beta_gradient:Gradient -> @gamma_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @beta_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @result:0xa2af805348d03b11eb853850859654f6/tensor_batch_norm.Evaluated -> Pair(Tensor, Layer)

def line_norm_evaluated source · line 208 · raw

@+epsilon:F32 -> @gamma_gradient:Gradient -> @beta_gradient:Gradient -> @gamma_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @beta_velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @result:0xa2af805348d03b11eb853850859654f6/tensor_layer_norm.Evaluated -> Pair(Tensor, Layer)

def pooled_evaluated source · line 212 · raw

@measured:Pair(Tensor, List<&2, U32>) -> Pair(Tensor, Layer)

def evaluate source · line 218 · raw

@x:Tensor -> @layer:Layer -> Pair(Tensor, Layer)

The output in inference mode: a batch normalization uses its running statistics and leaves them unchanged; a layer normalization has none.

def descended source · line 235 · raw

@parameter:Tensor -> @tensor:Tensor -> @velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @+descent:Descent -> Pair(Tensor, 0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity)

def stepped source · line 239 · raw

@parameter:Tensor -> @gradient:Gradient -> @velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> @+descent:Descent -> Pair(Tensor, 0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity)

def convolution_stepped source · line 244 · raw

@+stride:U32 -> @+padding:U32 -> @weights:Pair(Tensor, 0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity) -> @bias:Pair(Tensor, 0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity) -> Layer

def norm_stepped source · line 249 · raw

@running_mean:Tensor -> @running_variance:Tensor -> @+epsilon:F32 -> @+momentum:F32 -> @gamma:Pair(Tensor, 0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity) -> @beta:Pair(Tensor, 0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity) -> Layer

def line_norm_stepped source · line 254 · raw

@+epsilon:F32 -> @gamma:Pair(Tensor, 0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity) -> @beta:Pair(Tensor, 0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity) -> Layer

def layer_stepped source · line 259 · raw

@layer:Layer -> @+descent:Descent -> Layer

def sgd_step_with source · line 273 · raw

@layer:Layer -> @rate:F32 -> @momentum:F32 -> @weight_decay:F32 -> Layer

Every parameter with a gradient takes one step of Optimizer.sgd_with, and the gradients are cleared. The layer keeps the velocities for the next step. A layer without gradients comes back unchanged.

def sgd_step source · line 278 · raw

@layer:Layer -> @rate:F32 -> Layer

Without momentum and weight decay: every parameter with a gradient becomes parameter - gradient * rate.

def recorded source · line 301 · raw

@result:Pair(Tensor, Pair(Saved, Layer)) -> @next:(@_:Tensor -> @_:Saved -> @_:Layer -> Pair(Tensor, Tape)) -> Pair(Tensor, Tape)

def joined source · line 305 · raw

@branch:Pair(Tensor, Tape) -> @shortcut:Pair(Tensor, Tape) -> @next:(@_:Tensor -> @_:Tensor -> @_:Tape -> @_:Tape -> Pair(Tensor, Tape)) -> Pair(Tensor, Tape)

def network_forward source · line 310 · raw

@network:Network -> @x:Tensor -> @tape:Tape -> Pair(Tensor, Tape)

def forward_network source · line 321 · raw

@x:Tensor -> @network:Network -> Pair(Tensor, Tape)

(the output, what backward_network reads). Batch normalizations use the statistics of the batch and move their running statistics.

def returned source · line 324 · raw

@result:Pair(Tensor, Layer) -> @next:(@_:Tensor -> @_:Layer -> Pair(Tensor, Network)) -> Pair(Tensor, Network)

def met source · line 328 · raw

@branch:Pair(Tensor, Network) -> @shortcut:Pair(Tensor, Network) -> @next:(@_:Tensor -> @_:Tensor -> @_:Network -> @_:Network -> Pair(Tensor, Network)) -> Pair(Tensor, Network)

def network_backward source · line 333 · raw

@tape:Tape -> @dy:Tensor -> @network:Network -> Pair(Tensor, Network)

def backward_network source · line 346 · raw

@dy:Tensor -> @tape:Tape -> Pair(Tensor, Network)

dy, the gradient of the loss with respect to the network output, and what forward_network saved: (the gradient of the network input, the network whose layers hold the gradients of their parameters). A residual block gives its input the sum of the gradients of its branch and of its shortcut.

def in_front source · line 349 · raw

@layer:Layer -> @result:Pair(Tensor, Network) -> Pair(Tensor, Network)

def block_in_front source · line 353 · raw

@branch:Network -> @shortcut:Network -> @result:Pair(Tensor, Network) -> Pair(Tensor, Network)

def network_evaluated source · line 357 · raw

@network:Network -> @x:Tensor -> Pair(Tensor, Network)

def evaluate_network source · line 368 · raw

@x:Tensor -> @network:Network -> Pair(Tensor, Network)

The output in inference mode and the network: batch normalizations use their running statistics and leave them unchanged.

def network_stepped source · line 371 · raw

@network:Network -> @+descent:Descent -> Network

def sgd_network_with source · line 378 · raw

@network:Network -> @rate:F32 -> @momentum:F32 -> @weight_decay:F32 -> Network

sgd_step_with on every layer, in order.

def sgd_network source · line 381 · raw

@network:Network -> @rate:F32 -> Network

def velocity_saved source · line 388 · raw

@+prefix:String -> @velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> IO(0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity)

A parameter at prefix.bin and its velocity: prefix_moving.bin holds 1 when prefix_velocity.bin holds it, 0 before the first step with momentum.

def parameter_saved source · line 400 · raw

@+prefix:String -> @parameter:Tensor -> @velocity:0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity -> IO(Pair(Tensor, 0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity))

def layer_saved source · line 406 · raw

@+prefix:String -> @layer:Layer -> IO(Layer)

def loaded_on source · line 428 · raw

@+path:String -> @+shape:List<&2, U32> -> @+workers:U32 -> IO(Tensor)

def velocity_loaded source · line 433 · raw

@moving:Bool -> @+path:String -> @+shape:List<&2, U32> -> @+workers:U32 -> IO(0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity)

def parameter_loaded source · line 441 · raw

@+prefix:String -> @+shape:List<&2, U32> -> @counted:Pair(Tensor, U32) -> IO(Pair(Tensor, 0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity))

def parameter_restored source · line 451 · raw

@+prefix:String -> @measured:Pair(Tensor, List<&2, U32>) -> IO(Pair(Tensor, 0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity))

The parameter saved under prefix, in the shape and on the workers of the one it replaces.

def statistic_loaded source · line 455 · raw

@+path:String -> @+shape:List<&2, U32> -> @counted:Pair(Tensor, U32) -> IO(Tensor)

def statistic_restored source · line 459 · raw

@+path:String -> @measured:Pair(Tensor, List<&2, U32>) -> IO(Tensor)

def aligned source · line 463 · raw

@parameter:Pair(Tensor, 0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity) -> Pair(Tensor, 0xa2af805348d03b11eb853850859654f6/tensor_optimizer.Velocity)

def layer_restored source · line 467 · raw

@+prefix:String -> @layer:Layer -> IO(Layer)

def numbered source · line 493 · raw

@+next:U32 -> @network:Network -> IO(Numbered)

def numbered_then source · line 496 · raw

@result:Numbered -> @next:(@_:U32 -> @_:Network -> IO(Numbered)) -> IO(Numbered)

def visited source · line 500 · raw

@saving:Bool -> @+prefix:String -> @layer:Layer -> IO(Layer)

def walked source · line 505 · raw

@network:Network -> @+folder:String -> @+first:U32 -> @+saving:Bool -> IO(Numbered)

def walked_network source · line 519 · raw

@result:Numbered -> IO(Network)

def save_network source · line 527 · raw

@+folder:String -> @network:Network -> IO(Network)

The parameters, running statistics and velocities of every layer in order, as raw FP32 files folder/layer0_weights.bin and so on. Gradients are not saved and the network comes back without them. A tensor that cannot be written is failed in the network.

def restore_network source · line 532 · raw

@+folder:String -> @network:Network -> IO(Network)

The network that save_network wrote into folder, read in the shapes and on the workers of the layers of network, which has the same layers.

def mse source · line 539 · raw

@prediction:Tensor -> @target:Tensor -> Pair(Tensor, Pair(Tensor, Tensor))

The mean squared error of prediction against a target of its shape: (the loss, (its gradient with respect to prediction, the target)).

def cross_entropy source · line 545 · raw

@logits:Tensor -> @target:Tensor -> Pair(Tensor, Pair(Tensor, Tensor))

The mean cross entropy of logits [N,K] against a target [N,K] whose rows are class probabilities: (the loss, (its gradient with respect to logits, the target)).

def one_hot source · line 549 · raw

@labels:Tensor -> @+classes:U32 -> Tensor

The class numbers [N], each a float, as one-hot rows [N,classes].

Templates

template split source · line 297 · raw

@-Result:Type -> @copies:Pair(Tensor, Tensor) -> @next:(@_:Tensor -> @_:Tensor -> Result) -> Result