串联层在Keras多任务中做什么?

2024-04-25 16:40:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Keras中实现了一个简单的多任务模型。我使用了共享层标题下的documentation中给出的代码。你知道吗

我知道,在多任务学习中,我们共享模型中的一些初始层,最后的层根据link对特定任务进行单独设置。你知道吗

在kerasapi中有以下两种情况,第一种情况下,我使用keras.layers.concatenate,而另一种情况下,我没有使用任何keras.layers.concatenate。你知道吗

我张贴的代码以及每个案件的模型如下。你知道吗

案例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模型Model visualization

案例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模型 Model visualization for case-2

在这两种情况下,仅共享LSTM层。在案例1中,我们有keras.layers.concatenate,但在案例2中,我们没有任何keras.layers.concatenate。你知道吗

我的问题是,哪一个是多任务处理,案例1还是案例2?另外,在case-1中keras.layers.concatenate的作用是什么?


Tags: thefromimportlayerinputmodellayers案例
1条回答
网友
1楼 · 发布于 2024-04-25 16:40:09

两者都是多任务模型,因为这只取决于是否有多个输出,每个输出关联一个任务。你知道吗

不同之处在于,第一个模型显式地连接共享层生成的特性,因此两个输出任务都可以考虑来自两个输入的信息。第二个模型只从一个输入直接连接到一个输出,而不考虑其他输入。这里模型之间的唯一联系是它们共享LSTM权重。你知道吗

相关问题 更多 >