赞
踩
一、RNN简介
1、RNN网络最大的特点就是可以处理序列特征,就是我们的一组动态特征。比如,我们可以通过将前三天每天的特征(是否下雨,是否有太阳等)输入到网络,从而来预测第四天的天气。
我们可以看RNN的网络结构如下:
二、RNN cell用法
- import torch
-
- batch_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)
- print(dataset)
- hidden = torch.zeros(batch_size, hidden_size)
- print(hidden)
-
- for idx, input in enumerate(dataset):
- print( '=' * 20, idx, '=' * 20)
- print( 'Input size: ', input.shape)
- hidden = cell(input, hidden)
- print( 'outputs size: ', hidden.shape)
- print(hidden)
三、RNN用法
- import torch
-
- batch_size = 1 # 批处理大小
- seq_len = 3 # 序列长度
- input_size = 4 # 输入维度
- hidden_size = 2 # 隐藏层维度
- num_layers = 4 # 隐藏层数量
-
- 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)
- print( 'Output:', out)
- print( 'Hidden size: ', hidden.shape)
- print( 'Hidden: ', hidden)
四、Embedding
把input变为稠密的数据
代码:
- import torch
-
- # parameters
- num_class = 4
- input_size = 4
- hidden_size = 8
- embedding_size = 10
- num_layers = 2
- batch_size = 1
- seq_len = 5
-
- # 准备数据集
- idx2char = ['e', 'h', 'l', 'o']
- x_data = [[1, 0, 2, 2, 3]] # (batch, seq_len)
- y_data = [3, 1, 2, 3, 2] # (batch * seq_len)
-
- inputs = torch.LongTensor(x_data) # Input should be LongTensor: (batchSize, seqLen)
- labels = torch.LongTensor(y_data) # Target should be LongTensor: (batchSize * seqLen)
-
- # 构建模型
- 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) # (batch, seqLen, embeddingSize)
- x, _ = self.rnn(x, hidden) # 输出(声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/182953推荐阅读
相关标签
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。