【问题标题】:Keras Custom Layer ValueError: An operation has `None` for gradientKeras Custom Layer ValueError:一个操作对渐变有“无”
【发布时间】:2020-05-28 15:12:17
【问题描述】:

我创建了一个自定义 Keras 层。该模型编译良好,但在训练时出现以下错误:

ValueError: 一个操作有None 用于梯度。请确保 您的所有操作都定义了渐变(即 可微)。没有梯度的常见操作:K.argmax,K.round, K.eval。

我的自定义层中是否有任何实现错误?

class SpatialLayer(Layer):

    def __init__(self, output_dim, **kwargs):
        self.output_dim = output_dim
        super(SpatialLayer, self).__init__(**kwargs)

    def build(self, input_shape):
        self.bias = None
        self.built = True
        self.kernelA = self.add_weight(name='kernelA', shape=(input_shape[1]-2, self.output_dim), initializer='uniform', trainable=True)

    def compute_output_shape(self, input_shape):
        return (input_shape[0], input_shape[1]-2, input_shape[1]-2, self.output_dim)


    def call(self, inputs):
        x_shape = tf.shape(inputs)
        top_values, top_indices = tf.nn.top_k(tf.reshape(inputs, (-1,)), 10, sorted=True,)
        top_indices = tf.stack(((top_indices // x_shape[1]), (top_indices % x_shape[1])), -1)
        top_indices = tf.cast(top_indices, dtype=tf.float32)
        t1 = tf.reshape(top_indices, (1,10,2))
        t2 = tf.reshape(top_indices, (10,1,2))
        result = tf.norm(t1-t2, ord='euclidean', axis=2)
        x = tf.placeholder(tf.float32, shape=[None, 10, 10, 1])
        tensor_zeros = tf.zeros_like(x)
        matrix = tensor_zeros + result
        return K.dot(matrix, self.kernelA)


    model = applications.VGG16(weights = "imagenet", include_top=False, input_shape = (img_width, img_height, 3))
    model.layers.pop()
    new_custom_layers = model.layers[-1].output
    model.layers[-1].trainable = False

    new_custom_layers = Conv2D(filters=1, kernel_size=(3, 3))(new_custom_layers)
    new_custom_layers = SpatialLayer(output_dim=1)(new_custom_layers)
    new_custom_layers = Flatten()(new_custom_layers)
    new_custom_layers = Dense(1024, activation="relu")(new_custom_layers)
    new_custom_layers = Dropout(0.5)(new_custom_layers)
    new_custom_layers = Dense(1024, activation="relu")(new_custom_layers)

任何帮助将不胜感激。

说明

我的自定义 Keras 层的输入是一个张量 (?, 12,12,1),它表示来自给定图像的特征图。 例如:

[[147.00  20.14 ... 0 34.2  0   ]
 [ 12.00  10.14 ... 0 45.2  0   ]
 ...
 [100.00  60.14 ... 0 34.2  99.1]
 [ 90.00  65.14 ... 0 12.2  00.1]]

我想从这个张量中获取前 10 个值的坐标, 例如:(0,0), (10,0) ...., (10,11),即 10 个坐标。

最后,我想计算坐标之间的距离矩阵。我正在使用欧几里得距离。 例如:

       coord1 coord2 ... coord9 cood10
coord1   0     12.3       13.1   2.3
coord2  1.3      0        3.2    9.1
  .
  .
  .
coord9  4.2     5.2        0     4.2
coor10  1.1     5.6       9.1     0

这个矩阵 (?, 10,10,1) 将是层输出。

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    您不能通过不可微分的函数进行反向传播。 而且您的功能不可微。

    您丢弃了值top_values,只保留了整数常量top_indices

    在模型中使用此层的唯一方法是,如果之前的所有内容都不可训练。 (或者,如果您找到另一种以可微分方式计算所需内容的方法 - 这意味着:必须涉及输入值的操作)

    【讨论】:

    • Daniel,我需要计算输入张量的前 10 个值之间的距离矩阵。这个矩阵将是层输出。你对我如何做到这一点有什么建议吗? Here 是我的另一篇文章中的距离矩阵示例。谢谢!
    • 它们是坐标对吗?您想如何对对进行排序?
    • 我编辑了帖子,解释了我打算在自定义层中做什么。我的目标是对图像进行分类。谢谢。
    • 我认为做你想做的事并不容易......另一方面,你可以尝试直接使用坐标而不是特征图。在这种情况下,你会很好地回答这个问题:stackoverflow.com/questions/59988185/…
    • 而且由于某种原因,我认为这与您的案例密切相关...stackoverflow.com/questions/60082775/… 这是 Kaggle 比赛吗? :p
    猜你喜欢
    • 2018-12-20
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    • 2016-07-21
    • 2015-06-22
    • 1970-01-01
    相关资源
    最近更新 更多