Skip to content

Commit 8241125

Browse files
authored
handle min = max case during data normalization (#208)
* handle min = max case * add warning message
1 parent e6daeee commit 8241125

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/NeuralNetwork/NeuralNetworkData.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,6 @@ class NeuralNetworkData {
284284
const dataLength = dataRaw.length;
285285
// the copy of the inputs.meta[inputOrOutput]
286286
const inputMeta = Object.assign({}, inputOrOutputMeta);
287-
288287
// normalized output object
289288
const normalized = {};
290289
Object.keys(inputMeta).forEach((k) => {
@@ -343,6 +342,11 @@ class NeuralNetworkData {
343342
return normalized;
344343
}
345344

345+
if (min === max) {
346+
console.warn(
347+
"🟪 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."
348+
);
349+
}
346350
// if the dtype is a number
347351
if (inputArray.every((v) => typeof v === "number")) {
348352
const normalized = inputArray.map((v) =>

src/NeuralNetwork/NeuralNetworkUtils.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ class NeuralNetworkUtils {
44
}
55

66
/**
7-
* normalizeValue
8-
* @param {*} value
9-
* @param {*} min
10-
* @param {*} max
7+
* Normalize a value between min and max, return 0 if min === max
8+
* @param {number} value - The value to normalize
9+
* @param {number} min - The minimum bound
10+
* @param {number} max - The maximum bound
1111
*/
12-
// eslint-disable-next-line class-methods-use-this
1312
normalizeValue(value, min, max) {
13+
// When min is equal to max, set everything to 0
14+
if (min === max) {
15+
return 0;
16+
}
1417
return (value - min) / (max - min);
1518
}
1619

@@ -22,6 +25,9 @@ class NeuralNetworkUtils {
2225
*/
2326
// eslint-disable-next-line class-methods-use-this
2427
unnormalizeValue(value, min, max) {
28+
if (min === max) {
29+
return min;
30+
}
2531
return value * (max - min) + min;
2632
}
2733

0 commit comments

Comments
 (0)