Reputation: 77
In using tensorflow's tf.tensordot
, I have run into some strange results. Running the following code block
import tensorflow as tf
import numpy as np
a = np.arange(6, dtype=np.int32).reshape(3,2)
b = np.arange(1,7, dtype=np.int32).reshape(2,3)
sess = tf.Session()
print(sess.run(tf.tensordot(a, b, [[0,1],[0,1]])))
print(sess.run(tf.tensordot(a, b, [[0,1],[1,0]])))
print(sess.run(tf.tensordot(a, b, [[1,0],[0,1]])))
print(sess.run(tf.tensordot(a, b, [[1,0],[1,0]])))
produces
70
65
65
60
I can't figure out what contraction is happening here. Another interesting thing is that trying to do this with numpy's tensordot will return an error for several of the attempted axes.
Upvotes: 2
Views: 267
Reputation: 24661
You have found a bug in tensorflow.
According to the documentation of tf.tensordot
,
The axis
a_axes[i]
ofa
must have the same dimension as axisb_axes[i]
ofb
for alli
inrange(0, len(a_axes))
.
So for example, tf.tensordot(a, b, [[0,1],[0,1]]))
should have returned an error, because a
is 3x2
and b
is 2x3
. But it does not — that's the bug.
Instead, it proceeds as if the tensors were compatible. If a
and b
were compatible, then tf.tensordot(a, b, [[0,1],[0,1]]))
would be a simple dot product. What tf.tensordot
does internally is that it basically flattens a
and b
and compute the dot product.
In your case, a
and b
have the same number of elements, therefore the computation of the dot product succeeds, despite the fact that they have an incompatible shape.
You can file a bug to the TF team here.
Upvotes: 1