【问题标题】:why does this simple Keras model have two parameters?为什么这个简单的 Keras 模型有两个参数?
【发布时间】:2020-07-31 03:34:44
【问题描述】:

我试图了解 Keras 中的模型和图层是如何工作的。我创建了一个非常简单的模型,它具有 - 或者至少我的意图是它具有 - 一个输入、一个输出和它们之间的一个连接:

from keras import layers
from keras import models
model = models.Sequential()
model.add(layers.Dense(1, activation='linear', input_shape=(1,)))

我希望它有一个参数 - 输入乘以产生输出的数字 - 但是当我显示模型的摘要时,我看到它有 两个 参数:

model.summary()

Model: "sequential_7"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_7 (Dense)              (None, 1)                 2         
=================================================================
Total params: 2
Trainable params: 2
Non-trainable params: 0
_________________________________________________________________

另外,当我打印权重时,我看到其中两个:

model.get_weights()
[array([[0.02002084]], dtype=float32), array([0.], dtype=float32)]

为什么这个模型有两个参数?

【问题讨论】:

    标签: machine-learning keras neural-network


    【解决方案1】:

    另一个参数是偏差,它被添加到乘法结果中。我可以这样证明:

    import numpy as np
    weights = [np.asarray([[44]]), np.asarray([10])]
    model.set_weights(wagi)
    model.predict([1])
    
    array([[54.]], dtype=float32)
    

    【讨论】:

      【解决方案2】:

      获取参数个数的公式:

      output_size * (input_size+1) == 参数个数。

      在你的情况下, output_size 和 input_size 是 1 所以,参数个数是2。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-10
        • 1970-01-01
        • 2014-05-04
        • 2016-06-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多