Python torch.nn.LSTM Examples
The following are 30
code examples of torch.nn.LSTM().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
torch.nn
, or try the search function
.
Example #1
Source File: recurrent_attention.py From Attentive-Filtering-Network with MIT License | 7 votes |
def __init__(self, batch=32, bidirectional=True): super(BLSTM, self).__init__() self.bidirectional = bidirectional self.hidden = self.init_hidden(batch) self.lstm = nn.LSTM(257, 50, num_layers=2, bidirectional=True) self.fc = nn.Linear(50*2,1) ## Weights initialization def _weights_init(m): if isinstance(m, nn.Conv2d or nn.Linear or nn.GRU or nn.LSTM): init.xavier_normal_(m.weight) m.bias.data.zero_() elif isinstance(m, nn.BatchNorm2d or nn.BatchNorm1d): m.weight.data.fill_(1) m.bias.data.zero_() self.apply(_weights_init)
Example #2
Source File: BiLSTM.py From pytorch_NER_BiLSTM_CNN_CRF with Apache License 2.0 | 7 votes |
def __init__(self, **kwargs): super(BiLSTM, self).__init__() for k in kwargs: self.__setattr__(k, kwargs[k]) V = self.embed_num D = self.embed_dim C = self.label_num paddingId = self.paddingId self.embed = nn.Embedding(V, D, padding_idx=paddingId) if self.pretrained_embed: self.embed.weight.data.copy_(self.pretrained_weight) else: init_embedding(self.embed.weight) self.dropout_embed = nn.Dropout(self.dropout_emb) self.dropout = nn.Dropout(self.dropout) self.bilstm = nn.LSTM(input_size=D, hidden_size=self.lstm_hiddens, num_layers=self.lstm_layers, bidirectional=True, batch_first=True, bias=True) self.linear = nn.Linear(in_features=self.lstm_hiddens * 2, out_features=C, bias=True) init_linear(self.linear)
Example #3
Source File: lstm.py From OpenNRE with MIT License | 7 votes |
def __init__(self, input_size=50, hidden_size=256, dropout=0, bidirectional=False, num_layers=1, activation_function="tanh"): """ Args: input_size: dimention of input embedding hidden_size: hidden size dropout: dropout layer on the outputs of each RNN layer except the last layer bidirectional: if it is a bidirectional RNN num_layers: number of recurrent layers activation_function: the activation function of RNN, tanh/relu """ super().__init__() if bidirectional: hidden_size /= 2 self.lstm = nn.LSTM(input_size, hidden_size, num_layers, nonlinearity=activation_function, dropout=dropout, bidirectional=bidirectional)
Example #4
Source File: seq2sql_condition_predict.py From SQL_Database_Optimization with BSD 3-Clause "New" or "Revised" License | 7 votes |
def __init__(self, N_word, N_h, N_depth, max_col_num, max_tok_num, gpu): super(Seq2SQLCondPredictor, self).__init__() print "Seq2SQL where prediction" self.N_h = N_h self.max_tok_num = max_tok_num self.max_col_num = max_col_num self.gpu = gpu self.cond_lstm = nn.LSTM(input_size=N_word, hidden_size=N_h/2, num_layers=N_depth, batch_first=True, dropout=0.3, bidirectional=True) self.cond_decoder = nn.LSTM(input_size=self.max_tok_num, hidden_size=N_h, num_layers=N_depth, batch_first=True, dropout=0.3) self.cond_out_g = nn.Linear(N_h, N_h) self.cond_out_h = nn.Linear(N_h, N_h) self.cond_out = nn.Sequential(nn.Tanh(), nn.Linear(N_h, 1)) self.softmax = nn.Softmax()
Example #5
Source File: selection_predict.py From SQL_Database_Optimization with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, N_word, N_h, N_depth, max_tok_num, use_ca): super(SelPredictor, self).__init__() self.use_ca = use_ca self.max_tok_num = max_tok_num self.sel_lstm = nn.LSTM(input_size=N_word, hidden_size=N_h/2, num_layers=N_depth, batch_first=True, dropout=0.3, bidirectional=True) if use_ca: print "Using column attention on selection predicting" self.sel_att = nn.Linear(N_h, N_h) else: print "Not using column attention on selection predicting" self.sel_att = nn.Linear(N_h, 1) self.sel_col_name_enc = nn.LSTM(input_size=N_word, hidden_size=N_h/2, num_layers=N_depth, batch_first=True, dropout=0.3, bidirectional=True) self.sel_out_K = nn.Linear(N_h, N_h) self.sel_out_col = nn.Linear(N_h, N_h) self.sel_out = nn.Sequential(nn.Tanh(), nn.Linear(N_h, 1)) self.softmax = nn.Softmax()
Example #6
Source File: AudioEncoder.py From video-caption-openNMT.pytorch with MIT License | 6 votes |
def __init__(self, num_layers, bidirectional, rnn_size, dropout, sample_rate, window_size): super(AudioEncoder, self).__init__() self.num_layers = num_layers self.num_directions = 2 if bidirectional else 1 self.hidden_size = rnn_size self.layer1 = nn.Conv2d(1, 32, kernel_size=(41, 11), padding=(0, 10), stride=(2, 2)) self.batch_norm1 = nn.BatchNorm2d(32) self.layer2 = nn.Conv2d(32, 32, kernel_size=(21, 11), padding=(0, 0), stride=(2, 1)) self.batch_norm2 = nn.BatchNorm2d(32) input_size = int(math.floor((sample_rate * window_size) / 2) + 1) input_size = int(math.floor(input_size - 41) / 2 + 1) input_size = int(math.floor(input_size - 21) / 2 + 1) input_size *= 32 self.rnn = nn.LSTM(input_size, rnn_size, num_layers=num_layers, dropout=dropout, bidirectional=bidirectional)
Example #7
Source File: recurrent.py From Character-Level-Language-Modeling-with-Deeper-Self-Attention-pytorch with MIT License | 6 votes |
def forward(self, inputs, hidden): def select_layer(h_state, i): # To work on both LSTM / GRU, RNN if isinstance(h_state, tuple): return tuple([select_layer(s, i) for s in h_state]) else: return h_state[i] next_hidden = [] for i, layer in enumerate(self.layers): next_hidden_i = layer(inputs, select_layer(hidden, i)) output = next_hidden_i[0] if isinstance(next_hidden_i, tuple) \ else next_hidden_i if i + 1 < self.num_layers: output = self.dropout(output) if self.residual and inputs.size(-1) == output.size(-1): inputs = output + inputs else: inputs = output next_hidden.append(next_hidden_i) if isinstance(hidden, tuple): next_hidden = tuple([torch.stack(h) for h in zip(*next_hidden)]) else: next_hidden = torch.stack(next_hidden) return inputs, next_hidden
Example #8
Source File: cnn_train_new.py From Cuff_less_BP_Prediction with MIT License | 6 votes |
def __init__(self, num_classes=1): super(ConvNet, self).__init__() self.layer1 = nn.Sequential( nn.Conv2d(1, 32,kernel_size=2, stride=1, padding=2), nn.BatchNorm2d(32), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2)) self.layer2 = nn.Sequential( nn.Conv2d(32, 64, kernel_size=2, stride=1, padding=2), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2)) self.lstm = nn.LSTM(32256,100,1) self.layer3 = nn.Sequential( nn.Conv2d(1, 20, kernel_size=2, stride=1, padding=2), nn.BatchNorm2d(20), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2)) self.fc = nn.Linear(32976, 32) self.fc2 = nn.Linear(32, 1)
Example #9
Source File: enc_PTRUNK.py From ConvLab with MIT License | 6 votes |
def __init__(self, hidden_size, output_size, n_layers=1, dropout=0.1): super(PtrDecoderRNN, self).__init__() self.hidden_size = hidden_size self.output_size = output_size ### Vocab size self.n_layers = n_layers self.dropout = dropout self.embedding = nn.Embedding(output_size, hidden_size) self.embedding_dropout = nn.Dropout(dropout) self.lstm = nn.LSTM(2*hidden_size, hidden_size, n_layers, dropout=dropout) self.W1 = nn.Linear(2*hidden_size, hidden_size) self.v = nn.Parameter(torch.rand(hidden_size)) stdv = 1. / math.sqrt(self.v.size(0)) self.v.data.normal_(mean=0, std=stdv) self.concat = nn.Linear(hidden_size * 2, hidden_size) self.U = nn.Linear(hidden_size, output_size) self.W = nn.Linear(hidden_size, 1) if USE_CUDA: self.embedding = self.embedding.cuda() self.embedding_dropout = self.embedding_dropout.cuda() self.lstm = self.lstm.cuda() self.W1 = self.W1.cuda() self.v = self.v.cuda() self.U = self.U.cuda() self.W = self.W.cuda()
Example #10
Source File: rnn_encoder.py From OpenChem with MIT License | 6 votes |
def forward(self, inp, previous_hidden=None): """ inp: shape batch_size, seq_len, input_size previous_hidden: if given shape n_layers * num_directions, batch_size, embedding_dim. Initialized automatically if None return: embedded """ inp = inp.permute(1, 0, 2) batch_size = inp.size()[1] if previous_hidden is None: previous_hidden = self.init_hidden(batch_size) if self.layer == 'LSTM': cell = self.init_cell(batch_size) previous_hidden = (previous_hidden, cell) output, _ = self.rnn(inp, previous_hidden) embedded = output[-1, :, :].squeeze(0) return embedded, previous_hidden
Example #11
Source File: usermodule.py From ConvLab with MIT License | 6 votes |
def __init__(self, vocab_size, embed_size, hidden_size, input_dropout_p=0, dropout_p=0, n_layers=1, rnn_cell='GRU', variable_lengths=False, embedding=None, update_embedding=True): super(Encoder, self).__init__() self.vocab_size = vocab_size self.hidden_size = hidden_size self.n_layers = n_layers self.input_dropout = nn.Dropout(p=input_dropout_p) if rnn_cell == 'LSTM': self.rnn_cell = nn.LSTM elif rnn_cell == 'GRU': self.rnn_cell = nn.GRU else: raise ValueError("Unsupported RNN Cell: {0}".format(rnn_cell)) self.variable_lengths = variable_lengths self.embedding = nn.Embedding(vocab_size, embed_size) if embedding is not None: self.embedding.weight = nn.Parameter(embedding) self.embedding.weight.requires_grad = update_embedding self.rnn = self.rnn_cell(embed_size, hidden_size, n_layers, batch_first=True, dropout=dropout_p)
Example #12
Source File: rnn.py From slot-filling with MIT License | 6 votes |
def __init__(self, vocab_size, label_size, mode='elman', bidirectional=False, cuda=False, is_training=True): super(SlotFilling, self).__init__() self.is_training = is_training embedding_dim = 100 hidden_size = 75 self.embedding = nn.Embedding(vocab_size, embedding_dim) if mode == 'lstm': self.rnn = nn.LSTM(input_size=embedding_dim, hidden_size=hidden_size, bidirectional=bidirectional, batch_first=True) else: self.rnn = RNN(input_size=embedding_dim, hidden_size=hidden_size, mode=mode, cuda=cuda, bidirectional=bidirectional, batch_first=True) if bidirectional: self.fc = nn.Linear(2*hidden_size, label_size) else: self.fc = nn.Linear(hidden_size, label_size)
Example #13
Source File: modules.py From BAMnet with Apache License 2.0 | 6 votes |
def __init__(self, vocab_size, embed_size, hidden_size, dropout=None, \ bidirectional=False, shared_embed=None, init_word_embed=None, rnn_type='lstm', use_cuda=True): super(EncoderRNN, self).__init__() if not rnn_type in ('lstm', 'gru'): raise RuntimeError('rnn_type is expected to be lstm or gru, got {}'.format(rnn_type)) if bidirectional: print('[ Using bidirectional {} encoder ]'.format(rnn_type)) else: print('[ Using {} encoder ]'.format(rnn_type)) if bidirectional and hidden_size % 2 != 0: raise RuntimeError('hidden_size is expected to be even in the bidirectional mode!') self.dropout = dropout self.rnn_type = rnn_type self.use_cuda = use_cuda self.hidden_size = hidden_size // 2 if bidirectional else hidden_size self.num_directions = 2 if bidirectional else 1 self.embed = shared_embed if shared_embed is not None else nn.Embedding(vocab_size, embed_size, padding_idx=0) model = nn.LSTM if rnn_type == 'lstm' else nn.GRU self.model = model(embed_size, self.hidden_size, 1, batch_first=True, bidirectional=bidirectional) if shared_embed is None: self.init_weights(init_word_embed)
Example #14
Source File: 51_rnn_sentiment.py From deep-learning-note with MIT License | 6 votes |
def forward(self, inputs): # inputs的形状是(批量大小, 词数),因为LSTM需要将序列长度(seq_len)作为第一维,所以将输入转置后 # 再提取词特征,输出形状为(词数, 批量大小, 词向量维度) embeddings = self.embedding(inputs.permute(1, 0)) # rnn.LSTM只传入输入embeddings,因此只返回最后一层的隐藏层在各时间步的隐藏状态。 # outputs形状是(词数, 批量大小, 2 * 隐藏单元个数) outputs, _ = self.encoder(embeddings) # output, (h, c) # 连结初始时间步和最终时间步的隐藏状态作为全连接层输入。它的形状为 # (批量大小, 4 * 隐藏单元个数)。 encoding = torch.cat((outputs[0], outputs[-1]), -1) outs = self.decoder(encoding) return outs
Example #15
Source File: aggregator_predict.py From SQL_Database_Optimization with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, N_word, N_h, N_depth, use_ca): super(AggPredictor, self).__init__() self.use_ca = use_ca self.agg_lstm = nn.LSTM(input_size=N_word, hidden_size=N_h/2, num_layers=N_depth, batch_first=True, dropout=0.3, bidirectional=True) if use_ca: print "Using column attention on aggregator predicting" self.agg_col_name_enc = nn.LSTM(input_size=N_word, hidden_size=N_h/2, num_layers=N_depth, batch_first=True, dropout=0.3, bidirectional=True) self.agg_att = nn.Linear(N_h, N_h) else: print "Not using column attention on aggregator predicting" self.agg_att = nn.Linear(N_h, 1) self.agg_out = nn.Sequential(nn.Linear(N_h, N_h), nn.Tanh(), nn.Linear(N_h, 6)) self.softmax = nn.Softmax()
Example #16
Source File: seq2seq_atten.py From video_captioning_rl with MIT License | 6 votes |
def __init__(self, args): super(EncoderFrames, self).__init__() # self.use_abs = use_abs self.vid_dim = args.vid_dim self.embed_size = args.embed self.hidden_dim = args.hid self.enable_cuda = args.cuda self.num_layers = args.num_layers self.args = args if args.birnn: self.birnn = 2 else: self.birnn = 1 # projection layer self.linear = nn.Linear(self.vid_dim, self.embed_size, bias=False) # video embedding self.rnn = nn.LSTM(self.embed_size, self.hidden_dim, self.num_layers, batch_first=True, bidirectional=self.args.birnn, dropout=args.dropout) self.dropout = nn.Dropout(args.dropout) self.init_weights()
Example #17
Source File: seq2seq_atten.py From video_captioning_rl with MIT License | 6 votes |
def __init__(self, args): """Set the hyper-parameters and build the layers.""" super(DecoderRNN, self).__init__() self.enable_cuda = args.cuda self.embed_size = args.embed self.hidden_size = args.hid self.vocab_size = args.max_vocab_size if args.birnn: self.birnn = 2 else: self.birnn = 1 self.num_layers = args.num_layers self.args = args self.input_proj = nn.Linear(self.birnn*self.hidden_size+self.embed_size, self.embed_size) self.embed = nn.Embedding(self.vocab_size, self.embed_size) self.atten = Attention(args, self.birnn*self.hidden_size, self.hidden_size) self.lstm = nn.LSTM(self.embed_size+self.birnn*self.hidden_size, self.hidden_size, self.num_layers, batch_first=True, dropout=args.dropout) self.linear = nn.Linear(self.hidden_size, self.vocab_size) self.init_weights()
Example #18
Source File: model.py From ConvLab with MIT License | 6 votes |
def whatCellType(input_size, hidden_size, cell_type, dropout_rate): if cell_type == 'rnn': cell = nn.RNN(input_size, hidden_size, dropout=dropout_rate, batch_first=False) init_gru(cell) return cell elif cell_type == 'gru': cell = nn.GRU(input_size, hidden_size, dropout=dropout_rate, batch_first=False) init_gru(cell) return cell elif cell_type == 'lstm': cell = nn.LSTM(input_size, hidden_size, dropout=dropout_rate, batch_first=False) init_lstm(cell) return cell elif cell_type == 'bigru': cell = nn.GRU(input_size, hidden_size, bidirectional=True, dropout=dropout_rate, batch_first=False) init_gru(cell) return cell elif cell_type == 'bilstm': cell = nn.LSTM(input_size, hidden_size, bidirectional=True, dropout=dropout_rate, batch_first=False) init_lstm(cell) return cell
Example #19
Source File: models.py From models with MIT License | 6 votes |
def __init__(self,n_bins,ip_bin_size,hm,args): super(recurrent_encoder,self).__init__() self.bin_rnn_size=args.bin_rnn_size self.ipsize=ip_bin_size self.seq_length=n_bins self.num_directions=2 if args.bidirectional else 1 if (hm==False): self.bin_rnn_size=args.bin_rnn_size else: self.bin_rnn_size=args.bin_rnn_size // 2 self.bin_rep_size=self.bin_rnn_size*self.num_directions self.rnn=nn.LSTM(self.ipsize,self.bin_rnn_size,num_layers=args.num_layers,dropout=args.dropout,bidirectional=args.bidirectional) self.bin_attention=rec_attention(hm,args)
Example #20
Source File: LSTMcuda.py From Action-Recognition with MIT License | 5 votes |
def __init__(self, hidden_dim=160, label_size=8, input_dim=48, num_layers = 2): super(LSTMClassifier, self).__init__() self.hiddenDim = hidden_dim self.layers = num_layers self.embedding = nn.Linear(input_dim, 32) self.lstm = nn.LSTM(input_size=32, hidden_size=hidden_dim, num_layers = num_layers) self.fullyConnected = nn.Linear(hidden_dim, label_size) self.hidden = self.init_hidden()
Example #21
Source File: LSTMcuda.py From Action-Recognition with MIT License | 5 votes |
def __init__(self, hidden_dim=160, label_size=8, input_dim=48, num_layers = 2): super(LSTMClassifier, self).__init__() self.hiddenDim = hidden_dim self.layers = num_layers self.embedding = nn.Linear(input_dim, 32) self.lstm = nn.LSTM(input_size=32, hidden_size=hidden_dim, num_layers = num_layers) self.fullyConnected = nn.Linear(hidden_dim, label_size) self.hidden = self.init_hidden()
Example #22
Source File: ImageEncoder.py From video-caption-openNMT.pytorch with MIT License | 5 votes |
def __init__(self, num_layers, bidirectional, rnn_size, dropout): super(ImageEncoder, self).__init__() self.num_layers = num_layers self.num_directions = 2 if bidirectional else 1 self.hidden_size = rnn_size self.layer1 = nn.Conv2d(3, 64, kernel_size=(3, 3), padding=(1, 1), stride=(1, 1)) self.layer2 = nn.Conv2d(64, 128, kernel_size=(3, 3), padding=(1, 1), stride=(1, 1)) self.layer3 = nn.Conv2d(128, 256, kernel_size=(3, 3), padding=(1, 1), stride=(1, 1)) self.layer4 = nn.Conv2d(256, 256, kernel_size=(3, 3), padding=(1, 1), stride=(1, 1)) self.layer5 = nn.Conv2d(256, 512, kernel_size=(3, 3), padding=(1, 1), stride=(1, 1)) self.layer6 = nn.Conv2d(512, 512, kernel_size=(3, 3), padding=(1, 1), stride=(1, 1)) self.batch_norm1 = nn.BatchNorm2d(256) self.batch_norm2 = nn.BatchNorm2d(512) self.batch_norm3 = nn.BatchNorm2d(512) input_size = 512 self.rnn = nn.LSTM(input_size, rnn_size, num_layers=num_layers, dropout=dropout, bidirectional=bidirectional) self.pos_lut = nn.Embedding(1000, input_size)
Example #23
Source File: LSTMcuda.py From Action-Recognition with MIT License | 5 votes |
def __init__(self, hidden_dim=160, label_size=8, input_dim=48, num_layers = 2): super(LSTMClassifier, self).__init__() self.hiddenDim = hidden_dim self.layers = num_layers self.embedding = nn.Linear(input_dim, 32) self.lstm = nn.LSTM(input_size=32, hidden_size=hidden_dim, num_layers = num_layers) self.fullyConnected = nn.Linear(hidden_dim, label_size) self.hidden = self.init_hidden()
Example #24
Source File: pytorch_lstm.py From deep-smoke-machine with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __init__(self): super().__init__() self.convolution = nn.Sequential( nn.Conv2d(in_channels=3, out_channels=96, kernel_size=7, stride=2), nn.MaxPool2d(kernel_size=3, stride=2), nn.ReLU(), nn.LocalResponseNorm(size=2), nn.Conv2d(in_channels=96, out_channels=256, kernel_size=5, stride=2), nn.MaxPool2d(kernel_size=3, stride=2), nn.ReLU(), nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, stride=1), nn.ReLU(), nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1), nn.ReLU(), nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1), nn.MaxPool2d(kernel_size=3, stride=2), nn.ReLU(), ) mock_input = torch.randn(8, 3, 224, 224) mock_output = self.convolution(mock_input) flattened_output = torch.flatten(mock_output, start_dim=1) self.num_nodes = flattened_output.shape[1] # Get number of nodes from flattened value's size, then convert 0 dim tensor to integer fc_in_dim = self.num_nodes self.lstm = nn.LSTM(self.num_nodes, self.num_nodes, num_layers=2) self.full_conn1 = nn.Linear(in_features=fc_in_dim, out_features=4096) self.full_conn2 = nn.Linear(in_features=4096, out_features=2048) self.full_conn3 = nn.Linear(in_features=2048, out_features=2) #self.full_conn3 = nn.Linear(in_features=4096, out_features=2)
Example #25
Source File: LSTM.py From Action-Recognition with MIT License | 5 votes |
def __init__(self, hidden_dim=64,label_size=49,modified_input_dim=64): super(LSTMClassifier, self).__init__() self.hidden_dim = hidden_dim self.fully_connected = nn.Sequential(nn.Linear(75, 70),nn.ReLU(),nn.Linear(70, 64),nn.ReLU()) self.lstm = nn.LSTM(input_size=modified_input_dim, hidden_size=hidden_dim) self.hidden2label = nn.Linear(hidden_dim, label_size) self.hidden = self.init_hidden()
Example #26
Source File: twoLSTM.py From Action-Recognition with MIT License | 5 votes |
def __init__(self, hidden_dim=128, label_size=49, input_dim=75, num_layers = 1): super(LSTMClassifier, self).__init__() self.hiddenDim = hidden_dim self.layers = num_layers self.embedding = nn.Linear(input_dim, 64) self.lstm = nn.LSTM(input_size=64, hidden_size=hidden_dim, num_layers = num_layers) self.fullyConnected = nn.Linear(hidden_dim, label_size) self.hidden = self.init_hidden()
Example #27
Source File: fourLSTMcuda.py From Action-Recognition with MIT License | 5 votes |
def __init__(self, hidden_dim=128, label_size=49, input_dim=75, num_layers = 1): super(LSTMClassifier, self).__init__() self.hiddenDim = hidden_dim self.layers = num_layers self.embedding = nn.Linear(input_dim, 64) self.lstm = nn.LSTM(input_size=64, hidden_size=hidden_dim, num_layers = num_layers) self.fullyConnected = nn.Linear(hidden_dim, label_size) self.hidden = self.init_hidden()
Example #28
Source File: basicLSTM.py From Action-Recognition with MIT License | 5 votes |
def __init__(self, hidden_dim=128, label_size=49, input_dim=75, num_layers = 1): super(LSTMClassifier, self).__init__() self.hiddenDim = hidden_dim self.layers = num_layers self.lstm = nn.LSTM(input_size=input_dim, hidden_size=hidden_dim, num_layers = num_layers) self.fullyConnected = nn.Linear(hidden_dim, label_size) self.hidden = self.init_hidden()
Example #29
Source File: LSTM2.py From Action-Recognition with MIT License | 5 votes |
def __init__(self, hidden_dim=64,label_size=49,modified_input_dim=64): super(LSTMClassifier, self).__init__() self.hidden_dim = hidden_dim self.fully_connected = nn.Sequential(nn.Linear(75, 70),nn.ReLU(),nn.Linear(70, 64),nn.ReLU()) self.lstm = nn.LSTM(input_size=modified_input_dim, hidden_size=hidden_dim) self.hidden2label = nn.Linear(hidden_dim, label_size) self.hidden = self.init_hidden()
Example #30
Source File: VideoEncoder.py From video-caption-openNMT.pytorch with MIT License | 5 votes |
def __init__(self, num_layers, bidirectional, rnn_size, dropout, dim_vid=2048): super(VideoEncoder, self).__init__() self.num_layers = num_layers self.num_directions = 2 if bidirectional else 1 self.dim_hidden = rnn_size // self.num_directions self.linear = nn.Linear(dim_vid, self.dim_hidden) self.dropout = nn.Dropout(dropout) self.rnn = nn.LSTM(self.dim_hidden, self.dim_hidden, num_layers=num_layers, dropout=dropout, bidirectional=bidirectional)