简体   繁体   English

ValueError:顺序层的输入 0 与层不兼容(神经网络)

[英]ValueError: Input 0 of layer sequential is incompatible with the layer (Neural Networks)

Full error:完整错误:

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

The Issue问题

I have been struggling a lot with setting up a neural.network as it constantly complains about the shape received.我一直在努力设置一个 neural.network,因为它不断抱怨接收到的形状。 The x_trian and y_train both have a shape of (20,) but when I input that as the input_shape it says it expected the input shape to have value 20 but instead recieved (None,1). x_trian 和 y_train 的形状均为 (20,),但是当我将其输入为 input_shape 时,它表示它希望输入形状的值为 20,但收到的却是 (None,1)。

I do not undersatnd where the (None,1) is coming from because when I print the shape of both the x_train and y_train it gives me (20,).我不理解 (None,1) 的来源,因为当我打印 x_train 和 y_train 的形状时,它给了我 (20,)。 They are both numpy arrays.它们都是 numpy arrays。

The Code代码

# (the training_data and testing_data are both just numpy arrays with 0 being the data and 1 being the label)
x_train = training_data[:, 0]  # training feature
y_train = training_data[:, 1]  # training label
x_test = testing_data[:, 0]  # testing feature
y_test = testing_data[:, 1]  # testing label

# Create neural network.
from keras.models import Sequential
from keras.layers import Dense

model = Sequential()
model.add(Dense(16, input_dim=20, activation='relu', input_shape=(20,)))
model.add(Dense(12, activation='relu'))
model.add(Dense(12, activation='relu'))
model.add(Dense(2, activation='softmax'))

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

What I tried我试过的

I then changed the input_shape to (None,1) but then it said:然后我将 input_shape 更改为 (None,1) 但随后它说:

ValueError: Shapes (None, 1) and (None, 1, 2) are incompatible

So, I then changed it to (None, 1, 2) and then it said:所以,我然后将其更改为 (None, 1, 2) 然后它说:

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

Which then sends back me to the original error.然后将我送回原来的错误。

I then found that (20,) is only a dimension of 1 so I changed the input_dim to 1 and got:然后我发现 (20,) 只是 1 的维度,所以我将 input_dim 更改为 1 并得到:

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

Conclusion结论

I am almost certain it has to do with the input_dim, input_shape or the Dense unit (which is 16 on the first model.add (the one the errors complain about) but I am very unsure how to change the values to fit my data.我几乎可以肯定它与 input_dim、input_shape 或 Dense 单元(在第一个 model.add(错误抱怨的那个)上是 16)有关,但我非常不确定如何更改值以适合我的数据。

I know the shape is (20,) and I know the dimension is 1, so it might just have to do with the Dense unit's value (which is 16 on the first model.add which is what the compiler complains about).我知道形状是 (20,) 并且我知道维度是 1,所以它可能只与 Dense 单位的值有关(第一个 model.add 是 16,这是编译器抱怨的)。 I read up about what the units measure it but still strugle to understand it.我阅读了有关单位测量的内容,但仍在努力理解它。

In x_train , since you have 20 data points and each data point is a single number, the correct shape would be (1,) .x_train中,由于您有 20 个数据点并且每个数据点都是一个数字,因此正确的形状应该是(1,) Keras expects you to input the shape of each datapoint, not the length of the whole dataset itself. Keras 希望您输入每个数据点的形状,而不是整个数据集本身的长度。 For example, if my data looked like例如,如果我的数据看起来像

# shape of x_train is (2,3)
# shape of each data point is (3,)    
x_train = np.array([[1,2,1],
                    [1,2,3]])

then my input shape would be (3,) .那么我的输入形状将是(3,)

In your case, you could change the model to look like this:在您的情况下,您可以将 model 更改为如下所示:

from keras.layers import Input

model = Sequential()
model.add(Input(shape=(1,)))
model.add(Dense(16, activation='relu'))
model.add(Dense(12, activation='relu'))
model.add(Dense(12, activation='relu'))
model.add(Dense(2, activation='softmax'))

Also, since you're using loss='categorical_crossentropy' , you would have to use one-hot encoding for your y_train , which has to be in the shape (20,2) .此外,由于您使用的是loss='categorical_crossentropy' ,因此您必须对y_train使用单热编码,其形状必须为(20,2) You can refer here how to convert to one hot encoding: Convert array of indices to 1-hot encoded numpy array您可以在这里参考如何转换为一种热编码: Convert array of indices to 1-hot encoded numpy array

暂无
暂无

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

相关问题 构建简单的神经网络: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: 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: 层序列 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: 层序号_2 的输入 0 与层不兼容 - ValueError: Input 0 of layer sequential_2 is incompatible with the layer 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 无法解决 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 ValueError: Input 0 of layer "sequential_8" is in compatible with the layer - 深度学习 model - ValueError: Input 0 of layer "sequential_8" is incompatible with the layer - deep learning model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM