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