Tensorflow.js inputShape doesn't match model input

霸气de小男生 提交于 2019-12-31 04:08:09

问题


This seems to be pretty basic but I can't figure it out.

So I have sample/data/inputs that is an array of arrays of 10 ints and the output/label it's just an array of integers.

Let me explain as it might be that my data is not properly structured. Based on the input of 10 integers I tell the model the result is the 1 integer in the label/output.

On top of that I cannot batch the data because they are sequential. Meaning the inputs shift by one to the right so the first nine integers in sample[i+1] are the last 9 of sample[i] plus a new one.

Here is how I have it coded.

let labels = [1,0,0...]

let samples = [[0,1,1,0,1,0,1,1,1,0], ...]

basically an array of arrays of 10.

const model = tf.sequential();
let input = tf.tensor2d(samples);
model.add(tf.layers.dense({ units: 10, batchInputShape: [1, 10], activation: "sigmoid" }));
model.add(tf.layers.dense({ units: 1, activation: "softmax" }));
model.summary();
model.compile({ loss: "meanSquaredError", optimizer: "sgd", metrics: ["accuracy"] });
model.fit(labels, input, { batchSize: 1, shuffle: false, verbose: 1 });

When I try this or any other input combination i get the following

UnhandledPromiseRejectionWarning: Error: Error when checking model input: the Array of Tensors that you are passing to your model is not the size the model expected. Expected to see 1 Tensor(s), but instead got the following list of Tensor(s): 0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0

Thanks in advance for your help.


回答1:


inputShape doesn't match model input

The error indicates that there is a shape mismatch between the input of the model and the data.

How to go about solving this issue ?

  • The features should have the same shape as the batchInputShape or should be one dimension higher than the InputShape. The first dimension of the batchInputShape is generally null to allow for different batchInputShape during training. But by specifying here to be exactly 1 (different of null), the features of the training should exactly have the first dimension to be equal to 1. Had it been null, it could be possible to have features of shape [b, ...InputShape]

  • the labels should of the shape [b, LastLayerUnit]. Again, by specifying a hard coded value of the batch (different of null) the labels first dimension should exactly be of that length.

What is the batch dimension ?

here is an interesting answer to understand it. Simply put given a model. The following allows to train the model:

model.fit(features, label)

features is an array of feature whereas feature is one element from which we want to make a prediction. The batch size is therefore the length of that array. The first layer of a model can either have the parameters inputShape or batchInputShape or both, knowing that the batchInputShape will take precedence over the inputShape. When only inputShape is supplied, batchInputShape = [null, ...InputShape], thus indicating that we can fit the model with a various length of feature elements provided that the labels has the same length of label which makes sense for a label needs to be supplied for each feature.

Therefore

      inputShape = batchInputShape[1:] // python notation 
      inputShape = batchInputShape.slice(1) // js notation

At any rate a feature should have the same shape as inputShape.

const model = tf.sequential();
model.add(tf.layers.dense({ units: 10, batchInputShape: [1, 10], activation: "sigmoid" }));
model.add(tf.layers.dense({ units: 1, activation: "softmax" }));
model.summary();
model.compile({ loss: "meanSquaredError", optimizer: "sgd", metrics: ["accuracy"] });


const arr = Array.from({length: 10}, (_, k) => k+1 )
const features =  tf.tensor(arr, [1, 10])
const labels = tf.tensor([1], [1, 1])

logs = await model.fit(features, labels, { batchSize: 1, shuffle: false, verbose: 1 });
console.log(logs)


来源:https://stackoverflow.com/questions/55737522/tensorflow-js-inputshape-doesnt-match-model-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!