forked from tensorflow/tfjs-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
187 lines (170 loc) · 5.11 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* @license
* Copyright 2018 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import * as tf from '@tensorflow/tfjs';
import * as Papa from 'papaparse';
const BASE_URL =
'https://gist.githubusercontent.com/ManrajGrover/6589d3fd3eb9a0719d2a83128741dfc1/raw/d0a86602a87bfe147c240e87e6a9641786cafc19/';
/**
*
* @param {Array<Object>} data Downloaded data.
*
* @returns {Promise.Array<number[]>} Resolves to data with values parsed as
* floats.
*/
async function parseCsv (data) {
return new Promise(resolve => {
data = data.map((row) => {
return Object.keys(row).sort().map(key => parseFloat(row[key]));
});
resolve(data);
});
};
/**
* Downloads and returns the csv.
*
* @param {string} filename Name of file to be loaded.
*
* @returns {Promise.Array<number[]>} Resolves to parsed csv data.
*/
export async function loadCsv(filename) {
return new Promise(resolve => {
const url = `${BASE_URL}${filename}.csv`;
console.log(` * Downloading data from: ${url}`);
Papa.parse(url, {
download: true,
header: true,
complete: (results) => {
resolve(parseCsv(results['data']));
}
})
});
};
/**
* Shuffles data and label using Fisher-Yates algorithm.
*/
export async function shuffle(data, label) {
let counter = data.length;
let temp = 0;
let index = 0;
while (counter > 0) {
index = (Math.random() * counter) | 0;
counter--;
// data:
temp = data[counter];
data[counter] = data[index];
data[index] = temp;
// label:
temp = label[counter];
label[counter] = label[index];
label[index] = temp;
}
};
/**
* Calculate the arithmetic mean of a vector.
*
* @param {Array} vector The vector represented as an Array of Numbers.
*
* @returns {number} The arithmetic mean.
*/
function mean(vector) {
let sum = 0;
for (const x of vector) {
sum += x;
}
return sum / vector.length;
};
/**
* Calculate the standard deviation of a vector.
*
* @param {Array} vector The vector represented as an Array of Numbers.
*
* @returns {number} The standard deviation.
*/
function stddev(vector) {
let squareSum = 0;
const vectorMean = mean(vector);
for (const x of vector) {
squareSum += (x - vectorMean) * (x - vectorMean);
}
return Math.sqrt(squareSum / (vector.length - 1));
};
/**
* Normalize a vector by its mean and standard deviation.
*
* @param {Array} vector Vector to be normalized.
* @param {number} vectorMean Mean to be used.
* @param {number} vectorStddev Standard Deviation to be used.
*
* @returns {Array} Normalized vector.
*/
const normalizeVector = (vector, vectorMean, vectorStddev) => {
return vector.map(x => (x - vectorMean) / vectorStddev);
};
/**
* Normalizes the dataset
*
* @param {Array} dataset Dataset to be normalized.
* @param {boolean} isTrainData Whether it is training data or not.
* @param {Array} vectorMeans Mean of each column of dataset.
* @param {Array} vectorStddevs Standard deviation of each column of dataset.
*
* @returns {Object} Contains normalized dataset, mean of each vector column,
* standard deviation of each vector column.
*/
export function normalizeDataset(
dataset, isTrainData = true, vectorMeans = [], vectorStddevs = []) {
const numFeatures = dataset[0].length;
let vectorMean;
let vectorStddev;
for (let i = 0; i < numFeatures; i++) {
const vector = dataset.map(row => row[i]);
if (isTrainData) {
vectorMean = mean(vector);
vectorStddev = stddev(vector);
vectorMeans.push(vectorMean);
vectorStddevs.push(vectorStddev);
} else {
vectorMean = vectorMeans[i];
vectorStddev = vectorStddevs[i];
}
const vectorNormalized =
normalizeVector(vector, vectorMean, vectorStddev);
vectorNormalized.forEach((value, index) => {
dataset[index][i] = value;
});
}
return {dataset, vectorMeans, vectorStddevs};
};
/**
* Binarizes a tensor based on threshold of 0.5.
*
* @param {tf.Tensor} y Tensor to be binarized.
* @param {number} threshold (default: 0.5).
* @returns {tf.Tensor} Binarized tensor.
*/
export function binarize(y, threshold) {
if (threshold == null) {
threshold = 0.5;
}
tf.util.assert(
threshold >= 0 && threshold <= 1,
`Expected threshold to be >=0 and <=1, but got ${threshold}`);
return tf.tidy(() => {
const condition = y.greater(tf.scalar(threshold));
return tf.where(condition, tf.onesLike(y), tf.zerosLike(y));
});
}