"[0] [0]"是什么意思? keras模型中连接的各层的层数.摘要是什么意思? [英] What does the "[0][0]" of the layers connected to in keras model.summary mean?

查看:194
本文介绍了"[0] [0]"是什么意思? keras模型中连接的各层的层数.摘要是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下表所示,input_1[0][0][0][0]是什么意思?

As is depicted in the following table, what does the [0][0] of the input_1[0][0] mean?

__________________________________________________
Layer (type)          Output Shape  Param # Connected to           
===================================================================
input_1 (InputLayer)  (None, 1)     0                              
___________________________________________________________________
dropout_1 (Dropout)   (None, 1)     0       input_1[0][0]          
___________________________________________________________________
dropout_2 (Dropout)   (None, 1)     0       input_1[0][0]          
===================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
___________________________________________________________________

推荐答案

这是一个好问题,但是要回答这个问题,我们必须深入研究Keras中各层如何相互连接的内部原理.因此,让我们开始吧:

That's a good question, however to answer it we must dive into the internals of how layers are connected to each other in Keras. So let's start:

0)什么是张量?

张量是表示数据的数据结构,它们基本上是n维数组.层之间传递的所有数据和信息都必须是张量.

A tensor is a data structure that represent data and they are basically n-dimensional arrays. All the data and information passed between layers must be tensors.

1)什么是图层?

从最简单的意义上讲,层是一个计算单元,其中它获得一个或多个输入张量,然后对其施加一组运算(例如乘法,加法等),并给出结果是一个或多个输出张量.当您在某些输入张量上应用图层时,在引擎盖下会创建一个节点.

In the simplest sense, a layer is a computation unit where it gets one or more input tensors, then applies a set of operations (e.g. multiplication, addition, etc.) on them, and gives the result as one or more output tensors. When you apply a layer on some input tensors, under the hood a Node is created.

2)那么什么是节点?

为了表示两层之间的连通性,Keras内部使用了 Node 类.将图层应用于某些新输入时,将创建一个节点并将其添加到该图层的_inbound_nodes属性中.此外,当一层的输出被另一层使用时,会创建一个新节点并将其添加到该层的_outbound_nodes属性.因此,从本质上讲,此数据结构使Keras可以使用Node类型的对象的以下属性来查找各层之间的连接方式:

To represent the connectivity between two layers, Keras internally uses an object of Node class. When a layer is applied on some new input, a node is created and is added to the _inbound_nodes property of that layer. Further, when the output of a layer is used by another layer, a new node is created and is added to _outbound_nodes property of that layer. So essentially, this data structure lets Keras to find out how layers are connected to each other using the following properties of an object of type Node:

  • input_tensors:这是一个包含节点输入张量的列表.
  • output_tensors:这是一个包含节点输出张量的列表.
  • inbound_layers:这是一个列表,其中包含input_tensors所在的图层.
  • outbound_layers:使用者层,即采用input_tensors并将其转换为output_tensors的层.
  • node_indices:这是一个整数列表,其中包含input_tensors的节点索引(将在以下问题的答案中对此进行详细说明).
  • tensor_indices:这是一个整数列表,其中包含input_tensors在它们相应的入站层中的索引(将在下面的问题的答案中对此进行解释).
  • input_tensors: it is a list containing the input tensors of the node.
  • output_tensors: it is a list containing the output tensors of the node.
  • inbound_layers: it is a list which contains the layers where the input_tensors come from.
  • outbound_layers: the consumer layers, i.e. the layers that takes input_tensors and turns them into output_tensors.
  • node_indices: it is a list of integers which contains the node indices of input_tensors (would explain this more in the answer to the following question).
  • tensor_indices: it is a list of integers which contains the indices of input_tensors within their corresponding inbound layer (would explain this more in the answer to the following question).

3)很好!现在告诉我模型摘要的连接到"列中的那些值是什么意思?

为了更好地理解这一点,让我们创建一个简单的模型.首先,让我们创建两个输入层:

To better understand this, let's create a simple model. First, let's create two input layers:

inp1 = Input((10,))
inp2 = Input((20,))

接下来,我们创建一个具有两个输出张量的Lambda层,第一个输出是输入张量除以2,第二个输出是输入张量乘以2:

Next, we create a Lambda layer that has two output tensors, the first output is the input tensor divided by 2, and the second output is the input tensor multiplied by 2:

lmb_layer = Lambda(lambda x: [x/2, x*2])

让我们在inp1inp2上应用此lambda层:

Let's apply this lambda layer on inp1 and inp2:

a1, b1 = lmb_layer(inp1)
a2, b2 = lmb_layer(inp2)

完成此操作后,已创建两个节点并将其添加到lmb_layer_inbound_nodes属性中:

After doing this, two nodes have been created and added to _inbound_nodes property of lmb_layer:

>>> lmb_layer._inbound_nodes
[<keras.engine.base_layer.Node at 0x7efb9a105588>,
 <keras.engine.base_layer.Node at 0x7efb9a105f60>]

