【问题标题】:ValueError : Input 0 of layer sequential_13 is incompatible with the layer:ValueError : 层序贯_13 的输入 0 与层不兼容:
【发布时间】:2021-08-31 05:07:34
【问题描述】:

我在使用 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)

【问题讨论】:

  • 哪一行会引发这个错误?
  • @Kaveh 我在正文中添加了一个错误。如果您能帮助我,我将不胜感激。

标签: python tensorflow keras artificial-intelligence perceptron


【解决方案1】:

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

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

  • 您应该有一个输入层来定义您的输入张量,它的形状是 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

【讨论】:

  • 非常感谢。我不知道解决这个问题需要多长时间。你是编码天才吗?太感谢了。我还有一个问题,如何将 20% 的训练数据设置为验证集,并仅使用测试数据进行性能评估?
  • 我已将拆分数据集的代码添加到我的答案中。
  • 非常感谢。你的回答很有帮助。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-04
  • 2021-05-31
  • 2020-11-28
  • 2021-09-13
  • 1970-01-01
相关资源
最近更新 更多