Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let wordIndex = sentimentMetadata['word_index'];
console.log('indexFrom = ' + indexFrom);
console.log('maxLen = ' + maxLen);
console.log('model_type', sentimentMetadata['model_type']);
console.log('vocabulary_size', sentimentMetadata['vocabulary_size']);
console.log('max_len', sentimentMetadata['max_len']);
const inputText =
text.trim().toLowerCase().replace(/(\.|\,|\!)/g, '').split(/\s+/g); // tokenized
console.log(inputText);
// Look up word indices.
const inputBuffer = tf.buffer([1, maxLen], 'float32');
for (let i = 0; i < inputText.length; ++i) {
const word = inputText[i];
if (typeof wordIndex[word] == 'undefined') { // TODO(cais): Deal with OOV words.
console.log(word, wordIndex[word]);
}
inputBuffer.set(wordIndex[word] + indexFrom, 0, i);
}
const input = inputBuffer.toTensor();
console.log(text, "\n", input);
const beginMs = performance.now();
const predictOut = model.predict(input);
const score = predictOut.dataSync()[0];
predictOut.dispose();
const endMs = performance.now();
// Sample a token.
// We know that outputTokens.shape is [1, 1, n], so no need for slicing.
const logits = outputTokens.reshape([outputTokens.shape[2]]);
const sampledTokenIndex = logits.argMax().dataSync()[0];
const sampledChar = this.reverseTargetCharIndex[sampledTokenIndex];
decodedSentence += sampledChar;
// Exit condition: either hit max length or find stop character.
if (sampledChar === '\n' ||
decodedSentence.length > this.maxDecoderSeqLength) {
stopCondition = true;
}
// Update the target sequence (of length 1).
targetSeq = tf.buffer([1, 1, this.numDecoderTokens]);
targetSeq.set(1, 0, 0, sampledTokenIndex);
// Update states.
statesValue = [h, c];
}
return decodedSentence;
}
this.decodeSequence = (inputSeq) => {
// Encode the inputs state vectors.
let statesValue = this.encoderModel.predict(inputSeq);
// Generate empty target sequence of length 1.
let targetSeq = tf.buffer([1, 1, this.numDecoderTokens]);
// Populate the first character of the target sequence with the start
// character.
targetSeq.set(1, 0, 0, this.targetTokenIndex['\t']);
// Sample loop for a batch of sequences.
// (to simplify, here we assume that a batch of size 1).
let stopCondition = false;
let decodedSentence = '';
while (!stopCondition) {
const predictOutputs =
this.decoderModel.predict([targetSeq.toTensor()].concat(statesValue));
const outputTokens = predictOutputs[0];
const h = predictOutputs[1];
const c = predictOutputs[2];
// Sample a token.
this.encodeString = (str) => {
const strLen = str.length;
const encoded =
tf.buffer([1, this.maxEncoderSeqLength, this.numEncoderTokens]);
for (let i = 0; i < strLen; ++i) {
if (i >= this.maxEncoderSeqLength) {
console.error(
'Input sentence exceeds maximum encoder sequence length: ' +
this.maxEncoderSeqLength);
}
const tokenIndex = this.inputTokenIndex[str[i]];
if (tokenIndex == null) {
console.error(
'Character not found in input token index: "' + tokenIndex + '"');
}
encoded.set(1, 0, i, tokenIndex);
}
return encoded.toTensor();
}
function generateDataset(numExamples, sequenceLength) {
const sequencesBuffer = tf.buffer([numExamples, sequenceLength, 2]);
const labelsBuffer = tf.buffer([numExamples, 1]);
for (let i = 0; i < numExamples; ++i) {
const [sequence, label] = generateSequenceAndLabel(sequenceLength);
for (let j = 0; j < sequenceLength; ++j) {
sequencesBuffer.set(1, i, j, sequence[j]);
}
labelsBuffer.set(label, i, 0);
}
return [sequencesBuffer.toTensor(), labelsBuffer.toTensor()];
}
function generateDataset(numExamples, sequenceLength) {
const sequencesBuffer = tf.buffer([numExamples, sequenceLength, 2]);
const labelsBuffer = tf.buffer([numExamples, 1]);
for (let i = 0; i < numExamples; ++i) {
const [sequence, label] = generateSequenceAndLabel(sequenceLength);
for (let j = 0; j < sequenceLength; ++j) {
sequencesBuffer.set(1, i, j, sequence[j]);
}
labelsBuffer.set(label, i, 0);
}
return [sequencesBuffer.toTensor(), labelsBuffer.toTensor()];
}