Understanding the architecture of the Keras model (tensor index)

This script defines a dummy using a functional API

from keras.layers import Input, Dense
from keras.models import Model
import keras

inputs = Input(shape=(100,), name='A_input')
x = Dense(20, activation='relu', name='B_dense')(inputs)
shared_l = Dense(20, activation='relu', name='C_dense_shared')
x = keras.layers.concatenate([shared_l(x), shared_l(x)], name='D_concat')

model = Model(inputs=inputs, outputs=x)
print(model.summary())

displays the next output

____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
A_input (InputLayer)             (None, 100)           0                                            
____________________________________________________________________________________________________
B_dense (Dense)                  (None, 20)            2020        A_input[0][0]                    
____________________________________________________________________________________________________
C_dense_shared (Dense)           (None, 20)            420         B_dense[0][0]                    
                                                                   B_dense[0][0]                    
____________________________________________________________________________________________________
D_concat (Concatenate)           (None, 40)            0           C_dense_shared[0][0]             
                                                                   C_dense_shared[1][0]             
====================================================================================================

My question is about the contents of the column Connected to. I understand that a layer can have multiple nodes.

In this case, it C_dense_sharedhas two nodes, and is D_concatconnected to both of them ( C_dense_shared[0][0]and C_dense_shared[1][0]). Therefore, the first index ( node_index) is clear to me. But what does the second index mean? From the source code I read that it is tensor_index:

layer_name[node_index][tensor_index]

But what does it mean tensor_index? And in what situations can it have a value other than 0?

+4
source share
1 answer

, docstring Node :

    tensor_indices: a list of integers,
        the same length as `inbound_layers`.
        `tensor_indices[i]` is the index of `input_tensors[i]` within the
        output of the inbound layer
        (necessary since each inbound layer might
        have multiple tensor outputs, with each one being
        independently manipulable).

tensor_index , . " " (, ), . , LSTM 3 , return_state=True:

  • , return_sequences=True

Lambda:

def generate_powers(x):
    return [x, K.sqrt(x), K.square(x)]

model_input = Input(shape=(10,))
powers = Lambda(generate_powers)(model_input)
x = Concatenate()(powers)
x = Dense(10, activation='relu')(x)
x = Dense(1, activation='sigmoid')(x)
model = Model(model_input, x)

model.summary() , concatenate_5 lambda_7[0][0], lambda_7[0][1] lambda_7[0][2]:

____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
input_7 (InputLayer)             (None, 10)            0                                            
____________________________________________________________________________________________________
lambda_7 (Lambda)                [(None, 10), (None, 1 0           input_7[0][0]                    
____________________________________________________________________________________________________
concatenate_5 (Concatenate)      (None, 30)            0           lambda_7[0][0]                   
                                                                   lambda_7[0][1]                   
                                                                   lambda_7[0][2]                   
____________________________________________________________________________________________________
dense_8 (Dense)                  (None, 10)            310         concatenate_5[0][0]              
____________________________________________________________________________________________________
dense_9 (Dense)                  (None, 1)             11          dense_8[0][0]                    
====================================================================================================
Total params: 321 
Trainable params: 321 
Non-trainable params: 0
____________________________________________________________________________________________________
+3

Source: https://habr.com/ru/post/1683845/


All Articles