tf.layers.conv2d和tf.contrib.slim.conv2d之间的区别

9

我正在尝试将我正在使用的网络从使用tf-slim的conv2d转换为使用tf.layers.conv2d,因为看起来tf.layers是更受支持和未来可靠的选项。函数签名非常相似,但两者之间在算法上是否有什么不同呢?我得到了与预期不同的输出张量维度。

x = tf.layers.conv2d(inputs=x,
                     filters=256,
                     kernel_size=[3,3],
                     trainable=True)

与此相反:
x = slim.conv2d(x, 256, 3)
2个回答

17

我得到的输出张量维度与预期不同。

这是因为默认情况下,slim.conv2d使用same填充方式,而tf.layers.conv2d使用valid填充方式。

如果你想要复制完全相同的行为,这里是正确的实现:

x = tf.layers.conv2d(x, 256, 3, padding='same')

谢谢!问题已解决。 - Drew M

6

tf.slim包的描述可以更好地解释它与普通TensorFlow的区别:具体来说,在“层”下面,可以找到以下内容:

Layers

While the set of TensorFlow operations is quite extensive, developers of neural networks typically think of models in terms of higher level concepts like "layers", "losses", "metrics", and "networks". A layer, such as a Convolutional Layer, a Fully Connected Layer or a BatchNorm Layer are more abstract than a single TensorFlow operation and typically involve several operations. Furthermore, a layer usually (but not always) has variables (tunable parameters) associated with it, unlike more primitive operations. For example, a Convolutional Layer in a neural network is composed of several low level operations:

  • Creating the weight and bias variables
  • Convolving the weights with the input from the previous layer
  • Adding the biases to the result of the convolution.
  • Applying an activation function.

Using only plain TensorFlow code, this can be rather laborious:

input = ...
with tf.name_scope('conv1_1') as scope:
  kernel = tf.Variable(tf.truncated_normal([3, 3, 64, 128], dtype=tf.float32,
                                           stddev=1e-1), name='weights')
  conv = tf.nn.conv2d(input, kernel, [1, 1, 1, 1], padding='SAME')
  biases = tf.Variable(tf.constant(0.0, shape=[128], dtype=tf.float32),
                       trainable=True, name='biases')
  bias = tf.nn.bias_add(conv, biases)
  conv1 = tf.nn.relu(bias, name=scope)

To alleviate the need to duplicate this code repeatedly, TF-Slim provides a number of convenient operations defined at the more abstract level of neural network layers. For example, compare the code above to an invocation of the corresponding TF-Slim code:

input = ...
net = slim.conv2d(input, 128, [3, 3], scope='conv1_1')
简而言之,slim操作符为您提供了一些巧妙的抽象,使您不必担心TensorFlow的所有细节 - 如果您问我,这是一个很好的补充。然而,似乎这仍在积极开发中,因此在(未来)开发中积极使用它之前,我建议您再多了解一些相关知识。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接