简体   繁体   English

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

[英]ValueError: Input 0 of layer sequential_2 is incompatible with the layer

I have the following code:我有以下代码:

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)

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten

model = Sequential()
#Defining layers of the model
model.add(Dense(2056, activation='relu', input_shape = (3072,)))
model.add(Dense(10, activation='softmax')) 

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()

history = model.fit(x_train, y_train, batch_size=1000, epochs=50)

And I am facing the following error:我面临以下错误:

ValueError: Input 0 of layer sequential_2 is incompatible with the layer: expected axis -1 of input shape to have value 3072 but received input with shape (1000, 32, 32, 3) ValueError:层序 2 的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3072,但接收到形状为 (1000, 32, 32, 3) 的输入

I want to keep the input_shape as 3072 only.我只想将 input_shape 保持为 3072。 How can I reshape my y_test to solve this?如何重塑我的 y_test 来解决这个问题?

You should Flatten your input data before passing them to Dense layer.在将输入数据传递给Dense层之前,您应该先Flatten输入数据。

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

This should fix the problem.这应该可以解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError: 层“sequential_2”的输入 0 与层不兼容:预期形状=(None, 256, 256, 3),发现形状=(None, 1, 256, 256, 3) - ValueError: Input 0 of layer "sequential_2" is incompatible with the layer: expected shape=(None, 256, 256, 3), found shape=(None, 1, 256, 256, 3) ValueError: 层序列 1 的输入 0 与层不兼容 - ValueError: Input 0 of layer sequential_1 is incompatible with the layer TensorFlow ValueError:层顺序的输入0与层不兼容 - TensorFlow ValueError: Input 0 of layer sequential is incompatible with the layer "ValueError: Input 0 of layer "sequential" is in compatible with the layer" 在预测中 - "ValueError: Input 0 of layer "sequential" is incompatible with the layer" In prediction ValueError: 层序号_3 的输入 0 与层不兼容: - ValueError: Input 0 of layer sequential_3 is incompatible with the layer: ValueError:“顺序”层的输入 0 与该层不兼容 - ValueError: Input 0 of layer "sequential" is incompatible with the layer sequential_2 层的输入 0 与该层不兼容:预期 ndim=3,发现 ndim=2。 收到完整形状:(无,1) - Input 0 of layer sequential_2 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 1) 构建简单的神经网络:ValueError: Input 0 of layer sequence is in compatible with the layer - Building a simple Neural Network: ValueError: Input 0 of layer sequential is incompatible with the layer 无法解决 ValueError: 层序贯_1 的输入 0 与层不兼容 - Cannot solve ValueError: Input 0 of layer sequential_1 is incompatible with the layer ValueError: 层序号_40 的输入 0 与层不兼容 - ValueError: Input 0 of layer sequential_40 is incompatible with the layer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM