Python – How do I make a custom TensorFlow tf.nn.conv2d()?

How do I make a custom TensorFlow tf.nn.conv2d()?… here is a solution to the problem.

How do I make a custom TensorFlow tf.nn.conv2d()?

Almost identical to the existing conv2d(), but I would like to add a special feature to it.

Suppose we have a filter of shape 5×5 and an input of shape 100×100.

As I understand it, conv2d() does it internally:

  1. Select a 5×5 input area from the entire input.
  2. Calculate the convolution between the 5×5 input region and the filter.
  3. Move to the next input area at a given stride.

In contrast, customized_conv2d wants to do internally :

  1. Select a 5×5 input area from the entire input. (same).
  2. Subtract the

  3. scalar value of f (5×5 input range) from each value in the 5×5 input range. (Supplement).
  4. Calculate the convolution between the subtraction of the 5×5 input region and the filter. (same).
  5. Move to the next input area at a given stride. (same).

where f(x) is (max(x)

+ min(x))/2

  • Is it easy to do?
  • Can custom code run on the GPU?

Browse the relevant source code of nn_ops.py, gen_nn_ops.py, and nn_ops.cc for help.

Thanks in advance!

Append:

What I’ve learned so far :

1) Probably the easiest way to run on a CPU is to make a custom TensorFlow. Adjust as little as possible, tensorflow/core/kernels/ conv_ops.cc and tensorflow/ core/kernels/deep_conv2d.cc. With this particular TensorFlow, conv2d() behaves as expected, not the way it was. In this case, adding a new operation by copying the relevant line and renaming all functions/methods from the python wrapper to C++ code, such as customized_conv2d() may be excessive effort.

2) There is no hope of running on a GPU via CUDA. It seems that TensorFlow’s conv2d() was eventually called cudnnConvolutionForward() In the NVIDIA CUDA Deep Neural Network Library (cuDNN). The main part of conv2d() is done in this library. Software License Agreement (SLA) for NVIDIA cuDNN does not allow reverse engineering or modification. No source code for the library is provided.

3) However, there may be another hope for running on a GPU via OpenCL.

Solution

Since convolution itself is linear, inserting any linear operation can be done by first convolving via tf.nn.conv2d and then doing that operation.

(

max(x) + min(x))/2 on each 5x5 patch can pass (tf.nn.max_pool(x) – tf.nn.max_pool(-x)) * 0.5. To subtract it after conv, you also need to multiply the result by the sum of the corresponding convolution kernels. After that, you can apply a nonlinear activation function.

But, in general, I don’t know how to efficiently add non-linear operations like getting the z-score for each 5x5 patch before convolution multiplication. Perhaps other answers can provide some insight.

Related Problems and Solutions