Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
srush committed Dec 22, 2017
1 parent 9993472 commit ee90695
Show file tree
Hide file tree
Showing 16 changed files with 180 additions and 173 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ matrix:
include:
- env: LINT_CHECK
python: "2.7"
install: pip install flake8
install: pip install flake8 pep8-naming
script: flake8
6 changes: 3 additions & 3 deletions onmt/Models.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,9 @@ def beam_update(self, idx, positions, beam_size):
""" Update when beam advances. """
for e in self._all:
a, br, d = e.size()
sentStates = e.view(a, beam_size, br // beam_size, d)[:, :, idx]
sentStates.data.copy_(
sentStates.data.index_select(1, positions))
sent_states = e.view(a, beam_size, br // beam_size, d)[:, :, idx]
sent_states.data.copy_(
sent_states.data.index_select(1, positions))


class RNNDecoderState(DecoderState):
Expand Down
4 changes: 2 additions & 2 deletions onmt/Optim.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self, method, lr, max_grad_norm,
self.adagrad_accum = adagrad_accum
self.opt = opt

def _setRate(self, lr):
def _set_rate(self, lr):
self.lr = lr
self.optimizer.param_groups[0]['lr'] = self.lr

Expand All @@ -69,7 +69,7 @@ def step(self):
clip_grad_norm(self.params, self.max_grad_norm)
self.optimizer.step()

def updateLearningRate(self, ppl, epoch):
def update_learning_rate(self, ppl, epoch):
"""
Decay learning rate if val perf does not improve
or we hit the start_decay_at limit.
Expand Down
12 changes: 6 additions & 6 deletions onmt/io/IO.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
EOS_WORD = '</s>'


def __getstate__(self):
def _getstate(self):
return dict(self.__dict__, stoi=dict(self.stoi))


def __setstate__(self, state):
def _setstate(self, state):
self.__dict__.update(state)
self.stoi = defaultdict(lambda: 0, self.stoi)


torchtext.vocab.Vocab.__getstate__ = __getstate__
torchtext.vocab.Vocab.__setstate__ = __setstate__
torchtext.vocab.Vocab.__getstate__ = _getstate
torchtext.vocab.Vocab.__setstate__ = _setstate


def get_fields(data_type, n_src_features, n_tgt_features):
Expand Down Expand Up @@ -478,9 +478,9 @@ def _read_audio_file(path, src_dir, side, sample_rate, window_size,
win_length = n_fft
hop_length = int(sample_rate * window_stride)
# STFT
D = librosa.stft(sound, n_fft=n_fft, hop_length=hop_length,
d = librosa.stft(sound, n_fft=n_fft, hop_length=hop_length,
win_length=win_length, window=window)
spect, _ = librosa.magphase(D)
spect, _ = librosa.magphase(d)
spect = np.log1p(spect)
spect = torch.FloatTensor(spect)
if normalize_audio:
Expand Down
2 changes: 1 addition & 1 deletion onmt/modules/ConvMultiStepAttention.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, input_size):
self.linear_in = nn.Linear(input_size, input_size)
self.mask = None

def applyMask(self, mask):
def apply_mask(self, mask):
self.mask = mask

def forward(self, base_target_emb, input, encoder_out_top,
Expand Down
4 changes: 2 additions & 2 deletions onmt/modules/Gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import torch.nn as nn


def ContextGateFactory(type, embeddings_size, decoder_size,
attention_size, output_size):
def context_gate_factory(type, embeddings_size, decoder_size,
attention_size, output_size):
"""Returns the correct ContextGate class"""

gate_types = {'source': SourceContextGate,
Expand Down
6 changes: 3 additions & 3 deletions onmt/modules/ImageEncoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def load_pretrained_vectors(self, opt):
pass

def forward(self, input, lengths=None):
batchSize = input.size(0)
batch_size = input.size(0)
# (batch_size, 64, imgH, imgW)
# layer 1
input = F.relu(self.layer1(input[:, :, :, :]-0.5), True)
Expand Down Expand Up @@ -93,8 +93,8 @@ def forward(self, input, lengths=None):
for row in range(input.size(2)):
inp = input[:, :, row, :].transpose(0, 2)\
.transpose(1, 2)
row_vec = torch.Tensor(batchSize).type_as(inp.data)\
.long().fill_(row)
row_vec = torch.Tensor(batch_size).type_as(inp.data)\
.long().fill_(row)
pos_emb = self.pos_lut(Variable(row_vec))
with_pos = torch.cat(
(pos_emb.view(1, pos_emb.size(0), pos_emb.size(1)), inp), 0)
Expand Down
2 changes: 2 additions & 0 deletions onmt/modules/SRU.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
This implementation is adpoted from the author of the paper:
https://github.com/taolei87/sru/blob/master/cuda_functional.py.
"""
# flake8: noqa

import subprocess
import platform
import os
Expand Down
26 changes: 13 additions & 13 deletions onmt/modules/WeightNorm.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ def forward(self, x, init=False):
self.V.data.copy_(torch.randn(self.V.data.size()).type_as(
self.V.data) * 0.05)
# norm is out_features * 1
V_norm = self.V.data / \
v_norm = self.V.data / \
self.V.data.norm(2, 1).expand_as(self.V.data)
# batch_size * out_features
x_init = F.linear(x, Variable(V_norm)).data
x_init = F.linear(x, Variable(v_norm)).data
# out_features
m_init, v_init = x_init.mean(0).squeeze(
0), x_init.var(0).squeeze(0)
Expand Down Expand Up @@ -119,10 +119,10 @@ def forward(self, x, init=False):
# out_channels, in_channels // groups, * kernel_size
self.V.data.copy_(torch.randn(self.V.data.size()
).type_as(self.V.data) * 0.05)
V_norm = self.V.data / self.V.data.view(self.out_channels, -1)\
v_norm = self.V.data / self.V.data.view(self.out_channels, -1)\
.norm(2, 1).view(self.out_channels, *(
[1] * (len(self.kernel_size) + 1))).expand_as(self.V.data)
x_init = F.conv2d(x, Variable(V_norm), None, self.stride,
x_init = F.conv2d(x, Variable(v_norm), None, self.stride,
self.padding, self.dilation, self.groups).data
t_x_init = x_init.transpose(0, 1).contiguous().view(
self.out_channels, -1)
Expand All @@ -144,20 +144,20 @@ def forward(self, x, init=False):
self.b_avg.copy_(self.b.data)
return Variable(x_init)
else:
V, g, b = get_vars_maybe_avg(
v, g, b = get_vars_maybe_avg(
self, ['V', 'g', 'b'], self.training,
polyak_decay=self.polyak_decay)

scalar = torch.norm(V.view(self.out_channels, -1), 2, 1)
scalar = torch.norm(v.view(self.out_channels, -1), 2, 1)
if len(scalar.size()) == 2:
scalar = g / scalar.squeeze(1)
else:
scalar = g / scalar

W = scalar.view(self.out_channels, *
([1] * (len(V.size()) - 1))).expand_as(V) * V
w = scalar.view(self.out_channels, *
([1] * (len(v.size()) - 1))).expand_as(v) * v

x = F.conv2d(x, W, b, self.stride,
x = F.conv2d(x, w, b, self.stride,
self.padding, self.dilation, self.groups)
return x

Expand Down Expand Up @@ -192,12 +192,12 @@ def forward(self, x, init=False):
# in_channels, out_channels, *kernel_size
self.V.data.copy_(torch.randn(self.V.data.size()).type_as(
self.V.data) * 0.05)
V_norm = self.V.data / self.V.data.transpose(0, 1).contiguous() \
v_norm = self.V.data / self.V.data.transpose(0, 1).contiguous() \
.view(self.out_channels, -1).norm(2, 1).view(
self.in_channels, self.out_channels,
*([1] * len(self.kernel_size))).expand_as(self.V.data)
x_init = F.conv_transpose2d(
x, Variable(V_norm), None, self.stride,
x, Variable(v_norm), None, self.stride,
self.padding, self.output_padding, self.groups).data
# self.out_channels, 1
t_x_init = x_init.tranpose(0, 1).contiguous().view(
Expand Down Expand Up @@ -228,10 +228,10 @@ def forward(self, x, init=False):
scalar = g / \
torch.norm(V.transpose(0, 1).contiguous().view(
self.out_channels, -1), 2, 1).squeeze(1)
W = scalar.view(self.in_channels, self.out_channels,
w = scalar.view(self.in_channels, self.out_channels,
*([1] * (len(V.size()) - 2))).expand_as(V) * V

x = F.conv_transpose2d(x, W, b, self.stride,
x = F.conv_transpose2d(x, w, b, self.stride,
self.padding, self.output_padding,
self.groups)
return x
6 changes: 3 additions & 3 deletions onmt/modules/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from onmt.modules.UtilClass import LayerNorm, Bottle, BottleLinear, \
BottleLayerNorm, BottleSoftmax, Elementwise
from onmt.modules.Gate import ContextGateFactory
from onmt.modules.Gate import context_gate_factory
from onmt.modules.GlobalAttention import GlobalAttention
from onmt.modules.ConvMultiStepAttention import ConvMultiStepAttention
from onmt.modules.ImageEncoder import ImageEncoder
Expand All @@ -25,8 +25,8 @@
LayerNorm, Bottle, BottleLinear, BottleLayerNorm, BottleSoftmax,
TransformerEncoder, TransformerDecoder, Embeddings, Elementwise,
MatrixTree, WeightNormConv2d, ConvMultiStepAttention,
CNNEncoder, CNNDecoder, StackedLSTM, StackedGRU, ContextGateFactory,
CopyGeneratorLossCompute, AudioEncoder]
CNNEncoder, CNNDecoder, StackedLSTM, StackedGRU,
context_gate_factory, CopyGeneratorLossCompute, AudioEncoder]

if can_use_sru:
__all__.extend([SRU, check_sru_requirement])

0 comments on commit ee90695

Please sign in to comment.