网站里的个人中心下拉列表怎么做,装饰工程 技术支持 东莞网站建设,龙岩app开发定制,优化大师网页版#x1f368; 本文为#x1f517;365天深度学习训练营 中的学习记录博客#x1f356; 原作者#xff1a;K同学啊 一、什么是RNN
RNN与传统神经网络最大的区别在于#xff0c;每次都会将前一次的输出结果#xff0c;带到下一隐藏层中一起训练。如下图所示#xff1a;
… 本文为365天深度学习训练营 中的学习记录博客 原作者K同学啊 一、什么是RNN
RNN与传统神经网络最大的区别在于每次都会将前一次的输出结果带到下一隐藏层中一起训练。如下图所示
二、前期工作
1. 设置GPU
import tensorflow as tfgpus tf.config.list_physical_devices(GPU)if gpus:gpu0 gpus[0] #如果有多个GPU仅使用第0个GPUtf.config.experimental.set_memory_growth(gpu0, True) #设置GPU显存用量按需使用tf.config.set_visible_devices([gpu0],GPU)
2. 导入数据
数据介绍 age年龄 sex性别 cp胸痛类型 (4 values) trestbps静息血压 chol血清胆甾醇 (mg/dl fbs空腹血糖 120 mg/dl restecg静息心电图结果 (值 0,1 ,2) thalach达到的最大心率 exang运动诱发的心绞痛 oldpeak相对于静止状态运动引起的ST段压低 slope运动峰值 ST 段的斜率 ca荧光透视着色的主要血管数量 (0-3) thal0 正常1 固定缺陷2 可逆转的缺陷 target0 心脏病发作的几率较小 1 心脏病发作的几率更大 import pandas as pd
import numpy as npdf pd.read_csv(rD:\Personal Data\Learning Data\DL Learning Data\heart.csv)
df输出
3. 检查数据
df.isnull().sum()输出
三、数据预处理
1. 划分数据集
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_splitx df.iloc[:,:-1]
y df.iloc[:,-1]x_train, x_test, y_train, y_test train_test_split(x, y, test_size0.1, random_state1)
x_train.shape, y_train.shape输出
2. 标准化
# 将每一列特征标准化为标准正太分布注意标准化是针对每一列而言的
sc StandardScaler()
x_train sc.fit_transform(x_train)
x_test sc.transform(x_test)x_train x_train.reshape(x_train.shape[0], x_train.shape[1], 1)
x_test x_test.reshape(x_test.shape[0], x_test.shape[1], 1)
3. 构建RNN模型
import tensorflow
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,LSTM,SimpleRNNmodel Sequential()
model.add(SimpleRNN(128, input_shape (13,1),return_sequencesTrue,activationrelu))
model.add(SimpleRNN(64,return_sequencesTrue, activationrelu))
model.add(SimpleRNN(32, activationrelu))
model.add(Dense(64, activationrelu))
model.add(Dense(1, activationsigmoid))
model.summary()
输出
五、编译模型
opt tf.keras.optimizers.Adam(learning_rate1e-4)
model.compile(lossbinary_crossentropy, optimizeropt,metrics[accuracy])
六、训练模型
epochs 50history model.fit(x_train, y_train,epochsepochs,batch_size128,validation_data(x_test, y_test),verbose1)
部分输出
model.evaluate(x_test,y_test)
输出
七、模型评估
import matplotlib.pyplot as pltacc history.history[accuracy]
val_acc history.history[val_accuracy]loss history.history[loss]
val_loss history.history[val_loss]epochs_range range(epochs)plt.figure(figsize(14, 4))
plt.subplot(1, 2, 1)plt.plot(epochs_range, acc, labelTraining Accuracy)
plt.plot(epochs_range, val_acc, labelValidation Accuracy)
plt.legend(loclower right)
plt.title(Training and Validation Accuracy)plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, labelTraining Loss)
plt.plot(epochs_range, val_loss, labelValidation Loss)
plt.legend(locupper right)
plt.title(Training and Validation Loss)
plt.show()
最后准确率输出
scores model.evaluate(x_test, y_test, verbose0)
print(%s: %.2f%% % (model.metrics_names[1], scores[1]*100))
八、总结 注意numpy与panda以及matplotlib等之间的兼容性注意对每一列的特征数据标准化处理