递归神经网络

时间:2020-03-11 07:17:33

标签: pandas numpy tensorflow md5 hashlib

请让我们知道以下代码中的任何错误,但没有得到期望的结果-

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)

将值数据帧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)

使用sklearn的train_test_split函数将数据X,y拆分为参数test_size = 0.33和random_state = RANDOM_SEED。**

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个节点。

    模型= Sequential() model.add(LSTM(200,activation ='relu',input_shape = {n_steps,1))) model.add(密集(100,激活='relu')) model.add(密集(50,激活='relu')) model.add(Dense(1))

在合并模型时,传递以下参数- -optimizer as Adam -损失为毫秒 -metrics作为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)

airline-passengers.zip

RNN_Question.zip

毕竟我们正在运行-

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") 

这里我们的输出应该匹配,但不匹配。

1 个答案:

答案 0 :(得分:1)

尝试一下:

    @field:Default
    @field:Inject
    protected open lateinit var bm2: BeanManager

它可以工作,但会在输出中添加额外的行,但没关系