tf.nn.conv2d与tf.layers.conv2d

2024-03-28 16:52:49 发布

您现在位置:Python中文网/ 问答频道 /正文

使用tf.nn.*比使用tf.layers.*有什么优势吗?

例如,doc中的大多数示例使用tf.nn.conv2d,但不清楚它们为什么这样做。


Tags: 示例doclayerstfnn优势conv2d
3条回答

正如其他人提到的,参数是不同的,特别是“过滤器”。conv2d使用一个张量作为过滤器,这意味着您可以在cifar10 code中指定权重衰减(或者其他属性)。(是否希望/需要在conv层中进行重量衰减是另一个问题。)

kernel = _variable_with_weight_decay('weights',
                                     shape=[5, 5, 3, 64],
                                     stddev=5e-2,
                                     wd=0.0)
conv = tf.nn.conv2d(images, kernel, [1, 1, 1, 1], padding='SAME')

我不太确定如何在tf.layers.conv2d中设置权重衰减,因为它只接受一个整数作为过滤器。也许用kernel_constraint

另一方面,tf.layers.conv2d会自动处理激活和偏移,而如果使用tf.nn.conv2d,则必须为它们编写附加代码

对于卷积,它们是相同的。更准确地说,tf.layers.conv2d(实际上是_Conv)使用tf.nn.convolution作为后端。你可以按照呼叫链:tf.layers.conv2d>Conv2D>Conv2D.apply()>_Conv>_Conv.apply()>_Layer.apply()>_Layer.\__call__()>_Conv.call()>nn.convolution()...

正如GBY提到的,它们使用相同的实现。

参数略有不同。

对于tf.nn.conv2d:

filter: A Tensor. Must have the same type as input. A 4-D tensor of shape [filter_height, filter_width, in_channels, out_channels]

对于tf.layers.conv2d:

filters: Integer, the dimensionality of the output space (i.e. the number of filters in the convolution).

在加载预先训练的模型(示例代码:https://github.com/ry/tensorflow-vgg16)时,我将使用tf.nn.conv2d,对于从头开始训练的模型,我将使用tf.layers.conv2d。

相关问题 更多 >