Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tensorflow:使用自定义渐变创建自定义二维图层_Python_Tensorflow_Gradient_Layer_Backpropagation - Fatal编程技术网

Python Tensorflow:使用自定义渐变创建自定义二维图层

Python Tensorflow:使用自定义渐变创建自定义二维图层,python,tensorflow,gradient,layer,backpropagation,Python,Tensorflow,Gradient,Layer,Backpropagation,我想要一个Conv2d层,它能够返回一个梯度,在某些索引处的值乘以0.2。原有的功能应该保持不变 我的想法是对Conv2d类进行子类化,并使用setter方法保存groundtruth为0的索引: import tensorflow as tf class BackpassFilter(tf.keras.layers.Layer): def __init__(self, kernel_initializer, num_filter=48, kernel_size=3, stride=1

我想要一个Conv2d层,它能够返回一个梯度,在某些索引处的值乘以0.2。原有的功能应该保持不变

我的想法是对Conv2d类进行子类化,并使用setter方法保存groundtruth为0的索引:

import tensorflow as tf

class BackpassFilter(tf.keras.layers.Layer):
    def __init__(self, kernel_initializer, num_filter=48, kernel_size=3, stride=1, padding='same', name='keypoints_3_4'):
    super(BackpassFilter, self).__init__()
    self.fc = tf.keras.layers.Conv2D(kernel_initializer, num_filter, kernel_size, stride, padding, name, kernel_initializer=kernel_initializer)

    def call(self, input):
        return self.fc(input)

    def add_filter_indices(self, ground_truth):
        self.tmp_indices = tf.where(tf.equal(ground_truth, 0))

    @tf.custom_gradient
    def custom_gradient(self, x):
        ...
我发现@tf.custom_梯度是我可以使用的。如何实现访问原始渐变、操纵和返回它们的方法?我是否必须以某种方式注册自定义渐变以便在类中使用