'tensorflow code error ValueError: Exception encountered when calling layer "sequential" (type Sequential)
'Learn from local files. There are 4760 training data and 240 test data. I'm getting these errors, what's the problem?' '''
train_dir=''
train_csv = pd.read_csv(train_dir+'train.csv')
train_images=[]
train_labels=[]
for file in train_csv['data']:
image=np.array(Image.open(train_dir+'train/'+file))
train_images.append(image)
for label in train_csv['label']:
train_labels.append(label)
train_images=np.array(train_images)
train_labels=np.array(train_labels)
test_dir=''
test_csv = pd.read_csv(test_dir+'test.csv')
test_images=[]
test_labels=[]
for file in test_csv['data']:
image=np.array(Image.open(test_dir+'test/'+file))
test_images.append(image)
for label in test_csv['label']:
test_labels.append(label)
test_images=np.array(test_images)
test_labels=np.array(test_labels)
import numpy as np
import tensorflow as tf
from tensorflow.keras.datasets import fashion_mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD,Adam,Adagrad,RMSprop
print("train_images",train_images.shape)
print("train_labels",train_labels.shape)
print("test_images",test_images.shape)
print("test_labels",test_labels.shape)
train_images=train_images.reshape(4760,4096,3)
test_images=test_images.reshape(240,4096,3)
x_train=train_images.astype(np.float32)/255.0
x_test=test_images.astype(np.float32)/255.0
y_train=tf.keras.utils.to_categorical(train_labels,5)
y_test=tf.keras.utils.to_categorical(test_labels,5)
print("x_train",x_train.shape)
print("y_train",y_train.shape)
print("x_test",x_test.shape)
print("y_test",y_test.shape)
n_input=4096
n_hidden1=5000
n_hidden2=2500
n_hidden3=2500
n_hidden4=2500
n_output=5
batch_siz=256
n_epoch=5
def build_model():
model=Sequential()
model.add(Dense(units=n_hidden1,activation='relu',input_shape=(n_input,)))
model.add(Dense(units=n_hidden2,activation='relu'))
model.add(Dense(units=n_hidden3,activation='relu'))
model.add(Dense(units=n_hidden4,activation='relu'))
model.add(Dense(units=n_output,activation='softmax'))
return model
dmlp_adam=build_model()
dmlp_adam.compile(loss='categorical_crossentropy',optimizer=Adam(),metrics=['accuracy'])
hist_adam=dmlp_adam.fit(x_train,y_train,batch_size=batch_siz,epochs=n_epoch,validation_data=(x_test,y_test),verbose=2)
''' 'error:WARNING:tensorflow:Model was constructed with shape (None, 4096) for input KerasTensor(type_spec=TensorSpec(shape=(None, 4096), dtype=tf.float32, name='dense_input'), name='dense_input', description="created by layer 'dense_input'"), but it was called on an input with incompatible shape (None, 4096, 3).
ValueError: Exception encountered when calling layer "sequential" (type Sequential).
Input 0 of layer "dense" is incompatible with the layer: expected axis -1 of input shape to have value 4096, but received input with shape (None, 4096, 3)
Call arguments received by layer "sequential" (type Sequential):
• inputs=tf.Tensor(shape=(None, 4096, 3), dtype=float32)
• training=True
• mask=None
'
Solution 1:[1]
Choosing too many neurons may lead to overfitting and increase the training time of the network. It is preferred that the number of hidden neurons(units) should be somewhere between the size of the input layer and the size of the output layer. Please find the working code below.
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Flatten
import numpy as np
x_train=tf.random.uniform((4760,4096,3))
x_test=tf.random.uniform((240,4096,3))
# It generates labels randomly within the range of 0 to 5
import random
def Rand(start, end, num):
res = []
for j in range(num):
res.append(random.randint(start, end))
return res
# Train and Test labels
y_test=np.array(Rand(0,4,240))
y_train=np.array(Rand(0,4,4760))
y_train=tf.keras.utils.to_categorical(y_train)
y_test=tf.keras.utils.to_categorical(y_test)
print("x_train",x_train.shape)
print("y_train",y_train.shape)
print("x_test",x_test.shape)
print("y_test",y_test.shape)
# Changing the input shape from 4096 to (4096,3) resolves the issue as the input is of shape (4096,3).
n_input=(4096,3)
n_hidden1= 128 #5000
n_hidden2= 64 #2500
n_hidden3= 32 #2500
n_hidden4= 16 #2500
n_output=5
batch_siz=256
n_epoch=5
def build_model():
model=Sequential()
model.add(Dense(units=n_hidden1,activation='relu',input_shape=(n_input)))
model.add(Dense(units=n_hidden2,activation='relu'))
model.add(Dense(units=n_hidden3,activation='relu'))
model.add(Dense(units=n_hidden4,activation='relu'))
# Added flatten layer to overcome ValueError: Shapes (None, 5) and (None, 4096, 5) are incompatible
model.add(Flatten())
model.add(Dense(units=n_output,activation='softmax'))
return model
model=build_model()
model.summary()
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
model.fit(x_train,y_train,batch_size=batch_siz,epochs=n_epoch,validation_data=(x_test,y_test),verbose=2)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tfer3 |
