@@ -632,8 +632,8 @@ def batch2TrainData(voc, pair_batch):
632
632
#
633
633
# Finally, if passing a padded batch of sequences to an RNN module, we
634
634
# must pack and unpack padding around the RNN pass using
635
- # ``torch. nn.utils.rnn.pack_padded_sequence`` and
636
- # ``torch. nn.utils.rnn.pad_packed_sequence`` respectively.
635
+ # ``nn.utils.rnn.pack_padded_sequence`` and
636
+ # ``nn.utils.rnn.pad_packed_sequence`` respectively.
637
637
#
638
638
# **Computation Graph:**
639
639
#
@@ -679,11 +679,11 @@ def forward(self, input_seq, input_lengths, hidden=None):
679
679
# Convert word indexes to embeddings
680
680
embedded = self .embedding (input_seq )
681
681
# Pack padded batch of sequences for RNN module
682
- packed = torch . nn .utils .rnn .pack_padded_sequence (embedded , input_lengths )
682
+ packed = nn .utils .rnn .pack_padded_sequence (embedded , input_lengths )
683
683
# Forward pass through GRU
684
684
outputs , hidden = self .gru (packed , hidden )
685
685
# Unpack padding
686
- outputs , _ = torch . nn .utils .rnn .pad_packed_sequence (outputs )
686
+ outputs , _ = nn .utils .rnn .pad_packed_sequence (outputs )
687
687
# Sum bidirectional GRU outputs
688
688
outputs = outputs [:, :, :self .hidden_size ] + outputs [:, : ,self .hidden_size :]
689
689
# Return output and final hidden state
@@ -755,18 +755,18 @@ def forward(self, input_seq, input_lengths, hidden=None):
755
755
#
756
756
757
757
# Luong attention layer
758
- class Attn (torch . nn .Module ):
758
+ class Attn (nn .Module ):
759
759
def __init__ (self , method , hidden_size ):
760
760
super (Attn , self ).__init__ ()
761
761
self .method = method
762
762
if self .method not in ['dot' , 'general' , 'concat' ]:
763
763
raise ValueError (self .method , "is not an appropriate attention method." )
764
764
self .hidden_size = hidden_size
765
765
if self .method == 'general' :
766
- self .attn = torch . nn .Linear (self .hidden_size , hidden_size )
766
+ self .attn = nn .Linear (self .hidden_size , hidden_size )
767
767
elif self .method == 'concat' :
768
- self .attn = torch . nn .Linear (self .hidden_size * 2 , hidden_size )
769
- self .v = torch . nn .Parameter (torch .FloatTensor (hidden_size ))
768
+ self .attn = nn .Linear (self .hidden_size * 2 , hidden_size )
769
+ self .v = nn .Parameter (torch .FloatTensor (hidden_size ))
770
770
771
771
def dot_score (self , hidden , encoder_output ):
772
772
return torch .sum (hidden * encoder_output , dim = 2 )
@@ -1021,8 +1021,8 @@ def train(input_variable, lengths, target_variable, mask, max_target_len, encode
1021
1021
loss .backward ()
1022
1022
1023
1023
# Clip gradients: gradients are modified in place
1024
- _ = torch . nn .utils .clip_grad_norm_ (encoder .parameters (), clip )
1025
- _ = torch . nn .utils .clip_grad_norm_ (decoder .parameters (), clip )
1024
+ _ = nn .utils .clip_grad_norm_ (encoder .parameters (), clip )
1025
+ _ = nn .utils .clip_grad_norm_ (decoder .parameters (), clip )
1026
1026
1027
1027
# Adjust model weights
1028
1028
encoder_optimizer .step ()
0 commit comments