python - tf.layers.dense 和 tf.nn.xw_plus_b 的区别

标签 python python-3.x tensorflow

TF中的tf.layers.densetf.nn.xw_plus_b有什么区别? 当“activation”参数作为 None 传递时,tf.layers.dense 中使用的默认激活是什么?

最佳答案

tf.nn.xw_plus_b 是一种低级操作,仅计算 x*W+b 并需要现有变量。

tf.layers.dense 是创建变量的高级“层”,应用激活可以设置约束并应用正则化。

根据documentation默认激活是线性的(无激活)。

activation: Activation function (callable). Set it to None to maintain a linear activation.

更新

在 Tensorflow 1.12 Dense 层继承 keras.layers.Dense ( code ):

@tf_export('layers.Dense')
class Dense(keras_layers.Dense, base.Layer):

该层的 Keras 实现执行以下操作(code):

  def call(self, inputs):
    inputs = ops.convert_to_tensor(inputs, dtype=self.dtype)
    rank = common_shapes.rank(inputs)
    if rank > 2:
      # Broadcasting is required for the inputs.
      outputs = standard_ops.tensordot(inputs, self.kernel, [[rank - 1], [0]])
      # Reshape the output back to the original ndim of the input.
      if not context.executing_eagerly():
        shape = inputs.get_shape().as_list()
        output_shape = shape[:-1] + [self.units]
        outputs.set_shape(output_shape)
    else:
      outputs = gen_math_ops.mat_mul(inputs, self.kernel)
    if self.use_bias:
      outputs = nn.bias_add(outputs, self.bias)
    if self.activation is not None:
      return self.activation(outputs)  # pylint: disable=not-callable
    return outputs

所以它不是使用tf.nn.xw_plus_b实现的,而是使用了两个独立的操作。

回答你的问题:没有激活、约束和正则化的Dense层应该和tf.nn.xw_plus_b一样。

关于python - tf.layers.dense 和 tf.nn.xw_plus_b 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53760992/

相关文章:

python - 如何在 python 中取消引用 urlencoded unicode 字符串?

python - 如果重复的项目按特定顺序出现,则从列表中删除它们

tensorflow - 如何想象在具有 3 个颜色 channel 的图像上进行卷积/池化

python - 导入错误 : No module named 'tensorflow.contrib.lite.python.tflite_convert'

python - 当使用多个样本时,如何通过 Keras 使用多元时间序列预测

python - Selenium WebDriver python : checkbox cannot click, 默认复选框不可见,悬停时可见

python - 使用 if-return-return 还是 if-else-return 效率更高?

python - 基于PHP代码在Python中创建MD5哈希

python-3.x - 为什么 Cython 中的这个循环和它的 Python 等效循环一样慢?

python - 覆盖子类 Python 枚举中的方法