How to compute the backprop of tf.nn.conv2d without using tf.keras.layers.conv2d

Viewed 128

I'm trying to figure out how to backpropagate gradients from Conv2D, but T.gradient always returns None.

I'm using Python 3.7.5 and TensorFlow 2.2.0.

import tensorflow as tf
import numpy as np

arr = np.arange(0, 9, dtype = np.float32).reshape((1, 3, 3, 1))
kernel = np.ones((3, 3), dtype = np.float32).reshape((3, 3, 1, 1))

tf_arr = tf.constant(arr)
tf_kernel = tf.constant(kernel)

with tf.GradientTape(persistent = True, watch_accessed_variables = True) as T:
    tf_out =  tf.nn.conv2d(tf_arr, tf_kernel, [1, 1, 1, 1], padding = "SAME", data_format = 'NHWC')


print(T.gradient(tf_kernel, tf_arr))
0 Answers
Related