1、实现SRN
(Simple Recurrent Network,只有一个隐藏层的神经网络)
(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)
w=np.ones(8)
U=np.ones(4)
print('--------------------------------------')
for input_t in inputs:
print('inputs is ', input_t)
print('state_t is ', state_t)
in_h1 = np.dot([w[0], w[2]], input_t) + np.dot([U[1], U[3]], state_t)
in_h2 = np.dot([w[1], w[3]], input_t) + np.dot([U[0], U[2]], state_t)
state_t = in_h1, in_h2
output_y1 = np.dot([w[4], w[6]], [in_h1, in_h2])
output_y2 = np.dot([w[5], w[7]], [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)
w=np.ones(8)
U=np.ones(4)
print('--------------------------------------')
for input_t in inputs:
print('inputs is ', input_t)
print('state_t is ', state_t)
in_h1 = np.tanh(np.dot([w[0], w[2]], input_t) + np.dot([U[1], U[3]], state_t))
in_h2 = np.tanh(np.dot([w[1], w[3]], input_t) + np.dot([U[0], U[2]], state_t))
state_t = in_h1, in_h2
output_y1 = np.dot([w[4], w[6]], [in_h1, in_h2])
output_y2 = np.dot([w[5], w[7]], [in_h1, in_h2])
print('output_y is ', output_y1, output_y2)
print('----------------')
(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)
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])
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)
(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]))
2、实现“序列到序列”
nn.RNN
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)
inputs=torch.randn(batch_size ,seq_len,input_size )
hidden=torch.zeros(num_layers ,batch_size ,hidden_size )
out,hidden =cell(inputs,hidden)
print('Outputs size:',out.shape)
print('Output:',out)
print('Hidden size:',hidden.shape)
print('Hidden:',hidden)
将单词拆分为字母,每个字母对应一个数字,将数字转换成独热编码
import torch
batch_size = 1
num_layers=1
input_size = 4
hidden_size = 4
seq_len=5
idx2char=['e','h','l','o']
x_data=[1,0,2,2,3]
y_data=[3,1,2,3,2]
one_hot_lookup=[[1,0,0,0],
[0,1,0,0],
[0,0,1,0],
[0,0,0,1]]
x_one_hot=[one_hot_lookup[x] for x in x_data ]
inputs=torch.Tensor(x_one_hot ).view(seq_len ,batch_size ,input_size )
labels=torch.LongTensor (y_data )
class Model(torch.nn.Module ):
def __init__(self,input_size,hidden_size,batch_size,num_layers=1):
super(Model ,self).__init__()
self.num_layers=num_layers
self.batch_size =batch_size
self.input_size=input_size
self.hidden_size=hidden_size
self.rnn=torch.nn.RNN (input_size= self.input_size ,hidden_size= self.hidden_size ,num_layers= num_layers )
def forward(self,input):
hidden=torch.zeros(self.num_layers ,self.batch_size ,self.hidden_size )
out,_=self.rnn(input,hidden)
return out.view(-1,self.hidden_size )
def init_hidden(self):
return torch.zeros(self.batch_size,self.hidden_size )
net=Model(input_size,hidden_size,batch_size ,num_layers )
criterion=torch.nn.CrossEntropyLoss ()#交叉熵
optimizer=torch.optim.Adam (net.parameters() ,lr=0.1)#优化器
for epoch in range(15):
optimizer.zero_grad()
outputs=net(inputs )
loss=criterion (outputs ,labels )
loss.backward()
optimizer.step()
_,idx=outputs .max(dim=1)
idx=idx.data.numpy()
print('Predicted :',''.join([idx2char [x]for x in idx]),end='')
print(',Epoch [%d/15] loss=%.4f'%(epoch +1,loss.item()))
通过测试发现,当学习率=0.1时,效果最好,大于0.1或者小于0.1都不能很快的预测出想要的结果。但是,在学习率=0.1时,也会出现不稳定的情况,不能在15轮内预测出‘ohlol’。
3、“编码器-解码器”的简单实现
编码器-解码器(异步的序列到序列)
在自然语言处理中,输入输出都可以是不定长序列,此时可以用编码器—解码器(encoder-decoder )
编码器接受一个长度可变的序列作为输入, 并将其转换为具有固定形状的编码状态。
解码器将固定形状的编码状态映射到长度可变的序列。
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]))
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, 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 = enc_input.transpose(0, 1)
dec_input = dec_input.transpose(0, 1)
_, h_t = self.encoder(enc_input, enc_hidden)
outputs, _ = self.decoder(dec_input, h_t)
model = self.fc(outputs)
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:
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))
pred = model(enc_input_batch, h_0, dec_intput_batch)
pred = pred.transpose(0, 1)
loss = 0
for i in range(len(dec_output_batch)):
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)
hidden = torch.zeros(1, 1, n_hidden).to(device)
output = model(enc_input, hidden, dec_input)
predict = output.data.max(2, keepdim=True)[1]
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
nn.RNNCell可以看作是RNN的基础构成单元,它可以接受序列中单步的输入,且必须传入隐藏状态。对于序列中的每个步骤,它都会单独进行计算,并输出该步骤的隐藏状态。这种方式需要手动写循环来处理序列数据的每一个步骤。
nn.RNN则可以接受一个序列的输入,它默认会传入全0的隐藏状态,也可以自定义隐藏状态传入。与nn.RNNCell不同,nn.RNN的设计使得用户不需要手动写循环来处理序列数据的每一个步骤,这在实际应用中更为方便。
总结来说,nn.RNN和nn.RNNCell的区别在于处理序列数据的方式,nn.RNNCell需要手动写循环,而nn.RNN则可以自动处理整个序列。
5、谈一谈对“序列”、“序列到序列”的理解
序列:一串按照特定顺序排列的数据元素,比如一串数字、一串字母或者一串汉字。
序列到序列:一种深度学习模型,通常用于将一个序列转化为另一个序列。
序列到序列分为两种:同步的序列到序列模式,异步的序列到序列模式。
同步的序列到序列模式:主要用于序列标注任务(每一时刻都有输入和输出,输入序列和输出序列的长度相同)。
异步的序列到序列模式(编码器-解码器):输入序列和输出序列不需要有严格的对应关系,也不需要保持相同的长度。
6、总结本周理论课和作业,写心得体会
循环神经网络
- 具有短期记忆能力
- 神经元可接受其他神经元信息,也可接受自身信息,形成又换路的网络结构
- 和前馈神经网络相比,更符合生物神经网络结构
- 已广泛应用在语音识别、语言模型、自然语言生成等任务上
- 容易扩展到更广义的记忆网络模型:递归神经网络、图网络
给网络增强短期记忆力的方法
- 延时神经网络(在前馈网络中的非输出层都添加一个延时器)
- 有外部输入的非线性自回归模型(用变量 y t y_t yt的历史信息来预测自己)
- 循环神经网络(通过使用带自反馈的神经元)
简单循环网络(SRN):只有一个隐藏层的神经单元
z
t
=
U
h
t
−
1
+
W
x
t
+
b
z_t=Uh_{t-1}+Wx_t+b
zt=Uht−1+Wxt+b
完全连接的循环网络,其输入为
x
t
x_t
xt,输出为
y
t
y_t
yt
状态更新:
h
t
=
f
(
U
h
t
−
1
+
W
x
t
+
b
)
h_t=f(Uh_{t-1}+Wx_t+b)
ht=f(Uht−1+Wxt+b)
y
t
=
V
h
t
y_t=Vh_t
yt=Vht
其中
h
h
h为隐状态,
f
(
⋅
)
f(·)
f(⋅)为非线性激活函数,
U
、
W
、
b
U、W、b
U、W、b和
V
V
V为网络参数
序列到类别:主要用于序列分类问题,输入为序列,输出为类别
同步的序列到序列模式:主要用于序列标注任务,每一时刻都有输入和输出,输入序列和输出序列的长度相同。
异步的序列到序列模式:编码器-解码器模型,输入序列和输出序列不需要有严格的对应关系,也不需要保持相同的长度。
心得体会
在实现SRN的过程中,我首先使用了Numpy来实现。通过编写自定义的循环,我创建了简单的神经网络,并用Numpy来执行计算。虽然这种方法很直观,但是效率较低。
在基础上增加了激活函数tanh之后,我发现这增加了模型的表达能力。tanh函数是一种常用的激活函数,它可以增加模型的非线性表达能力。
然后,我使用了PyTorch的nn.RNNCell和RNN来实现SRN。我发现这比使用Numpy更加高效。
在实现“序列到序列”的过程中,我学习了如何使用编码器和解码器来构建一个简单的机器翻译模型。编码器将输入序列转换成一个上下文向量,而解码器则根据这个上下文向量生成输出序列。
nn.RNNCell和nn.RNN是PyTorch中用于实现循环神经网络的两个重要类。nn.RNNCell提供了一个基础的RNN单元,可以用于构建更复杂的模型。而nn.RNN则提供了一个高级的接口,可以方便地构建和训练RNN模型。这两个类都可以用于实现SRN和“编码器-解码器”等任务。
参考链接:
【深度学习】动手学深度学习——编码器-解码器_深度学习 编码器和解码器_岁月漫长_的博客-CSDN博客
【23-24 秋学期】NNDL 作业9 RNN - SRN-CSDN博客
Seq2Seq的PyTorch实现 - mathor
《PyTorch深度学习实践》完结合集_哔哩哔哩_bilibili
完全图解RNN、RNN变体、Seq2Seq、Attention机制 - 知乎