Skip to content

Commit b4ded40

Browse files
committed
Spacing change only.
1 parent 45d1f2f commit b4ded40

6 files changed

Lines changed: 234 additions & 226 deletions

File tree

data_loaders/PrepareData.py

Lines changed: 83 additions & 80 deletions
Large diffs are not rendered by default.

models/BiLSTM.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class BiLSTM(nn.Module):
3434
Class for Bidirectional LSTM operations.
3535
"""
3636

37-
def __init__(self, batch_size=config["sampling"]["batch_size"],
37+
def __init__(self,batch_size=config["sampling"]["batch_size"],
3838
use_cuda=config["model"]["use_cuda"],
3939
bidirectional=config["lstm_params"]["bidirectional"],
4040
input_size=config["prep_vecs"]["input_size"],
@@ -54,7 +54,7 @@ def __init__(self, batch_size=config["sampling"]["batch_size"],
5454
and if there is only one layer, dropout will not be used.
5555
:param bidirectional: If True, becomes a bidirectional RNN. Default: False
5656
"""
57-
super(BiLSTM, self).__init__()
57+
super(BiLSTM,self).__init__()
5858
self.use_cuda = use_cuda
5959
self.bidirectional = bidirectional
6060
self.batch_size = batch_size
@@ -93,7 +93,7 @@ def __init__(self, batch_size=config["sampling"]["batch_size"],
9393
# logger.debug(self.lstm.weight_ih_l0_reverse)
9494
# logger.debug(self.lstm._all_weights)
9595

96-
def init_hid(self, batch_size=config["sampling"]["batch_size"], requires_grad=True, r1=-1, r2=1, num_directions=2):
96+
def init_hid(self,batch_size=config["sampling"]["batch_size"],requires_grad=True,r1=-1,r2=1,num_directions=2):
9797
"""
9898
Generates h0 and c0 for LSTM initialization with values range from r1 to r2.
9999
@@ -108,18 +108,18 @@ def init_hid(self, batch_size=config["sampling"]["batch_size"], requires_grad=Tr
108108
num_directions = 1
109109
# r1, r2 = -1, 1 ## To generate numbers in range(-1,1)
110110
if self.use_cuda and torch.cuda.is_available():
111-
cell_init = Variable((r2 - r1) * torch.rand(self.lstm.num_layers * num_directions, batch_size,
112-
self.lstm.hidden_size) - r2, requires_grad=requires_grad).cuda()
113-
hid_init = Variable((r2 - r1) * torch.rand(self.lstm.num_layers * num_directions, batch_size,
114-
self.lstm.hidden_size) - r2, requires_grad=requires_grad).cuda()
111+
cell_init = Variable((r2 - r1) * torch.rand(self.lstm.num_layers * num_directions,batch_size,
112+
self.lstm.hidden_size) - r2,requires_grad=requires_grad).cuda()
113+
hid_init = Variable((r2 - r1) * torch.rand(self.lstm.num_layers * num_directions,batch_size,
114+
self.lstm.hidden_size) - r2,requires_grad=requires_grad).cuda()
115115
else:
116-
cell_init = Variable((r2 - r1) * torch.rand(self.lstm.num_layers * num_directions, batch_size,
117-
self.lstm.hidden_size) - r2, requires_grad=requires_grad)
118-
hid_init = Variable((r2 - r1) * torch.rand(self.lstm.num_layers * num_directions, batch_size,
119-
self.lstm.hidden_size) - r2, requires_grad=requires_grad)
120-
return hid_init, cell_init
116+
cell_init = Variable((r2 - r1) * torch.rand(self.lstm.num_layers * num_directions,batch_size,
117+
self.lstm.hidden_size) - r2,requires_grad=requires_grad)
118+
hid_init = Variable((r2 - r1) * torch.rand(self.lstm.num_layers * num_directions,batch_size,
119+
self.lstm.hidden_size) - r2,requires_grad=requires_grad)
120+
return hid_init,cell_init
121121

122-
def forward(self, inputs, training=True, requires_grad=True, dropout=config["model"]["dropout"],
122+
def forward(self,inputs,training=True,requires_grad=True,dropout=config["model"]["dropout"],
123123
dropout_external=config["model"]["dropout_external"]):
124124
"""
125125
Runs the bidirectional LSTM, produces outputs and hidden states.
@@ -145,23 +145,23 @@ def forward(self, inputs, training=True, requires_grad=True, dropout=config["mod
145145
:param inputs: The inputs should be a list of shape (seq_len, batch, input_size).
146146
:return: Returns the LSTM outputs: (seq_len, batch, num_directions * hidden_size), as well as the cell state (cn: (num_layers * num_directions, batch, hidden_size)) and final hidden representations (hn: (num_layers * num_directions, batch, hidden_size)).
147147
"""
148-
(h0, c0) = self.init_hid(self.batch_size, requires_grad=requires_grad)
148+
(h0,c0) = self.init_hid(self.batch_size,requires_grad=requires_grad)
149149
# logger.debug("self.hidden 1: {}".format(self.hidden))
150-
outputs, (hn, cn) = self.lstm(inputs, (h0, c0))
150+
outputs,(hn,cn) = self.lstm(inputs,(h0,c0))
151151

152152
if dropout_external and dropout > 0.0: ## Need to use dropout externally as Pytorch LSTM applies dropout only on
153153
## last layer and if there is only one layer, dropout will not be applied.
154154
logger.info("Applying dropout externally.")
155-
outputs = F.dropout(outputs, p=dropout, training=training, inplace=False)
155+
outputs = F.dropout(outputs,p=dropout,training=training,inplace=False)
156156
# assert input.shape == text_lstm.shape, "Input {} and Output {} shape should match.".format(input.shape, text_lstm.shape)
157-
return outputs, hn
157+
return outputs,hn
158158

159159

160160
if __name__ == '__main__':
161-
test_blstm = BiLSTM(input_size=3, hid_size=2, batch_size=2, num_layers=1, dropout=0.2, use_cuda=False,
161+
test_blstm = BiLSTM(input_size=3,hid_size=2,batch_size=2,num_layers=1,dropout=0.2,use_cuda=False,
162162
bidirectional=True)
163163
print(test_blstm)
164-
input = torch.rand(2, 1, 3) ## (batch_size, seq_size, input_size)
164+
input = torch.rand(2,1,3) ## (batch_size, seq_size, input_size)
165165
logger.debug("input: {}".format(input))
166166
logger.debug("input Shape: {}".format(input.shape))
167167
result = test_blstm.forward(input,
@@ -171,6 +171,6 @@ def forward(self, inputs, training=True, requires_grad=True, dropout=config["mod
171171
logger.debug("result: {}".format(result[0].shape))
172172
logger.debug("result: {}".format(result[1][0].shape))
173173
logger.debug("result: {}".format(result[1][1].shape))
174-
result = test_blstm.forward(input, dropout_external=True)
174+
result = test_blstm.forward(input,dropout_external=True)
175175
logger.debug("result: {}".format(result[0]))
176176
logger.debug("result: {}".format(result[0].shape))

models/EmbedText.py

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@
3131
from config import configuration as config
3232

3333

34-
def CNN_layer(in_planes, out_planes, kernel_size=config["cnn_params"]["kernel_size"],
35-
stride=config["cnn_params"]["stride"], padding=config["cnn_params"]["padding"],
36-
bias=config["cnn_params"]["bias"], dropout=config["model"]["dropout"]):
34+
def CNN_layer(in_planes,out_planes,kernel_size=config["cnn_params"]["kernel_size"],
35+
stride=config["cnn_params"]["stride"],padding=config["cnn_params"]["padding"],
36+
bias=config["cnn_params"]["bias"],dropout=config["model"]["dropout"]):
3737
"""Convolution with padding to get embeddings for input pre-trained sentences.
3838
3939
Default: Convolution = 1 x 1 with stride = 2.
4040
"""
4141
seq = nn.Sequential(
42-
nn.Conv1d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, bias=bias, padding=padding),
42+
nn.Conv1d(in_planes,out_planes,kernel_size=kernel_size,stride=stride,bias=bias,padding=padding),
4343
nn.BatchNorm1d(out_planes),
4444
nn.ReLU(True),
45-
nn.MaxPool1d(kernel_size=kernel_size, stride=stride + 1)
45+
nn.MaxPool1d(kernel_size=kernel_size,stride=stride + 1)
4646
)
4747
if dropout > 0.0: ## Add dropout module
4848
list_seq = list(seq.modules())[1:]
@@ -52,12 +52,16 @@ def CNN_layer(in_planes, out_planes, kernel_size=config["cnn_params"]["kernel_si
5252
return seq
5353

5454

55-
def init_hid(bidirectional=config["lstm_params"]["bidirectional"], batch_size=config["sampling"]["batch_size"],
56-
num_layers=config["lstm_params"]["num_layers"], hid_size=config["lstm_params"]["hid_size"],
57-
requires_grad=True, low_val=-1, high_val=1,use_cuda=config["model"]["use_cuda"]):
55+
def init_hid(bidirectional=config["lstm_params"]["bidirectional"],batch_size=config["sampling"]["batch_size"],
56+
num_layers=config["lstm_params"]["num_layers"],hid_size=config["lstm_params"]["hid_size"],
57+
requires_grad=True,low_val=-1,high_val=1,use_cuda=config["model"]["use_cuda"]):
5858
"""
5959
Generates h0 and c0 for LSTM initialization with values range from r1 to r2.
6060
61+
:param use_cuda:
62+
:param num_layers:
63+
:param hid_size:
64+
:param bidirectional:
6165
:param high_val: Max value of the range
6266
:param low_val: Min value of the range
6367
:param batch_size:
@@ -70,41 +74,42 @@ def init_hid(bidirectional=config["lstm_params"]["bidirectional"], batch_size=co
7074
# r1, r2 = -1, 1 ## To generate numbers in range(-1,1)
7175
if use_cuda and torch.cuda.is_available():
7276
cell_init = Variable((high_val - low_val)
73-
* torch.rand(num_layers * num_directions, batch_size, hid_size)
74-
- high_val, requires_grad=requires_grad).cuda()
77+
* torch.rand(num_layers * num_directions,batch_size,hid_size)
78+
- high_val,requires_grad=requires_grad).cuda()
7579
hid_init = Variable((high_val - low_val)
76-
* torch.rand(num_layers * num_directions, batch_size, hid_size)
77-
- high_val, requires_grad=requires_grad).cuda()
80+
* torch.rand(num_layers * num_directions,batch_size,hid_size)
81+
- high_val,requires_grad=requires_grad).cuda()
7882
else:
7983
cell_init = Variable((high_val - low_val)
80-
* torch.rand(num_layers * num_directions, batch_size, hid_size)
81-
- high_val, requires_grad=requires_grad)
84+
* torch.rand(num_layers * num_directions,batch_size,hid_size)
85+
- high_val,requires_grad=requires_grad)
8286
hid_init = Variable((high_val - low_val)
83-
* torch.rand(num_layers * num_directions, batch_size, hid_size)
84-
- high_val, requires_grad=requires_grad)
85-
return hid_init, cell_init
87+
* torch.rand(num_layers * num_directions,batch_size,hid_size)
88+
- high_val,requires_grad=requires_grad)
89+
return hid_init,cell_init
8690

