LSTM维度
原先看了一些lstm的视频,和文档直说看的似懂非懂,计算机最主要的还是实践,输出是什么还是print出来给我们看看更直观,本人也是学渣一枚,所以傻瓜式的方法把我看的一篇bilstm-atten的代码的模型给切出来模型那部分,每一步输出了结果,注释部分就是维度的变化.大家也可以看着这个加深感受.
#这个代码是一个双层的LSTM的再加atten的模型 完整代码后面在补充
import torch
import torch.nn as nn
from torch.autograd import Variable
import const
class AttentionModel(torch.nn.Module):
def __init__(self):
super(AttentionModel, self).__init__()
"""
Arguments
---------
batch_size : Size of the batch which is same as the batch_size of the data returned by the TorchText BucketIterator
output_size : num classes
hidden_sie : Size of the hidden_state of the LSTM
vocab_size : Size of the vocabulary containing unique words
embedding_dim : Embeddding dimension of GloVe word embeddings
--------
"""
self.batch_size=64
self.output_size=24
self.hidden_size=256
self.vocab_size=6301
self.embed_dim=300
self.bidirectional=True
self.dropout =0.3
self.sequence_length =20
self.lookup_table = nn.Embedding(self.vocab_size, self.embed_dim,padding_idx=const.PAD)#
# indices = indices.to(torch.int64)
self.lookup_table.weight.data.uniform_(-1., 1.)
self.layer_size = 1 #args.layer_size
self.lstm = nn.LSTM(input_size=self.embed_dim,hidden_size=self.hidden_size,num_layers=self.layer_size,
dropout=self.dropout,bidirectional=self.bidirectional)
if self.bidirectional:
self.num_direction =2
else: