【问题标题】:ValueError: Input 0 of layer sequential is incompatible with the layer (Neural Networks)ValueError:层顺序的输入 0 与层不兼容(神经网络)
【发布时间】: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


    【解决方案1】:

    x_train 中,由于您有 20 个数据点并且每个数据点都是一个数字,因此正确的形状应该是 (1,)。 Keras 希望您输入每个数据点的形状,而不是整个数据集本身的长度。 例如,如果我的数据看起来像

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

    那么我的输入形状将是(3,)

    在您的情况下,您可以将模型更改为如下所示:

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

    此外,由于您使用的是loss='categorical_crossentropy',因此您必须对y_train 使用单热编码,其形状必须为(20,2)。您可以在这里参考如何转换为一种热编码:Convert array of indices to 1-hot encoded numpy array

    【讨论】:

    • 这太棒了,感谢您为此付出的所有努力!我只是尝试添加 model.add(Input(shape=(1,))) ,但仍然遇到相同的错误。我在该链接上关注了一些答案,但似乎大多数答案都有错误。例如,如果我执行categorical_labels = np.eye(3)[y_train],它就会抱怨该代码并说IndexError: arrays used as indices must be of integer (or boolean) type。另一个答案是categorical_labels = np.eye(y_train.max()+1)[y_train],但这给了我一个错误:TypeError: cannot perform reduce with flexible type
    • 使用categorical_labels = pd.get_dummies(y_train) 可以,但是在尝试拟合模型时会显示nimplementedError: Cast string to float is not supported
    猜你喜欢
    • 2021-07-04
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 2021-09-13
    • 1970-01-01
    • 2021-08-31
    相关资源
    最近更新 更多