Discussion:
[theano-users] weird dimensionality change after T.repeat
Wenpeng Yin
2018-06-14 03:45:50 UTC
Permalink
Hi guys,

I have a very simple code:

import numpy as np
import theano
import theano.tensor as T

sents_id_matrix=T.imatrix() #(2, 4)
repeat_common_input = T.repeat(sents_id_matrix, 3, axis=0)
output = repeat_common_input.reshape((2*3,3))
train_model = theano.function([sents_id_matrix], output)
if __name__ == '__main__':
cost_i= train_model(np.asarray([[1,2,3,4],[5,6,7,8]], dtype='int32'))
print cost_i


Basically I wanted to reshape an input matrix of size (2,4) into (6,4). In
the place "reshape((2*3,3))" I intentionally put a wrong resulting
dimension so as to generate the error info. My question is that the error
is different as I expected:

ValueError: total size of new array must be unchanged
Apply node that caused the error: Reshape{2}(Alloc.0, TensorConstant{[6
3]})
Toposort index: 4
Inputs types: [TensorType(int32, 3D), TensorType(int64, vector)]
Inputs shapes: [(2, 3, 4), (2,)]
The error should be due to the line "output =
repeat_common_input.reshape((2*3,3))".
However, I thought the dimensionality of "repeat_common_input" should be
(6,4) in stead of the tensor3 -- (2,3,4)

Interesting, if the train_model directly sets "repeat_common_input" as
output, then it is a matrix of (6, 4)

This is a toy code to reproduce my bug. As what exactly the internal shape
is will influence my other operations, such as I planed to do max over some
dimensions, but if a tensor variable has different dimensionality as I
expected, my code will have error.

Thanks for any help.
--
---
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 theano-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Pascal Lamblin
2018-06-14 22:31:16 UTC
Permalink
T.repeat is just a shortcut for a series of operations, that first
create a tensor of shape (2, 3, 4), and then reshape it to (2*3, 4).

During the optimization phase, Theano will combine that reshape with
yours, to avoid reshaping twice. This is why the error message is
confusing: it refers to the optimized graph, not the original one you wrote.

If you disable all optimizations (for instance with 'optimizer=None'),
the message becomes what you expect:

ValueError: cannot reshape array of size 24 into shape (6,3)
Apply node that caused the error: Reshape{2}(Reshape{2}.0,
TensorConstant{[6 3]})
Toposort index: 9
Inputs types: [TensorType(int32, matrix), TensorType(int64, vector)]
Inputs shapes: [(6, 4), (2,)]
Inputs strides: [(16, 4), (8,)]
Inputs values: ['not shown', array([6, 3])]
Outputs clients: [['output']]
Post by Wenpeng Yin
Hi guys,
import numpy as np
import theano
import theano.tensor as T
sents_id_matrix=T.imatrix() #(2, 4)
repeat_common_input = T.repeat(sents_id_matrix, 3, axis=0)
output = repeat_common_input.reshape((2*3,3))
train_model = theano.function([sents_id_matrix], output)
    cost_i= train_model(np.asarray([[1,2,3,4],[5,6,7,8]],
dtype='int32'))
    print cost_i
Basically I wanted to reshape an input matrix of size (2,4) into (6,4).
In the place "reshape((2*3,3))" I intentionally put a wrong resulting
dimension so as to generate the error info. My question is that the
ValueError: total size of new array must be unchanged
Apply node that caused the error: Reshape{2}(Alloc.0,
TensorConstant{[6 3]})
Toposort index: 4
Inputs types: [TensorType(int32, 3D), TensorType(int64, vector)]
Inputs shapes: [(2, 3, 4), (2,)]
The error should be due to the line "output =
repeat_common_input.reshape((2*3,3))".
However, I thought the  dimensionality of "repeat_common_input" should
be (6,4) in stead of the tensor3 -- (2,3,4)
Interesting, if the train_model directly sets "repeat_common_input" as
output, then it is a matrix of (6, 4)
This is a toy code to reproduce my bug. As what exactly the internal
shape is will influence my other operations, such as I planed to do max
over some dimensions, but if a tensor variable has different
dimensionality as I expected, my code will have error.
Thanks for any help.
--
---
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
For more options, visit https://groups.google.com/d/optout.
--
Pascal Lamblin
--
---
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 theano-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...