tensorflow - 这个 Dense 层是如何计算它的维度的?
问题描述
layer = tf.keras.layers.Dense(10, input_shape=(None, 5))
x = layer(tf.zeros([10,5]))
x 是 [10,10] 矩阵。这是为什么?为什么不是 [10,5]?它似乎在内部执行 [10,5] * [5,10]。
Dense 层如何具有 [5,10] 的形状?
解决方案
You're passing in an input of shape (None, 5)
Then you have a dense layer with 10 units. This dense layer have two sets of trainable parameters.
kernel => A (5,10) Matrix
bias => A (10) vector
The dense layer know the correct shape to construct because, you're passing the input_shape
parameter.
Then when you call layer(tf.zeros([10,5]))
, it does the following computation.
matmul(input[10,5], kernel[5, 10]) + bias[1, 10] => x[10,10]
So as you can see, it results in a [10,10]
matrix.
推荐阅读
- javascript - 向 Flask 提交多个表单(400 BAD REQUEST)
- javascript - 如何将对象作为参数传递并在构造函数中引用它
- c++ - 你可以为一个没有创建的闭包添加一个上值吗?
- java - 如何在特定时区的特定语言环境中为人类格式化瞬间
- python - 在标题和正文中加载具有不同分隔符的 .tab 文件
- python - 带有使用 config.py 的 Python 烧瓶应用程序的 Gitlab CI?
- machine-learning - OneHotEncoding 2500 个不同的分类变量
- macos - 如何在本地启动石墨 docker 容器?
- python - 三个不同的列表合二为一
- kotlin - 如何使用正确的可空值获取 TypeName