Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

batch_dot has different behaviour in keras.backend and tf.keras.backend #13300

Closed
MarshallCN opened this issue Sep 10, 2019 · 3 comments
Closed

Comments

@MarshallCN
Copy link

I found that the output of keras.backend.batch_dot function is different from the tensorflow.keras.backend.batch_dot. The tensorflow version is 1.14.0, and the Keras version is 2.2.5

#using Google Colab
import tensorflow as tf   #1.14.0  #v1.14.0-0-g87989f6959 
from keras import backend as K #2.2.5

c = tf.ones(shape=(1,2,1))
h = tf.ones(shape=(1,2,1,16))

p1 = K.batch_dot(c, h, [2, 2])
print(K.int_shape(p1))  
####output: (1,2,2,16)

p2 = tf.keras.backend.batch_dot(c, h, [2, 2])
print(p2.shape)  
####output: (1,2,16)

However, in Keras version 2.2.4, these two functions produce identical results. So I think it might have some problem. Also, I wonder which result is actually correct?

#using localhost Jupyter
import tensorflow as tf
from keras import backend as K  #2.2.4

c = tf.ones(shape=(1,2,1))
h = tf.ones(shape=(1,2,1,16))

p3 = K.batch_dot(c, h, [2, 2])
print(K.int_shape(p3))  
####ourput: (1,2,16)

p4 = tf.keras.backend.batch_dot(c, h, [2, 2])
print(p4.shape)  
####output: (1,2,16)

@fchollet
Copy link
Member

I guess some change was made in multi-backend Keras that wasn't ported to tf.keras. @farizrahman4u do you remember what it was?

@johnny12150
Copy link

I thought below code should output shape (9, 8, 7, 4, 5) in Google Colab

from keras import backend as K
a = K.ones((9, 8, 7, 4, 2))
b = K.ones((9, 8, 7, 2, 5))
c = K.batch_dot(a, b)
print(c.shape)

But why am i getting shape (9, 8, 7, 4, 8, 7, 5)?
Is there any way to get shape (9, 8, 7, 4, 5) again?

@fchollet
Copy link
Member

The correct behavior is now the Keras behavior in 2.2.5 and higher. We will update tf.keras to follow the same behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants