Issue
New Python developer here. I looked at other similar posts here but i'm not able to get it right. Would appreciate any help.
print('X_train:', X_train.shape)
print('y_train:', y_train1.shape)
print('X_test:', X_train.shape)
print('y_test:', y_train1.shape)
X_train: (42000, 32, 32) y_train: (42000,) X_test: (42000, 32, 32) y_test: (42000,)
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D
def featuremodel() :
model = Sequential()
model.add(Conv2D(32, kernel_size=4, activation='relu', input_shape=(X_train.shape[0],32,64)))
model.add(MaxPooling2D(pool_size=3))
model.add(Conv2D(64, kernel_size=4, activation='relu'))
model.add(Flatten())
model.add(Dense(len(y_train[0]), activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['acc'])
model.summary()
model.fit(X_train, y_train, epochs = 10, validation_data = (X_test,y_test))
return model
ValueError: Input 0 of layer sequential_7 is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: (None, 1024)
Solution
The input shape you have specified should be changed. Your input has 42000 samples and each one has (32,32)
shape. You should not pass number of samples (42000)
to input layer, and you should add a channel dimension. So the input shape should be (32,32,1)
.
The modified code should be like this:
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D
# test data
X_train = tf.random.uniform((42000,32,32))
y_train1 = tf.random.uniform((42000,))
X_train = tf.expand_dims(X_train, axis=3) #add channel axis (42000,32,32) => (42000,32,32,1)
model = Sequential()
model.add(Conv2D(32, kernel_size=4, activation='relu', input_shape=(32,32,1))) #change input shape
model.add(MaxPooling2D(pool_size=3))
model.add(Conv2D(64, kernel_size=4, activation='relu'))
model.add(Flatten())
#last layer should have output like your y data. in this case it should be 1, since you have 1 y for each sample
model.add(Dense(1, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['acc'])
model.summary()
model.fit(X_train, y_train1, epochs = 10) #, validation_data = (X_test,y_test))
Answered By - Kaveh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.