keras.layers.LSTM, Dense, etc. transfer input_ The shape parameter is given to the first layer

@Created on: 20210413
@Modified on: 20210413

1. Background

At keras In the Sequential sequential model API of layers, the Sequential model is a linear stack of multiple network layers. You can pass the list of layers to the Sequential constructor. The methods and properties include:

  • model.layers is a flattened list that contains the network layer of the model.
  • model.inputs is a list of model input tensors.
  • model.outputs is a list of model output tensors.

But keras There is no explicit input in the network layer of layers (including Dense, LSTM, ConvLSTM2D, etc.)_ Shape parameter.

  • Why set it like this;
  • What other similar parameters can be set?

2. Specifies the size of the input data

The model needs to know the size of its expected input. For this reason, the first layer in the sequential model (only the first layer, because the lower layer can automatically infer the size) needs to receive information about its input size. There are several ways to do this:

  • Pass an input_ The shape parameter is given to the first layer. It is a tuple representing size (an integer or a tuple of None, where None may be any positive integer). In input_ The size of the batch that does not contain data in the shape.
  • Some 2D layers, such as density, support input through the parameter_ Dim specifies the input size, and some 3D timing layers support input_dim and input_length parameter.
  • If you need to specify a fixed batch size for your input (which is useful for stateful RNNs), you can pass a batch_ The size parameter gives a layer. If you also batch_size=32 and input_ If shape = (6,8) is passed to a layer, the size of each batch of input is (32,6,8).
    Therefore, the following code snippet is equivalent:
model = Sequential()
model.add(Dense(32, input_shape=(784,)))
model = Sequential()
model.add(Dense(32, input_dim=784))

The above reference is from:
keras-docs-zh-master_ Textversion -

3. There is no explicit input in the core network layer_ shape, input_ How to pass the dim parameter?

I use tensorflow 2.3.0, and the corresponding keras version is 2.31 (possibly).
input_shape, input_dim parameters are passed through * * kwargs.
Take LSTM as an example:

  • The API interface of LSTM is inherited from recurrent LSTM´╝Ť
  • recurrent.LSTM inherits from RNN(Layer): there is input in it_ Source of shape;
  • RNN(Layer) inherits from Layer: there are allowed dictionary keys in it.
@keras_export('keras.layers.LSTM', v1=[])
class LSTM(recurrent.DropoutRNNCellMixin, recurrent.LSTM):

class LSTM(RNN):

class RNN(Layer):
    if 'input_shape' not in kwargs and (
        'input_dim' in kwargs or 'input_length' in kwargs):
      input_shape = (kwargs.pop('input_length', None),
                     kwargs.pop('input_dim', None))
      kwargs['input_shape'] = input_shape

class Layer(module.Module, version_utils.LayerVersionSelector):
allowed_kwargs = {

The above is from the official code.

4. * args and * * kwargs

4.1 *args usage

*Args is to pass a variable parameter list to the function argument. The number of this parameter list is unknown, and even the length can be 0. The following code demonstrates how to use args.

def test_args(first, *args):
    print('Required argument: ', first)
    for v in args:
        print ('Optional argument: ', v)

test_args(1, 2, 3, 4)

The first parameter is a parameter that must be passed in, so the first formal parameter is used, while the last three parameters are passed in as an argument as a variable parameter list and are used as a tuple. The running results of the code are as follows:

Required argument:  1
<class 'tuple'>
Optional argument:  2
Optional argument:  3
Optional argument:  4

4.2 usage of * * kwargs

**Kwargs is to pass the dictionary of a variable keyword parameter to the function argument. Similarly, the length of the parameter list can be 0 or other values. The following code demonstrates how to use kwargs.

def test_kwargs(first, *args, **kwargs):
   print('Required argument: ', first)
   for v in args:
      print ('Optional argument (args): ', v)
   for k, v in kwargs.items():
      print ('Optional argument %s (kwargs): %s' % (k, v))

test_kwargs(1, 2, 3, 4, k1=5, k2=6)

As mentioned earlier, args type is a tuple, while kwargs is a dictionary dict, and args can only be in front of kwargs. The running results of the code are as follows:

Reference from: * args and * * kwargs in Python

Keywords: Machine Learning Deep Learning

Added by bandit8 on Tue, 08 Mar 2022 01:09:42 +0200