1
votes

I am trying i basic Tensorflow JS model for text classification But I am getting this shape error

(node:11400) UnhandledPromiseRejectionWarning: Error: Length of values '33693' does not match the size inferred by the shape '10000'. 

Here is my code

var X_Train = // tokenize sentences
var Y_Train = // encoded label
const xs = tf.tensor2d(X_Train);
const ys = Y_Train;
console.log('Shape of X '); 
console.log(X_Train[0]);
console.log(xs);
console.log('Shape of Y '); 
console.log(Y_Train[0]);
console.log(ys);

const model = tf.sequential();

model.add(tf.layers.embedding({
inputDim: 4,
outputDim: 10,
inputLength: 10,
trainable: true
}));
model.add(tf.layers.flatten());
model.add(tf.layers.dense({units: 1, activation: 'sigmoid'}));
 model.compile({loss: 'meanSquaredError',optimizer: 'adam'});
model.fit(xs,tf.stack(ys))

Here is my Output

Shape of X [ 1996, 7357, 1997, 1037, 2931, 6398, 1999, 1996, 13678, 1012 ] Tensor { kept: false, isDisposedInternal: false, shape: [ 1000, 10 ], dtype: 'float32', size: 10000, strides: [ 10 ], dataId: { id: 2 }, id: 2, rankType: '2' } Shape of Y 0 [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 3, 91, 92, 93, 94, 95, 96, 97, 98, ... 900 more items ] (node:18640) UnhandledPromiseRejectionWarning: Error: Length of values '33693' does not match the size inferred by the shape '10000'. at assert (C:\Users\project\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:337:15) at new TensorBuffer (C:\Users\project\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:3591:13) at Object.buffer (C:\Users\project\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:7400:12) at MathBackendCPU.bufferSync (C:\Users\project\node_modules@tensorflow\tfjs-backend-cpu\dist\tf-backend-cpu.node.js:239:25) at Object.gatherV2 [as kernelFunc] (C:\Users\project\node_modules@tensorflow\tfjs-backend-cpu\dist\tf-backend-cpu.node.js:6592:24) at kernelFunc (C:\Users\project\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:4666:32) at C:\Users\project\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:4727:27 at Engine.scopedRun (C:\Users\project\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:4539:23) at Engine.runKernelFunc (C:\Users\project\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:4723:14) at Engine.runKernel (C:\Users\project\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:4595:21) at gather_ (C:\Users\project\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:14426:19) at Object.gather__op [as gather] (C:\Users\project\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:5519:29) at C:\Users\project\node_modules@tensorflow\tfjs-layers\dist\tf-layers.node.js:18091:20 at C:\Users\project\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:4529:22 at Engine.scopedRun (C:\Users\project\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:4539:23) at Engine.tidy (C:\Users\project\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:4528:21) (Use node --trace-warnings ... to show where the warning was created) (node:18640) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:18640) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I tried to use different model architecture but still ended up with same error. when I change the loss function I still got some different error. Any idea what am I missing or what model architecture I need to use for that kind of data

1
The shape of xs and tf.stack(ys) does not match what the model is expectingedkeveked

1 Answers

0
votes

The shape of xs and tf.stack(ys) does not match what the model is expecting. Here is an example to help you see the shape expected

const model = tf.sequential();
model.add(tf.layers.embedding({
inputDim: 4,
outputDim: 10,
inputLength: 10,
trainable: true
}));
model.add(tf.layers.flatten());
model.add(tf.layers.dense({units: 1, activation: 'sigmoid'}));

model.compile({loss: 'meanSquaredError',optimizer: 'adam'});
await model.fit(tf.ones([1, 10]),tf.tensor([1], [1, 1]))