繁体   English   中英

python 中的神经网络:警告:tensorflow:Model 是用形状(无,7)构建的

[英]Neural Network in python: WARNING:tensorflow:Model was constructed with shape (None, 7) for input

我有来自 xlsx 的 60 行和 9 列的数据(x 为 8 列,y 为 1 列)。 我将其分为 80% 的数据训练和 20% 的数据测试。 所以我有 12 个用于测试,48 个用于培训。 所以我有 x 测试,y 测试,x 训练和 y 训练。 我在 python 中使用神经网络,我有这个代码

import tensorflow as tf
from tensorflow.keras.optimizers import Adam
model = tf.keras.models.Sequential()
adam = Adam(learning_rate=0.01)
model.add(tf.keras.layers.Dense(units=8, activation='relu')) #input ada sembilan belas
model.add(tf.keras.layers.Dense(units=4, activation='relu')) #hidden layer 19 node
model.add(tf.keras.layers.Dense(units=1, activation='linear')) #output layer tidak menggunakan aktivasi
model.compile(loss='mae', optimizer=adam)
model.fit(x_train_CWA, y_train_CWA, epochs=1000)

然后我用这段代码预测 x 测试

y_pred_CWA=model.predict(x_test_CWA)

之后我可以预测那个数据,我想用这个代码输入一个新数据

A = float(input("A : "))
B = float(input("B: "))
C = float(input("C: "))
D = float(input ("D: "))
E = float(input ("E : "))
F = float(input ("F: "))
G = float(input ("G: "))
H = float(input ("H"))

然后我输入新数据,我想预测新数据,所以我用这段代码排列输入数据

Prediction_CWA = np.array([A, B, C, D, E, F, G, H])

然后我用这段代码预测

CWA_Prediction = model.predict(Prediction_CWA)

但我发现了这样的错误

ValueError: Exception encountered when calling layer "sequential_11" (type Sequential).

Input 0 of layer "dense_25" is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (None,)

Call arguments received:
  • inputs=tf.Tensor(shape=(None,), dtype=float32)
  • training=False
  • mask=None

当使用 model.predict 进行 SINGLE 预测时,您需要扩展数组的维度以提供批量维度,因此请尝试

Prediction_CWA = np.expand_dims(Prediction_CWA, axis=0)

然后做 model.predict

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM