deepbox/nn
Losses & Initialization
Regression and classification losses, ranking and sequence losses, and in-place weight initializers.
- Losses
- Initializers
Overview
- Losses take
(predictions, targets)and often areductionof"mean"(default),"sum"or"none". When the input is aGradTensor, which is whatforwardreturns during training, the loss is aGradTensor, so you can callbackward()and read the number withloss.item(). - With plain tensors,
mseLossand most others return aTensor, butcrossEntropyLossandbinaryCrossEntropyWithLogitsLossreturn a number.ctcLoss,gaussianNLLLossandpoissonNLLLosstake and return plain tensors, so they cannot drive training. crossEntropyLoss(logits, target)expects raw logits, so do not apply softmax first. The target is either integer class indices of shape[n]or class probabilities of shape[n, nClasses]. Options areweight,ignoreIndexandlabelSmoothing.- Initializers such as
xavierUniform_,kaimingNormal_andzeros_fill a tensor in place and return it. The trailing underscore follows PyTorch and is part of the name.kaimingNormal_is the camelCase form of the deprecatedkaiming_normal_. They draw from the global generator, sosetSeedmakes them repeatable. Gradient clipping is on the Module, Containers & Trainer page.
BinaryCrossEntropyWithLogitsOptions
interfaceOptions for binaryCrossEntropyWithLogitsLoss.
readonly reduction?: LossReduction"mean"(default),"sum", or"none"to return the element-wise loss.readonly posWeight?: number | TensorWeight of the positive class.
readonly weight?: TensorElement-wise rescaling weight that broadcasts against the logits.
CrossEntropyLossOptions
interfaceOptions for crossEntropyLoss.
readonly reduction?: LossReduction"mean"(default),"sum", or"none"to return one loss per sample.readonly weight?: Tensor | readonly number[]Per-class rescaling weights, length
n_classes.readonly ignoreIndex?: numberClass index to skip (class-index targets only).
readonly labelSmoothing?: numberLabel smoothing in [0, 1].
CtcLossOptions
interfaceOptions for ctcLoss.
readonly blank?: numberIndex of the blank label (default: 0).
readonly reduction?: LossReduction"mean"(default) divides each sample's loss by its target length (at least 1) and averages over the batch, as PyTorch does.readonly zeroInfinity?: booleanReplace infinite losses (inputs too short for the target) with zero (default: false).
LossReduction
typeHow per-sample losses are combined: average, total, or kept per sample.
type LossReduction = "mean" | "sum" | "none";NllLossOptions
interfaceOptions for nllLoss.
readonly reduction?: LossReduction"mean"(default),"sum", or"none"to return one loss per sample.readonly weight?: Tensor | readonly number[]Per-class rescaling weights, length
n_classes.readonly ignoreIndex?: numberClass index to skip: such samples add no loss and are not counted by
"mean".
calculateFanInOut
function calculateFanInOut(tensor: AnyTensor): { fanIn: number; fanOut: number; };Parameters
tensorAnyTensor
Returns
{ fanIn: number; fanOut: number; }
calculateGain
function calculateGain(nonlinearity: string, param?: number): number;Recommended gain for a nonlinearity (mirrors torch.nn.init.calculate_gain).
Parameters
nonlinearitystring- One of
linear,conv1d,conv2d,conv3d,conv_transpose1d,conv_transpose2d,conv_transpose3d,sigmoid,tanh,relu,leaky_relu,selu paramoptionalnumber- Negative slope for
leaky_relu(default: 0.01)
Returns
numberThe gain factor
Throws
InvalidParameterErrorFor an unknown nonlinearity or a non-finite slope
constant
function constant<T extends AnyTensor>(tensor: T, val: number): T;Fill tensor with a constant value.
Works for every numeric dtype. Integer and bool tensors store the truncated value (bool: non-zero is true); int64 converts through BigInt. Half-precision tensors store the rounded value.
Parameters
tensorT- Tensor to fill in-place
valnumber- Value to fill with
Returns
TThe modified tensor
Throws
DTypeErrorFor string tensorsInvalidParameterErrorIfvalis not finite for an integer orint64tensor
constant_
function constant_<T extends AnyTensor>(tensor: T, val: number): T;Fill tensor with a constant value.
Works for every numeric dtype. Integer and bool tensors store the truncated value (bool: non-zero is true); int64 converts through BigInt. Half-precision tensors store the rounded value.
Parameters
tensorT- Tensor to fill in-place
valnumber- Value to fill with
Returns
TThe modified tensor
Throws
DTypeErrorFor string tensorsInvalidParameterErrorIfvalis not finite for an integer orint64tensor
eye_
function eye_<T extends AnyTensor>(tensor: T): T;Fill a 2-D tensor with the identity matrix (ones on the main diagonal, zeros elsewhere). The tensor may be rectangular.
Parameters
tensorT- 2-D tensor to fill in-place
Returns
TThe modified tensor
Throws
InvalidParameterErrorIf the tensor is not 2-D
kaimingNormal
function kaimingNormal<T extends AnyTensor>(tensor: T, a?: number, mode?: "fan_in" | "fan_out", nonlinearity?: string): T;Fill tensor using Kaiming normal initialization (He initialization).
Draws from N(0, std²) where std = gain / sqrt(fan). Recommended for ReLU activations.
Parameters
tensorT- Floating-point tensor to fill in-place (at least 1-D)
aoptionalnumber- Negative slope of rectifier (used when
nonlinearityis 'leaky_relu', default: 0) modeoptional"fan_in" | "fan_out"- 'fan_in' (default) or 'fan_out'
nonlinearityoptionalstring- Activation function name (default: 'leaky_relu')
Returns
TThe modified tensor
Throws
InvalidParameterErrorFor a scalar tensor, an unknown mode or an unknown nonlinearity
kaimingNormal_
function kaimingNormal_<T extends AnyTensor>(tensor: T, a?: number, mode?: "fan_in" | "fan_out", nonlinearity?: string): T;Fill tensor using Kaiming normal initialization (He initialization).
Draws from N(0, std²) where std = gain / sqrt(fan). Recommended for ReLU activations.
Parameters
tensorT- Floating-point tensor to fill in-place (at least 1-D)
aoptionalnumber- Negative slope of rectifier (used when
nonlinearityis 'leaky_relu', default: 0) modeoptional"fan_in" | "fan_out"- 'fan_in' (default) or 'fan_out'
nonlinearityoptionalstring- Activation function name (default: 'leaky_relu')
Returns
TThe modified tensor
Throws
InvalidParameterErrorFor a scalar tensor, an unknown mode or an unknown nonlinearity
kaimingUniform
function kaimingUniform<T extends AnyTensor>(tensor: T, a?: number, mode?: "fan_in" | "fan_out", nonlinearity?: string): T;Fill tensor using Kaiming uniform initialization (He initialization).
Draws from U(-bound, bound) where bound = gain * sqrt(3 / fan). Recommended for ReLU activations.
Parameters
tensorT- Floating-point tensor to fill in-place (at least 1-D)
aoptionalnumber- Negative slope of rectifier (used when
nonlinearityis 'leaky_relu', default: 0) modeoptional"fan_in" | "fan_out"- 'fan_in' (default) or 'fan_out'
nonlinearityoptionalstring- Activation function name (default: 'leaky_relu')
Returns
TThe modified tensor
Throws
InvalidParameterErrorFor a scalar tensor, an unknown mode or an unknown nonlinearity
kaimingUniform_
function kaimingUniform_<T extends AnyTensor>(tensor: T, a?: number, mode?: "fan_in" | "fan_out", nonlinearity?: string): T;Fill tensor using Kaiming uniform initialization (He initialization).
Draws from U(-bound, bound) where bound = gain * sqrt(3 / fan). Recommended for ReLU activations.
Parameters
tensorT- Floating-point tensor to fill in-place (at least 1-D)
aoptionalnumber- Negative slope of rectifier (used when
nonlinearityis 'leaky_relu', default: 0) modeoptional"fan_in" | "fan_out"- 'fan_in' (default) or 'fan_out'
nonlinearityoptionalstring- Activation function name (default: 'leaky_relu')
Returns
TThe modified tensor
Throws
InvalidParameterErrorFor a scalar tensor, an unknown mode or an unknown nonlinearity
normal_
function normal_<T extends AnyTensor>(tensor: T, mean?: number, std?: number): T;Fill tensor with values drawn from a normal distribution N(mean, std²).
Parameters
tensorT- Floating-point tensor to fill in-place
meanoptionalnumber- Mean of the distribution (default: 0)
stdoptionalnumber- Standard deviation, must be >= 0 (default: 1)
Returns
TThe modified tensor
Throws
InvalidParameterErrorIfmeanis not finite orstdis negative or not finiteDTypeErrorIf the tensor is not floating-point
ones
function ones<T extends AnyTensor>(tensor: T): T;Fill tensor with ones.
Parameters
tensorT- Tensor to fill in-place
Returns
TThe modified tensor
ones_
function ones_<T extends AnyTensor>(tensor: T): T;Fill tensor with ones.
Parameters
tensorT- Tensor to fill in-place
Returns
TThe modified tensor
orthogonal
function orthogonal<T extends AnyTensor>(tensor: T, gain?: number): T;Fill tensor with a (semi-)orthogonal matrix.
A normal random matrix is orthonormalized with Gram-Schmidt (applied twice for accuracy). With more columns than rows the rows are orthonormal; with more rows than columns the columns are. Tensors with more than two dimensions are treated as a [shape[0], prod(shape[1:])] matrix, like PyTorch. The result is scaled by gain.
Parameters
tensorT- Floating-point tensor of at least 2 dimensions, filled in-place
gainoptionalnumber- Scaling factor (default: 1.0)
Returns
TThe modified tensor
Throws
InvalidParameterErrorIf the tensor has fewer than 2 dimensions orgainis not finite
orthogonal_
function orthogonal_<T extends AnyTensor>(tensor: T, gain?: number): T;Fill tensor with a (semi-)orthogonal matrix.
A normal random matrix is orthonormalized with Gram-Schmidt (applied twice for accuracy). With more columns than rows the rows are orthonormal; with more rows than columns the columns are. Tensors with more than two dimensions are treated as a [shape[0], prod(shape[1:])] matrix, like PyTorch. The result is scaled by gain.
Parameters
tensorT- Floating-point tensor of at least 2 dimensions, filled in-place
gainoptionalnumber- Scaling factor (default: 1.0)
Returns
TThe modified tensor
Throws
InvalidParameterErrorIf the tensor has fewer than 2 dimensions orgainis not finite
sparse_
function sparse_<T extends AnyTensor>(tensor: T, sparsity?: number, std?: number): T;Fill a 2-D tensor as a sparse matrix with normally distributed non-zero entries.
In every column, ceil(sparsity * rows) randomly chosen entries are set to zero and the rest are drawn from N(0, std²), as in torch.nn.init.sparse_.
Parameters
tensorT- Floating-point 2-D tensor to fill in-place
sparsityoptionalnumber- Fraction of elements in each column to be zero, in [0, 1] (default: 0.1)
stdoptionalnumber- Standard deviation of the non-zero entries, must be >= 0 (default: 0.01)
Returns
TThe modified tensor
Throws
InvalidParameterErrorIf the tensor is not 2-D or an argument is out of range
trunc_normal_
function trunc_normal_<T extends AnyTensor>(tensor: T, mean?: number, std?: number, a?: number, b?: number): T;Fill tensor with values drawn from a normal distribution N(mean, std²) truncated to [a, b] (mirrors torch.nn.init.trunc_normal_).
Uses rejection sampling, so the result is exactly the truncated normal.
Parameters
tensorT- Floating-point tensor to fill in-place
meanoptionalnumber- Mean of the underlying normal (default: 0)
stdoptionalnumber- Standard deviation of the underlying normal, must be >= 0 (default: 1)
aoptionalnumber- Lower cut-off (default: -2)
boptionalnumber- Upper cut-off (default: 2)
Returns
TThe modified tensor
Throws
InvalidParameterErrorIf a parameter is not finite,std < 0,a > b, or the interval lies so far in the tail that sampling would not finishDTypeErrorIf the tensor is not floating-point
truncNormal_
function truncNormal_<T extends AnyTensor>(tensor: T, mean?: number, std?: number, a?: number, b?: number): T;Fill tensor with values drawn from a normal distribution N(mean, std²) truncated to [a, b] (mirrors torch.nn.init.trunc_normal_).
Uses rejection sampling, so the result is exactly the truncated normal.
Parameters
tensorT- Floating-point tensor to fill in-place
meanoptionalnumber- Mean of the underlying normal (default: 0)
stdoptionalnumber- Standard deviation of the underlying normal, must be >= 0 (default: 1)
aoptionalnumber- Lower cut-off (default: -2)
boptionalnumber- Upper cut-off (default: 2)
Returns
TThe modified tensor
Throws
InvalidParameterErrorIf a parameter is not finite,std < 0,a > b, or the interval lies so far in the tail that sampling would not finishDTypeErrorIf the tensor is not floating-point
uniform_
function uniform_<T extends AnyTensor>(tensor: T, low?: number, high?: number): T;Fill tensor with values drawn from a uniform distribution U(low, high).
Parameters
tensorT- Floating-point tensor to fill in-place
lowoptionalnumber- Lower bound (default: 0)
highoptionalnumber- Upper bound (default: 1)
Returns
TThe modified tensor
Throws
InvalidParameterErrorIf a bound is not finite orlow > highDTypeErrorIf the tensor is not floating-point
xavierNormal
function xavierNormal<T extends AnyTensor>(tensor: T, gain?: number): T;Fill tensor using Xavier normal initialization.
Draws from N(0, std²) where std = gain * sqrt(2 / (fan_in + fan_out)). Recommended for sigmoid/tanh activations.
Parameters
tensorT- Floating-point tensor to fill in-place (at least 1-D)
gainoptionalnumber- Scaling factor, must be >= 0 (default: 1.0)
Returns
TThe modified tensor
Throws
InvalidParameterErrorFor a scalar tensor or an invalid gain
xavierNormal_
function xavierNormal_<T extends AnyTensor>(tensor: T, gain?: number): T;Fill tensor using Xavier normal initialization.
Draws from N(0, std²) where std = gain * sqrt(2 / (fan_in + fan_out)). Recommended for sigmoid/tanh activations.
Parameters
tensorT- Floating-point tensor to fill in-place (at least 1-D)
gainoptionalnumber- Scaling factor, must be >= 0 (default: 1.0)
Returns
TThe modified tensor
Throws
InvalidParameterErrorFor a scalar tensor or an invalid gain
xavierUniform
function xavierUniform<T extends AnyTensor>(tensor: T, gain?: number): T;Fill tensor using Xavier uniform initialization.
Draws from U(-a, a) where a = gain * sqrt(6 / (fan_in + fan_out)). Recommended for sigmoid/tanh activations.
Parameters
tensorT- Floating-point tensor to fill in-place (at least 1-D)
gainoptionalnumber- Scaling factor, must be >= 0 (default: 1.0)
Returns
TThe modified tensor
Throws
InvalidParameterErrorFor a scalar tensor or an invalid gain
xavierUniform_
function xavierUniform_<T extends AnyTensor>(tensor: T, gain?: number): T;Fill tensor using Xavier uniform initialization.
Draws from U(-a, a) where a = gain * sqrt(6 / (fan_in + fan_out)). Recommended for sigmoid/tanh activations.
Parameters
tensorT- Floating-point tensor to fill in-place (at least 1-D)
gainoptionalnumber- Scaling factor, must be >= 0 (default: 1.0)
Returns
TThe modified tensor
Throws
InvalidParameterErrorFor a scalar tensor or an invalid gain
zeros
function zeros<T extends AnyTensor>(tensor: T): T;Fill tensor with zeros.
Parameters
tensorT- Tensor to fill in-place
Returns
TThe modified tensor
zeros_
function zeros_<T extends AnyTensor>(tensor: T): T;Fill tensor with zeros.
Parameters
tensorT- Tensor to fill in-place
Returns
TThe modified tensor
binaryCrossEntropyLoss
function binaryCrossEntropyLoss(predictions: AnyTensor, targets: AnyTensor, reduction?: LossReduction): AnyTensor;Parameters
predictionsAnyTensortargetsAnyTensorreductionoptionalLossReduction
Returns
AnyTensor
binaryCrossEntropyWithLogitsLoss
function binaryCrossEntropyWithLogitsLoss(input: AnyTensor, target: AnyTensor, options?: BinaryCrossEntropyWithLogitsOptions): number | Tensor | GradTensor;Parameters
inputAnyTensortargetAnyTensoroptionsoptionalBinaryCrossEntropyWithLogitsOptions
Returns
number | Tensor | GradTensor
cosineEmbeddingLoss
function cosineEmbeddingLoss(x1: Tensor, x2: Tensor, y: Tensor, margin?: number, reduction?: LossReduction): Tensor;Cosine Embedding Loss for similarity/contrastive learning.
Formula:
loss = 1 - cos(x1, x2) if y == 1 = max(0, cos(x1, x2) - margin) if y == -1The cosine is dot(x1, x2) / sqrt((|x1|^2 + 1e-12) * (|x2|^2 + 1e-12)), as in PyTorch.
Parameters
x1Tensor- First input, shape (N, D) or (D,)
x2Tensor- Second input, same shape as
x1 yTensor- Labels: 1 (similar) or -1 (dissimilar), one per row of
x1 marginoptionalnumber- Margin for dissimilar pairs (default: 0)
reductionoptionalLossReduction- How to reduce the loss
Returns
Tensor
Throws
ShapeErrorIfx1andx2differ in shape, have more than 2 dimensions, oryhas the wrong lengthInvalidParameterErrorIf a label is neither 1 nor -1
crossEntropyLoss
function crossEntropyLoss(input: AnyTensor, target: AnyTensor, options?: CrossEntropyLossOptions): number | Tensor | GradTensor;Parameters
inputAnyTensortargetAnyTensoroptionsoptionalCrossEntropyLossOptions
Returns
number | Tensor | GradTensor
ctcLoss
function ctcLoss(logProbs: Tensor, targets: Tensor, inputLengths: Tensor, targetLengths: Tensor, options?: CtcLossOptions): Tensor;Connectionist Temporal Classification (CTC) Loss.
Details
CTC loss for sequence-to-sequence models where the alignment between input and output is unknown (e.g., speech recognition, OCR).
Algorithm: Forward (alpha) computation in log-space over an extended label sequence with interleaved blanks.
With the default "mean" reduction each sample's loss is divided by its target length (at least 1) before averaging over the batch, matching PyTorch's CTCLoss. A sample whose input is too short to emit its target has an infinite loss unless zeroInfinity is set. Target length 0 is allowed (the loss is then the negative log-probability of emitting only blanks).
Parameters
logProbsTensor- Log-probabilities of shape (T, N, C) where T = input length (time steps), N = batch size, C = number of classes (including blank)
targetsTensor- Either the concatenated target sequences (1D, length at least the sum of all target lengths) or a padded matrix of shape (N, S). Values must be in [0, C) and must not equal
blank. inputLengthsTensor- Lengths of each input sequence, shape (N,)
targetLengthsTensor- Lengths of each target sequence, shape (N,)
optionsoptionalCtcLossOptions- Configuration options
Returns
TensorLoss value
gaussianNLLLoss
function gaussianNLLLoss(input: Tensor, target: Tensor, variance: Tensor, options?: { full?: boolean; eps?: number; reduction?: LossReduction; }): Tensor;Gaussian Negative Log Likelihood Loss.
Details
Formula:
loss = 0.5 * (log(var) + (input - target)^2 / var + log(2π))Use Cases: - Probabilistic regression where the model predicts both mean and variance - Heteroscedastic regression (varying noise)
variance may have the same shape as input, the shape of input with the last dimension removed, or the shape of input with the last dimension set to 1 (one variance shared by all outputs of a sample). Variances below eps are clamped to eps.
Parameters
inputTensor- Predicted means
targetTensor- Target values
varianceTensor- Predicted variances (must be non-negative)
optionsoptional{ full?: boolean; eps?: number; reduction?: LossReduction; }- Configuration options
Returns
TensorLoss value
Throws
InvalidParameterErrorIfvariancecontains a negative valueShapeErrorIfvariancehas an incompatible shape
huberLoss
function huberLoss(predictions: AnyTensor, targets: AnyTensor, delta?: number, reduction?: LossReduction): AnyTensor;Parameters
predictionsAnyTensortargetsAnyTensordeltaoptionalnumberreductionoptionalLossReduction
Returns
AnyTensor
klDivLoss
function klDivLoss(input: AnyTensor, target: AnyTensor, reduction?: LossReduction | "batchmean", logTarget?: boolean): AnyTensor;Parameters
inputAnyTensortargetAnyTensorreductionoptionalLossReduction | "batchmean"logTargetoptionalboolean
Returns
AnyTensor
maeLoss
function maeLoss(predictions: AnyTensor, targets: AnyTensor, reduction?: LossReduction): AnyTensor;Parameters
predictionsAnyTensortargetsAnyTensorreductionoptionalLossReduction
Returns
AnyTensor
marginRankingLoss
function marginRankingLoss(x1: AnyTensor, x2: AnyTensor, y: AnyTensor, margin?: number, reduction?: LossReduction): AnyTensor;Parameters
x1AnyTensorx2AnyTensoryAnyTensormarginoptionalnumberreductionoptionalLossReduction
Returns
AnyTensor
mseLoss
function mseLoss(predictions: AnyTensor, targets: AnyTensor, reduction?: LossReduction): AnyTensor;Parameters
predictionsAnyTensortargetsAnyTensorreductionoptionalLossReduction
Returns
AnyTensor
nllLoss
function nllLoss(logProbs: AnyTensor, targets: AnyTensor, reductionOrOptions?: LossReduction | NllLossOptions): AnyTensor;Parameters
logProbsAnyTensortargetsAnyTensorreductionOrOptionsoptionalLossReduction | NllLossOptions
Returns
AnyTensor
poissonNLLLoss
function poissonNLLLoss(input: Tensor, target: Tensor, options?: { logInput?: boolean; full?: boolean; eps?: number; reduction?: LossReduction; }): Tensor;Poisson Negative Log Likelihood Loss.
Formula:
loss = exp(input) - target * inputWhen logInput=false:
loss = input - target * log(input + eps)Use Cases: - Count data modeling (Poisson regression) - Event rate prediction
Parameters
inputTensor- Log of expected rate (or rate itself if logInput=false)
targetTensor- Target counts (non-negative)
optionsoptional{ logInput?: boolean; full?: boolean; eps?: number; reduction?: LossReduction; }- Configuration options
Returns
TensorLoss value
rmseLoss
function rmseLoss(predictions: AnyTensor, targets: AnyTensor): AnyTensor;Parameters
predictionsAnyTensortargetsAnyTensor
Returns
AnyTensor
smoothL1Loss
function smoothL1Loss(predictions: AnyTensor, targets: AnyTensor, beta?: number, reduction?: LossReduction): AnyTensor;Parameters
predictionsAnyTensortargetsAnyTensorbetaoptionalnumberreductionoptionalLossReduction
Returns
AnyTensor
tripletMarginLoss
function tripletMarginLoss(anchor: AnyTensor, positive: AnyTensor, negative: AnyTensor, marginOrOptions?: number | TripletMarginLossOptions, reduction?: LossReduction): AnyTensor;Parameters
anchorAnyTensorpositiveAnyTensornegativeAnyTensormarginOrOptionsoptionalnumber | TripletMarginLossOptionsreductionoptionalLossReduction
Returns
AnyTensor
These older spellings still work in 1.x. New code should use the names above.
kaiming_normal_Prefer kaimingNormal_.kaiming_uniform_Prefer kaimingUniform_.xavier_normal_Prefer xavierNormal_.xavier_uniform_Prefer xavierUniform_.
import { parameter, tensor } from "deepbox/ndarray";import { Linear, crossEntropyLoss, mseLoss, xavierUniform_, zeros_ } from "deepbox/nn"; const labels = tensor([0, 1]); // Plain tensors: crossEntropyLoss returns a number.console.log(crossEntropyLoss(tensor([[2, 0.5, 0.1], [0.1, 1.8, 0.1]]), labels)); // A GradTensor input gives a differentiable loss.const scores = parameter([[2, 0.5, 0.1], [0.1, 1.8, 0.1]]);const loss = crossEntropyLoss(scores, labels, { labelSmoothing: 0.1 });loss.backward();console.log(loss.item(), scores.grad?.shape); console.log(mseLoss(tensor([0.9, 0.1, 0.8]), tensor([1, 0, 1])).toString()); const layer = new Linear(4, 2);xavierUniform_(layer.getWeight());const bias = layer.getBias();if (bias) zeros_(bias);Found a mistake? Open an issue.