本文介绍了串联层在Keras多任务中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Keras中实现一个简单的多任务模型.我使用了标题下的文档中给出的代码.共享层.

I am implementing a simple multitask model in Keras. I used the code given in the documentation under the heading of shared layers.

我知道在多任务学习中,我们共享模型中的一些初始层,并且根据链接.

I know that in multitask learning, we share some of the initial layers in our model and the final layers are made individual to the specific tasks as per the link.

我在keras API中有以下两种情况,第一种是使用keras.layers.concatenate,而另一种则是不使用任何keras.layers.concatenate.

I have following two cases in keras API where in the first, I am using keras.layers.concatenate while in the other, I am not using any keras.layers.concatenate.

我将针对每种情况发布代码和模型,如下所示.

I am posting the codes as well as the models for each case as follows.

Case-1代码

import keras
from keras.layers import Input, LSTM, Dense
from keras.models import Model
from keras.models import Sequential
from keras.layers import Dense
from keras.utils.vis_utils import plot_model

tweet_a = Input(shape=(280, 256))
tweet_b = Input(shape=(280, 256))

# This layer can take as input a matrix
# and will return a vector of size 64
shared_lstm = LSTM(64)

# When we reuse the same layer instance
# multiple times, the weights of the layer
# are also being reused
# (it is effectively *the same* layer)
encoded_a = shared_lstm(tweet_a)
encoded_b = shared_lstm(tweet_b)

# We can then concatenate the two vectors:
merged_vector = keras.layers.concatenate([encoded_a, encoded_b], axis=-1)

# And add a logistic regression on top
predictions1 = Dense(1, activation='sigmoid')(merged_vector)
predictions2 = Dense(1, activation='sigmoid')(merged_vector)

# We define a trainable model linking the
# tweet inputs to the predictions
model = Model(inputs=[tweet_a, tweet_b], outputs=[predictions1, predictions2])

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

案例1模型

第2种情况下的代码

import keras
from keras.layers import Input, LSTM, Dense
from keras.models import Model
from keras.models import Sequential
from keras.layers import Dense
from keras.utils.vis_utils import plot_model

tweet_a = Input(shape=(280, 256))
tweet_b = Input(shape=(280, 256))

# This layer can take as input a matrix
# and will return a vector of size 64
shared_lstm = LSTM(64)

# When we reuse the same layer instance
# multiple times, the weights of the layer
# are also being reused
# (it is effectively *the same* layer)
encoded_a = shared_lstm(tweet_a)
encoded_b = shared_lstm(tweet_b)



# And add a logistic regression on top
predictions1 = Dense(1, activation='sigmoid')(encoded_a )
predictions2 = Dense(1, activation='sigmoid')(encoded_b)

# We define a trainable model linking the
# tweet inputs to the predictions
model = Model(inputs=[tweet_a, tweet_b], outputs=[predictions1, predictions2])

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

案例2模型

在两种情况下,仅共享LSTM层.在情况1中,我们有keras.layers.concatenate,但在情况2中,我们没有任何keras.layers.concatenate.

In both cases, the LSTMlayer is shared only. In case-1, we have keras.layers.concatenate but in case-2, we don't have any keras.layers.concatenate.

我的问题是,多任务处理是案例1还是案例2? Morover,案例1中keras.layers.concatenate的功能是什么?

My question is, which one is multitasking, case-1 or case-2? Morover, what is the function of keras.layers.concatenate in case-1?

推荐答案

两者都是多任务模型,因为这仅取决于是否有多个输出且每个输出关联一个任务.

Both are multi-task models, as this only depends if there are multiple outputs with one task associated to each output.

区别在于您的第一个模型显式连接了共享层产生的要素,因此两个输出任务都可以考虑来自两个输入的信息.第二种模型仅具有从一个输入直接连接到其中一个输出的连接,而没有考虑另一输入.这些模型之间的唯一联系是它们共享LSTM权重.

The difference is that your first model explicitly concatenates features produced by the shared layer, so both output tasks can consider information from both inputs. The second model only has connections from one input directly to one of the outputs, without considering the other input. The only link between models here is that they share the LSTM weights.

这篇关于串联层在Keras多任务中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 16:48