3131from 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
102106class 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"]):
211216if __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