-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (60 loc) · 1.19 KB
/
index.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
const tf = require("@tensorflow/tfjs")
require("@tensorflow/tfjs-node")
const iris = require("./iris.json")
const irisTesting = require("./iris-testing.json")
// convert/setup our data
const trainingData = tf.tensor2d(
iris.map(item => [
item.sepal_length,
item.sepal_width,
item.petal_length,
item.petal_width
])
)
const outputData = tf.tensor2d(
iris.map(item => [
item.species === "setosa" ? 1 : 0,
item.species === "virginica" ? 1 : 0,
item.species === "versicolor" ? 1 : 0
])
)
const testingData = tf.tensor2d(
irisTesting.map(item => [
item.sepal_length,
item.sepal_width,
item.petal_length,
item.petal_width
])
)
// build neural network
const model = tf.sequential()
model.add(
tf.layers.dense({
inputShape: [4],
activation: "sigmoid",
units: 5
})
)
model.add(
tf.layers.dense({
inputShape: [5],
activation: "sigmoid",
units: 3
})
)
model.add(
tf.layers.dense({
activation: "sigmoid",
units: 3
})
)
model.compile({
loss: "meanSquaredError",
optimizer: tf.train.adam(0.06)
})
// train/fit our network
const startTime = Date.now()
model.fit(trainingData, outputData, { epochs: 100 }).then(history => {
// test network
model.predict(testingData).print()
})