形状中的“无”表示它没有预定义的数字。例如,它可以是您在训练期间使用的批次大小,并且您希望通过不为其分配任何值来使其灵活,以便可以更改批次大小。该模型将从图层的上下文中推断形状。
要使节点连接到每一层,可以执行以下操作:
for layer in model.layers:
print(layer.name, layer.inbound_nodes, layer.outbound_nodes)
0
形状中的“无”表示它没有预定义的数字。例如,它可以是您在训练期间使用的批次大小,并且您希望通过不为其分配任何值来使其灵活,以便可以更改批次大小。该模型将从图层的上下文中推断形状。
要使节点连接到每一层,可以执行以下操作:
for layer in model.layers:
print(layer.name, layer.inbound_nodes, layer.outbound_nodes)
0
对于密集层:
output_size * (input_size + 1) == number_parameters
对于转换层:
output_channels * (input_channels * window_size + 1) == number_parameters
考虑以下示例,
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=input_shape),
Conv2D(64, (3, 3), activation='relu'),
Conv2D(128, (3, 3), activation='relu'),
Dense(num_classes, activation='softmax')
])
model.summary()
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 222, 222, 32) 896
_________________________________________________________________
conv2d_2 (Conv2D) (None, 220, 220, 64) 18496
_________________________________________________________________
conv2d_3 (Conv2D) (None, 218, 218, 128) 73856
_________________________________________________________________
dense_9 (Dense) (None, 218, 218, 10) 1290
=================================================================
计算参数
assert 32 * (3 * (3*3) + 1) == 896
assert 64 * (32 * (3*3) + 1) == 18496
assert 128 * (64 * (3*3) + 1) == 73856
assert num_classes * (128 + 1) == 1290
0
我将514维实值输入提供给Keras中的Sequential
模型。我的模型按以下方式构造:
predictivemodel = Sequential()
predictivemodel.add(Dense(514, input_dim=514, W_regularizer=WeightRegularizer(l1=0.000001,l2=0.000001), init='normal'))
predictivemodel.add(Dense(257, W_regularizer=WeightRegularizer(l1=0.000001,l2=0.000001), init='normal'))
predictivemodel.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
当我打印model.summary()
,得到以下结果:
Layer (type) Output Shape Param # Connected to
================================================================
dense_1 (Dense) (None, 514) 264710 dense_input_1[0][0]
________________________________________________________________
activation_1 (None, 514) 0 dense_1[0][0]
________________________________________________________________
dense_2 (Dense) (None, 257) 132355 activation_1[0][0]
================================================================
Total params: 397065
________________________________________________________________
对于density_1层,参数数量为264710。其获得方式为:514(输入值)* 514(第一层中的神经元)+ 514(偏置值)
对于密集_2层,参数数目为132355。其获得为:514(输入值)* 257(第二层中的神经元)+ 257(第二层中神经元的偏置值)
0
参数的数量是7850,因为对于每个隐藏的单位,您都有784个输入权重和一个带偏置的连接权重。这意味着每个隐藏的单位都会为您提供785个参数。您有10个单位,因此总计为7850。
这个额外的偏差项的作用确实很重要。它大大增加了模型的容量。您可以阅读详细信息,例如此处的“偏差在神经网络中的作用” 。
0
我有一个简单的NN模型,用于使用Keras(Theano后端)从以python编写的28x28px图像中检测手写数字:
这样可以很好地运行,我的准确率约为90%。然后,我通过执行
print(model0.summary())
来执行以下命令,以获取网络结构的摘要。输出以下内容:我不明白它们如何达到7850个总参数,这实际上意味着什么?