我有以下 Tensorflow python 脚本:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
import numpy as np
x = [20, 23, 25, 28, 30, 37, 40, 43, 46]
y = [45, 51, 55, 61, 65, 79, 85, 91, 97]
study_model = Sequential([Dense(units=1, input_shape=[1])])
study_model.compile(optimizer='adam', loss='mean_squared_error')
x = np.array(x, dtype=int)
y = np.array(y, dtype=int)
history = study_model.fit(x, y, epochs=25)
n = 38
result = study_model.predict([n])[0][0]
rounded_number = round(result, 2)
print(f"If I study for {n} hours, I will get { rounded_number} marks as my grade.")
当我运行模型时,出现以下错误:
raise ValueError(f"Unrecognized data type: x={x} (of type {type(x)})")
ValueError: Unrecognized data type: x=[38] (of type <class 'list'>)
我已将数组转换为 numpy 数组并验证顺序模型是否正确
我需要使用 numpy 数组传入预测值:
result = study_model.predict(x=np.array([n]))[0][0]