如何理解Keras中一个简单的中性网络Python代码的密集层参数

use*_*911 2 python keras

import numpy as np
    from keras.models import Sequential
    from keras.layers.core import Dense, Activation

    # X has shape (num_rows, num_cols), where the training data are stored
    # as row vectors
    X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32)

    # y must have an output vector for each input vector
    y = np.array([[0], [0], [0], [1]], dtype=np.float32)

    # Create the Sequential model
    model = Sequential()

    # 1st Layer - Add an input layer of 32 nodes with the same input shape as
    # the training samples in X
    model.add(Dense(32, input_dim=X.shape[1]))

    # Add a softmax activation layer
    model.add(Activation('softmax'))

    # 2nd Layer - Add a fully connected output layer
    model.add(Dense(1))

    # Add a sigmoid activation layer
    model.add(Activation('sigmoid'))
Run Code Online (Sandbox Code Playgroud)

我是 Keras 的新手,正在努力理解它。

model.add(Dense(32, input_dim=X.shape[1]))32每个训练实例的均值有 32 个输入变量,其维度由 给出input_dim。但在输入 X 向量中,

array([[0., 0.],
       [0., 1.],
       [1., 0.],
       [1., 1.]], dtype=float32)
Run Code Online (Sandbox Code Playgroud)

有4个训练实例。看起来对于每个示例,只有两个输入变量。那么这如何对应于 Dense 层定义中的“32”呢?这个网络是什么样子的?

小智 5

如果你试试

model.summary()
Run Code Online (Sandbox Code Playgroud)

你会得到最后一个问题的答案。

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 32)                96        
_________________________________________________________________
activation_1 (Activation)    (None, 32)                0         
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 33        
_________________________________________________________________
activation_2 (Activation)    (None, 1)                 0         
=================================================================
Total params: 129
Trainable params: 129
Non-trainable params: 0
_________________________________________________________________
Run Code Online (Sandbox Code Playgroud)

网络输入是与dense_1层(32个节点)相连的2个节点(变量)。总共 32*2 个权重 + 32 个偏差为您提供 96 个参数。希望这可以帮助。