1. 实现SRN
(1)numpy实现
复现
import numpy as np
inputs=np.array([[1., 1.],
[1., 1.],
[2., 2.]])
print('inputs is ', inputs)
state_t = np.zeros(2, ) # 初始化存储器
print('state_t is ', state_t)
w1, w2, w3, w4, w5, w6, w7, w8 = 1., 1., 1., 1., 1., 1., 1., 1.
U1, U2, U3, U4 = 1., 1., 1., 1.
print('--------------------------------------')
for input_t in inputs:
print('inputs is ', input_t)
print('state_t is ', state_t)
in_h1 = np.dot([w1, w3], input_t) + np.dot([U2, U4], state_t) # 点乘积
in_h2 = np.dot([w2, w4], input_t) + np.dot([U1, U3], state_t)
state_t = in_h1, in_h2
output_y1 = np.dot([w5, w7], [in_h1, in_h2])
output_y2 = np.dot([w6, w8], [in_h1, in_h2])
print('output_y is ', output_y1, output_y2)
print('---------------')
和其他不同的是有一个存储器,便于循环使用。存储两个,二维矩阵
(2)在1的基础上增加激活函数tanh
import numpy as np
inputs = np.array([[1., 1.],
[1., 1.],
[2., 2.]]) # 初始化输入序列
print('inputs is ', inputs)
state_t = np.zeros(2, ) # 初始化存储器
print('state_t is ', state_t)
w1, w2, w3, w4, w5, w6, w7, w8 = 1., 1., 1., 1., 1., 1., 1., 1.
U1, U2, U3, U4 = 1., 1., 1., 1.
print('--------------------------------------')
for input_t in inputs:
print('inputs is ', input_t)
print('state_t is ', state_t)
in_h1 = np.tanh(np.dot([w1, w3], input_t) + np.dot([U2, U4], state_t))
in_h2 = np.tanh(np.dot([w2, w4], input_t) + np.dot([U1, U3], state_t))
state_t = in_h1, in_h2
output_y1 = np.dot([w5, w7], [in_h1, in_h2])
output_y2 = np.dot([w6, w8], [in_h1, in_h2])
print('output_y is ', output_y1, output_y2)
print('---------------')
没有tanh函数,输出的值将全部是输入值和权重的点积。这意味着网络将无法学习复杂的模式或特征,因为输出将仅仅是输入和权重的线性组合,而且没有激活函数就不能学习非线性,大多数问题都是非线性的,也有可能导致梯度爆炸。
(3)使用nn.RNNCell实现
import torch
batch_size = 1 # 只用一个样本训练
seq_len = 3 # 序列长度
input_size = 2 # 输入序列维度
hidden_size = 2 # 隐藏层维度
output_size = 2 # 输出层维度
# RNNCell
cell = torch.nn.RNNCell(input_size=input_size, hidden_size=hidden_size)
# 初始化参数 https://zhuanlan.zhihu.com/p/342012463
for name, param in cell.named_parameters(): # cell的参数
if name.startswith("weight"): # 检查name是否有以weight的开头的
torch.nn.init.ones_(param) # 初始化和param相同形状的全1矩阵
else:
torch.nn.init.zeros_(param)
# 线性层
liner = torch.nn.Linear(hidden_size, output_size)
liner.weight.data = torch.Tensor([[1, 1], [1, 1]]) # 所有权重为1
liner.bias.data = torch.Tensor([0.0])
seq = torch.Tensor([[[1, 1]],
[[1, 1]],
[[2, 2]]])
hidden = torch.zeros(batch_size, hidden_size)
output = torch.zeros(batch_size, output_size)
for idx, input in enumerate(seq): # 遍历索引和值
print('=' * 20, idx, '=' * 20)
print('Input :', input)
print('hidden :', hidden)
hidden = cell(input, hidden)
output = liner(hidden)
print('output :', output)
torch.nn.RNNCell()用于构建一个基本的循环神经网络(RNN)单元。
基本语法cell = torch.nn.RNNCell(input_size, hidden_size, output_size)
输入特征数,隐藏层神经元数量,输出特征
输出会输出输出数据,隐藏状态。
结果grad_fn=<AddmmBackward0>是
一般来说,它表示计算了矩阵乘法和加法操作的梯度。
在PyTorch中,反向传播是通过链式法则来计算损失函数对模型参数的梯度。AddmmBackward0
是PyTorch中的一个类,用于计算和存储张量操作(例如矩阵乘法和加法)的梯度。
(4)使用nn.RNN实现
import torch
batch_size = 1
seq_len = 3 # 输入序列长度
input_size = 2
hidden_size = 2
num_layers = 1
output_size = 2
cell = torch.nn.RNN(input_size=input_size, hidden_size=hidden_size, num_layers=num_layers)
for name, param in cell.named_parameters(): # 初始化参数
if name.startswith("weight"):
torch.nn.init.ones_(param)
else:
torch.nn.init.zeros_(param)
# 线性层
liner = torch.nn.Linear(hidden_size, output_size)
liner.weight.data = torch.Tensor([[1, 1], [1, 1]])
liner.bias.data = torch.Tensor([0.0])
inputs = torch.Tensor([[[1, 1]],
[[1, 1]],
[[2, 2]]])
hidden = torch.zeros(num_layers, batch_size, hidden_size)
out, hidden = cell(inputs, hidden)
print('Input :', inputs[0])
print('hidden:', 0, 0)
print('Output:', liner(out[0]))
print('--------------------------------------')
print('Input :', inputs[1])
print('hidden:', out[0])
print('Output:', liner(out[1]))
print('--------------------------------------')
print('Input :', inputs[2])
print('hidden:', out[1])
print('Output:', liner(out[2]))
nn.Cell和nn.RNNCell区别?
nn.Cell是PyTorch中实现神经网络的基本类,它提供了基础的神经网络单元,可以在任意时刻使用输入和参数来计算输出。
nn.RNNCell则是特定于循环神经网络(RNN)的Cell,它继承自Cell并增加了一些特定于RNN的功能。RNNCell读取了0时刻的隐层信息h0,剩下的过程是自动循环完成的,而RNNCell则需要自己写循环处理。
nn.RNN基本语法
用来构建不同类型的循环神经网络,如基础 RNN、LSTM(长短期记忆)和 GRU(门控循环单元)。,
nn.RNN
的参数包括:
1.input_size
:输入特征的大小。
2.hidden_size
:隐藏状态的大小。
3.num_layers
:RNN 的层数。默认值是 1。如果你想使用多层的 RNN,你可以设置这个参数大于 1。
4.batch_first
:如果设置为 True,那么输入数据的形状应该是 [batch_size, sequence_length, input_size]
,而不是 [sequence_length, batch_size, input_size]
。默认值是 False
。
5.nonlinearity
:激活函数。默认值是 'relu'。你可以选择 'tanh' 或 'relu'。
6.bidirectional
:如果设置为 True,那么 RNN 将是双向的。
grad_fn=<SelectBack,ward0>
正在使用反向传播(backpropagation)算法来更新网络的权重。
2. 实现“序列到序列”
要实现的是一个分类问题
独热向量?
只有一个元素为1,其余元素均为0的向量。这种向量常用于表示类别特征,即将每个类别特征转换为独热向量。长度和类别数量相同
import torch
batch_size=1
seq_len =3
input_size=4
hidden_size=2
num_layers=1
cell = torch.nn.RNN(input_size=input_size,hidden_size=hidden_size,
num_layers=num_layers,batch_first=True)
#(seaLen. batchSize.indutSizeinputs = torch. randn(batch size, seg len, input size)hidden = torch.zeros(num layers,batch size, hidden size)
inputs = torch.randn(batch_size, seq_len, input_size)
hidden = torch.zeros(num_layers,batch_size, hidden_size)
out,hidden = cell(inputs, hidden)
print(' Output size:', out.shape)
print (' Output:', out)
print('Hidden size:',hidden.shape)
print('Hidden:',hidden)
3. “编码器-解码器”的简单实现
# -*- coding: utf-8 -*-
import torch
import numpy as np
import torch.nn as nn
import torch.utils.data as Data
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
letter = [c for c in 'SE?abcdefghijklmnopqrstuvwxyz']
letter2idx = {n: i for i, n in enumerate(letter)}
seq_data = [['man', 'women'], ['black', 'white'], ['king', 'queen'], ['girl', 'boy'], ['up', 'down'], ['high', 'low']]
# Seq2Seq Parameter
n_step = max([max(len(i), len(j)) for i, j in seq_data]) # max_len(=5)
n_hidden = 128
n_class = len(letter2idx) # classfication problem
batch_size = 3
def make_data(seq_data):
enc_input_all, dec_input_all, dec_output_all = [], [], []
for seq in seq_data:
for i in range(2):
seq[i] = seq[i] + '?' * (n_step - len(seq[i])) # 'man??', 'women'
enc_input = [letter2idx[n] for n in (seq[0] + 'E')] # ['m', 'a', 'n', '?', '?', 'E']
dec_input = [letter2idx[n] for n in ('S' + seq[1])] # ['S', 'w', 'o', 'm', 'e', 'n']
dec_output = [letter2idx[n] for n in (seq[1] + 'E')] # ['w', 'o', 'm', 'e', 'n', 'E']
enc_input_all.append(np.eye(n_class)[enc_input])
dec_input_all.append(np.eye(n_class)[dec_input])
dec_output_all.append(dec_output) # not one-hot
# make tensor
return torch.Tensor(enc_input_all), torch.Tensor(dec_input_all), torch.LongTensor(dec_output_all)
'''
enc_input_all: [6, n_step+1 (because of 'E'), n_class]
dec_input_all: [6, n_step+1 (because of 'S'), n_class]
dec_output_all: [6, n_step+1 (because of 'E')]
'''
enc_input_all, dec_input_all, dec_output_all = make_data(seq_data)
class TranslateDataSet(Data.Dataset):
def __init__(self, enc_input_all, dec_input_all, dec_output_all):
self.enc_input_all = enc_input_all
self.dec_input_all = dec_input_all
self.dec_output_all = dec_output_all
def __len__(self): # return dataset size
return len(self.enc_input_all)
def __getitem__(self, idx):
return self.enc_input_all[idx], self.dec_input_all[idx], self.dec_output_all[idx]
loader = Data.DataLoader(TranslateDataSet(enc_input_all, dec_input_all, dec_output_all), batch_size, True)
# Model
class Seq2Seq(nn.Module):
def __init__(self):
super(Seq2Seq, self).__init__()
self.encoder = nn.RNN(input_size=n_class, hidden_size=n_hidden, dropout=0.5) # encoder
self.decoder = nn.RNN(input_size=n_class, hidden_size=n_hidden, dropout=0.5) # decoder
self.fc = nn.Linear(n_hidden, n_class)
def forward(self, enc_input, enc_hidden, dec_input):
# enc_input(=input_batch): [batch_size, n_step+1, n_class]
# dec_inpu(=output_batch): [batch_size, n_step+1, n_class]
enc_input = enc_input.transpose(0, 1) # enc_input: [n_step+1, batch_size, n_class]
dec_input = dec_input.transpose(0, 1) # dec_input: [n_step+1, batch_size, n_class]
# h_t : [num_layers(=1) * num_directions(=1), batch_size, n_hidden]
_, h_t = self.encoder(enc_input, enc_hidden)
# outputs : [n_step+1, batch_size, num_directions(=1) * n_hidden(=128)]
outputs, _ = self.decoder(dec_input, h_t)
model = self.fc(outputs) # model : [n_step+1, batch_size, n_class]
return model
model = Seq2Seq().to(device)
criterion = nn.CrossEntropyLoss().to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
for epoch in range(5000):
for enc_input_batch, dec_input_batch, dec_output_batch in loader:
# make hidden shape [num_layers * num_directions, batch_size, n_hidden]
h_0 = torch.zeros(1, batch_size, n_hidden).to(device)
(enc_input_batch, dec_intput_batch, dec_output_batch) = (
enc_input_batch.to(device), dec_input_batch.to(device), dec_output_batch.to(device))
# enc_input_batch : [batch_size, n_step+1, n_class]
# dec_intput_batch : [batch_size, n_step+1, n_class]
# dec_output_batch : [batch_size, n_step+1], not one-hot
pred = model(enc_input_batch, h_0, dec_intput_batch)
# pred : [n_step+1, batch_size, n_class]
pred = pred.transpose(0, 1) # [batch_size, n_step+1(=6), n_class]
loss = 0
for i in range(len(dec_output_batch)):
# pred[i] : [n_step+1, n_class]
# dec_output_batch[i] : [n_step+1]
loss += criterion(pred[i], dec_output_batch[i])
if (epoch + 1) % 1000 == 0:
print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.6f}'.format(loss))
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Test
def translate(word):
enc_input, dec_input, _ = make_data([[word, '?' * n_step]])
enc_input, dec_input = enc_input.to(device), dec_input.to(device)
# make hidden shape [num_layers * num_directions, batch_size, n_hidden]
hidden = torch.zeros(1, 1, n_hidden).to(device)
output = model(enc_input, hidden, dec_input)
# output : [n_step+1, batch_size, n_class]
predict = output.data.max(2, keepdim=True)[1] # select n_class dimension
decoded = [letter[i] for i in predict]
translated = ''.join(decoded[:decoded.index('E')])
return translated.replace('?', '')
print('test')
print('man ->', translate('man'))
print('mans ->', translate('mans'))
print('king ->', translate('king'))
print('black ->', translate('black'))
print('up ->', translate('up'))
4.简单总结nn.RNNCell、nn.RNN
这个已在上面总结,此处不再赘述。
5.谈一谈对“序列”、“序列到序列”的理解
序列:在深度学习中一般为带有时间先后顺序(拥有逻辑结构)的一段具有连续关系的数据(文本,语音等等),或者是生活中有位置,顺序的固定序列信息。
序列到序列学习(sequence to sequence learning, Seq2seq)是将一个输入的单词序列转换为另一个输出的单词序列的任务,相当于有条件的语言生成。自然语言处理、语音处理等领域中的机器翻译、摘要生成、对话生成、语音识别等任务都属于这类问题。基本模型使用 RNN 实现编码和解码,实际上是一个有条件的 RNN 语言模型。RNN 通常是 LSTM 和 GRU。基本模型编码器是 RNN,如 LSTM
6.总结本周理论课和作业,写心得体会
理论课总结:
1.RNN 简单用公式描述为。w和b为权重参数和超参数。而且一般输出层用全连接神经网络连接。
循环和其他不同的是,循环有一个存储器,存储之前的输出,第i层神经元在m时刻的输入取决于i-1层神经元在m时刻的输出和i层神经元在m-1时刻的输出。讲述了简单的循环神经网络,能够处理时序问题,这是前馈神经网络的不足
应用到序列分类问题,情感分类,同步,异步序列到序列
作业总结:了解了循环神经网络内部是怎么运行的,以及代码实现。
不加激活函数有可能导致梯度的爆炸。
以及nn.RNNCell和nn.RNN的区别,输入的参数不相同以及前者是特征循环神经网络RNN的神经网络单元,后者是通用
序列到序列的怎么运作的,注意的就是独热向量编码的长度(一个),和类别数量相同。
编码器-译码器 英语译英语
不同的是计算loss的时候因为输出的pred是三维,因此单独使用for循环
有开始标志,和结束标志
REF:
【23-24 秋学期】NNDL 作业9 RNN - SRN-CSDN博客