200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 循环神经网络(RNN)的搭建 附带例子和完整代码——2

循环神经网络(RNN)的搭建 附带例子和完整代码——2

时间:2023-11-03 01:15:08

相关推荐

循环神经网络(RNN)的搭建 附带例子和完整代码——2

问题:

用RNN实现输入一个字母,预测出下一个字母(字母使用独热编码):

输入abcd,预测出e

输入bcde,预测出a

输入cdea,预测出b

输入deab,预测出c

输入eabc,预测出d

分析:

1.import

2.train,test:需要把输入的字母转换成独热编码,然后乱序,接着把x_train给reshape一下,转换成符合SimpleRNN的要求[送入样本数,循环核时间展开步数,每个时间步输入特征个数]

3.model=tf.keras.Sequential([…])

pile(…)

5.断点续训+模型保存+model.fit(…)

6.model.summary()

7.把训练的参数打印到txt文件中

8.acc/loss可视化

9.基于保存的模型进行预测

完整代码:

import numpy as npimport tensorflow as tffrom tensorflow.keras.layers import Dense, SimpleRNNimport matplotlib.pyplot as pltimport osinput_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['b']], id_to_onehot[w_to_id['c']], id_to_onehot[w_to_id['d']], id_to_onehot[w_to_id['e']]],[id_to_onehot[w_to_id['c']], id_to_onehot[w_to_id['d']], id_to_onehot[w_to_id['e']], id_to_onehot[w_to_id['a']]],[id_to_onehot[w_to_id['d']], id_to_onehot[w_to_id['e']], id_to_onehot[w_to_id['a']], id_to_onehot[w_to_id['b']]],[id_to_onehot[w_to_id['e']], id_to_onehot[w_to_id['a']], id_to_onehot[w_to_id['b']], id_to_onehot[w_to_id['c']]],]y_train = [w_to_id['e'], w_to_id['a'], w_to_id['b'], w_to_id['c'], w_to_id['d']]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);输入4个字母出结果,循环核时间展开步数为4; 表示为独热码有5个输入特征,每个时间步输入特征个数为5x_train = np.reshape(x_train, (len(x_train), 4, 5))y_train = np.array(y_train)model = tf.keras.Sequential([SimpleRNN(3),Dense(5, activation='softmax')])pile(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_4pre1.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和loss曲线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[a]] for a in alphabet1]# 使alphabet符合SimpleRNN输入要求:[送入样本数, 循环核时间展开步数, 每个时间步输入特征个数]。此处验证效果送入了1个样本,送入样本数为1;输入4个字母出结果,所以循环核时间展开步数为4; 表示为独热码有5个输入特征,每个时间步输入特征个数为5alphabet = np.reshape(alphabet, (1, 4, 5))result = model.predict([alphabet])pred = tf.argmax(result, axis=1)pred = int(pred)tf.print(alphabet1 + '->' + input_word[pred])

可以看出只有

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[‘b’]], id_to_onehot[w_to_id[‘c’]], id_to_onehot[w_to_id[‘d’]], id_to_onehot[w_to_id[‘e’]]],

[id_to_onehot[w_to_id[‘c’]], id_to_onehot[w_to_id[‘d’]], id_to_onehot[w_to_id[‘e’]], id_to_onehot[w_to_id[‘a’]]],

[id_to_onehot[w_to_id[‘d’]], id_to_onehot[w_to_id[‘e’]], id_to_onehot[w_to_id[‘a’]], id_to_onehot[w_to_id[‘b’]]],

[id_to_onehot[w_to_id[‘e’]], id_to_onehot[w_to_id[‘a’]], id_to_onehot[w_to_id[‘b’]], id_to_onehot[w_to_id[‘c’]]],

]

x_train = np.reshape(x_train, (len(x_train), 4, 5))和上个例子不同

因为输入4个字母出结果,所以循环核时间展开步数为4,所以上面的第二个参数是4

注:本文来自于中国大学mooc中北京大学的人工智能实践:Tensorflow笔记,在此感谢北大的曹健老师

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。