gpt4 book ai didi

tensorflow - 通过嵌套的 tf.map_fn 反向传播梯度

转载 作者:行者123 更新时间:2023-12-03 20:59:05 28 4
gpt4 key购买 nike

我想在维度为 的矩阵中每个像素的深度 channel 对应的每个向量上映射一个 TensorFlow 函数。 [batch_size, H, W, n_channels] .

换句话说,对于我在批处理中拥有的每个大小为 H x W 的图像:

  • 我提取了一些具有相同大小 H x W 的特征图 F_k(其编号为 n_channels)(因此,特征图一起是形状为 [H, W, n_channels] 的张量;
  • 然后,我希望将自定义函数应用于与每个特征图 F_k 的第 i 行和第 j 列相关联的向量 v_ij,但要全面探索深度 channel (例如 v 具有维度 [1 x 1 x n_channels])。理想情况下,所有这些都将并行发生。

  • 可以在下面找到解释该过程的图片。与图片的唯一区别是输入和输出“感受野”的大小均为 1x1(将函数独立应用于每个像素)。

    enter image description here

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

    我想 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 的建议,我添加了一个可能的解决方案。我仍然不知道为什么我以前的代码不起作用。对此的任何见解仍将不胜感激。

    最佳答案

    @gabriele 关于必须依赖于 batch_size,您是否尝试过以下方式?此函数不依赖于 batch_size。您可以更换 map_fn随心所欲。

    def apply_function_on_pixel_features(incoming):

    # get input shape:
    _, W, H, C = incoming.get_shape().as_list()
    incoming_flat = tf.reshape(incoming, shape=[-1, C])

    # apply function on every vector of shape [1, C]
    out_matrix = tf.map_fn(lambda x: x+1, incoming_flat) # dimension remains unchanged

    # go back to the input shape shape [None, W, H, C]
    out_matrix = tf.reshape(out_matrix, shape=[-1, W, H, C])

    return out_matrix

    我测试的完整代码如下。
    import numpy as np
    import tensorflow as tf
    from tensorflow.keras.losses import categorical_crossentropy

    def apply_function_on_pixel_features(incoming):

    # get input shape:
    _, W, H, C = incoming.get_shape().as_list()
    incoming_flat = tf.reshape(incoming, shape=[-1])

    # apply function on every vector of shape [1, C]
    out_matrix = tf.map_fn(lambda x: x+1, incoming_flat) # dimension remains unchanged

    # go back to the input shape shape [None, W, H, C]
    out_matrix = tf.reshape(out_matrix, shape=[-1, W, H, C])

    return out_matrix

    H = 32
    W = 32
    x1 = tf.placeholder(tf.float32, [None, H, W, 1])
    labels = tf.placeholder(tf.float32, [None, 10])
    x2 = tf.layers.conv2d(x1, filters=1, 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.layers.flatten(x3)
    x4 = tf.layers.dense(x4, units=10, activation='softmax')

    loss = categorical_crossentropy(labels, x4)
    optimizer = tf.train.AdamOptimizer(0.001)
    train_op = optimizer.minimize(loss)


    x = np.zeros(shape=(10, H, W, 1))
    y = np.random.choice([0,1], size=(10, 10))


    with tf.Session() as sess:
    tf.global_variables_initializer().run()
    sess.run(train_op, feed_dict={x1: x, labels:y})

    关于tensorflow - 通过嵌套的 tf.map_fn 反向传播梯度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59094897/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com