第一节点对应于lmb_layer与第一输入层(inp1)的连通性,第二节点对应于该层与第二输入层(inp2)的连通性.此外,这些节点中的每个节点都有两个输出张量(对应于a1b1a2b2):

The first node corresponds to the connectivity of the lmb_layer with the first input layer (inp1) and the second node corresponds to the connectivity of this layer with the second input layer (inp2). Further, each of those nodes have two output tensors (corresponding to a1,b1 and a2,b2):

>>> lmb_layer._inbound_nodes[0].output_tensors
[<tf.Tensor 'lambda_1/truediv:0' shape=(?, 10) dtype=float32>,
 <tf.Tensor 'lambda_1/mul:0' shape=(?, 10) dtype=float32>]

>>> lmb_layer._inbound_nodes[1].output_tensors
[<tf.Tensor 'lambda_1_1/truediv:0' shape=(?, 20) dtype=float32>,
 <tf.Tensor 'lambda_1_1/mul:0' shape=(?, 20) dtype=float32>]

现在,让我们创建并应用四个不同的Dense层,并将其应用于我们获得的四个输出张量:

Now, let's create and apply four different Dense layers and apply them on the four output tensors we obtained:

d1 = Dense(10)(a1)
d2 = Dense(20)(b1)
d3 = Dense(30)(a2)
d4 = Dense(40)(b2)

model = Model(inputs=[inp1, inp2], outputs=[d1, d2, d3, d4])
model.summary()

模型摘要如下所示:

Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            (None, 10)           0                                            
__________________________________________________________________________________________________
input_2 (InputLayer)            (None, 20)           0                                            
__________________________________________________________________________________________________
lambda_1 (Lambda)               multiple             0           input_1[0][0]                    
                                                                 input_2[0][0]                    
__________________________________________________________________________________________________
dense_1 (Dense)                 (None, 10)           110         lambda_1[0][0]                   
__________________________________________________________________________________________________
dense_2 (Dense)                 (None, 20)           220         lambda_1[0][1]                   
__________________________________________________________________________________________________
dense_3 (Dense)                 (None, 30)           630         lambda_1[1][0]                   
__________________________________________________________________________________________________
dense_4 (Dense)                 (None, 40)           840         lambda_1[1][1]                   
==================================================================================================
Total params: 1,800
Trainable params: 1,800
Non-trainable params: 0
__________________________________________________________________________________________________

在图层的连接到"列中,值的格式为:layer_name[x][y]. layer_name对应于此层的输入张量所来自的层.例如,所有Dense层都连接到lmb_layer,因此从该层获取其输入. [x][y]分别对应于输入张量的节点索引(即node_indices)和张量索引(即tensor_indices).例如:

In the "Connected to" column for a layer the values have a format of: layer_name[x][y]. The layer_name corresponds to the layer where the input tensors of the this layer comes from. For example, all the Dense layers are connected to lmb_layer and therefore get their inputs from this layer. The [x][y] corresponds to the node index (i.e. node_indices) and tensor index (i.e. tensor_indices) of the the input tensors, respectively. For example:

  • dense_1层应用于a1,这是lmb_layer的第一个(即索引:0)入站节点的第一个(即索引:0)输出张量,因此显示连通性如:lambda_1[0][0].

  • The dense_1 layer is applied on a1 which is the first (i.e. index: 0) output tensor of the first (i.e. index: 0) inbound node of lmb_layer, therefore the connectivity is displayed as: lambda_1[0][0].

dense_2层应用于b1,这是lmb_layer的第一个(即索引:0)入站节点的第二个(即索引:1)输出张量,因此显示连通性如:lambda_1[0][1].

The dense_2 layer is applied on b1 which is the second (i.e. index: 1) output tensor of the first (i.e. index: 0) inbound node of lmb_layer, therefore the connectivity is displayed as: lambda_1[0][1].

dense_3层应用于a2,这是lmb_layer的第二个入站节点(即索引:1)的第一个(即索引:0)输出张量,因此显示连通性如:lambda_1[1][0].

The dense_3 layer is applied on a2 which is the first (i.e. index: 0) output tensor of the second (i.e. index: 1) inbound node of lmb_layer, therefore the connectivity is displayed as: lambda_1[1][0].

dense_4层应用于b2,这是lmb_layer的第一个(即索引:1)入站节点的第二个(即索引:1)输出张量,因此显示连通性如:lambda_1[1][1].

The dense_4 layer is applied on b2 which is the second (i.e. index: 1) output tensor of the first (i.e. index: 1) inbound node of lmb_layer, therefore the connectivity is displayed as: lambda_1[1][1].

就是这样!如果您想进一步了解summary方法的工作原理,可以查看

That's it! If you want to know more how summary method works, you can take a look at the print_summary function. And if you want to find out how the connections are printed, you can take a look at the print_layer_summary_with_connections function.

这篇关于"[0] [0]"是什么意思? keras模型中连接的各层的层数.摘要是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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