簡體   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