Skip to content

Commit

Permalink
[sentiment] Revise *_CHAR to *_INDEX for clarity (tensorflow#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
caisq authored Feb 21, 2019
1 parent 66d9f35 commit 482226b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ to another project.
</tr>
<tr>
<td><a href="./date-conversion-attention">date-conversion-attention</a></td>
<td></td>
<td></td>
<td>Attention RNN for text-to-text conversion</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td><a href="https://storage.googleapis.com/tfjs-examples/date-conversion-attention/dist/index.html">🔗</a></td>
<td>Text</td>
<td>Text-to-text conversion</td>
<td>Attention mechanism, RNN</td>
<td>Node</td>
<td>Browser and Node</td>
<td>Layers</td>
<td>Saving to filesystem and loading in browser</td>
</tr>
<tr>
<td><a href="./iris">iris</a></td>
Expand Down Expand Up @@ -144,7 +144,7 @@ to another project.
<td><a href="./lstm-text-generation">lstm-text-generation</a></td>
<td><a href="https://storage.googleapis.com/tfjs-examples/lstm-text-generation/dist/index.html">🔗</a></td>
<td>Text</td>
<td>Sequence-to-prediction</td>
<td>Sequence prediction</td>
<td>RNN: LSTM</td>
<td>Browser</td>
<td>Browser</td>
Expand Down
6 changes: 3 additions & 3 deletions sentiment/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import * as https from 'https';
import * as os from 'os';
import * as path from 'path';

import {OOV_CHAR, PAD_CHAR, padSequences} from './sequence_utils';
import {OOV_INDEX, PAD_INDEX, padSequences} from './sequence_utils';

// `import` doesn't seem to work with extract-zip.
const extract = require('extract-zip');
Expand All @@ -36,7 +36,7 @@ const METADATA_TEMPLATE_URL =
*
* @param {string} filePath Data file on local filesystem.
* @param {string} numWords Number of words in the vocabulary. Word indices
* that exceed this limit will be marked as `OOV_CHAR`.
* that exceed this limit will be marked as `OOV_INDEX`.
* @param {string} maxLen Length of each sequence. Longer sequences will be
* pre-truncated; shorter ones will be pre-padded.
* @return {tf.Tensor} The dataset represented as a 2D `tf.Tensor` of shape
Expand All @@ -60,7 +60,7 @@ function loadFeatures(filePath, numWords, maxLen) {
seq = [];
} else {
// Sequence continues.
seq.push(value >= numWords ? OOV_CHAR : value);
seq.push(value >= numWords ? OOV_INDEX : value);
}
index += 4;
}
Expand Down
4 changes: 2 additions & 2 deletions sentiment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import * as tf from '@tensorflow/tfjs';
import * as loader from './loader';
import * as ui from './ui';
import {OOV_CHAR, padSequences} from './sequence_utils';
import {OOV_INDEX, padSequences} from './sequence_utils';

const HOSTED_URLS = {
model:
Expand Down Expand Up @@ -65,7 +65,7 @@ class SentimentPredictor {
const sequence = inputText.map(word => {
let wordIndex = this.wordIndex[word] + this.indexFrom;
if (wordIndex > this.vocabularySize) {
wordIndex = OOV_CHAR;
wordIndex = OOV_INDEX;
}
return wordIndex;
});
Expand Down
6 changes: 3 additions & 3 deletions sentiment/sequence_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
* Utilities for sequential data.
*/

export const PAD_CHAR = 0;
export const OOV_CHAR = 2;
export const PAD_INDEX = 0; // Index of the padding character.
export const OOV_INDEX = 2; // Index fo the OOV character.

/**
* Pad and truncate all sequences to the same length
Expand All @@ -34,7 +34,7 @@ export const OOV_CHAR = 2;
* @param {number} value Padding value.
*/
export function padSequences(
sequences, maxLen, padding = 'pre', truncating = 'pre', value = PAD_CHAR) {
sequences, maxLen, padding = 'pre', truncating = 'pre', value = PAD_INDEX) {
// TODO(cais): This perhaps should be refined and moved into tfjs-preproc.
return sequences.map(seq => {
// Perform truncation.
Expand Down

0 comments on commit 482226b

Please sign in to comment.