-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathincremental_recurrence_chaplot_module.py
50 lines (42 loc) · 1.86 KB
/
incremental_recurrence_chaplot_module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import numpy as np
import torch
import torch.nn as nn
from torch.autograd import Variable
from utils.cuda import cuda_tensor
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
weight_shape = list(m.weight.data.size())
fan_in = np.prod(weight_shape[1:4])
fan_out = np.prod(weight_shape[2:4]) * weight_shape[0]
w_bound = np.sqrt(6. / (fan_in + fan_out))
m.weight.data.uniform_(-w_bound, w_bound)
m.bias.data.fill_(0)
elif classname.find('Linear') != -1:
weight_shape = list(m.weight.data.size())
fan_in = weight_shape[1]
fan_out = weight_shape[0]
w_bound = np.sqrt(6. / (fan_in + fan_out))
m.weight.data.uniform_(-w_bound, w_bound)
m.bias.data.fill_(0)
class IncrementalRecurrenceChaplotModule(nn.Module):
def __init__(self, input_emb_dim, output_emb_dim):
super(IncrementalRecurrenceChaplotModule, self).__init__()
self.input_emb_dim = input_emb_dim
self.output_emb_dim = output_emb_dim
self.lstm = nn.LSTMCell(input_emb_dim, output_emb_dim)
def init_weights(self):
self.apply(weights_init)
self.lstm.bias_ih.data.fill_(0)
self.lstm.bias_hh.data.fill_(0)
def forward(self, input_vector, hidden_vectors):
"""
@param image_vector: batch of sequence of image embedding
@param hidden_vectors: hidden vectors for each batch """
if hidden_vectors is None:
dims = (1, self.output_emb_dim)
hidden_vectors = (Variable(cuda_tensor(torch.zeros(*dims)), requires_grad=False),
Variable(cuda_tensor(torch.zeros(*dims)), requires_grad=False))
new_hidden_vector = self.lstm(input_vector, hidden_vectors)
return new_hidden_vector