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

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

问题描述

如下表所示,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).此外,每个节点都有两个输出张量(对应于 a1,b1a2,b2):

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 上,它是第一个(即索引:0)入站节点的第一个(即索引:0)输出张量lmb_layer,因此连通性显示为: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 上,它是第一个(即索引:0)入站节点的第二个(即索引:1)输出张量lmb_layer,因此连通性显示为: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 上,它是第二个(即索引:1)入站节点的第一个(即索引:0)输出张量lmb_layer,因此连通性显示为: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,它是第一个(即索引:1)入站节点的第二个(即索引:1)输出张量lmb_layer,因此连通性显示为: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 方法的工作原理,可以查看 print_summary 函数.如果您想了解连接的打印方式,可以查看 print_layer_summary_with_connections 函数.

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 model.summary 中连接的层数是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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