Keras连接层:不同类型的连接函数之间的差异 [英] Keras Concatenate Layers: Difference between different types of concatenate functions

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

问题描述

我最近才开始与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种不同形式的连接函数merge/ 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()

我知道第二个用于功能性API,但是三个之间有什么区别?该文档对此似乎还不清楚.

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()

First, the backend: 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)


Then, the 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).

最后,来自图层模块的连接功能:keras.layers.concatenate(inputs, axis=-1)

Finally, the concatenate function from the layers module: 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字有关.


Older functions

In Keras 1, people had functions that were meant to receive "layers" as input and return an output "layer". Their names were related to the merge word.

但是,由于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连接层:不同类型的连接函数之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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