简体   繁体   English

函数式 API 中的 Keras Multiply() 层

[英]Keras Multiply() layer in functional API

Under the new API changes, how do you do element-wise multiplication of layers in Keras?在新的 API 变化下,如何在 Keras 中进行元素级的层乘法? Under the old API, I would try something like this:在旧的 API 下,我会尝试这样的事情:

merge([dense_all, dense_att], output_shape=10, mode='mul')

I've tried this (MWE):我试过这个(MWE):

from keras.models import Model
from keras.layers import Input, Dense, Multiply

def sample_model():
        model_in = Input(shape=(10,))
        dense_all = Dense(10,)(model_in)
        dense_att = Dense(10, activation='softmax')(model_in)
        att_mull = Multiply([dense_all, dense_att]) #merge([dense_all, dense_att], output_shape=10, mode='mul')
        model_out = Dense(10, activation="sigmoid")(att_mull)
        return 0

if __name__ == '__main__':
        sample_model()

Full trace:完整跟踪:

Using TensorFlow backend.
Traceback (most recent call last):
  File "testJan17.py", line 13, in <module>
    sample_model()
  File "testJan17.py", line 8, in sample_model
    att_mull = Multiply([dense_all, dense_att]) #merge([dense_all, dense_att], output_shape=10, mode='mul')
TypeError: __init__() takes exactly 1 argument (2 given)

EDIT:编辑:

I tried implementing tensorflow's elementwise multiply function.我尝试实现 tensorflow 的元素乘法函数。 Of course, the result is not a Layer() instance, so it doesn't work.当然,结果不是一个Layer()实例,所以它不起作用。 Here's the attempt, for posterity:这是对后代的尝试:

def new_multiply(inputs): #assume two only - bad practice, but for illustration...
        return tf.multiply(inputs[0], inputs[1])


def sample_model():
        model_in = Input(shape=(10,))
        dense_all = Dense(10,)(model_in)
        dense_att = Dense(10, activation='softmax')(model_in) #which interactions are important?
        new_mult = new_multiply([dense_all, dense_att])
        model_out = Dense(10, activation="sigmoid")(new_mult)
        model = Model(inputs=model_in, outputs=model_out)
        model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
        return model

With keras > 2.0:使用keras > 2.0:

from keras.layers import multiply
output = multiply([dense_all, dense_att])

Under the functional API, you just use the multiply function, note the lowercase "m".在函数式 API 下,您只需使用multiply函数,注意小写的“m”。 The Multiply class is a layer as you see, intended to be used with the sequential API.如您所见,Multiply 类是一个层,旨在与顺序 API 一起使用。

More information in https://keras.io/layers/merge/#multiply_1 https://keras.io/layers/merge/#multiply_1中的更多信息

You need to add one more open close parenthesis at front.您需要在前面再添加一个左括号。

from keras.layers import Multiply
att_mull = Multiply()([dense_all, dense_att])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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