![](/img/trans.png)
[英]"ValueError: Input 0 of layer "sequential" is incompatible with the layer" In prediction
[英]Building a simple Neural Network: ValueError: Input 0 of layer sequential is incompatible with the layer
這個簡單的神經網絡讓我頭疼;-) 為什么它給我以下錯誤:
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)
你批處理數據了嗎?
我認為數據應該是 (batch_size, None, 11)
None 是輸入數據的長度 11 是每個特征的數量
此外,如果您批量處理,您可能需要填充數據
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.