MCPcopy Create free account
hub / github.com/tensorflow/tfjs-examples / encode

Method encode

addition-rnn/index.js:58–68  ·  view source on GitHub ↗

* Convert a string into a one-hot encoded tensor. * * @param str The input string. * @param numRows Number of rows of the output tensor. * @returns The one-hot encoded 2D tensor. * @throws If `str` contains any characters outside the `CharacterTable`'s * vocabulary.

(str, numRows)

Source from the content-addressed store, hash-verified

56 * vocabulary.
57 */
58 encode(str, numRows) {
59 const buf = tf.buffer([numRows, this.size]);
60 for (let i = 0; i < str.length; ++i) {
61 const char = str[i];
62 if (this.charIndices[char] == null) {
63 throw new Error(`Unknown character: '${char}'`);
64 }
65 buf.set(1, i, this.charIndices[char]);
66 }
67 return buf.toTensor().as2D(numRows, this.size);
68 }
69
70 encodeBatch(strings, numRows) {
71 const numExamples = strings.length;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected