声明
声明:本系列博客是我在学习人工智能实践:TensorFlow笔记(曹健,北京大学,软件与微电子学院)所做的笔记。所以,其中的绝大部分内容引自这系列视频,博客中的代码也是视频配套所附带的代码,其中部分代码可能会因需要而改动。侵删。在本系列博客中,其中包含视频中的引用,也包括我自己对知识的理解,思考和总结。本系列博客的目的主要有两个,一个是可以作为我自己的学习笔记,时常复习巩固。第二个是可以为想学习TensorFlow 2 相关知识的朋友提供一些参考。
正文
循环神经网络:借助循环核提取时间特征后,送入全连接网络。
1.循环核
- 有些数据是与时间序列相关的,是可以根据上文预测出下文的。
循环核:参数时间共享,循环层提取时间信息。
循环核具有记忆力,通过不同时刻的参数共享,实现了对时间序列的信息提取。
可以设定记忆体的个数,改变记忆容量。当记忆体个数被指定,输入xt,输出yt维度被指定。周围的待训练参数的维度也就被限定了。
- 前向传播时:记忆体内存储的状态信息ht,在每个时刻都被刷新,三个参数矩阵wxh,whh,why自始至终都是固定不变的。
- 反向传播时:三个参数矩阵wxh,whh,why被梯度下降法更新。
y t = s o f t m a x ( h t w h y + b y ) y_t=softmax(h_tw_{hy}+by) yt=softmax(htwhy+by)
h t = t a n h ( x t w x h + h t − 1 w h h + b h ) h_t=tanh(x_tw_{xh}+h_{t-1}w_{hh}+bh) ht=tanh(xtwxh+ht−1whh+bh)
记忆体内存储着每个时刻的状态信息ht。
当前时刻循环核的输出特征yt.
2. 循环核按时间步展开
按照时间步展开,就是把循环核按照时间轴方向展开。
每个时刻记忆体状态信息ht被刷新,记忆体周围的参数矩阵wxh,whh,why是固定不变的。我们训练优化的就是这些参数矩阵,训练完成后,使用效果最好的参数矩阵,执行前向传播,输出预测结果。
3.循环计算层
循环计算层:向输出方向生长。
每个循环核构成一层循环计算层,循环计算层的层数是向输出方向增长的。
上图中,这个网络有三个循环核,构成了三层循环计算层。他们中的每个循环核中记忆体的个数是根据需求任意指定的。
4.TF描述循环计算层
tf.keras.layers.SimpleRNN(记忆体个数,activation=‘激活函数’,return_sequences=是否每个时刻输出ht到下一层)
activation=‘激活函数’(不写,默认使用tanh)指定使用什么激活函数计算ht
return_sequences=True 各时间步输出ht
return_sequences=False 仅最后时间步输出ht(默认)
一般,最后一层的循环核用False,仅在最后一个时间步输出ht,中间的层循环核用True,每个时间步都把ht输出到下一层。
5.对数据维度的要求
API对送入循环层的数据维度是有要求的,要求送入循环层的数据是三维的:
入RNN时,x_train维度:[送入样本数,循环核时间展开步数,每个时间步输入特征个数]
6.用RNN实现输入一个字母,预测下一个字母
字母预测:输入一个字母,预测下一个字母,输入a预测出b,输入b预测出c,…输入e预测出a。
神经网络的输入都是数字,所以要先把用到的a,b,c,d,e用数字表示出来,最简单直接的方法就是用独热码对这5个字母编码。
以下是完整代码:
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense, SimpleRNN
import matplotlib.pyplot as plt
import os
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-hot
x_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)
# 使x_train符合SimpleRNN输入要求:[送入样本数, 循环核时间展开步数, 每个时间步输入特征个数]。
# 此处整个数据集送入,送入样本数为len(x_train);输入1个字母出结果,循环核时间展开步数为1; 表示为独热码有5个输入特征,每个时间步输入特征个数为5
x_train = np.reshape(x_train, (len(x_train), 1, 5))
y_train = np.array(y_train)
model = tf.keras.Sequential([
SimpleRNN(3),
Dense(5, activation='softmax')
])
model.compile(optimizer=tf.keras.optimizers.Adam(0.01),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy'])
checkpoint_save_path = "./checkpoint/rnn_onehot_1pre1.ckpt"
if os.path.exists(checkpoint_save_path + '.index'):
print('-------------load the model-----------------')
model.load_weights(checkpoint_save_path)
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,
save_weights_only=True,
save_best_only=True,
monitor='loss') # 由于fit没有给出测试集,不计算测试集准确率,根据loss,保存最优模型
history = model.fit(x_train, y_train, batch_size=32, epochs=100, callbacks=[cp_callback])
model.summary()
# 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 = history.history['sparse_categorical_accuracy']
loss = history.history['loss']
plt.subplot(1, 2, 1)
plt.plot(acc, label='Training Accuracy')
plt.title('Training Accuracy')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(loss, label='Training 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个输入特征,每个时间步输入特征个数为5
alphabet = np.reshape(alphabet, (1, 1, 5))
result = model.predict([alphabet])
pred = tf.argmax(result, axis=1)
pred = int(pred)
tf.print(alphabet1 + '->' + input_word[pred])
运行结果:
input the number of test alphabet:3
input test alphabet:b
b->c
input test alphabet:a
a->b
input test alphabet:e
e->a
感谢观看!
如有错误,欢迎批评指正!