I am writing a simple code to compute one-hot encoding for a list of indices. Eg: [1,2,3] => [[0,1,0,0],[0,0,1,0],[0,0,0,1]]
I write a function to do the same for a single vector:
n_val =4
def encoding(x_t):
z = T.zeros((x_t.shape[0], n_val))
one_hot = T.set_subtensor(z[T.arange(x_t.shape[0]), x_t], 1)
return one_hot
To repeat the same function over the rows of a matrix, I do the following,
x = T.imatrix()
[m],_ = theano.scan(fn = encoding, sequences = x)
Y = T.stacklists(m)
f= theano.function([x],Y)
I am expecting a 3D tensor with each slice corresponding to the one-hot encoding of the rows of the matrix.
I am getting the following error while compiling the function,
/Library/Python/2.7/site-packages/theano/tensor/var.pyc in __iter__(self)
594 except TypeError:
595 # This prevents accidental iteration via builtin.sum(self)
--> 596 raise TypeError(('TensorType does not support iteration. '
597 'Maybe you are using builtin.sum instead of '
598 'theano.tensor.sum? (Maybe .max?)'))
TypeError: TensorType does not support iteration. Maybe you are using builtin.sum instead of theano.tensor.sum? (Maybe .max?)
Can someone please help me understand where I am going wrong and how I can modify the code to obtain what I need?
Thanks in advance.