功能性API中的Keras Multiply()层 [英] Keras Multiply() layer in functional API
本文介绍了功能性API中的Keras Multiply()层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在新的API更改下,如何在Keras中进行层的元素逐级乘法?在旧的API下,我会尝试这样的事情:
Under the new API changes, how do you do element-wise multiplication of layers in Keras? Under the old API, I would try something like this:
merge([dense_all, dense_att], output_shape=10, mode='mul')
我已经尝试过(MWE):
I've tried this (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()
完整跟踪:
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)
我尝试实现tensorflow的逐元素乘法函数.当然,结果不是 Layer()
实例,因此它不起作用.这是后代的尝试:
I tried implementing tensorflow's elementwise multiply function. Of course, the result is not a Layer()
instance, so it doesn't work. 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
推荐答案
使用 keras
> 2.0:
from keras.layers import multiply
output = multiply([dense_all, dense_att])
这篇关于功能性API中的Keras Multiply()层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文