RNN循环神经网络——embedding的理解

11 篇文章 0 订阅
1 篇文章 0 订阅

一、RNN 循环神经网络_embedding替换独热码

以实现连续数据预测为例,在这里先引入几个概念

1. 1循环核:

具有记忆力,通过不同时刻的参数共享,实现了对时间序列的特征提取。

前向传播时:记忆体内存储的状态信息ht,在每个时刻被刷新,三个参数矩阵在时间展开步(一层循环)内保持不变。
反向传播时:参数w和b被梯度下降法更新。

在这里插入图片描述
更新方法如下:

在这里插入图片描述

1.2 循环核按时间步展开

实质上就是按时间轴方向展开,同层同一个循环核完成的操作。

在这里插入图片描述
注意与增加循环核(增加循环层区分开)

1.3循环计算层

在这里插入图片描述
增加循环核,增加循环曾,向输出方向增长。

二、tf描述一个循环计算层

2.1基本函数

tf.keras.layers.SimpleRNN(记忆体个数, activation='激活函数',
return_sequences='是否每个时刻都输出ht到下一层')

##############解释
activation='激活函数' # 默认为tanh
return_sequences=True #中间循环层用True,每个ht都输出给下一层,最后时间步输出时False,仅在最后时间步输出ht。

在这里插入图片描述
中间循环层每个时间步传递ht给下层神经元,供下一层神经元使用。
在这里插入图片描述
只在最后时间步输出ht计算最后输出值。

2.1进入RNN时,x_train的维度

API对循环层的数据维度是由要求的

[送入样本数,循环核时间展开步数,每个时间步输入的特征个数]
在这里插入图片描述
第一个图:两个样本,时间步:1,每个样本3个特征
第二个图:一个样本,时间步:4,一个样本中含有(带有两个特征的)四个输入,每个时间步输入一个,一个输入中有两个特征。

2.3循环计算过程

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和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[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])

abcde的预测,输入a输出b 输入b输出c以此类推。
编码方式为独热码。

三、Embedding编码代替独热码

独热码:数据量大,过于稀疏,映射之间相互独立,没有表现出相关性。
Embedding编码:是一种单词编码方式,用低维向量实现了编码,可以通过NN的训练不断优化。

3.1思想:

将0 ,1, 2, 3, 4等数值标签用多维可更新向量表示,不再是一维独热码的形式。

3.2代码实现:

tf.keras.layers.Embedding(词汇表大小,编码维度)

在循环层前加入Embedding层,编码维度:表示多少个数字表示一个单词。


# API对输入Embedding的x_train维度也是有要求的
# [送入样本数,循环核时间展开步长]
x_train = np.reshape(x_train,(len(x_train),1))# 可抽象理解为样本数个行 1列 
#如果是2的话 就是每一个样本对应两个输入也就是两个时间步

##########定义网络结构时
model = tf.keras.Sequential([
	Embedding(5,2),#Embedding层,对输入数据进行编码,五个样本,每个用两个数字表示。则5代表样本数,2代表特征数
	SimpleRNN(3),#三个循环核的循环神经网络,按上述输入RNN需要[送入样本数,循环核时间展开步数,每个时间步输入的特征个数],
	#则Embedding中2代表了特征数,x_train经过reshape维度后的1代表了时间展开步数,len(x_train)代表了样本数,。
	#由此可通俗理解Embedding的参数和使用。
	])

3.3参数理解

    见注释。RNN最终还是会获取到 [送入样本数,循环核时间展开步数,每个时间步输入的特征个数] 
由此可以反过来捋清楚Embedding函数和参数代表的含义。

感谢北京大学曹健老师的《tensorflow笔记》,收益良多,基本属于课上内容,搬砖至此,作为笔记。

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

plus_left

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值