用于多任务学习神经网络的 Keras 的顺序与功能 API [英] Keras' Sequential vs Functional API for Multi-Task Learning Neural Network

查看:18
本文介绍了用于多任务学习神经网络的 Keras 的顺序与功能 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为多任务深度学习任务设计一个神经网络.在 Keras API 中,我们可以使用顺序"或函数"方法来构建这样的神经网络.在下面,我提供了用于构建网络的代码,使用这两种方法构建具有两个输出的网络:

I would like to design a neural network for a multi-task deep learning task. Within the Keras API we can either use the "Sequential" or "Functional" approach to build such a neural network. Underneath I provide the code I used to build a network using both approaches to build a network with two outputs:

顺序

seq_model = Sequential()
seq_model.add(LSTM(32, input_shape=(10,2)))
seq_model.add(Dense(8))
seq_model.add(Dense(2))
seq_model.summary()

功能性

input1 = Input(shape=(10,2))
lay1 = LSTM(32, input_shape=(10,2))(input1)
lay2 = Dense(8)(lay1)
out1 = Dense(1)(lay2)
out2 = Dense(1)(lay2)
func_model = Model(inputs=input1, outputs=[out1, out2])
func_model.summary()

当我查看模型的两个摘要输出时,每个输出都包含相同数量的可训练参数:

When I look at both the summary outputs for the models, each of them contains identical number of trainable params:

到目前为止,这看起来不错 - 但是当我绘制两个模型(使用 keras.utils.plot_model)时,我开始怀疑自己,结果如下图:

Up until now, this looks fine - however I start doubting myself when I plot both models (using keras.utils.plot_model) which results in the followings graphs:

我个人不知道如何解释这些.当使用多任务学习方法时,我希望输出层之前的层的所有神经元(在我的例子中为 8)连接到两个输出神经元.对我来说,这清楚地显示在 Functional API 中(我有两个 Dense(1) 实例),但这在 Sequential API 中不是很清楚.尽管如此,可训练参数的数量是相同的;这表明最后一层的 Sequential API 也完全连接到密集输出层中的两个神经元.

Personally I do not know how to interpret these. When using a multi-task learning approach, I want all neurons (in my case 8) of the layer before the output-layer to connect to both output neurons. For me this clearly shows in the Functional API (where I have two Dense(1) instances), but this is not very clear from the Sequential API. Nevertheless, the amount of trainable params is identical; suggesting that also the Sequential API the last layer is fully connected to both neurons in the Dense output layer.

谁能向我解释这两个示例之间的差异,或者它们是否完全相同并导致相同的神经网络架构?另外,在这种情况下,更喜欢哪一个?

Could anybody explain to me the differences between those two examples, or are those fully identical and result in the same neural network architecture? Also, which one would be preferred in this case?

先谢谢你.

推荐答案

Sequential 和函数式 keras API 的区别:

The difference between Sequential and functional keras API:

sequential API 允许您为大多数情况逐层创建模型问题.它的局限性在于它不允许您创建模型共享层或具有多个输入或输出.

The sequential API allows you to create models layer-by-layer for most problems. It is limited in that it does not allow you to create models that share layers or have multiple inputs or outputs.

函数式 API 允许您创建具有更多功能的模型灵活性,因为您可以轻松定义层连接到的模型不仅仅是上一层和下一层.其实你可以连接层到(字面意思)任何其他层.因此,创建复杂的siamese 网络和残差网络等网络成为可能.

the functional API allows you to create models that have a lot more flexibility as you can easily define models where layers connect to more than just the previous and next layers. In fact, you can connect layers to (literally) any other layer. As a result, creating complex networks such as siamese networks and residual networks become possible.

回答您的问题:

不,这些API不一样,层数相同是正常的.使用哪一种?这取决于您要使用此网络的用途.你培训是为了什么?您希望输出是什么?

No these APIs are not the same and the number of layers is normal that are the same number. Which one to use? It depends on the use you want to make of this network. What are you doing the training for? What do you want the output to be?

我推荐此链接以充分利用该概念.

I recommend this link to make the most of the concept.

序列模型功能模型

希望我能帮助你更好地理解.

I hope I helped you understand better.

这篇关于用于多任务学习神经网络的 Keras 的顺序与功能 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