序列数据处理与生成网络:原理、应用与实现
1. LSTMs和GRUs
1.1 高级API
PyTorch不仅提供了细粒度的 LSTMCell 和 GRUCell API,还考虑到了用户不需要细粒度操作的情况。 torch.nn 模块提供了LSTM和GRU网络的高级API,它们封装了 LSTMCell 和 GRUCell ,并使用cuDNN(CUDA深度神经网络)实现高效执行。以下是一个 Encoder 类的示例代码:
import torch.nn as nn
class Encoder(nn.Module):
def __init__(self, config):
super(Encoder, self).__init__()
self.config = config
if config.type == 'LSTM':
self.rnn = nn.LSTM(input_size=config.embed_dim, hidden_size=config.hidden_size,
num_layers=config.n_layers, dropout=config.dropout,
bidirectional=config.birnn)
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



