Python ValueError:层顺序_2的输入0与层不兼容

Python ValueError:层顺序_2的输入0与层不兼容,python,numpy,tensorflow,keras,deep-learning,Python,Numpy,Tensorflow,Keras,Deep Learning,我有以下代码: import tensorflow as tf import keras from keras.datasets import cifar10 (x_train, y_train), (x_test, y_test) = cifar10.load_data() import numpy as np x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], x_train.shape[2], 3))

我有以下代码:

import tensorflow as tf
import keras
from keras.datasets import cifar10

(x_train, y_train), (x_test, y_test) = cifar10.load_data()

import numpy as np

x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], x_train.shape[2], 3))
print(x_train.shape)
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], x_test.shape[2], 3))
print(x_test.shape)

x_train = x_train.astype('float32')/255.0
x_test  = x_test.astype('float32')/255.0

from keras.utils import to_categorical
y_train = to_categorical(y_train, num_classes = 10)
y_test = to_categorical(y_test, num_classes = 10)

我面临着以下错误:

ValueError:layer sequential_2的输入0与层不兼容:输入形状的轴-1应具有值3072,但接收到形状的输入(1000、32、32、3)


我只想将输入_形状保留为3072。如何重塑y_测试以解决此问题?

在将输入数据传递到
密集层之前,您应该
展平

model = Sequential()
#Defining layers of the model
model.add(Flatten(input_shape=(32,32,3)) # 32*32*3 = 3072
model.add(Dense(2056, activation='relu'))
model.add(Dense(10, activation='softmax')) 
这应该可以解决问题。

谢谢:)我尝试在第一个密集层之后使用“展平”。但这确实奏效了。
model = Sequential()
#Defining layers of the model
model.add(Flatten(input_shape=(32,32,3)) # 32*32*3 = 3072
model.add(Dense(2056, activation='relu'))
model.add(Dense(10, activation='softmax'))