-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtensorflow.js
More file actions
62 lines (49 loc) · 1.72 KB
/
Copy pathtensorflow.js
File metadata and controls
62 lines (49 loc) · 1.72 KB
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
// code not working yet
const tf = require('@tensorflow/tfjs-node');
const data = {
// assume we already have vector representations of the text examples
inputs: vectorRepresentations,
// imagine we have such 3 classes
output: [0, 0, 2, 1, 2, 1, 0, 1],
}
// tensors are TensorFlow vectors to simplify the internal
// processing for the library
const inputTensors = tf.tensor(data.inputs);
const outputTensors = tf.tensor(data.outputs);
const model = tf.sequential();
// 1st layer: a 1d convolutional network
model.add(tf.layers.conv1d({
filters: 100,
kernelSize: 3,
strides: 1,
activation: 'relu',
padding: 'valid',
inputShape: [MAX_WORDS_LENGTH, GLOVE_VECTOR_DIMENSIONS],
}));
// transform 2d input into 1d
model.add(tf.layers.globalMaxPool1d({}));
// the final layer with one neuron
model.add(tf.layers.dense({ units: 1, activation: 'sigmoid' }));
// here are some tuning, read in the TF docs for more
model.compile({
optimizer: tf.train.adam(LEARNING_RATE),
loss: 'binaryCrossentropy',
metrics: ['accuracy'],
});
// print the model architecture
model.summary();
// train the model
await model.fit(inputs, answers, {
// the default size, how many inputs to process per time
batchSize: 32,
// how many times to "process", simply put
epochs: EPOCHS,
// the fraction of the inputs to be in the validation set:
// the set, which isn't trained on, but participates in calculating
// the model's metrics such as accuracy and loss
validationSplit: 0.2,
// shuffle inputs randomly to have a different starting seed every time
shuffle: true,
});
// save the model to load in the future and run classifications
await model.save('file://./data/models/myFirstModel');