[英]Building a simple Neural Network: ValueError: Input 0 of layer sequential is incompatible with the layer
this simple neural network is giving me a headache;-) Why does it give me the following error:这个简单的神经网络让我头疼;-) 为什么它给我以下错误:
ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. ValueError: 层序的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 Full shape received: (None, 11)收到的完整形状:(无,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)
did you batch the data?你批处理数据了吗?
I think the data should be (batch_size, None, 11)我认为数据应该是 (batch_size, None, 11)
None is the length of the data input 11 is the number of features in each one None 是输入数据的长度 11 是每个特征的数量
Also if you batch you might need to pad your data此外,如果您批量处理,您可能需要填充数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.