Home > Software engineering >  How to reshape machine learning data to match input shape?
How to reshape machine learning data to match input shape?

Time:02-17

So I have some machine learning data split into testing and training data. The data is imported from a csv file and split into training and testing data using a numpy array. I manage to split the data fine but when I try to use this data in the model I get an error of:

ValueError: Input 0 of layer "mobilenetv2_1.00_3998" is incompatible with the layer: expected shape=(None, 3998, 140, 1), found shape=(None, 140, 1)

I have tried to reshape the data to match the input shape of the model. This still doesn't work and not really sure how to go about doing this. The data needs to be reshaped but with the correct values.

training dataset consists of:

[[ 0.00770334 -1.4224063  -2.4392433  ...  2.1296244   1.7076529
   0.2145994 ]
 [-0.9572602  -2.1521447  -2.7491045  ... -3.784852   -2.7787943
  -1.727039  ]

testing dataset consists of: [1. 0. 0. ... 1. 0. 0.]

shape of data:

x_train: (3998, 140)
x_test: (1000, 140)
y_train: (3998,)
y_test: (1000,)

The size of the each testing and training set: x_train: 559720 x_test: 140000 y_train: 3998 y_test: 1000

here is my code:

model = tf.keras.applications.MobileNetV2((3998, 140, 1), classes=10, weights = None)
model.compile("adam", "sparse_categorical_crossentropy", metrics=["accuracy"])
x_train, x_test, y_train, y_test = model_selection.train_test_split(x, y, test_size=0.2, random_state=123)

x_train = x_train.reshape(3998, 140, 1) 
x_test = x_test.reshape(1000, 140, 1)

CodePudding user response:

tf.keras.applications.MobileNetV2 is for images only, meaning a shape of (None, Height, Width, 3), where None is the batch size and 3 is the number of channels. But your training data seems to have a shape of (None, 140) which does not match the required input shape. So, try to use a different model which matches your data shape, and your error will be eliminated

CodePudding user response:

I am sorry can't comment in the question

what is the shape of your input excluding batch is it (3998, 140, 1) or (140, 1)

if it is (140, 1)

i think this part should be

tf.keras.applications.MobileNetV2((140, 1), classes=10, weights = None)

but if am correct mobile net input should have 3 dimension like (240, 240, 3)

link

but the 1 data shape is (3998, 140, 1) then you should add batch dimension to it before passing to the model

x_train = x_train.reshape(1, 3998, 140, 1)
  • Related