Skip to content

Instantly share code, notes, and snippets.

@codeas
Created May 18, 2020 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeas/a78c17a8829a8b934c7fb5b9ccc30c84 to your computer and use it in GitHub Desktop.
Save codeas/a78c17a8829a8b934c7fb5b9ccc30c84 to your computer and use it in GitHub Desktop.
/**
* Calculates the mean and standard deviation of each column of an array.
*
* @param {Tensor2d} data Dataset from which to calculate the mean and
* std of each column independently.
*
* @returns {Object} Contains the mean and std of each vector
* column as 1d tensors.
*/
function determineMeanAndStddev_(data) {
const dataMean = data.mean(0);
const diffFromMean = data.sub(dataMean);
const squaredDiffFromMean = diffFromMean.square();
const variance = squaredDiffFromMean.mean(0);
const dataStd = variance.sqrt();
return { dataMean, dataStd };
}
/**
* Given expected mean and standard deviation, normalizes a dataset by
* subtracting the mean and dividing by the standard deviation.
*
* @param {Tensor2d} data: Data to normalize.
* Shape: [numSamples, numFeatures].
* @param {Tensor1d} mean: Expected mean of the data. Shape [numFeatures].
* @param {Tensor1d} std: Expected std of the data. Shape [numFeatures]
*
* @returns {Tensor2d}: Tensor the same shape as data, but each column
* normalized to have zero mean and unit standard deviation.
*/
function normalizeTensor_(data, dataMean, dataStd) {
return data.sub(dataMean).div(dataStd);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment