-
-
Save codeas/a78c17a8829a8b934c7fb5b9ccc30c84 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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