tf.layers.dense和tf.nn.xw_plus_b之间的区别

SUD*_*193 4 python python-3.x tensorflow

tf.layers.densetf.nn.xw_plus_bin有TF什么区别?将tf.layers.dense“ activation”参数传递为时,默认的激活方式是None什么?

dm0*_*m0_ 6

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

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

根据文档,默认激活为线性(无激活)。

activation:激活功能(可调用)。将其设置为None以保持线性激活。

更新资料

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

@tf_export('layers.Dense')
class Dense(keras_layers.Dense, base.Layer):
Run Code Online (Sandbox Code Playgroud)

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

  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
Run Code Online (Sandbox Code Playgroud)

因此,它不是通过tf.nn.xw_plus_b使用两个单独的操作来实现的。

要回答您的问题:Dense没有激活的图层,约束和正则化应与相同tf.nn.xw_plus_b