Thanks for testing, Shanquing.
I tried the same, and it worked, so I started with that base code and rewrote the code I originally had on top of it.
Now I'm getting an error on this line:
tf.multinomial(preds, 1, null, true).dataSync()[0];
that says "ERROR: Failed to generate text: TF Node backend does not support normalized logits passed to multinomial, Error: TF Node backend does not support normalized logits passed to multinomial
at NodeJSKernelBackend.multinomial (/work/test_lstm_weights/node_modules/@tensorflow/tfjs-node/dist/nodejs_kernel_backend.js:862:19)..."
Does that mean that with the node version of tfjs, I can't normalize logits?
The same line works fine with the regular version of tfjs. The 'preds' variable is the same in both versions:
preds:Tensor
[0.000011, 7e-7, 0.0000261, ..., 6e-7, 0.0000011, 8e-7]
(The entire function is here:
function sample(preds, temperature) {
return tf.tidy(() => {
const logPreds = tf.div(tf.log(preds), temperature);
const expPreds = tf.exp(logPreds);
const sumExpPreds = tf.sum(expPreds);
preds = tf.div(expPreds, sumExpPreds);
console.log('preds: '+preds);
// Treat preds a the probabilites of a multinomial distribution and
// randomly draw a sample from the distribution.
return tf.multinomial(preds, 1, null, true).dataSync()[0];
});
}
)
I'm not super familiar with deep learning and tensorflow (this code is based on the
lstm example here), but my guess is that I should not be doing this (which looks like it's normalizing the preds) when using tfjs-node:
const logPreds = tf.div(tf.log(preds), temperature);
const expPreds = tf.exp(logPreds);
const sumExpPreds = tf.sum(expPreds);
preds = tf.div(expPreds, sumExpPreds);
What would be a way around this? Can I just directly feed preds into tf.multinomial, or would that ruin the lstm? Can I use the normal tfjs instead of tfjs-node so that I can still normalize?