~/bend-docscommunity

tensor_loss.bend checks

raw source on the hub · import stelliferous@0.0.4.0/tensor_loss.bend as Tensor_loss

Losses with their gradient, in a fixed FP32 order. The prediction becomes the gradient, and the target returns.

8 imports
import Base
import ./tensor.bend as Tensors
import ./tensor_shape.bend as Shapes
import ./tensor_tiled_reduce.bend as TiledReduce
import ./math_sum.bend as Sum
import ./tensor_f32.bend as F
import ./tensor_tiled.bend as Tiled
import ./math_fp32.bend as Arithmetic

Types

type Reduction source · line 15 · raw

Data

Definitions

def Tensor source · line 12 · raw

Type

def mean source · line 19 · raw

Reduction

def sum source · line 22 · raw

Reduction

def square source · line 25 · raw

@value:F32 -> F32

def reduced source · line 29 · raw

@reduction:Reduction -> @+count:F32 -> @total:Tensor -> Tensor

def mse_squares source · line 34 · raw

@+reduction:Reduction -> @+count:F32 -> @target:Tensor -> @pair:Pair(Tensor, Tensor) -> Pair(Tensor, Pair(Tensor, Tensor))

def mse_counted source · line 40 · raw

@+reduction:Reduction -> @target:Tensor -> @shaped:Pair(Tensor, List<&2, U32>) -> Pair(Tensor, Pair(Tensor, Tensor))

def mse_difference source · line 44 · raw

@same:Bool -> @prediction:Tensor -> @kept:Tensor -> Tensor

def mse_kept source · line 49 · raw

@+reduction:Reduction -> @same:Bool -> @prediction:Tensor -> @pair:Pair(Tensor, Tensor) -> Pair(Tensor, Pair(Tensor, Tensor))

def mse_shaped source · line 53 · raw

@+reduction:Reduction -> @shaped:Pair(Tensor, List<&2, U32>) -> @measured:Pair(Tensor, List<&2, U32>) -> Pair(Tensor, Pair(Tensor, Tensor))

def mse source · line 62 · raw

@prediction:Tensor -> @target:Tensor -> @+reduction:Reduction -> Pair(Tensor, Pair(Tensor, Tensor))

The loss of prediction against a target of the same shape, the gradient of the loss with respect to prediction, and the target. With d = prediction - target: Mean gives sum(d * d) / count and (d * 2) / count, Sum gives sum(d * d) and d * 2; the squares are added in row-major order from +0.

def one_hot_rows source · line 69 · raw

@shape:List<&2, U32> -> @labels:Tensor -> @+classes:U32 -> Tensor

The class numbers [N] as rows [N,K] of a target: 1 where the column equals the class number, 0 elsewhere.

def one_hot_measured source · line 74 · raw

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

def one_hot source · line 78 · raw

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

def exponential_step source · line 83 · raw

@lanes:0xa2af805348d03b11eb853850859654f6/math_sum.Lanes -> @value:F32 -> 0xa2af805348d03b11eb853850859654f6/math_sum.Lanes

The exponentials of a row added in the lanes of math_sum.bend, and the logarithm of their sum.

def logarithm_total source · line 86 · raw

@lanes:0xa2af805348d03b11eb853850859654f6/math_sum.Lanes -> F32

def entropy_gradient source · line 89 · raw

@+images:U32 -> @log_probabilities:Pair(Tensor, Tensor) -> @targets:Pair(Tensor, Tensor) -> @target:Tensor -> Pair(Tensor, Pair(Tensor, Tensor))

def entropy_shifted source · line 95 · raw

@+images:U32 -> @shifted:Pair(Tensor, Tensor) -> @targets:Pair(Tensor, Tensor) -> Pair(Tensor, Pair(Tensor, Tensor))

def entropy_rows source · line 102 · raw

@+images:U32 -> @logits:Pair(Tensor, Tensor) -> @target:Tensor -> Pair(Tensor, Pair(Tensor, Tensor))

def entropy_failed source · line 108 · raw

@shape:List<&2, U32> -> @logits:Tensor -> @target:Tensor -> Pair(Tensor, Pair(Tensor, Tensor))

def entropy_ranked source · line 111 · raw

@shape:List<&2, U32> -> @logits:Tensor -> @target:Tensor -> Pair(Tensor, Pair(Tensor, Tensor))

def entropy_shaped source · line 116 · raw

@same:Bool -> @logits:Tensor -> @shape:List<&2, U32> -> @target:Tensor -> Pair(Tensor, Pair(Tensor, Tensor))

def entropy_measured source · line 121 · raw

@measured_logits:Pair(Tensor, List<&2, U32>) -> @measured_target:Pair(Tensor, List<&2, U32>) -> Pair(Tensor, Pair(Tensor, Tensor))

def cross_entropy source · line 133 · raw

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

logits [N,K] and a target [N,K] whose rows are class probabilities, such as one_hot of the class numbers: the mean loss, its gradient with respect to logits, and the target. With m the maximum of a row and the log probabilities p = (logits - m) - log(sum of exp(logits - m) over the row), the sum over the row in the lanes of math_sum.bend: the loss is sum(target * p) / -N, the sum of math_sum.bend in row-major order, and the gradient (exp(p) - target) / N.