TF
中的tf.layers.dense
和tf.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/