Keras Concatenate Layers:不同类型连接函数的区别 [英] Keras Concatenate Layers: Difference between different types of concatenate functions

查看:48
本文介绍了Keras Concatenate Layers:不同类型连接函数的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近才开始使用 Keras 并开始制作自定义层.但是,我对名称略有不同但功能相同的许多不同类型的图层感到困惑.

I just recently started playing around with Keras and got into making custom layers. However, I am rather confused by the many different types of layers with slightly different names but with the same functionality.

例如,有来自 https://keras.io/layers/的 3 种不同形式的连接函数合并/https://www.tensorflow.org/api_docs/python/tf/keras/backend/concatenate

keras.layers.Concatenate(axis=-1)
keras.layers.concatenate(inputs, axis=-1)
tf.keras.backend.concatenate()

我知道第 2 个用于函数式 API,但 3 个之间有什么区别?文档在这方面似乎有点不清楚.

I know the 2nd one is used for functional API but what is the difference between the 3? The documentation seems a bit unclear on this.

此外,对于第三个,我在下面看到了执行此操作的代码.为什么串联后一定要有._keras_shape这一行?

Also, for the 3rd one, I have seen a code that does this below. Why must there be the line ._keras_shape after the concatenation?

# Concatenate the summed atom and bond features
atoms_bonds_features = K.concatenate([atoms, summed_bond_features], axis=-1)

# Compute fingerprint
atoms_bonds_features._keras_shape = (None, max_atoms, num_atom_features + num_bond_features)

最后,在 keras.layers 下,似乎总是有 2 个重复.例如,Add() 和 add() 等.

Lastly, under keras.layers, there always seems to be 2 duplicates. For example, Add() and add(), and so on.

推荐答案

一、后端:tf.keras.backend.concatenate()

后端函数应该在内部"使用层.您只能在 Lambda 层、自定义层、自定义损失函数、自定义指标等中使用它.

Backend functions are supposed to be used "inside" layers. You'd only use this in Lambda layers, custom layers, custom loss functions, custom metrics, etc.

它直接作用于张量".

如果您不深入定制,这不是选择.(在您的示例代码中这是一个糟糕的选择 - 请参阅最后的详细信息).

It's not the choice if you're not going deep on customizing. (And it was a bad choice in your example code -- See details at the end).

如果你深入研究 keras 代码,你会注意到 Concatenate 层在内部使用了这个函数:

If you dive deep into keras code, you will notice that the Concatenate layer uses this function internally:

import keras.backend as K
class Concatenate(_Merge):  
    #blablabla   
    def _merge_function(self, inputs):
        return K.concatenate(inputs, axis=self.axis)
    #blablabla


然后,Layer:keras.layers.Concatenate(axis=-1)

与任何其他 keras 层一样,您可以在张量实例化调用它.

As any other keras layers, you instantiate and call it on tensors.

非常简单:

#in a functional API model:
inputTensor1 = Input(shape) #or some tensor coming out of any other layer   
inputTensor2 = Input(shape2) #or some tensor coming out of any other layer

#first parentheses are creating an instance of the layer
#second parentheses are "calling" the layer on the input tensors
outputTensor = keras.layers.Concatenate(axis=someAxis)([inputTensor1, inputTensor2])

这不适合顺序模型,除非前一层输出一个列表(这可能但不常见).

This is not suited for sequential models, unless the previous layer outputs a list (this is possible but not common).

最后,来自layers模块的concatenate函数:keras.layers.concatenate(inputs,axis=-1)

不是图层.这是一个函数,它将返回由内部 Concatenate 层产生的张量.

This is not a layer. This is a function that will return the tensor produced by an internal Concatenate layer.

代码很简单:

def concatenate(inputs, axis=-1, **kwargs):
   #blablabla
   return Concatenate(axis=axis, **kwargs)(inputs)


旧功能

在 Keras 1 中,人们拥有旨在接收层"的函数.作为输入并返回输出层".他们的名字与 merge 词有关.

但由于 Keras 2 没有提及或记录这些,我可能会避免使用它们,如果发现旧代码,我可能会将其更新为正确的 Keras 2 代码.

But since Keras 2 doesn't mention or document these, I'd probably avoid using them, and if old code is found, I'd probably update it to a proper Keras 2 code.

这个后端函数不应该在高级代码中使用.编码人员应该使用 Concatenate 层.

This backend function was not supposed to be used in high level codes. The coder should have used a Concatenate layer.

atoms_bonds_features = Concatenate(axis=-1)([atoms, summed_bond_features])   
#just this line is perfect

Keras 层将 _keras_shape 属性添加到其所有输出张量中,Keras 使用此属性来推断整个模型的形状.

Keras layers add the _keras_shape property to all their output tensors, and Keras uses this property for infering the shapes of the entire model.

如果您使用任何后端功能外部"层或损失/度量,您的输出张量将缺少此属性,并且会出现错误,告诉 _keras_shape 不存在.

If you use any backend function "outside" a layer or loss/metric, your output tensor will lack this property and an error will appear telling _keras_shape doesn't exist.

编码人员通过手动添加属性来创建一个糟糕的解决方法,而它应该由适当的 keras 层添加.(这现在可能有效,但在 keras 更新的情况下,此代码将中断,而正确的代码将保持正常)

The coder is creating a bad workaround by adding the property manually, when it should have been added by a proper keras layer. (This may work now, but in case of keras updates this code will break while proper codes will remain ok)

这篇关于Keras Concatenate Layers:不同类型连接函数的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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