|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2018 Google LLC. All Rights Reserved. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * ============================================================================= |
| 16 | + */ |
| 17 | + |
| 18 | +import * as tf from '@tensorflow/tfjs'; |
| 19 | + |
| 20 | +/** |
| 21 | + * A class for text data. |
| 22 | + * |
| 23 | + * This class manages the following: |
| 24 | + * |
| 25 | + * - Converting training data (as a string) into one-hot encoded vectors. |
| 26 | + * - Drawing random slices from the training data. This is useful for training |
| 27 | + * models and obtaining the seed text for model-based text generation. |
| 28 | + */ |
| 29 | +export class TextData { |
| 30 | + /** |
| 31 | + * Constructor of TextData. |
| 32 | + * |
| 33 | + * @param {string} dataIdentifier An identifier for this instance of TextData. |
| 34 | + * @param {string} textString The training text data. |
| 35 | + * @param {number} sampleLen Length of each training example, i.e., the input |
| 36 | + * sequence length expected by the LSTM model. |
| 37 | + * @param {number} sampleStep How many characters to skip when going from one |
| 38 | + * example of the training data (in `textString`) to the next. |
| 39 | + */ |
| 40 | + constructor(dataIdentifier, textString, sampleLen, sampleStep) { |
| 41 | + if (!dataIdentifier) { |
| 42 | + throw new Error('Model identifier is not provided.'); |
| 43 | + } |
| 44 | + |
| 45 | + this.dataIdentifier_ = dataIdentifier; |
| 46 | + |
| 47 | + this.textString_ = textString; |
| 48 | + this.textLen_ = textString.length; |
| 49 | + this.sampleLen_ = sampleLen; |
| 50 | + this.sampleStep_ = sampleStep; |
| 51 | + |
| 52 | + this.getCharSet_(); |
| 53 | + this.convertAllTextToIndices_(); |
| 54 | + this.generateExampleBeginIndices_(); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get data identifier. |
| 59 | + * |
| 60 | + * @returns {string} The data identifier. |
| 61 | + */ |
| 62 | + dataIdentifier() { |
| 63 | + return this.dataIdentifier_; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Get length of the training text data. |
| 68 | + * |
| 69 | + * @returns {number} Length of training text data. |
| 70 | + */ |
| 71 | + textLen() { |
| 72 | + return this.textLen_; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Get the length of each training example. |
| 77 | + */ |
| 78 | + sampleLen() { |
| 79 | + return this.sampleLen_; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Get the size of the character set. |
| 84 | + * |
| 85 | + * @returns {number} Size of the character set, i.e., how many unique |
| 86 | + * characters there are in the training text data. |
| 87 | + */ |
| 88 | + charSetSize() { |
| 89 | + return this.charSetSize_; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Generate the next epoch of data for training models. |
| 94 | + * |
| 95 | + * @param {number} numExamples Number examples to generate. |
| 96 | + * @returns {[tf.Tensor, tf.Tensor]} `xs` and `ys` Tensors. |
| 97 | + * `xs` has the shape of `[numExamples, this.sampleLen, this.charSetSize]`. |
| 98 | + * `ys` has the shape of `[numExamples, this.charSetSize]`. |
| 99 | + */ |
| 100 | + nextDataEpoch(numExamples) { |
| 101 | + const xsBuffer = new tf.TensorBuffer([ |
| 102 | + numExamples, this.sampleLen_, this.charSetSize_]); |
| 103 | + const ysBuffer = new tf.TensorBuffer([numExamples, this.charSetSize_]); |
| 104 | + for (let i = 0; i < numExamples; ++i) { |
| 105 | + const beginIndex = this.exampleBeginIndices_[ |
| 106 | + this.examplePosition_ % this.exampleBeginIndices_.length]; |
| 107 | + for (let j = 0; j < this.sampleLen_; ++j) { |
| 108 | + xsBuffer.set(1, i, j, this.indices_[beginIndex + j]); |
| 109 | + } |
| 110 | + ysBuffer.set(1, i, this.indices_[beginIndex + this.sampleLen_]); |
| 111 | + this.examplePosition_++; |
| 112 | + } |
| 113 | + return [xsBuffer.toTensor(), ysBuffer.toTensor()]; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Get the unique character at given index from the character set. |
| 118 | + * |
| 119 | + * @param {number} index |
| 120 | + * @returns {string} The unique character at `index` of the character set. |
| 121 | + */ |
| 122 | + getFromCharSet(index) { |
| 123 | + return this.charSet_[index]; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Convert text string to integer indices. |
| 128 | + * |
| 129 | + * @param {string} text Input text. |
| 130 | + * @returns {number[]} Indices of the characters of `text`. |
| 131 | + */ |
| 132 | + textToIndices(text) { |
| 133 | + const indices = []; |
| 134 | + for (let i = 0; i < text.length; ++i) { |
| 135 | + indices.push(this.charSet_.indexOf(text[i])); |
| 136 | + } |
| 137 | + return indices; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Get a random slice of text data. |
| 142 | + * |
| 143 | + * @returns {[string, number[]} The string and index representation of the |
| 144 | + * same slice. |
| 145 | + */ |
| 146 | + getRandomSlice() { |
| 147 | + const startIndex = |
| 148 | + Math.round(Math.random() * (this.textLen_ - this.sampleLen_ - 1)); |
| 149 | + const textSlice = this.slice_(startIndex, startIndex + this.sampleLen_); |
| 150 | + return [textSlice, this.textToIndices(textSlice)]; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Get a slice of the training text data. |
| 155 | + * |
| 156 | + * @param {number} startIndex |
| 157 | + * @param {number} endIndex |
| 158 | + * @param {bool} useIndices Whether to return the indices instead of string. |
| 159 | + * @returns {string | Uint16Array} The result of the slicing. |
| 160 | + */ |
| 161 | + slice_(startIndex, endIndex) { |
| 162 | + return this.textString_.slice(startIndex, endIndex); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Get the set of unique characters from text. |
| 167 | + */ |
| 168 | + getCharSet_() { |
| 169 | + this.charSet_ = []; |
| 170 | + for (let i = 0; i < this.textLen_; ++i) { |
| 171 | + if (this.charSet_.indexOf(this.textString_[i]) === -1) { |
| 172 | + this.charSet_.push(this.textString_[i]); |
| 173 | + } |
| 174 | + } |
| 175 | + this.charSetSize_ = this.charSet_.length; |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Convert all training text to integer indices. |
| 180 | + */ |
| 181 | + convertAllTextToIndices_() { |
| 182 | + this.indices_ = new Uint16Array(this.textToIndices(this.textString_)); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Generate the example-begin indices; shuffle them randomly. |
| 187 | + */ |
| 188 | + generateExampleBeginIndices_() { |
| 189 | + // Prepare beginning indices of examples. |
| 190 | + this.exampleBeginIndices_ = []; |
| 191 | + for (let i = 0; |
| 192 | + i < this.textLen_ - this.sampleLen_ - 1; |
| 193 | + i += this.sampleStep_) { |
| 194 | + this.exampleBeginIndices_.push(i); |
| 195 | + } |
| 196 | + |
| 197 | + // Randomly shuffle the beginning indices. |
| 198 | + tf.util.shuffle(this.exampleBeginIndices_); |
| 199 | + this.examplePosition_ = 0; |
| 200 | + } |
| 201 | +} |
0 commit comments