ValueError : 层序列 13 的输入 0 与层不兼容:

时间:2021-06-15 10:40:30

标签: python tensorflow keras artificial-intelligence perceptron

我在使用 Python 进行人工智能实践时遇到了以下问题。我该如何解决这个问题? 我想使用 MLP 识别 Cifar-10 数据。但是如下写的时候出现错误:我该怎么做才能解决这个问题? 请帮帮我。

我在正文中添加了一个错误。如果您能帮助我,我将不胜感激。

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)

错误:

/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)

1 个答案:

答案 0 :(得分:1)

您的代码中有几个问题。我修改了它们并粘贴了整个代码。我已经添加了描述作为评论。但是,还有更多建议可以让您的模型获得良好的准确性。

我在您的代码中列出了更重要的问题:

  • 你应该有一个输入层来定义你的输入张量,它的形状是 cifar10 数据集的 (32,32,3)。
  • 因为您的输入是图像而不是一维数组,所以您应该在将其提供给密集层之前添加一个展平层。
  • 最后一层不要使用tanh激活函数,因为你想要输出结果[0,1],tanh给出值[-1,1]。
  • 您没有在 model.compile() 方法中使用损失函数。我已经为你添加了。
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()

更新:用于拆分训练数据集以训练和验证使用如下代码:

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