1

Here is a code snippet :

seqLen = 4
seq = np.repeat([seqLen-1],seqLen)
print seq

def step2(idx):
    if T.lt(idx,seqLen-1):
        return T.constant(True)
    else:
        return T.constant(False)
    # return T.lt(idx,seqLen-1)

    final,updates = theano.scan(step2,sequences=[T.arange(0,4)]
                             ,outputs_info=None)

    iter = theano.function([], outputs=[final],updates=updates)
    result = iter()
    print 'result', result

The output that I am getting is -

result [array([1, 1, 1, 1], dtype=int8)]

Shouldn't the output be array([1, 1, 1, 0]). Am not able to understand how the sequence 'idx' is being passed to the step function. Shouldn't it send each value of T.arange(0,4) at each time step and then T.lt(idx,3) should return false just for the last time step and true otherwise.

Update: if I modify the step2 function as -

def step2(idx):
    return T.lt(idx,seqLen-1)

then I get the correct output as [1,1,1,0]. I need to use the conditional statement as a if condition only. Any help?

Update 2: Instead of using pythonic 'if' construct, I tried theano's 'ifelse' and it worked. But I want to execute a bunch of statements based on the condition and that is why 'ifelse' is not feasible.

Thanks

dragster
  • 448
  • 1
  • 4
  • 20
  • if your question is how can you build theano graphs using pythonic if-else constructs the answer is probably that you cannot. The possibility of using regular binary operators like `+` and `*` between theano ops is possibly because python allows to overload such binary operators, but I don't think such a thing is possible for ternary operators such as if-else constructs. See also [this related answer](http://stackoverflow.com/a/35859464/4063051) – glS Dec 21 '16 at 17:09

0 Answers0