TensorFlowJS
Standard Deviation
JavaScript
Data Analysis
Machine Learning

Calculate Standard Deviation in TensorflowJS?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Standard deviation is a very common normalization and analysis step, but TensorFlow.js does not expose a dedicated tf.std helper. The usual approach is to compute variance first, then take its square root, either manually or through tf.moments.

Using tf.moments for population standard deviation

tf.moments is the simplest built-in tool for this job. It returns a mean tensor and a variance tensor for the input along the axes you choose. Once you have the variance, standard deviation is just sqrt(variance).

javascript
1import * as tf from "@tensorflow/tfjs";
2
3const values = tf.tensor1d([2, 4, 4, 4, 5, 5, 7, 9]);
4
5const { mean, variance } = tf.moments(values);
6const stddev = tf.sqrt(variance);
7
8mean.print();    // 5
9variance.print(); // 4
10stddev.print();   // 2
11
12values.dispose();
13mean.dispose();
14variance.dispose();
15stddev.dispose();

This example computes the population standard deviation, which divides by N. That is usually what you want for tensor preprocessing and model math, but it is not the same as sample standard deviation from classical statistics.

If you prefer a one-function helper, wrap the logic in tf.tidy so temporary tensors are cleaned up automatically:

javascript
1import * as tf from "@tensorflow/tfjs";
2
3function standardDeviation(x, axis = null, keepDims = false) {
4  return tf.tidy(() => {
5    const { variance } = tf.moments(x, axis, keepDims);
6    return tf.sqrt(variance);
7  });
8}
9
10const matrix = tf.tensor2d([
11  [1, 2, 3],
12  [4, 5, 6],
13]);
14
15standardDeviation(matrix).print();     // scalar
16standardDeviation(matrix, 0).print();  // per column
17standardDeviation(matrix, 1).print();  // per row
18
19matrix.dispose();

The axis argument works the same way it does for reduction ops such as tf.mean. On a matrix, axis = 0 reduces rows and gives one value per column, while axis = 1 reduces columns and gives one value per row.

Computing it manually

Understanding the manual version is useful because it explains what tf.moments is doing under the hood and makes it easier to debug shape issues:

javascript
1import * as tf from "@tensorflow/tfjs";
2
3const x = tf.tensor1d([10, 12, 23, 23, 16, 23, 21, 16]);
4
5const mean = tf.mean(x);
6const centered = tf.sub(x, mean);
7const squared = tf.square(centered);
8const variance = tf.mean(squared);
9const stddev = tf.sqrt(variance);
10
11stddev.print();
12
13x.dispose();
14mean.dispose();
15centered.dispose();
16squared.dispose();
17variance.dispose();
18stddev.dispose();

This is more verbose than tf.moments, but it is helpful when you need to inspect intermediate tensors during development.

Population vs sample standard deviation

A common source of confusion is whether you want population or sample standard deviation. tf.moments gives you population variance. If you need sample standard deviation, apply Bessel's correction by multiplying the variance by N / (N - 1) before taking the square root.

javascript
1import * as tf from "@tensorflow/tfjs";
2
3function sampleStandardDeviation(x) {
4  return tf.tidy(() => {
5    const { variance } = tf.moments(x);
6    const n = x.size;
7    if (n < 2) {
8      throw new Error("Sample standard deviation needs at least 2 values");
9    }
10    return tf.sqrt(variance.mul(n / (n - 1)));
11  });
12}
13
14const values = tf.tensor1d([3, 7, 7, 19]);
15sampleStandardDeviation(values).print();
16values.dispose();

If you are implementing feature scaling for machine learning, population standard deviation is usually the expected choice. If you are reproducing a statistics textbook result, sample standard deviation may be the correct one instead.

Common Pitfalls

The most common mistake is treating variance as if it were already standard deviation. If the number looks too large, check whether you forgot the final tf.sqrt.

Memory leaks are another frequent issue, especially in long-running browser apps. TensorFlow.js tensors are not ordinary JavaScript objects, so garbage collection alone is not enough. Use tf.tidy for helper functions and call dispose() on tensors you keep around.

Shape handling also matters. When you reduce over an axis, the result shape changes. If later code expects the original rank, pass keepDims = true or reshape explicitly.

Finally, remember that .print() is convenient for debugging, while .data() and .dataSync() are better when you need the actual numeric values inside application logic.

Summary

  • TensorFlow.js does not provide a dedicated tf.std, but tf.moments gives mean and variance directly.
  • Population standard deviation is tf.sqrt(variance).
  • Use axis to compute row-wise, column-wise, or global standard deviations.
  • Apply Bessel's correction only when you specifically need sample standard deviation.
  • Wrap helper code in tf.tidy or dispose tensors manually to avoid memory leaks.

Course illustration
Course illustration

All Rights Reserved.