Skip to content

Commit

Permalink
Merge pull request #7 from carriepl/scan_nonseq_grad
Browse files Browse the repository at this point in the history
Add test case for the nonseq grad fix
  • Loading branch information
lamblin committed Feb 27, 2015
2 parents 1a6b407 + 2d8b62e commit 0582999
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions theano/scan_module/tests/test_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import numpy
from nose.plugins.skip import SkipTest
from nose.plugins.attrib import attr
from nose.tools import assert_raises
from numpy.testing import dec

import theano
Expand Down Expand Up @@ -3022,6 +3023,42 @@ def test_outputs_info_not_typed(self):
assert out[4] == 19
# 19.0

def test_crash_nonseq_grad(self):
# Test case was originally reported by Bitton Tenessi. It crashed
# during the grad operation and this tests validates that it now
# raises a NullTypeGradError instead because the gradient relies on
# the intermediary states of the random number generators used in the
# test. The test case was modified from the original for simplicity

rand_stream = tensor.shared_randomstreams.RandomStreams()
inp = tensor.matrix()
norm_inp = inp / tensor.sum(inp, axis=0)

def unit_dropout(out_idx):
def stochastic_pooling(in_idx):
# sample the input matrix for each column according to the
# column values
n = tensor.ones((inp.shape[1],))
pvals = norm_inp.T
pool_idx = rand_stream.multinomial(n=n, pvals=pvals)
pooled_row = inp.T[tensor.nonzero(pool_idx)]
return pooled_row.flatten()

pooled, updates_inner = theano.scan(fn=stochastic_pooling,
sequences=tensor.arange(inp.shape[0]))
# randomly dropout units with 50% probability
rand_nums = rand_stream.binomial(size=pooled.shape)
mask = tensor.nonzero(rand_nums)
masked = tensor.set_subtensor(pooled[mask], 0)
return tensor.max(masked, axis=0), updates_inner


out, updates_outer = theano.scan(unit_dropout,
sequences=[tensor.arange(inp.shape[0])])

assert_raises(theano.gradient.NullTypeGradError,
tensor.grad, out.sum(), inp)

def test_bugFunctioProvidesIntermediateNodesAsInputs(self):
# This is a bug recently reported by Ilya
# made it CPU friendly
Expand Down

0 comments on commit 0582999

Please sign in to comment.