-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgarbage
254 lines (210 loc) · 6.31 KB
/
garbage
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// const csv = Papa.unparse(formattedData, {
// header: true,
// columns: ["promptName", "eegData"],
// });
// // write csv to file
// fs.writeFileSync("data.csv", csv);
// console.log("csv", csv);
// // const csvUrl = "http://localhost:3232/api/data.csv";
// // const csvDataset = tf.data.csv(csvUrl, {
// // columnConfigs: {
// // promptName: {
// // isLabel: true,
// // },
// // },
// // });
// // // Number of features is the number of column names minus one for the label
// // // column.
// // console.log(await csvDataset.columnNames(), "reached 151");
// // let testConsoel = true;
// // // noramlize fucntion to normalize the data to be between 0 and 1 with min max of -1000 and 1000
// // // Prepare the Dataset for training.
// // const flattenedDataset = csvDataset
// // .map(({ xs, ys }) => {
// // // Convert xs(features) and ys(labels) from object form (keyed by
// // // column name) to array form.
// // if (testConsoel) {
// // console.log("xs", xs, "ys", ys, "142");
// // console.log("xs", Object.values(xs), "ys", Object.values(ys), "143");
// // testConsoel = false;
// // }
// // return { xs: Object.values(xs), ys: Object.values(ys) };
// // })
// // .batch(10);
// // const numOfFeatures = (await csvDataset.columnNames()).length - 1;
// // Define the model.
// // model.summary();
// // let singularLabel = [];
// // async function singleton() {
// // // let instance = tf.tensor(formattedData[0]);
// // // const c = tf.data.array(formattedData[Object.keys(formattedData)[0]]); //.batch(4);
// // // await c.forEachAsync((e) => {
// // singularData.push(formattedData[Object.keys(formattedData)[0]].features);
// // singularLabel.push(formattedData[Object.keys(formattedData)[0]].labelName);
// // console.log(
// // "127 singularData: ",
// // singularData,
// // "singularLabel:",
// // singularLabel
// // );
// // // });
// // }
// const csvDataset = tf.data.csv("http://localhost:3232/api/data.csv", {
// columnConfigs: {
// promptName: {
// isLabel: true,
// },
// },
// });
// console.log("csvDataset", csvDataset);
// const numOfFeatures = (await csvDataset.columnNames()).length - 1;
// console.log("numOfFeatures", numOfFeatures);
// // Prepare the dataset for training.
// const convertedData = csvDataset
// .map(({ xs, ys }) => {
// // Convert xs(features) and ys(labels) from object form (keyed by column name)
// // to array form.
// console.log("xs", xs, "ys", ys);
// return { xs: Object.values(xs), ys: Object.values(ys) };
// })
// .batch(10);
// // await singleton();
// // create a rnn model for eeg data with 2 layers
// const model = tf.sequential();
// // add a dense layer with 36 neurons
// model.add(
// tf.layers.dense({
// inputShape: [numOfFeatures],
// activation: "sigmoid",
// units: 36,
// })
// );
// // add a dense layer with 14 neurons
// model.add(
// tf.layers.dense({
// activation: "sigmoid",
// units: 14,
// })
// );
// // add a dense layer with 2 neurons
// model.add(
// tf.layers.dense({
// activation: "sigmoid",
// units: 2,
// })
// );
// // compile the model
// model.compile({
// optimizer: tf.train.adam(),
// loss: tf.losses.meanSquaredError,
// metrics: ["accuracy"],
// });
// // train the model
// await model.fitDataset(convertedData, {
// epochs: 10,
// callbacks: {
// onEpochEnd: async (epoch, logs) => {
// console.log("Epoch:", epoch, "Loss:", logs.loss);
// console.log("Epoch:", epoch, "Accuracy:", logs.acc);
// },
// },
// });
// create a sequential model
// const model = tf.sequential();
// // compile for string tensor
// // await model.compile({
// // optimizer: tf.train.adam(),
// // loss: tf.losses.meanSquaredError,
// // metrics: ["accuracy"],
// // });
// // await model.compile({
// await model.add(
// tf.layers.dense({
// inputShape: [numOfFeatures],
// activation: "sigmoid",
// units: 36,
// })
// );
// // Add a single hidden layern
// await model.add(
// tf.layers.dense({
// activation: "sigmoid",
// units: 14,
// })
// );
// // Add an output layer
// await model.add(
// tf.layers.dense({
// activation: "sigmoid",
// units: 1,
// })
// );
// model.summary();
// // Compile the model using the settings above.
// optimizer: tf.train.adam(),
// loss: tf.losses.meanSquaredError,
// metrics: ["accuracy"],
// });
// model.fitDataset(convertedData, {
// epochs: 100,
// shuffle: true,
// callbacks: {
// onEpochEnd: async (epoch, logs) => {
// console.log(`Epoch ${epoch}: loss = ${logs.loss}`);
// },
// },
// });
//Train the model using the singleton data
// const xs = tf.tensor(singularData);
// console.log(149, xs.shape);
// const ys = tf.tensor(singularLabel);
// console.log(152, ys.shape);
// await model.fit(xs, ys, {
// epochs: 10,
// shuffle: true,
// callbacks: {
// onEpochEnd: async (epoch, logs) => {
// console.log(`Epoch ${epoch}: loss = ${logs.loss}`);
// },
// },
// });
// Train the model using the data.
// const xs = tf.tensor(
// formattedData.map((item) => {
// return item.features;
// })
// );
// console.log(149, xs.shape);
// // const ys = tf.tensor(formattedData.map((item) => item.labelName));
// console.log(
// 152,
// formattedData.map((item) => item.labelName)
// );
// await model.fit(xs, xs, {
// epochs: 10,
// shuffle: true,
// callbacks: {
// onEpochEnd: async (epoch, logs) => {
// console.log(`Epoch ${epoch}: loss = ${logs.loss}`);
// },
// },
// });
// Save the model to a file
// model.save("file://./model");
// convert the data to a form we can use for training
const { inputs, labels } = convertToTensors(formattedData);
// train the model
await model.fit(inputs, labels, {
batchSize: 32,
epochs: 10,
callbacks: tf.node.tensorBoard("/tmp/tfjs_logs"),
});
// evaluate the model
const evalOutput = model.evaluate(inputs, labels);
console.log(`Accuracy: ${(evalOutput[1].dataSync()[0] * 100).toFixed(1)}%`);
// make some predictions using the model and compare them to the
// labels
const preds = model.predict(inputs).dataSync();
labels.dataSync().forEach((val, i) => {
console.log(`Label: ${val}, Prediction: ${preds[i]}`);
});