Search code examples
filtertensorflowdeep-learningconvolution

difference between convolution2d and conv2d in tensorflow in terms of ussage


In TensorFlow for 2D convolution we have:

tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None,
             data_format=None, name=None) 

and

tf.contrib.layers.convolution2d(*args, **kwargs)
  • I am not sure about differences?
  • I know that I should use the first one if I want to use a special filter, right? But what else? Especially about outputs?

Thank you


Solution

  • tf.nn.conv2d(...) is the core, low-level convolution functionality provided by TensorFlow. tf.contrib.layers.conv2d(...) is part of a higher-level API build around core-TensorFlow.

    Note, that in current TensorFlow versions, parts of layers are now in core, too, e.g. tf.layers.conv2d.

    The difference is simply, that tf.nn.conv2d is an op, that does convolution, nothing else. tf.layers.conv2d does more, e.g. it also creates variables for the kernel and the biases amongst other things.

    Check out the Tensorflow Tutorial on CNNs which uses Tensorflow core (here). With the low-level API the convolutional layers are created like this:

    def conv2d(x, W):
        return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
    
    W_conv1 = weight_variable([5, 5, 1, 32])
    b_conv1 = bias_variable([32])
    h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
    

    Compare that to the TF Layers Tutorial for CNNs (here). With TF Layers convolutional layers are create like this:

    conv1 = tf.layers.conv2d(
      inputs=input_layer,
      filters=32,
      kernel_size=[5, 5],
      padding="same",
      activation=tf.nn.relu)
    

    Without knowing your use case: Most likely you want to use tf.layers.conv2d.