8791

88-
def LSTM_layer(bidirectional=config["lstm_params"]["bidirectional"], dropout=config["model"]["dropout"],
89-
input_size=config["prep_vecs"]["input_size"], batch_first=config["lstm_params"]["batch_first"],
90-
num_layers=config["lstm_params"]["num_layers"], hid_size=config["lstm_params"]["hid_size"],
92+
def LSTM_layer(bidirectional=config["lstm_params"]["bidirectional"],dropout=config["model"]["dropout"],
93+
input_size=config["prep_vecs"]["input_size"],batch_first=config["lstm_params"]["batch_first"],
94+
num_layers=config["lstm_params"]["num_layers"],hid_size=config["lstm_params"]["hid_size"],
9195
bias=config["lstm_params"]["bias"]):
9296
"""Convolution with padding to get embeddings for input pre-trained sentences.
9397
9498
Default:
9599
"""
96-
lstm = nn.LSTM(input_size=input_size, hidden_size=hid_size, dropout=dropout, bias=bias, num_layers=num_layers,
97-
batch_first=batch_first, bidirectional=bidirectional)
100+
lstm = nn.LSTM(input_size=input_size,hidden_size=hid_size,dropout=dropout,bias=bias,num_layers=num_layers,
101+
batch_first=batch_first,bidirectional=bidirectional)
98102

99103
return lstm
100104

101105

102106
class EmbedText(nn.Module):
103107
"""Builds context sensitive embeddings for pre-trained sentences using either LSTM or CNN."""
104108

105-
def __init__(self, num_channels, layer_size, classify_count=config["model"]["classify_count"], requires_grad=True,
106-
hid_size=config["lstm_params"]["hid_size"], g_encoder=config["model"]["g_encoder"], use_cuda=config["model"]["use_cuda"],
107-
input_size=config["prep_vecs"]["input_size"], bidirectional=config["lstm_params"]["bidirectional"]):
109+
def __init__(self,num_channels,layer_size,classify_count=config["model"]["classify_count"],requires_grad=True,
110+
hid_size=config["lstm_params"]["hid_size"],g_encoder=config["model"]["g_encoder"],
111+
use_cuda=config["model"]["use_cuda"],
112+
input_size=config["prep_vecs"]["input_size"],bidirectional=config["lstm_params"]["bidirectional"]):
108113
"""
109114
Builds embeddings for pre-trained sentences using either LSTM or CNN.
110115
@@ -114,7 +119,7 @@ def __init__(self, num_channels, layer_size, classify_count=config["model"]["cla
114119
:param num_channels: Number of channels of samples
115120
:param useDroput: use Dropout with p=0.1 in each Conv block
116121
"""
117-
super(EmbedText, self).__init__()
122+
super(EmbedText,self).__init__()
118123
self.g_encoder = g_encoder
119124
self.use_cuda = use_cuda
120125

@@ -125,9 +130,9 @@ def __init__(self, num_channels, layer_size, classify_count=config["model"]["cla
125130
# logger.debug((self.hidden))
126131
self.output_size = hid_size * 2 ## 2 because bidirectional.
127132
elif self.g_encoder == "cnn":
128-
logger.debug((num_channels, layer_size))
129-
self.conv1 = CNN_layer(num_channels, layer_size)
130-
self.conv2 = CNN_layer(layer_size, layer_size)
133+
logger.debug((num_channels,layer_size))
134+
self.conv1 = CNN_layer(num_channels,layer_size)
135+
self.conv2 = CNN_layer(layer_size,layer_size)
131136
# self.conv3 = CNN_layer(layer_size, layer_size)
132137
# self.conv4 = CNN_layer(layer_size, layer_size)
133138

@@ -140,7 +145,7 @@ def __init__(self, num_channels, layer_size, classify_count=config["model"]["cla
140145

141146
if classify_count > 0: ## We want a linear layer as last layer of CNN network.
142147
self.use_linear_last = True
143-
self.last_linear_layer = nn.Linear(self.output_size, classify_count)
148+
self.last_linear_layer = nn.Linear(self.output_size,classify_count)
144149
self.output_size = classify_count
145150
else:
146151
self.use_linear_last = False
@@ -158,26 +163,26 @@ def __init__(self, num_channels, layer_size, classify_count=config["model"]["cla
158163
# self.weights_init(self.conv3)
159164
# self.weights_init(self.conv4)
160165

161-
def weights_init(self, module):
166+
def weights_init(self,module):
162167
"""
163168
Initialize weights to all the layers of the Network.
164169
:param module:
165170
"""
166171
for m in module.modules():
167-
if isinstance(m, nn.Conv2d):
168-
init.xavier_uniform_(m.weight, gain=np.sqrt(2))
169-
init.constant_(m.bias, 0)
170-
elif isinstance(m, nn.BatchNorm2d):
172+
if isinstance(m,nn.Conv2d):
173+
init.xavier_uniform_(m.weight,gain=np.sqrt(2))
174+
init.constant_(m.bias,0)
175+
elif isinstance(m,nn.BatchNorm2d):
171176
m.weight.data.fill_(1)
172177
m.bias.data.zero_()
173-
elif isinstance(m, nn.Conv1d):
174-
init.xavier_uniform_(m.weight, gain=np.sqrt(2))
175-
init.constant_(m.bias, 0)
176-
elif isinstance(m, nn.BatchNorm1d):
178+
elif isinstance(m,nn.Conv1d):
179+
init.xavier_uniform_(m.weight,gain=np.sqrt(2))
180+
init.constant_(m.bias,0)
181+
elif isinstance(m,nn.BatchNorm1d):
177182
m.weight.data.fill_(1)
178183
m.bias.data.zero_()
179184

180-
def forward(self, inputs, dropout_external=config["model"]["dropout_external"]):
185+
def forward(self,inputs,dropout_external=config["model"]["dropout_external"]):
181186
"""
182187
Runs the CNNText producing the embeddings and the gradients.
183188
@@ -186,7 +191,7 @@ def forward(self, inputs, dropout_external=config["model"]["dropout_external"]):
186191
:return: Embeddings of size [batch_size, 64]
187192
"""
188193
if self.g_encoder == "lstm":
189-
output, _ = self.lstm(inputs, self.hidden)
194+
output,_ = self.lstm(inputs,self.hidden)
190195
elif self.g_encoder == "cnn":
191196
output = self.conv1(inputs)
192197
output = self.conv2(output)
@@ -211,9 +216,9 @@ def forward(self, inputs, dropout_external=config["model"]["dropout_external"]):
211216
if __name__ == '__main__':
212217
import torch
213218

214-
a = torch.ones(1, 2, 4) ## batch_size, <don't matter>, input_size
219+
a = torch.ones(1,2,4) ## batch_size, <don't matter>, input_size
215220
logger.debug(a)
216-
cls = EmbedText(input_size=4, layer_size=1, classify_count=2, g_encoder="lstm")
217-
sim = cls.forward(a, batch_size=1, dropout_external=True)
221+
cls = EmbedText(input_size=4,layer_size=1,classify_count=2,g_encoder="lstm")
222+
sim = cls.forward(a,batch_size=1,dropout_external=True)
218223
logger.debug(sim)
219224
logger.debug(sim.shape)

0 commit comments

Comments
 (0)