gpt4 book ai didi

python - 如何理解 Keras 中简单神经网络 Python 代码的密集层参数

转载 作者:行者123 更新时间:2023-11-30 22:09:29 25 4
gpt4 key购买 nike

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'))

我是 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)

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

最佳答案

如果你尝试

model.summary()

您将得到上一个问题的答案。

_________________________________________________________________
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
_________________________________________________________________

网络输入是2个节点(变量),它们与dense_1层(32个节点)连接。总共 32*2 权重 + 32 个偏差为您提供 96 个参数。希望这会有所帮助。

关于python - 如何理解 Keras 中简单神经网络 Python 代码的密集层参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51912221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com