通过嵌套tf.map_fn反向传播渐变

问题描述 投票:3回答:1

我想在与尺寸为[[batch_size,H,W,n_channels]]的矩阵中每个像素的深度通道对应的每个向量上映射一个TensorFlow函数。 换句话说,对于我批量处理的每个尺寸为[[H x W

的图像:

我提取了一些具有相同大小

    H x W]的特征图
  1. F_k

(其数量为n_channels)(因此,所有特征图一起都是形状为[H,W,n_channels ];然后,我希望将自定义函数应用于与每个特征图F_k
  • i-th
  • 行和j-th列关联的向量v_ij ,但整体上探讨了深度通道(例如v的尺寸为[1 x 1 x n_channels])。理想情况下,所有这些都将并行发生。
    下面是解释该过程的图片。与图片的唯一区别是输入和输出“接收场”的大小均为1x1(将功能独立应用于每个像素)。 enter image description here

    这类似于将1x1卷积应用于矩阵;但是,我需要在深度通道上应用更通用的功能,而不是简单的求和运算。

    我认为tf.map_fn()可能是一个选项,我尝试了以下解决方案,在该解决方案中,我递归使用tf.map_fn()访问与每个像素相关的特征。但是,这种情况似乎不太理想,最重要的是

    在尝试反向传播渐变时会引发错误

    您是否知道发生这种情况的原因,以及如何构造代码以避免错误?这是我当前对该函数的实现:

    import tensorflow as tf from tensorflow import layers def apply_function_on_pixel_features(incoming): # at first the input is [None, W, H, n_channels] if len(incoming.get_shape()) > 1: return tf.map_fn(lambda x: apply_function_on_pixel_features(x), incoming) else: # here the input is [n_channels] # apply some function that applies a transfomration and returns a vetor of the same size output = my_custom_fun(incoming) # my_custom_fun() doesn't change the shape return output

    和我的代码正文:

    H = 128
    W = 132
    n_channels = 8
    
    x1 = tf.placeholder(tf.float32, [None, H, W, 1])
    x2 = layers.conv2d(x1, filters=n_channels, kernel_size=3, padding='same')
    
    # now apply a function to the features vector associated to each pixel
    x3 = apply_function_on_pixel_features(x2)  
    x4 = tf.nn.softmax(x3)
    
    loss = cross_entropy(x4, labels)
    optimizer = tf.train.AdamOptimizer(lr)
    train_op = optimizer.minimize(loss)  # <--- ERROR HERE!
    

    尤其是以下错误:

    File "/home/venvs/tensorflowGPU/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2481, in AddOp
        self._AddOpInternal(op)
    
    File "/home/venvs/tensorflowGPU/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2509, in _AddOpInternal
        self._MaybeAddControlDependency(op)
    File "/home/venvs/tensorflowGPU/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2547, in _MaybeAddControlDependency
        op._add_control_input(self.GetControlPivot().op)
    
    AttributeError: 'NoneType' object has no attribute 'op'
    

    整个错误堆栈和代码可以在here中找到。感谢您的帮助,

    G。


    更新:按照@ thushv89的建议,我为该问题添加了可能的解决方案。我仍然不知道为什么以前的代码不起作用。任何对此的见解将不胜感激。

    我想在一个尺寸为[batch_size,H,W,n_channels]的矩阵中,在与每个像素的深度通道相对应的每个向量上映射一个TensorFlow函数。换句话说,对于每个图像...

    tensorflow nested gradient backpropagation map-function
    1个回答
    0
    投票
    def apply_function_on_pixel_features(incoming, batch_size): # get input shape: _, W, H, C = incoming.get_shape().as_list() incoming_flat = tf.reshape(incoming, shape=[batch_size * W * H, C]) # apply function on every vector of shape [1, C] out_matrix = my_custom_fun(incoming_flat) # dimension remains unchanged # go back to the input shape shape [None, W, H, C] out_shape = tf.convert_to_tensor([batch_size, W, H, C]) out_matrix = tf.reshape(out_matrix, shape=out_shape) return out_matrix
    © www.soinside.com 2019 - 2024. All rights reserved.