简体   繁体   English

如何在TF2.0中使用自定义渐变创建keras层?

[英]How to create a keras layer with a custom gradient in TF2.0?

since in TensorFlow 2.0 they plan on unifying all high-level APIs under keras (which I'm not much familiar with) and removing Sessions altogether, I was wondering:由于在 TensorFlow 2.0 中,他们计划统一 keras 下的所有高级 API(我不太熟悉)并完全删除 Sessions,我想知道:

How can I create a custom keras layer that has a custom gradient?如何创建具有自定义渐变的自定义 keras 层?

I've seen the (quite limited) guide on creating custom layers in keras but it doesn't describe what we should do if we want our operation to have a custom gradient.我已经看到了在 keras 中创建自定义层的(相当有限的)指南,但它没有描述如果我们希望我们的操作具有自定义渐变我们应该做什么。

First of all, the "unification" of the APIs (as you call it) under keras doesn't prevent you from doing things like you did in TensorFlow 1.x.首先,keras 下 API 的“统一”(如您所称)并不会阻止您像在 TensorFlow 1.x 中那样做的事情。 Sessions might be gone but you can still define your model like any python function and train it eagerly without keras (ie through tf.GradientTape )会话可能会消失,但您仍然可以像任何 python 函数一样定义您的模型,并在没有 keras 的情况下急切地训练它(即通过tf.GradientTape

Now, if you want to build a keras model with a custom layer that performs a custom operation and has a custom gradient , you should do the following:现在,如果您想使用自定义层构建一个执行自定义操作并具有自定义渐变的 keras 模型,您应该执行以下操作:

a) Write a function that performs your custom operation and define your custom gradient. a) 编写一个函数来执行您的自定义操作并定义您的自定义渐变。 More info on how to do this here .有关如何在 此处执行此操作的更多信息。

@tf.custom_gradient
def custom_op(x):
    result = ... # do forward computation
    def custom_grad(dy):
        grad = ... # compute gradient
        return grad
    return result, custom_grad

Note that in the function you should treat x and dy as Tensors and not numpy arrays (ie perform tensor operations)请注意,在函数中,您应该将xdy视为张量而不是numpy 数组(即执行张量操作)

b) Create a custom keras layer that performs your custom_op . b) 创建一个自定义 keras 层来执行您的custom_op For this example I'll assume that your layer doesn't have any trainable parameters or change the shape of its input, but it doesn't make much difference if it does.在这个例子中,我假设你的层没有任何可训练的参数或改变其输入的形状,但如果有的话,它也没有太大区别。 For that you can refer to the guide that you posted check this one .为此,您可以参考您发布的指南,查看 指南。

class CustomLayer(tf.keras.layers.Layer):
    def __init__(self):
        super(CustomLayer, self).__init__()

    def call(self, x):
        return custom_op(x)  # you don't need to explicitly define the custom gradient
                             # as long as you registered it with the previous method

Now you can use this layer in a keras model and it will work.现在你可以在 keras 模型中使用这个层,它会起作用。 For example:例如:

inp = tf.keras.layers.Input(input_shape)
conv = tf.keras.layers.Conv2D(...)(inp)  # add params like the number of filters
cust = CustomLayer()(conv)  # no parameters in custom layer
flat = tf.keras.layers.Flatten()(cust)
fc = tf.keras.layers.Dense(num_classes)(flat)

model = tf.keras.models.Model(inputs=[inp], outputs=[fc])
model.compile(loss=..., optimizer=...)  # add loss function and optimizer
model.fit(...)  # fit the model

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM