pir
pir

Reputation: 5933

Theano converting from TensorType col to TensorType matrix

I'm getting an error due to checks inside Theano's scan function that check if the same type of variable is used multiple places. This function does not allow a (N, 1) of the col TensorType to be exchanged with a (N, 1) matrix (see error below).

How do I cast/convert a (N, 1) Tensor of the col TensorType to the matrix TensorType?

TypeError: ('The following error happened while compiling the node', forall_inplace,cpu,scan_fn}(TensorConstant{20}, InplaceDimShuffle{1,0,2}.0, IncSubtensor{InplaceSet;:int64:}.0, IncSubtensor{InplaceSet;:int64:}.0, IncSubtensor{InplaceSet;:int64:}.0, TensorConstant{20}, condpred_1_W_ih, condpred_1_W_ho, embedding_1_W, InplaceDimShuffle{x,0}.0, InplaceDimShuffle{x,0}.0, AdvancedIncSubtensor{inplace=False, set_instead_of_inc=True}.0), '\n', "Inconsistency in the inner graph of scan 'scan_fn' : an input and an output are associated with the same recurrent state and should have the same type but have type 'TensorType(int32, matrix)' and 'TensorType(int32, col)' respectively.")

Upvotes: 1

Views: 205

Answers (1)

Autonomous
Autonomous

Reputation: 9075

You need to use theano.tensor.patternbroadcast.

If you see here, the shape of fmatrix is (?, ?) and that of fcol is (?, 1). The meaning of ? is that dimension can take any value. So shape is not a good differentiator between fmatrix and fcol. Now, see in the broadcastable column. Last dimension of fmatrix is not broadcastable while that of fcol is. So the following code should convert between these types.

Let us convert a matrix to a col and then vice-versa.

from theano import tensor as T

x = T.fmatrix('x')
x_to_col = T.patternbroadcast(x, (False, True))
x.type
x_to_col.type

y = T.fcol('y')
y_to_matrix = T.patternbroadcast(y, (False, False))
y.type
y_to_matrix.type

Run the above commands in a console to see that the data types have indeed changed. So you either change your fmatrix variable or your fcol one.

Upvotes: 1

Related Questions