* 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)
| 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; |
nothing calls this directly
no outgoing calls
no test coverage detected