* Load IMDB targets from a file. * * @param {string} filePath Path to the binary targets file. * @return {tf.Tensor} The targets as `tf.Tensor` of shape `[numExamples, 1]` * and dtype `float32`. It has 0 or 1 values.
(filePath)
| 114 | * and dtype `float32`. It has 0 or 1 values. |
| 115 | */ |
| 116 | function loadTargets(filePath) { |
| 117 | const buffer = fs.readFileSync(filePath); |
| 118 | const numBytes = buffer.byteLength; |
| 119 | |
| 120 | let numPositive = 0; |
| 121 | let numNegative = 0; |
| 122 | |
| 123 | let ys = []; |
| 124 | for (let i = 0; i < numBytes; ++i) { |
| 125 | const y = buffer.readUInt8(i); |
| 126 | if (y === 1) { |
| 127 | numPositive++; |
| 128 | } else { |
| 129 | numNegative++; |
| 130 | } |
| 131 | ys.push(y); |
| 132 | } |
| 133 | |
| 134 | console.log( |
| 135 | `Loaded ${numPositive} positive examples and ` + |
| 136 | `${numNegative} negative examples.`); |
| 137 | return tf.tensor2d(ys, [ys.length, 1], 'float32'); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Get a file by downloading it if necessary. |