-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.js
31 lines (26 loc) · 908 Bytes
/
data.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
import * as tf from '@tensorflow/tfjs'
export function generateData(numPoints, coeff, sigma = 0.04) {
return tf.tidy(() => {
var coe = {}
for (var n in coeff) {
coe[n] = tf.scalar(coeff[n])
}
const xs = tf.randomUniform([numPoints], -1, 1)
// Generate polynomial data - can't add sequentially - has to be one go
var ys = tf
.randomNormal([numPoints], 0, sigma) // Add random noise to the generated data
.add(coe[0].mul(xs.pow(tf.scalar(0, 'int32'))))
.add(coe[3].mul(xs.pow(tf.scalar(3, 'int32'))))
.add(coe[2].mul(xs.pow(tf.scalar(2, 'int32'))))
.add(coe[1].mul(xs.pow(tf.scalar(1, 'int32'))))
// Normalize the y values to the range 0 to 1.
const ymin = ys.min()
const ymax = ys.max()
const yrange = ymax.sub(ymin)
const ysNormalized = ys.sub(ymin).div(yrange)
return {
xs,
ys: ysNormalized
}
})
}