Warlax56
Warlax56

Reputation: 1202

Shape error when predicting with a trained model in tensorflow.keras

I'm creating a 1D CNN using tensorflow.keras, following this tutorial, with some of the concepts from this tutorial. So far modeling and training seem to be working, but I can't seem to generate a prediction. Here's an example of what I'm dealing with:

Data

import numpy as np
from keras.utils import to_categorical

NUM_SAMPLES = 1000
NUM_TRACES = 6
SAMPLE_LENGTH = 512
trainX = np.random.rand(NUM_SAMPLES,SAMPLE_LENGTH,NUM_TRACES)
trainy = to_categorical(np.random.randint(2, size=NUM_SAMPLES))

In this example, I'm creating a dataset which represents what I'm working with. I have 1000 windows of time series data, each of which has 6 distinct traces (accelerometer x,y,z and gyro x,y,z). The lengths of these windows are 512 data points long.

Training

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv1D, MaxPooling1D

verbose, epochs, batch_size = 1, 3, 32
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape = trainX[0].shape))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(trainX, trainy, epochs=epochs, batch_size=batch_size, verbose=verbose)

Creating a simple CNN, this trains perfectly with the following output:

Train on 1000 samples
Epoch 1/3
1000/1000 [==============================] - 1s 1ms/sample - loss: 0.8802 - accuracy: 0.4930
Epoch 2/3
1000/1000 [==============================] - 1s 724us/sample - loss: 0.6726 - accuracy: 0.5920
Epoch 3/3
1000/1000 [==============================] - 1s 740us/sample - loss: 0.6291 - accuracy: 0.6760

Prediction (where the error comes up)

for testing purposes, I'm predicting a portion of training data. Running model.predict(trainX[0]) results in the following error:

ValueError: Error when checking input: expected conv1d_4_input to have 3 dimensions, but got array with shape (512, 6)

this seems peculiar, as I would expect compatibility of the training dataset; after all, trainX[0] is what defined the input_shape in the first place.

Upvotes: 0

Views: 537

Answers (2)

Mal Shridhar
Mal Shridhar

Reputation: 1

On my laptop X_new = [[30, 74, 11, 31, 20]], prediction = model.predict(X_new)

I got the correct result. On my desktop, I got an error about the shape being 1 and not 5. So I tried prediction = model.predict(np.array(X_new)) and it worked. I am not sure why it worked on one machine and not on the other. Both have the same version of TensorFlow.

Upvotes: 0

feifei Xiao
feifei Xiao

Reputation: 46

please runing the code: model.predict([trainX[0]]), and the model outputs the predicted results

Upvotes: 1

Related Questions