如何用wordpress建网站,工商联网站建设作用,重庆网站建设合肥公司,网站排名规则目录
一、卷积核与循环核
二、循环核
1.循环核引入
2.循环核#xff1a;循环核按时间步展开。
3.循环计算层#xff1a;向输出方向生长。
4.TF描述循环计算层
三、TF描述循环计算
四、RNN使用案例
1.数据集准备 2.Sequential中RNN
3.存储模型#xff0c;acc和lose…目录
一、卷积核与循环核
二、循环核
1.循环核引入
2.循环核循环核按时间步展开。
3.循环计算层向输出方向生长。
4.TF描述循环计算层
三、TF描述循环计算
四、RNN使用案例
1.数据集准备 2.Sequential中RNN
3.存储模型acc和lose可视化曲线和测试交互窗口
五、Embedding编码
Embedding改进上述案例
六、LSTM
1.循环时间核计算过程编辑 2.TF描述LSTM层
3.代码
七、GRU
1.循环时间核计算过程
2.TF描述GRU层
3.代码
总结 一、卷积核与循环核
卷积核参数空间共享卷积层提取空间信息。
循环核参数时间共享循环层提取时间信息
前向传播只有ht会变化
反向传播Wxh、Whh、Why才会变换
二、循环核
1.循环核引入 2.循环核循环核按时间步展开。 3.循环计算层向输出方向生长。 4.TF描述循环计算层
# 如何使用
tf.keras.layers.SimpleRNN(记忆体个数activation‘激活函数’
return_sequences是否每个时刻输出ht到下一层)
# 说明
activation‘激活函数’ 不写默认使用tanh
return_sequencesTrue 各时间步输出ht
return_sequencesFalse 仅最后时间步输出ht默认
例SimpleRNN(3, return_sequencesTrue
三、TF描述循环计算
举例说明 计算预测abcde 四、RNN使用案例
1.数据集准备
input_word abcde
w_to_id {a: 0, b: 1, c: 2, d: 3, e: 4} # 单词映射到数值id的词典
id_to_onehot {0: [1., 0., 0., 0., 0.], 1: [0., 1., 0., 0., 0.], 2: [0., 0., 1., 0., 0.], 3: [0., 0., 0., 1., 0.],4: [0., 0., 0., 0., 1.]} # id编码为one-hotx_train [id_to_onehot[w_to_id[a]], id_to_onehot[w_to_id[b]], id_to_onehot[w_to_id[c]],id_to_onehot[w_to_id[d]], id_to_onehot[w_to_id[e]]]
y_train [w_to_id[b], w_to_id[c], w_to_id[d], w_to_id[e], w_to_id[a]]
# 打乱顺序
np.random.seed(7)
np.random.shuffle(x_train)
np.random.seed(7)
np.random.shuffle(y_train)
tf.random.set_seed(7)2.Sequential中RNN
# Sequential层中的循环序列Rnn时间序列
model tf.keras.Sequential([SimpleRNN(3), # 存储单元Dense(5, activationsoftmax)
])model.compile(optimizertf.keras.optimizers.Adam(0.01),losstf.keras.losses.SparseCategoricalCrossentropy(from_logitsFalse),metrics[sparse_categorical_accuracy])checkpoint_save_path ./checkpoint/rnn_onehot_1pre1.ckptif os.path.exists(checkpoint_save_path .index):print(-------------load the model-----------------)model.load_weights(checkpoint_save_path)cp_callback tf.keras.callbacks.ModelCheckpoint(filepathcheckpoint_save_path,save_weights_onlyTrue,save_best_onlyTrue,monitorloss) # 由于fit没有给出测试集不计算测试集准确率根据loss保存最优模型history model.fit(x_train, y_train, batch_size32, epochs100, callbacks[cp_callback])model.summary()
3.存储模型acc和lose可视化曲线和测试交互窗口
# print(model.trainable_variables)
file open(./weights.txt, w) # 参数提取
for v in model.trainable_variables:file.write(str(v.name) \n)file.write(str(v.shape) \n)file.write(str(v.numpy()) \n)
file.close()############################################### show ################################################ 显示训练集和验证集的acc和loss曲线
acc history.history[sparse_categorical_accuracy]
loss history.history[loss]plt.subplot(1, 2, 1)
plt.plot(acc, labelTraining Accuracy)
plt.title(Training Accuracy)
plt.legend()plt.subplot(1, 2, 2)
plt.plot(loss, labelTraining Loss)
plt.title(Training Loss)
plt.legend()
plt.show()############### predict #############preNum int(input(input the number of test alphabet:))
for i in range(preNum):alphabet1 input(input test alphabet:)alphabet [id_to_onehot[w_to_id[alphabet1]]]# 使alphabet符合SimpleRNN输入要求[送入样本数 循环核时间展开步数 每个时间步输入特征个数]。此处验证效果送入了1个样本送入样本数为1输入1个字母出结果所以循环核时间展开步数为1; 表示为独热码有5个输入特征每个时间步输入特征个数为5alphabet np.reshape(alphabet, (1, 1, 5))result model.predict([alphabet])pred tf.argmax(result, axis1)pred int(pred)tf.print(alphabet1 - input_word[pred])
五、Embedding编码
目的每次时间预测都要进行编码而且编码很麻烦并且如果简单的话站的内存就比较多所以就引入Embedding
独热码数据量大 过于稀疏映射之间是独立的没有表现出关联性
Embedding是一种单词编码方法用低维向量实现了编码 这种编码通过神经网络训练优化能表达出单词间的相关性。 tf.keras.layers.Embedding(词汇表大小编码维度) Embedding改进上述案例
# Sequential有变化
model tf.keras.Sequential([Embedding(5, 2),SimpleRNN(3),Dense(5, activationsoftmax)
])# 输入有变化alphabet [w_to_id[alphabet1]]# 使alphabet符合Embedding输入要求[送入样本数 循环核时间展开步数]。# 此处验证效果送入了1个样本送入样本数为1输入1个字母出结果循环核时间展开步数为1。alphabet np.reshape(alphabet, (1, 1))
六、LSTM
1.循环时间核计算过程 2.TF描述LSTM层 tf.keras.layers.LSTM(记忆体个数return_sequences是否返回输出) return_sequencesTrue 各时间步输出ht return_sequencesFalse 仅最后时间步输出ht默认 3.代码
model tf.keras.Sequential([LSTM(80, return_sequencesTrue),Dropout(0.2),LSTM(100),Dropout(0.2),Dense(1)
])
七、GRU
1.循环时间核计算过程 2.TF描述GRU层 tf.keras.layers.GRU(记忆体个数return_sequences是否返回输出) return_sequencesTrue 各时间步输出ht return_sequencesFalse 仅最后时间步输出ht默认 3.代码
model tf.keras.Sequential([GRU(80, return_sequencesTrue),Dropout(0.2),GRU(100),Dropout(0.2),Dense(1)
])
总结
本文主要借鉴mooc曹健老师的《人工智能实践Tensorflow笔记》RNN 是最简单的循环神经网络它的优点是结构简单易于实现但是也有缺点比如梯度消失或爆炸、难以处理长期依赖等。LSTM 是一种改进的 RNN它的优点是能够避免梯度消失和长期依赖问题学习更长的序列但是也有缺点比如参数较多计算复杂度高。GRU 是一种简化的 LSTM它的优点是参数较少计算速度快但是也有缺点比如表达能力可能不如 LSTM 强 。选择情况一般来说LSTM 和 GRU 的表现要优于 RNN