python - Tensor multiplication in Tensorflow -
i trying carry out tensor multiplication in numpy/tensorflow.
i have 3 tensors- a (m x h), b (h x n x s), c (s x t)
.
i believe a x b x c
should produce tensor d (m x n x t)
.
here's code (using both numpy , tensorflow).
m = 5 n = 2 t = 3 h = 2 s = 3 a_np = np.random.randn(m, h) c_np = np.random.randn(s, t) b_np = np.random.randn(h, n, s) a_tf = tf.variable(a_np) c_tf = tf.variable(c_np) b_tf = tf.variable(b_np) # tensorflow tf.session() sess: sess.run(tf.global_variables_initializer()) print sess.run(a_tf) p = tf.matmul(a_tf, b_tf) sess.run(p)
this returns following error:
valueerror: shape must rank 2 rank 3 'matmul_2' (op: 'matmul') input shapes: [5,2], [2,2,3].
if try multiplication numpy matrices, following errors:
np.multiply(a_np, b_np) valueerror: operands not broadcast shapes (5,2) (2,2,3)
however, can use np.tensordot
follows:
np.tensordot(np.tensordot(a_np, b_np, axes=1), c_np, axes=1)
is there equivalent operation in tensorflow?
answer
in numpy, follows:
abc_np = np.tensordot(np.tensordot(a_np, b_np, axes=1), c_np, axes=1)
in tensorflow, follows:
ab_tf = tf.tensordot(a_tf, b_tf,axes = [[1], [0]]) ab_tf_c_tf = tf.tensordot(ab_tf, c_tf, axes=[[2], [0]]) tf.session() sess: sess.run(tf.global_variables_initializer()) abc_tf = sess.run(ab_tf_c_tf)
np.allclose(abc_np, abc_tf)
return true
.
try
tf.tensordot(a_tf, b_tf,axes = [[1], [0]])
for example:
x=tf.tensordot(a_tf, b_tf,axes = [[1], [0]]) x.get_shape() tensorshape([dimension(5), dimension(2), dimension(3)])
here tensordot documentation, , here relevant github reciprocity.
Comments
Post a Comment