user16021918
user16021918

Reputation:

ValueError : Input 0 of layer sequential_13 is incompatible with the layer:

I faced the following problems in conducting artificial intelligence practice using Python. How do I solve this problem? I want to recognize Cifar-10 data using MLP. However, an error occurs when you write as follows: What should I do to solve this problem? Please help me.

I added an error to the body. I'd appreciate it if you could help me.

import numpy as np
import tensorflow as tf
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D,MaxPooling2D,Flatten,Dense,Dropout
from tensorflow.keras.optimizers import Adam

# CIFAR-10 데이터셋을 읽고 신경망에 입력할 형태로 변환
(x_train,y_train),(x_test,y_test)=cifar10.load_data()
x_train=x_train.astype(np.float32)/255.0
x_test=x_test.astype(np.float32)/255.0
y_train=tf.keras.utils.to_categorical(y_train,10)
y_test=tf.keras.utils.to_categorical(y_test,10)

n_input=1024
n_hidden=1024
n_output=10

# 신경망 모델 설계
mlp=Sequential()
mlp.add(Dense(units=n_hidden,activation='tanh',input_shape=(n_input,),kernel_initializer='random_uniform',bias_initializer='zeros'))
mlp.add(Dense(units=n_output,activation='tanh',kernel_initializer='random_uniform',bias_initializer='zeros'))

# 신경망 모델 학습
mlp.compile(optimizer=Adam(),metrics=['accuracy'])
hist=mlp.fit(x_train, y_train, batch_size=128, epochs=30, validation_data=(x_test, y_test), verbose=2)

# 신경망 모델 정확률 평가
res=mlp.evaluate(x_test,y_test,verbose=0)
print("정확률은",res[1]*100)

Error:

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    984           except Exception as e:  # pylint:disable=broad-except
    985             if hasattr(e, "ag_error_metadata"):
--> 986               raise e.ag_error_metadata.to_exception(e)
    987             else:
    988               raise

ValueError: in user code:

    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:855 train_function  *
        return step_function(self, iterator)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:845 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1285 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2833 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3608 _call_for_each_replica
        return fn(*args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:838 run_step  **
        outputs = model.train_step(data)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:795 train_step
        y_pred = self(x, training=True)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py:1013 __call__
        input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_spec.py:255 assert_input_compatibility
        ' but received input with shape ' + display_shape(x.shape))

    ValueError: Input 0 of layer sequential_13 is incompatible with the layer: expected axis -1 of input shape to have value 1024 but received input with shape (None, 32, 32, 3)

Upvotes: 1

Views: 1043

Answers (1)

Kaveh
Kaveh

Reputation: 4980

You have several issues in your code. I modified them and pasted whole code. I have added descriptions as comments. However there are more advices for your model to get a good accuracy.

I list more important issues in your code:

  • You should have an input layer to define your input tensor and it's shape which is (32,32,3) for cifar10 dataset.
  • Because your input is an image and is not a 1d array, you should add a flatten layer before feeding it to dense layers.
  • Do not use tanh activation function for the last layer, because you want the output results [0,1], and tanh gives values [-1,1].
  • You had not used loss function in the model.compile() method. I have added for you.
import numpy as np
import tensorflow as tf
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D,MaxPooling2D,Flatten,Dense,Dropout
from tensorflow.keras.optimizers import Adam

# CIFAR-10 데이터셋을 읽고 신경망에 입력할 형태로 변환
(x_train,y_train),(x_test,y_test)=cifar10.load_data()
x_train=x_train.astype(np.float32)/255.0
x_test=x_test.astype(np.float32)/255.0
y_train=tf.keras.utils.to_categorical(y_train,10)
y_test=tf.keras.utils.to_categorical(y_test,10)

n_input=1024
n_hidden=1024
n_output=10

# 신경망 모델 설계
mlp=Sequential()
mlp.add(tf.keras.layers.InputLayer(input_shape=(32,32,3))) #add input layer and specify input shape
mlp.add(tf.keras.layers.Flatten())   #flatten your layer, since your input is a 3d (WHC) data
mlp.add(Dense(units=n_hidden,activation='tanh',kernel_initializer='random_uniform',bias_initializer='zeros')) #remove input_shape, since you don't need it
mlp.add(Dense(units=n_output,activation='softmax',kernel_initializer='random_uniform',bias_initializer='zeros')) #tanh is not a good activation since you want values between 0,1 as output

# 신경망 모델 학습
mlp.compile(optimizer=Adam(),metrics=['accuracy'], loss=tf.keras.losses.categorical_crossentropy) #add loss function
hist=mlp.fit(x_train, y_train, batch_size=128, epochs=30, validation_data=(x_test, y_test), verbose=2)

# 신경망 모델 정확률 평가
res=mlp.evaluate(x_test,y_test,verbose=0)
print("정확률은",res[1]*100)

import matplotlib.pyplot as plt

# 정확률 그래프
plt.plot(hist.history['accuracy'])
plt.plot(hist.history['val_accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train','Validation'], loc='best')
plt.grid()
plt.show()

# 손실 함수 그래프
plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train','Validation'],loc='best')
plt.grid()
plt.show()

UPDATE: For splitting train dataset to train and validation use code like this:

split_percent = 0.2         #exclude 20% for validation
split_index = int(x_train.shape[0]*(1-split_percent))
x_t = x_train[:split_index]  #x_train
y_t = y_train[:split_index]  #y_train
x_v = x_train[split_index:]  #x_val
y_v = y_train[split_index:]  #y_val

Upvotes: 1

Related Questions