Issue
I'm relatively new to programming neural networks and have been following a few tutorials about it before deciding to try and learn to program neural networks on my own using what i'd learnt. I've been trying to program a basic neural network just so i can learn how it works, but it keeps giving me an error. I would really appreciate it if someone could help.
Here's my code:
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
import pickle
pickle_in = open("X.pickle","rb")
X = pickle.load(pickle_in)
pickle_in = open("y.pickle","rb")
y = pickle.load(pickle_in)
X = X/255.0
model = Sequential()
model.add(Conv2D(8,(5, 5),padding="same",activation='relu',input_shape=(784,)))
model.add(Conv2D(32, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(Flatten())
model.add(Dense(1))
model.add(Activation('linear'))
model.add(Dense(y.shape[1]))
model.add(Activation('linear'))
model.summary()
model.compile(loss='mean_squared_error',optimizer='adam',metrics=['mae','mse', 'accuracy'])
model.fit(X, y, epochs=20, batch_size=10,verbose=2)
Here's the error message I'm getting:
str(x.shape.as_list()))
ValueError: Input 0 of layer conv2d is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [None, 784]
Thanks in advance!
Solution
The error is cause Conv2D
expects input shape to be of 4 dimensions i.e. [batch_size, height, width, channels]. You can do one thing i.e. reshape your input to the model.
X = X.reshape(-1, 28, 28, 1) # incase of single channel (grayscale)
# OR
X = X.reshape(-1, 28, 28, 3) # incase of RGB
# and then change the input shape of your `Conv2D` layer accordingly to
model = Sequential()
model.add(Conv2D(8,(5, 5),padding="same",activation='relu',input_shape=(28,28,1)))
# OR
model = Sequential()
model.add(Conv2D(8,(5, 5),padding="same",activation='relu',input_shape=(28,28,3)))
Answered By - Abhishek Prajapat
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.