简体   繁体   中英

How to write a Keras Layer for basic vector/tensor multiplication

So I want to multiply the outputs of an embedding by a constant vector. I'm using the functional API rather than Sequential.

word_seq = Input(shape = (SEQ_LEN,), dtype = "int32", name = "word_seq") 

word_embs = Embedding(output_dim = EMBED_DIM, input_dim = VOCAB_SIZE, input_length = SEQ_LEN)(word_seq)

If I understand this correctly, because I haven't given a batch shape, word_embs should have shape ( None, SEQ_LEN, EMBED_DIM ).

I have a constant vector (numpy array) q of shape ( SEQ_LEN ,). So the matrix multiplication I want to perform is q^T* (the seq_len by embed_dim matrix inside word_embs ).

I think I'll need to use the keras Variable to turn q into a tensor, but then a Dot layer or keras.backend.dot are both giving me trouble because of that None dimension on word_embeds. I don't want to use Flatten because that will reduce it to a single dimension, rather than just getting rid of the troublesome one. Is Reshape what I need then? Or can I pass word_embs[:] to the lambda layer or something?

Maybe I just don't know enough about tensors, but this is extremely frustrating. It seems like such an advanced python library should easily be able to do high school matrix multiplication but I can't figure it out.

You can use 1 as the batch dimension for the constant tensor in your Lambda:

import keras.backend as K
[...]

def my_lambda(x):
    q_array = np.zeros((1, SEQ_LEN))
    q_array = ... # your value
    q = K.constant(q_array.T)
    return Dot()([q, x])

result = Lambda(my_lambda)(word_embs)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM