Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Encode the current input sequence as a one-hot Tensor.
const inputBuffer =
new tf.TensorBuffer([1, sampleLen, charSetSize]);
// Make the one-hot encoding of the seeding sentence.
for (let i = 0; i < sampleLen; ++i) {
inputBuffer.set(1, 0, i, sentenceIndices[i]);
}
const input = inputBuffer.toTensor();
// Call model.predict() to get the probability values of the next
// character.
const output = model.predict(input);
// Sample randomly based on the probability values.
const winnerIndex = sample(tf.squeeze(output), temperature);
const winnerChar = textData.getFromCharSet(winnerIndex);
if (onTextGenerationChar != null) {
onTextGenerationChar(winnerChar);
}
generated += winnerChar;
sentenceIndices = sentenceIndices.slice(1);
sentenceIndices.push(winnerIndex);
// Memory cleanups.
input.dispose();
output.dispose();
}
return generated;
}
const action = tf.tidy(() => {
let action = tf.squeeze(this.actor.predict(tf.tensor([state])));
if (train) {
const noise = softmax(this.noise.sample());
action = action.mul(1-this.epsilon).add(tf.mul(noise, this.epsilon));
}
return action;
});
const data = await action.data();