I've been working on a logistic regression model using TensorFlow.js, focusing only on the core API. I'm generating synthetic data and then running a training function that contains all the necessary logic.
Here's a snippet of the code:
const tf = require('@tensorflow/tfjs-core');
const NUM_OF_CLASSES = 1;
const NUM_OF_EXAMPLES = 1000;
const NUM_OF_VARIABLES = 300;
const NUM_EPOCHS = 10;
function calculate_X(N, D) {
return tf.randomNormal([N, D], 0.0, 1.0);
}
function calculate_y(X) {
const stepData = tf.tidy(
() => tf.step(tf.slice2d(X, [0,0], [X.shape[0],1])).reshape([-1])
);
return stepData;
}
const X = calculate_X(NUM_OF_EXAMPLES, NUM_OF_VARIABLES);
const y = calculate_y(X);
function train(X, y) {
const w = tf.variable(tf.zeros([NUM_OF_VARIABLES, NUM_OF_CLASSES]));
const b = tf.variable(tf.zeros([NUM_OF_CLASSES]))
const model = x =>
x.matMul(w)
.add(b)
.softmax()
.as1D();
const optimizer = tf.train.adam(0.1 /* learningRate */);
for (let epoch = 0; epoch < NUM_EPOCHS; epoch++) {
optimizer.minimize(() => {
const predYs = model(X);
predYs.data().then(d => console.log('predYs', d));
y.data().then(d => console.log('y', d));
const loss = tf.losses.meanSquaredError(y, predYs);
loss.data().then(l => console.log('Loss', l));
return loss;
}, true, [b, w]);
}
}
train(X, y);
Despite the iterations running smoothly, the variables b
& w
are not updating in each run.