【发布时间】:2021-10-07 03:01:08
【问题描述】:
完全错误:
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)
问题
我一直在努力建立一个神经网络,因为它不断抱怨接收到的形状。 x_trian 和 y_train 都具有 (20,) 的形状,但是当我将其输入为 input_shape 时,它说它预期输入形状的值是 20,但收到的是 (None,1)。
我并不了解 (None,1) 的来源,因为当我打印 x_train 和 y_train 的形状时,它会给出 (20,)。它们都是 numpy 数组。
守则
# (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'])
我尝试了什么
然后我将 input_shape 更改为 (None,1) 但随后显示:
ValueError: Shapes (None, 1) and (None, 1, 2) are incompatible
所以,我把它改成 (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)
然后将我送回原来的错误。
然后我发现(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)
结论
我几乎可以肯定它与 input_dim、input_shape 或 Dense 单位(第一个模型上的 16.add(错误抱怨的那个))有关,但我非常不确定如何更改这些值以适应我的数据。
我知道形状是 (20,) 并且我知道维度是 1,所以它可能只与 Dense 单位的值有关(在第一个 model.add 上是 16,这是编译器抱怨的) .我阅读了有关单位测量它的内容,但仍然难以理解它。
【问题讨论】:
标签: python tensorflow neural-network data-mining