热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

pytorch深度学习实践循环神经网络0113

B站刘二大人:循环神经网络(基础篇)目录1、RNN概念2、numLayers含义3、RNN使用4、利用RNNCell训练hell

B站 刘二大人:循环神经网络(基础篇)

目录

1、RNN概念

2、numLayers含义

3、RNN使用

4、利用RNN Cell训练hello转换到ohlol

5、Embedding编码方式



1、RNN概念

        RNN Cell是线性层。

         隐层是RNN Cell里线性层矩阵w的行数。

         使用RNN Cell:

import torchbatch_size = 1 # 批处理大小
seq_len = 3 # 序列长度
input_size = 4 # 输入维度
hidden_size = 2 # 隐层维度cell = torch.nn.RNNCell(input_size=input_size, hidden_size=hidden_size) # 初始化# (seq, batch, features)
dataset = torch.randn(seq_len, batch_size, input_size)
hidden = torch.zeros(batch_size, hidden_size)# 这个循环就是处理seq_len长度的数据
for idx, data in enumerate(dataset):print('=' * 20, idx, '=' * 20)print('Input size:', data.shape, data)hidden = cell(data, hidden)print('hidden size:', hidden.shape, hidden)print(hidden)


2、numLayers含义


3、RNN使用

        input_size和hidden_size: 输入维度和隐层维度

        batch_size: 批处理大小

        seq_len: 序列长度

        num_layers: 隐层数目

        使用RNN:

import torchbatch_size = 1 # batch_size: 批处理大小
seq_len = 3 # seq_len: 序列长度
input_size = 4 # input_size:输入维度
hidden_size = 2 # hidden_size: 隐层维度
num_layers = 1 # num_layers: 隐层数目cell = torch.nn.RNN(input_size=input_size, hidden_size=hidden_size, num_layers=num_layers)# (seqLen, batchSize, inputSize)
inputs = torch.randn(seq_len, batch_size, input_size)
hidden = torch.zeros(num_layers, batch_size, hidden_size)out, hidden = cell(inputs, hidden)print('Output size:', out.shape) # (seq_len, batch_size, hidden_size)
print('Output:', out)
print('Hidden size:', hidden.shape) # (num_layers, batch_size, hidden_size)
print('Hidden:', hidden)

4、利用RNN Cell训练hello转换到ohlol

 

         代码如下:

import torch
input_size = 4
hidden_size = 4
batch_size = 1idx2char = ['e', 'h', 'l', 'o']
x_data = [1, 0, 2, 3, 3] # hello中各个字符的下标
y_data = [3, 1, 2, 3, 2] # ohlol中各个字符的下标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] # (seqLen, inputSize)inputs = torch.Tensor(x_one_hot).view(-1, batch_size, input_size)
labels = torch.LongTensor(y_data).view(-1, 1)
# torch.Tensor默认是torch.FloatTensor是32位浮点类型数据,torch.LongTensor是64位整型
print(inputs.shape, labels.shape)class Model(torch.nn.Module):def __init__(self, input_size, hidden_size, batch_size):super(Model, self).__init__()self.batch_size = batch_sizeself.input_size = input_sizeself.hidden_size = hidden_sizeself.rnncell = torch.nn.RNNCell(input_size=self.input_size, hidden_size=self.hidden_size)def forward(self, inputs, hidden):hidden = self.rnncell(inputs, hidden) # 输入和隐层转换为下一个隐层# shape of inputs:(batchSize, inputSize),shape of hidden:(batchSize, hiddenSize),return hiddendef init_hidden(self):return torch.zeros(self.batch_size, self.hidden_size) # 生成全0的h0net = Model(input_size, hidden_size, batch_size)criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(net.parameters(), lr=0.1)for epoch in range(15):loss = 0optimizer.zero_grad()hidden = net.init_hidden()print('Predicted string:', end='')for input, label in zip(inputs, labels):hidden = net(input, hidden)# 注意交叉熵在计算loss的时候维度关系,这里的hidden是([1, 4]), label是 ([1])loss += criterion(hidden, label)_, idx = hidden.max(dim = 1)print(idx2char[idx.item()], end='')loss.backward()optimizer.step()print(', Epoch [%d/15] loss=%.4f' % (epoch+1, loss.item()))

结果:

 

5、Embedding编码方式

       独热编码向量维度过高;
       独热编码向量稀疏,每个向量是一个为1其余为0;
       独热编码是硬编码,编码情况与数据特征无关;
       采用一种低维度的、稠密的、可学习数据的编码方式:Embedding。

         代码:

import torchinput_size = 4
num_class = 4
hidden_size = 8
embedding_size = 10
batch_size = 1
num_layers = 2
seq_len = 5idx2char_1 = ['e', 'h', 'l', 'o']
idx2char_2 = ['h', 'l', 'o']x_data = [[1, 0, 2, 2, 3]]
y_data = [3, 1, 2, 2, 3]# inputs 维度为(batchsize,seqLen)
inputs = torch.LongTensor(x_data)
# labels 维度为(batchsize*seqLen)
labels = torch.LongTensor(y_data)class Model(torch.nn.Module):def __init__(self):super(Model, self).__init__()self.emb = torch.nn.Embedding(input_size, embedding_size)self.rnn = torch.nn.RNN(input_size=embedding_size,hidden_size=hidden_size,num_layers=num_layers,batch_first=True)self.fc = torch.nn.Linear(hidden_size, num_class)def forward(self, x):hidden = torch.zeros(num_layers, x.size(0), hidden_size)x = self.emb(x) # 进行embedding处理x, _ = self.rnn(x, hidden)x = self.fc(x)return x.view(-1, num_class)net = Model()criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(net.parameters(), lr=0.05)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 string: ', ''.join([idx2char_1[x] for x in idx]), end='')print(", Epoch [%d/15] loss = %.3f" % (epoch + 1, loss.item()))

        结果:

 




 


推荐阅读
author-avatar
newbigstart
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有