Implementation:Tensorflow Tfjs Losses Test
| Knowledge Sources | |
|---|---|
| Domains | Testing, Layers_API |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
This test suite validates all loss function implementations in the TensorFlow.js Layers API. Loss functions measure the discrepancy between predicted and true values during model training. The tested functions include regression losses (MSE, MAE, MAPE, MSLE), hinge losses (squared hinge, hinge, categorical hinge), cross-entropy losses (categorical, sparse categorical, binary, with and without logits), and other losses (logcosh, Kullback-Leibler divergence, Poisson, cosine proximity). The suite also tests the loss function lookup utility and L2 normalization.
Code Reference
Source Location: tfjs-layers/src/losses_test.ts (443 lines)
Repository: GitHub
Test Describe Blocks
meanSquaredError- MSE for 1D and 2D inputsmeanAbsoluteError- MAE for 1D and 2D inputsmeanAbsolutePercentageError- MAPE for 1D and 2D inputsmeanSquaredLogarithmicError- MSLE for 2D inputssquaredHinge- Squared hinge losshinge- Hinge losscategoricalHinge- Categorical hinge for multi-classlogcosh- Log-cosh loss (smooth L1 approximation)categoricalCrossentropy- Multi-class cross-entropy (two blocks: function and loss wrapper)sparseCategoricalCrossentropy- Sparse categorical cross-entropy (two blocks)sigmoidCrossEntropyWithLogits- Binary cross-entropy from logitsbinaryCrossentropy- Binary cross-entropykullbackLeiblerDivergence- KL divergencepoisson- Poisson losscosineProximity- Cosine similarity losslosses get- Loss function lookup by string namel2Normalize- L2 normalization utility
I/O Contract
Inputs to tests:
- True label tensors (yTrue) and predicted tensors (yPred) of matching shapes
- 1D tensors for per-sample loss, 2D tensors for batched loss
- Various numeric ranges including zeros, negatives, and boundary values
Expected outputs/assertions:
- Loss values match hand-computed formulas (e.g., MSE:
(1^2 + 2^2 + 3^2) / 3) - 2D inputs produce per-sample loss vectors
- Cross-entropy losses handle edge cases (predictions near 0 or 1)
- Loss lookup returns correct function for string identifiers
- L2 normalization produces unit-length vectors along specified axis
Usage Example
describeMathCPUAndGPU('meanSquaredError', () => {
it('1D', () => {
const yTrue = tfc.zeros([3]);
const yPred = tensor1d([1, 2, 3]);
const expectedVal = scalar((1 * 1 + 2 * 2 + 3 * 3) / 3);
const result = losses.meanSquaredError(yTrue, yPred);
expectTensorsClose(result, expectedVal);
});
it('2D', () => {
const yTrue = tfc.zeros([2, 2]);
const yPred = tensor2d([[1, 2], [3, 4]], [2, 2]);
const expectedVal = tensor1d([(1 + 4) / 2, (9 + 16) / 2]);
const result = losses.meanSquaredError(yTrue, yPred);
expectTensorsClose(result, expectedVal);
});
});
Test Coverage Summary
| Category | Count | Details |
|---|---|---|
| Regression Losses | 8 | MSE, MAE, MAPE, MSLE (1D and 2D) |
| Hinge Losses | 3 | Hinge, squared hinge, categorical hinge |
| Cross-Entropy | 8 | Categorical, sparse categorical, binary, sigmoid with logits |
| Other Losses | 4 | Logcosh, KL divergence, Poisson, cosine proximity |
| Utilities | 2 | losses.get lookup, l2Normalize |
| Test Environment | Mixed | CPU, GPU, WebGL2 |