【发布时间】:2020-06-23 03:17:23
【问题描述】:
请让我们知道以下代码中的任何错误,没有得到期望的结果 -
from numpy import sqrt
from numpy import asarray
from pandas import read_csv
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
import tensorflow as tf
from sklearn import metrics
from sklearn.model_selection import train_test_split
将值 40 分配给变量 RANDOM_SEED,这将是种子值。使用存储在变量 RANDOM_SEED 中的值设置随机种子值。
RANDOM_SEED = 40
tf.random.set_seed(RANDOM_SEED)
将单变量序列拆分为样本
def split_sequence(sequence, n_steps):
X, y = list(), list()
for i in range(len(sequence)):
# find the end of this pattern
end_ix = i + n_steps
# check if we are beyond the sequence
if end_ix > len(sequence)-1:
break
# gather input and output parts of the pattern
seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
X.append(seq_x)
y.append(seq_y)
return asarray(X), asarray(y)
读取数据集airline-passengers.csv并将参数index_col设为0并将其保存在变量df中。
df = read_csv("airline-passengers.csv", index_col=0)
将 values 数据帧 df 的数据类型转换为 float32 并保存在变量值中。 将值 5 分配给变量 n_steps,即窗口大小。 使用函数 split_sequence 拆分样本并传递参数值和 n_steps 并将其保存在变量 X 和 y 中
values = df.values.astype('float32')
n_steps = 5
X, y = split_sequence(values, n_steps)
使用参数test_size=0.33和random_state=RANDOM_SEED的sklearn的train_test_split函数拆分数据X,y。**
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=RANDOM_SEED)
构造一个使用稠密类定义的全连接网络结构
-
创建一个顺序模型添加一个具有 200 个节点的 LSTM 层 激活函数为 relu,输入形状为 (n_steps,1)。
-
第一个隐藏层有100个节点,使用relu激活函数。
-
第二个隐藏层有50个节点,使用relu激活 功能。
-
输出层有1个节点。
模型 = 顺序() model.add(LSTM(200, activation='relu', input_shape=(n_steps,1))) model.add(密集(100,激活='relu')) model.add(密集(50,激活='relu')) model.add(密集(1))
编译模型时传递以下参数 - -作为亚当的优化器 -损失为mse -metrics as mae
model.compile(optimizer='Adam', loss='mse', metrics=['mae'])
使用 X_train、y_train、epochs=350、batch_size=32、verbose=0 拟合模型。
model.fit(X_train, y_train, epochs=350, batch_size=32, verbose=0)
在 X_test 上对测试数据(即)执行预测并将预测保存在变量 y_pred 中。
row = ([X_test])
y_pred = model.predict(row)
使用 sklearn 指标中的 mean_squared_error 函数计算变量 y_test 和 y_pred 的均方误差,并将其保存在变量 MSE 中。
通过对上述结果进行平方根计算变量 y_test 和 y_pred 的均方根误差,并将其保存在变量 RMSE 中。
使用 sklearn 指标中的 mean_absolute_error 函数计算变量 y_test 和 y_pred 的平均绝对误差,并将其保存在变量 MAE 中。
MSE = metrics.mean_squared_error(y_test,y_pred)
RMSE = sqrt(metrics.mean_squared_error(y_test,y_pred))
MAE = metrics.mean_absolute_error(y_test,y_pred)
print('MSE: %.3f, RMSE: %.3f, MAE: %.3f' % (MSE, RMSE,MAE))
MSE: 665.522, RMSE: 25.798, MAE: 17.127 ...我们得到的结果是错误的。
with open("MSE.txt", "w") as text_file:
MSE=str(MSE)
text_file.write(MSE)
with open("RMSE.txt", "w") as text_file:
RMSE=str(RMSE)
text_file.write(RMSE)
with open("MAE.txt", "w") as text_file:
MAE=str(MAE)
text_file.write(MAE)
# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
毕竟我们正在运行 -
from hashlib import md5
f = open("MSE.txt", "r")
s=f.read()
s=float(s)
s=round(s,3)
f1=open("RMSE.txt","r")
s1=f1.read()
s1=float(s1)
s1=round(s1,3)
f2=open("MAE.txt","r")
s2=f2.read()
s2=float(s2)
s2=round(s2,3)
if (md5(str(s).encode()).hexdigest() == '51ad543f7ac467cb8b518f1a04cc06af') and (md5(str(s1).encode()).hexdigest() == '6ad48a76bec847ede2ad2c328978bcfa') and (md5(str(s2).encode()).hexdigest() == '64bd1e146726e9f8622756173ab27831'):
print("Your MSE,RMSE and MAE Scores matched the expected output")
else :
print("Your MSE,RMSE and MAE Scores does not match the expected output")
这里我们的输出应该是匹配的,但不匹配。
【问题讨论】:
标签: pandas numpy tensorflow md5 hashlib