将多个张量成对的keras [英] Multiply multiple tensors pairwise keras
问题描述
我想问问是否有可能将两个张量成对相乘.例如,我有LSTM层的张量输出,
I want to ask if it is possible to multiply two tensors pairwise. So for example, I have tensor output from LSTM layer,
lstm=LSTM(128,return_sequences=True)(input)
output=some_function()(lstm)
some_function()
应该做h1*h2,h2*h3....hn-1*hn
我找到了我该如何利用两个Keras张量?没什么帮助,但是由于我将拥有可训练的参数,因此我将必须自己制作一个层.另外,some_function
层会自动解释输入尺寸,因为它会是hn-1
some_function()
should do h1*h2,h2*h3....hn-1*hn
I found How do I take the squared difference of two Keras tensors? little helpful but since, I will have trainable paramter, I will have to make my own layer. Also, will some_function
layer interpret input dimension automatically as it will be hn-1
我对如何处理call()
推荐答案
一种可能性是先进行两次裁剪操作,然后进行乘法运算. 这样就可以了!
One possibility is to do two crop operations and then a multiplication. This does the trick!
import numpy as np
from keras.layers import Input, Lambda, Multiply, LSTM
from keras.models import Model
from keras.layers import add
batch_size = 1
nb_timesteps = 4
nb_features = 2
hidden_layer = 2
in1 = Input(shape=(nb_timesteps,nb_features))
lstm=LSTM(hidden_layer,return_sequences=True)(in1)
# Make two slices
factor1 = Lambda(lambda x: x[:, 0:nb_timesteps-1, :])(lstm)
factor2 = Lambda(lambda x: x[:, 1:nb_timesteps, :])(lstm)
# Multiply them
out = Multiply()([factor1,factor2])
# set the two outputs so we can see both results
model = Model(in1,[out,lstm])
a = np.arange(batch_size*nb_timesteps*nb_features).reshape([batch_size,nb_timesteps,nb_features])
prediction = model.predict(a)
out_, lstm_ = prediction[0], prediction[1]
for x in range(nb_timesteps-1):
assert all( out_[0,x] == lstm_[0,x]*lstm_[0,x+1])
这篇关于将多个张量成对的keras的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!