Skip to content

Commit

Permalink
fixed: LSTM2D: replace tensor.mod() with tensor.switch() to prevent '…
Browse files Browse the repository at this point in the history
…float64' exception
  • Loading branch information
david-leon committed Nov 6, 2018
1 parent f0e7e71 commit 2939f09
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 3 additions & 2 deletions dandelion/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,8 @@ class LSTM2D(Module):
.step() can be used as LSTM2DCell by setting `process_input=True`
h_ini will be learned during training.
Note:
for current implementation, empirical tests show that 1) LSTM2D use much more memories than LSTM, exponentially with H*W; 2) LSTM2D speed much slower than LSTM
for current implementation, empirical tests show that 1) LSTM2D use much more memories than LSTM (5G vs 0.2G), exponentially with H*W;
2) LSTM2D speed much slower than LSTM (24s vs 6s)
"""

def __init__(self, input_dims, hidden_dim, peephole=True, initializer=init.Normal(0.1), grad_clipping=0, hidden_activation=tanh,
Expand Down Expand Up @@ -1575,7 +1576,7 @@ def step(self, input, h_left, c_left, x_pos, h_buffer, c_buffer, width, mask=Non
h_buffer = tensor.set_subtensor(h_buffer[x_pos, :, :], h)
c_buffer = tensor.set_subtensor(c_buffer[x_pos, :, :], c)
x_pos = x_pos + 1
x_pos = tensor.mod(x_pos, width)
x_pos = tensor.switch(x_pos >= width, 0, x_pos)
return h, c, x_pos, h_buffer, c_buffer

def forward(self, seq_input, h_ini=None, c_ini=None, seq_mask=None, backward=False, only_return_final=False, return_final_state=False):
Expand Down
8 changes: 6 additions & 2 deletions test/test_LSTM2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
# ------------------------------------------------------------------------------------------------
__author__ = 'dawei.leng'

import os
os.environ['THEANO_FLAGS'] = "floatX=float32, mode=FAST_RUN, warn_float64='raise'"

import theano
theano.config.floatX = 'float32'
floatX = theano.config.floatX
import theano.tensor as tensor
from dandelion.module import LSTM2D
from dandelion.objective import *
from dandelion.update import *
import numpy as np, time

import dandelion
dandelion_path = os.path.split(dandelion.__file__)[0]
print('dandelion path = %s\n' % dandelion_path)

def test_case_0():
# input_dim, hidden_dim, B, H, W = 5, 4, 2, 5, 7
Expand Down

0 comments on commit 2939f09

Please sign in to comment.