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

Method encode

addition-rnn-webworker/worker.js:50–60  ·  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

48 * vocabulary.
49 */
50 encode(str, numRows) {
51 const buf = tf.buffer([numRows, this.size]);
52 for (let i = 0; i < str.length; ++i) {
53 const char = str[i];
54 if (this.charIndices[char] == null) {
55 throw new Error(`Unknown character: '${char}'`);
56 }
57 buf.set(1, i, this.charIndices[char]);
58 }
59 return buf.toTensor().as2D(numRows, this.size);
60 }
61
62 encodeBatch(strings, numRows) {
63 const numExamples = strings.length;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected