MCPcopy
hub / github.com/tensorflow/tfjs-examples / padSequences

Function padSequences

sentiment/sequence_utils.js:36–64  ·  view source on GitHub ↗
(
    sequences, maxLen, padding = 'pre', truncating = 'pre', value = PAD_INDEX)

Source from the content-addressed store, hash-verified

34 * @param {number} value Padding value.
35 */
36export function padSequences(
37 sequences, maxLen, padding = 'pre', truncating = 'pre', value = PAD_INDEX) {
38 // TODO(cais): This perhaps should be refined and moved into tfjs-preproc.
39 return sequences.map(seq => {
40 // Perform truncation.
41 if (seq.length > maxLen) {
42 if (truncating === 'pre') {
43 seq.splice(0, seq.length - maxLen);
44 } else {
45 seq.splice(maxLen, seq.length - maxLen);
46 }
47 }
48
49 // Perform padding.
50 if (seq.length < maxLen) {
51 const pad = [];
52 for (let i = 0; i < maxLen - seq.length; ++i) {
53 pad.push(value);
54 }
55 if (padding === 'pre') {
56 seq = pad.concat(seq);
57 } else {
58 seq = seq.concat(pad);
59 }
60 }
61
62 return seq;
63 });
64}

Callers 3

predictMethod · 0.90
loadFeaturesFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected