Skip to content

Commit

Permalink
handle min = max case during data normalization (#208)
Browse files Browse the repository at this point in the history
* handle min = max case

* add warning message
  • Loading branch information
ziyuan-linn authored Oct 3, 2024
1 parent e6daeee commit 8241125
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/NeuralNetwork/NeuralNetworkData.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ class NeuralNetworkData {
const dataLength = dataRaw.length;
// the copy of the inputs.meta[inputOrOutput]
const inputMeta = Object.assign({}, inputOrOutputMeta);

// normalized output object
const normalized = {};
Object.keys(inputMeta).forEach((k) => {
Expand Down Expand Up @@ -343,6 +342,11 @@ class NeuralNetworkData {
return normalized;
}

if (min === max) {
console.warn(
"🟪 ml5.js NeuralNetwork warns: Normalization failed, all data entries for an input parameter are identical (min === max). The data for this input parameter will be set to 0, effectively removing it from the model. Please check your input data."
);
}
// if the dtype is a number
if (inputArray.every((v) => typeof v === "number")) {
const normalized = inputArray.map((v) =>
Expand Down
16 changes: 11 additions & 5 deletions src/NeuralNetwork/NeuralNetworkUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ class NeuralNetworkUtils {
}

/**
* normalizeValue
* @param {*} value
* @param {*} min
* @param {*} max
* Normalize a value between min and max, return 0 if min === max
* @param {number} value - The value to normalize
* @param {number} min - The minimum bound
* @param {number} max - The maximum bound
*/
// eslint-disable-next-line class-methods-use-this
normalizeValue(value, min, max) {
// When min is equal to max, set everything to 0
if (min === max) {
return 0;
}
return (value - min) / (max - min);
}

Expand All @@ -22,6 +25,9 @@ class NeuralNetworkUtils {
*/
// eslint-disable-next-line class-methods-use-this
unnormalizeValue(value, min, max) {
if (min === max) {
return min;
}
return value * (max - min) + min;
}

Expand Down

0 comments on commit 8241125

Please sign in to comment.