python - Reshape batch of tensors into batch of vectors in TensorFlow -
during creating calculation graph have tensor x
e.g. shape of [-1, a, b, c]
, reshape [-1, a*b*c]
tried way:
n = functools.reduce(operator.mul, x.shape[1:], 1) tf.reshape(x, [-1, n])
but i've got error:
typeerror: unsupported operand type(s) *: 'int' , 'dimension'
my question is: there tensorflow operation?
as error message tells you, there problem types. if create tensorflow placeholder, e.g. with
>>> import tensorflow tf >>> x = tf.placeholder(tf.float16, shape=(none, 3,7,4))
and call shape
on it, return value is
>>> x.shape tensorshape([dimension(none), dimension(3), dimension(7), dimension(4)])
and each element a
>>> x.shape[1] <class 'tensorflow.python.framework.tensor_shape.dimension'>
i.e. dimension
class of tensorflow. naturally, operator.mul
function doesn't know such type. luckily, tf.tensorshape
has as_list()
function, returns shape list of integers.
>>> x.shape.as_list() [none, 3, 7, 4]
with that, can calculate number of elements n
, you're used to:
>>> import functools, operator >>> n = functools.reduce(operator.mul, x.shape.as_list()[1:], 1) >>> n 84 >>> y = tf.reshape(x, [-1, n])
Comments
Post a Comment