-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample1.js
More file actions
36 lines (28 loc) · 689 Bytes
/
example1.js
File metadata and controls
36 lines (28 loc) · 689 Bytes
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
const tf = require('@tensorflow/tfjs')
const kgToLbs = kg => kg * 2.2
const xs = tf.tensor(Array.from({ length: 2000 }, (_, i) => i))
const ys = tf.tensor(Array.from({ length: 2000 }, (_, i) => kgToLbs(i)))
async function trainModel () {
const model = tf.sequential()
model.add(tf.layers.dense({
units: 1,
inputShape: 1
}))
model.compile({
loss: 'meanSquaredError',
optimizer: 'adam'
})
await model.fit(xs, ys, {
epochs: 500,
shuffle: true
})
return model
}
trainModel().then(model => {
console.log('Model trained')
const lbs = model
.predict(tf.tensor([10]))
.asScalar()
.dataSync()
console.log(`10 Kg to Lbs: ${lbs}`)
})