keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.33k stars 19.39k forks source link

Implementing CircularConvolution and CircularCorrelation Layers or Merges #2518

Closed sergeyf closed 7 years ago

sergeyf commented 8 years ago

This is a repost from Keras-users.

Hello,

I'd like to implement Holographic Embeddings of Knowledge Graphs in Keras, but I'm stumped as to how to go about it.

The key math is in Eq. 8: the k-th entry of the circular correlation is an inner-product between a and b circularly shifted forward by k indices.

I was thinking that the most direct way to do this would be to add a new merge mode, nothing in the backend documentation jumped out at me as being relevant to getting this done.

The only relevant function I found that should help is theano.tensor.roll

Any ideas of how to implement this in an efficient, elegant way with the tools available in the Keras backend? Hopefully something that would also work with TensorFlow so I could submit a pull request.

Thanks.

sergeyf commented 7 years ago

For posterity, this is how you implement circular correlation with TensorFlow. For circular convolution, just leave out the tf.conj. I have no idea if this will work with Theano.

def holographic_merge(inputs):
    a, b = inputs
    a_fft = tf.batch_fft(tf.complex(a, 0.0))
    b_fft = tf.batch_fft(tf.complex(b, 0.0))
    ifft = tf.batch_ifft(tf.conj(a_fft) * b_fft)
    return tf.cast(tf.real(ifft), 'float32')
waongyi commented 7 years ago

How do you compute the gradients for a,b ? I implement it with Theano.andbox.fourier.fft, ifft, but gradient for fft, ifft is not implemented yet, I guess (aborts with an error: Elemwise{real,no_inplace}.grad illegally returned an integer-valued variable. (Input index 0, dtype complex128))

Hui-Li commented 6 years ago

@sergeyf Implementation for tensorflow can be found in OpenKE

sergeyf commented 6 years ago

Thanks!

On Nov 28, 2017 6:28 AM, "Hui Li" notifications@github.com wrote:

@sergeyf https://github.com/sergeyf Implementation for tensorflow can be found in OpenKE https://github.com/thunlp/OpenKE/blob/9a2cc3f838e1bddb2fe8f65b9747d3aba36a72c1/models/HolE.py

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/fchollet/keras/issues/2518#issuecomment-347494786, or mute the thread https://github.com/notifications/unsubscribe-auth/ABya7C0j5KRXwk-WIsHPNlGpq8XByuj4ks5s6-5UgaJpZM4IQES6 .

desh2608 commented 6 years ago

@waongyi Were you able to implement it in Theano?