MCPcopy Index your code
hub / github.com/tensorflow/tfjs-examples / encodeString

Method encodeString

translation/index.js:110–129  ·  view source on GitHub ↗

* Encode a string (e.g., a sentence) as a Tensor3D that can be fed directly * into the TensorFlow.js model.

(str)

Source from the content-addressed store, hash-verified

108 * into the TensorFlow.js model.
109 */
110 encodeString(str) {
111 const strLen = str.length;
112 const encoded =
113 tf.buffer([1, this.maxEncoderSeqLength, this.numEncoderTokens]);
114 for (let i = 0; i < strLen; ++i) {
115 if (i >= this.maxEncoderSeqLength) {
116 console.error(
117 'Input sentence exceeds maximum encoder sequence length: ' +
118 this.maxEncoderSeqLength);
119 }
120
121 const tokenIndex = this.inputTokenIndex[str[i]];
122 if (tokenIndex == null) {
123 console.error(
124 'Character not found in input token index: "' + tokenIndex + '"');
125 }
126 encoded.set(1, 0, i, tokenIndex);
127 }
128 return encoded.toTensor();
129 }
130
131 decodeSequence(inputSeq) {
132 // Encode the inputs state vectors.

Callers 1

translateMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected