【问题标题】:Building a simple Neural Network: ValueError: Input 0 of layer sequential is incompatible with the layer构建简单的神经网络:ValueError: Input 0 of layer sequence is in compatible with the layer
【发布时间】:2021-06-29 18:20:18
【问题描述】:

这个简单的神经网络让我头疼 ;-) 为什么它给我以下错误:

ValueError:层顺序的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。收到的完整形状:(无,11)

X2=df[['idx', 'pm25', 'no2','o3','pm10','co','pm257davg','no27davg','o37davg','co7davg','pm107davg']]

y= df['newhospi']

# Hold-out
X_train, X_test, y_train, y_test = train_test_split(X1, y, test_size=0.33,random_state = 84)
X_train2, X_test2, y_train2, y_test2 = train_test_split(X2, y, test_size=0.33,random_state = 84)
print("Neural Network")
X_trainNN = np.array(X_train2)
X_trainNN = tf.reshape(X_trainNN, (22168,11))
y_trainNN = np.array(y_train2)
print(X_trainNN.shape)
print(X_trainNN)
print(y_trainNN)
NNmodel = Sequential()
NNmodel.add(layers.LSTM(units=11, activation='tanh', input_shape=(22168, 11)))
NNmodel.add(layers.Dense(1, activation="linear"))

# The compilation
NNmodel.compile(loss='mse', 
              optimizer='adam')

# The fit
NNmodel.fit(X_trainNN, y_trainNN,
         batch_size=16,
         epochs=10, verbose=1)


Neural Network
(22168, 11)
tf.Tensor(
[[  0.28908218   6.67968332   1.54108468 ...  66.30937824 138.94606806
    8.39463459]
 [  0.24173847  11.9746875    9.06678317 ...  52.58769686 208.32226453
   24.14914522]
 [  0.3659374    3.00680707   4.84386803 ...  44.65392901 131.1339603
    8.20872621]
 ...
 [  0.58642916   5.47423178   3.4945117  ...  78.65309818 135.69930972
   14.86935291]
 [  0.57049799   7.36216387  13.28439435 ...  25.219673   185.91964884
   16.81450579]
 [  0.60567525  17.38063329  17.44027664 ...  35.11048528 211.74802456
   14.11718522]], shape=(22168, 11), dtype=float64)
[ 0  3  3 ...  0 12 39]
2021-04-03 02:31:36.507250: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)
2021-04-03 02:31:36.507838: I tensorflow/core/platform/profile_utils/cpu_utils.cc:112] CPU Frequency: 1992005000 Hz
Epoch 1/10

    ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 11)

【问题讨论】:

  • 您不应该在输入形状中包含样本维度,它应该只是 input_shape=(11,)

标签: python python-3.x tensorflow keras neural-network


【解决方案1】:

你批量处理数据了吗?

我认为数据应该是 (batch_size, None, 11)
None 是数据输入的长度 11 是每个特征的数量

另外,如果您进行批处理,您可能需要填充数据

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    • 2022-11-06
    • 2021-05-05
    • 2022-09-24
    • 2021-08-01
    • 2022-01-03
    相关资源
    最近更新 更多