Keras Lambda Layer for matrix vector multiplication

纵然是瞬间 提交于 2021-02-07 10:30:54

问题


I am trying to have a lambda layer in keras that performs a vector matrix multiplication, before passing it to another layer. The matrix is fixed (I don't want to learn it). Code below:

model.add(Dropout(0.1))    
model.add(Lambda(lambda x: x.dot(A)))
model.add(Dense(output_shape, activation='softmax'))
model.compile(<stuff here>)}

A is the fixed matrix, and I want to do x.dot(A)

WHen I run this, I get the following error:

'Tensor' object has no attribute 'dot'

Same Error when I replace dot with matmul (I am using tensorflow backend)

Finally, when I replace the lambda layer by

model.add(Lambda(lambda x: x*A))

I get the error below:

model.add(Lambda(lambda x: x*G))

model.add(Dense(output_shape, activation='softmax'))

AttributeError: 'tuple' object has no attribute '_dims'

I'm new to Keras so any help will be appreciated. Thanks


回答1:


Create a function for the lambda:

import keras.backend as K
import numpy as np

numpyA = np.array(define A correctly here, with 2 dimensions)

def multA(x):
    A = K.variable(numpyA)

    return K.dot(x,A)

model.add(Lambda(multA))



回答2:


I think you can add a Dense layer with the initial weight being the matrix A, and set the arguments trainable=False and use_bias=False. This layer will be equivalent to a fixed matrix multiplication.

model.add(Dense(A.shape[1], trainable=False, weights=[A], use_bias=False))


来源:https://stackoverflow.com/questions/46570963/keras-lambda-layer-for-matrix-vector-multiplication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!