Hello,

I want to slice my data with a window at each step. And update the 
recurrent state and the starting point of the window according to a mask.
If he mask is one, starting point of the window is incremented and the 
recurrent state is updated. 
I'm getting some theano.scan errors that I couldn't manage to solve.
Any comments, any help is very much appreciated.

Here is the part of the code that fails:
import theano
import theano.tensor as T
import numpy as np

def crop_step(self, idx, strt_i, end_i, x_sub, x_large):
    x_sub = T.set_subtensor(x_sub[idx,:,:], x_large[idx,strt_i:end_i,:])
    return x_sub


def modified_rnn_step(self, m_t, h_tm1, s_count, x_large):
    #
    s_count2 = (m_t) * s_count + (1 - m_t) * (s_count + 1)
    #
    if m_t.ndim >= 1:
        m_t = m_t.dimshuffle(0, 'x')
    #
    str_ind = T.iround((s_count2) * self.sub_len)
    end_ind = T.iround((s_count2+1) * self.sub_len)
    sub_x = T.zeros((self.bs, self.sub_len, self.fea_dim))
    for m_ind in range(self.bs):
        sub_x = crop_step(str_ind, str_ind[m_ind], end_ind[m_ind], sub_x, 
x_large)
    #   
    t_steps = range(self.T)
    output = []
    pre_val = T.alloc(np.float32(0), self.bs, self.hidden_size)
    pre_val  = T.unbroadcast(pre_val,0)
    #unrolled
    for idx in t_steps:
        out_ = rnn_step(sub_x, pre_val)
        if isinstance(out_, T.TensorVariable):
            out_ = [out_]
        if isinstance(out_, tuple):
            out_ = list(out_)
        output.append(out_)
        prev_val = output[-1]
    h_t = m_t *h_tm1 + (1 - m_t) * prev_val
    return h_t, s_count2


x_mask = T.matrix('mask')
x_full = T.tensor3('data')
h_0 = T.alloc(np.float32(0), self.bs, self.hidden_size)
m_count = T.alloc(np.float32(0), self.bs)
_res, _ = theano.scan(fn=modified_rnn_step, sequences=[x_mask], outputs_info
=[h_0, m_count], non_sequences=[x_full], n_steps=x_mask.shape[1])


Here is the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/theano/scan_module/scan.py", 
line 1041, in scan
    scan_outs = local_op(*scan_inputs)
  File "/usr/local/lib/python2.7/dist-packages/theano/gof/op.py", line 611, 
in __call__
    node = self.make_node(*inputs, **kwargs)
  File 
"/usr/local/lib/python2.7/dist-packages/theano/scan_module/scan_op.py", 
line 544, in make_node
    inner_sitsot_out.type.ndim))
ValueError: When compiling the inner function of scan (the function called 
by scan in each of its iterations) the following error has been encountered: 
The initial state (`outputs_info` in scan nomenclature) of variable 
IncSubtensor{Set;:int64:}.0 (argument number 1) has 3 dimension(s), while 
the corresponding variable in the result of the inner function of scan (`fn`
) has 3 dimension(s) (it should be one less than the initial state). For 
example, if the inner function of scan returns a vector of size d and scan 
uses the values of the previous time-step, then the initial state in scan 
should be a matrix of shape (1, d). The first dimension of this matrix 
corresponds to the number of previous time-steps that scan uses in each of 
its iterations. In order to solve this issue if the two varialbe currently 
have the same dimensionality, you can increase the dimensionality of the 
variable in the initial state of scan by using dimshuffle or shape_padleft. 


When I use dimshuffle to fix this, I get the following error:


x_mask = T.mtx('mask')
x_full = T.tensor3('data')
h_0 = T.alloc(np.float32(0), self.bs, self.hidden_size)
m_count = T.alloc(np.float32(0), self.bs).dimshuffle('x', 0)
_res, _ = theano.scan(fn=modified_rnn_step, sequences=[x_mask], outputs_info
=[h_0, m_count], non_sequences=[x_full])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/theano/scan_module/scan.py", 
line 745, in scan
    condition, outputs, updates = scan_utils.get_updates_and_outputs(fn(*
args))
  File "<stdin>", line 21, in modified_rnn_step
  File "<stdin>", line 2, in crop_tensor_step
  File "/usr/local/lib/python2.7/dist-packages/theano/tensor/var.py", line 
505, in __getitem__
    return theano.tensor.subtensor.advanced_subtensor(self, *args)
  File "/usr/local/lib/python2.7/dist-packages/theano/gof/op.py", line 611, 
in __call__
    node = self.make_node(*inputs, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/theano/tensor/subtensor.py", 
line 2133, in make_node
    index = tuple(map(as_index_variable, index))
  File "/usr/local/lib/python2.7/dist-packages/theano/tensor/subtensor.py", 
line 2076, in as_index_variable
    return make_slice(idx)
  File "/usr/local/lib/python2.7/dist-packages/theano/gof/op.py", line 611, 
in __call__
    node = self.make_node(*inputs, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/theano/tensor/type_other.py", 
line 38, in make_node
    list(map(as_int_none_variable, inp)),
  File "/usr/local/lib/python2.7/dist-packages/theano/tensor/type_other.py", 
line 17, in as_int_none_variable
    x = theano.tensor.as_tensor_variable(x, ndim=0)
  File "/usr/local/lib/python2.7/dist-packages/theano/tensor/basic.py", 
line 180, in as_tensor_variable
    % ndim, x.type
ValueError: ('TensorType could not be cast to have 0 dimensions', TensorType
(int64, vector))


Without theano.scan, I could able to crop the data with a for loop. I don't 
see why I get this error.

Thanks a lot for your help :)

Cinar

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to