如果我理解正确,我认为这可以满足您的需求(在TF 2.x中,但在1.x中也是相同的):
import tensorflow as tf
kernel = tf.constant([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]], dtype=tf.float32)
img = tf.reshape(tf.range(90, dtype=tf.float32), [1, 5, 6, 3])
kernel_t = tf.tile(kernel[:, :, tf.newaxis, tf.newaxis], [1, 1, 3, 1])
eye = tf.eye(3, 3, batch_shape=[1, 1])
conv = tf.nn.separable_conv2d(img, kernel_t, eye, strides=[1, 1, 1, 1], padding='SAME')
result = conv * img - kernel[1, 1] * img * (img - 1)
kernel_np = kernel.numpy()
img_np = img.numpy()
result_np = result.numpy()
i, j, c = 3, 4, 1
v = img_np[0, i, j, c]
img_w = img_np[0, i - 1:i + 2, j - 1:j + 2, c]
kernel_scaled = kernel_np * v
kernel_scaled[1, 1] = kernel_np[1, 1]
val_test = (img_w * kernel_scaled).sum()
val_result = result_np[0, i, j, c]
print(val_test == val_result)