Keras 模型中的 Tensorflow op [英] Tensorflow op in Keras model

查看:38
本文介绍了Keras 模型中的 Tensorflow op的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Keras 模型中使用 tensorflow 操作.我之前尝试用 Lambda 层包装它,但我相信这会禁用该层的反向传播.

更具体地说,我正在尝试使用 此处 在 Keras 模型中,无需将其移植到 Keras 层(我希望稍后部署到 tensorflow).我可以以共享库的形式编译这些层并将它们加载到 python 中.这给了我 tensorflow 操作,但我不知道如何将它结合到 Keras 模型中.

Keras MNIST 模型的一个简单示例,例如一个 Conv2D 层被一个 tf.nn.conv2d 操作替换,这正是我正在寻找的.

我看过这个 教程,但它似乎与我正在寻找的相反.它似乎将 Keras 层插入到张量流图中.我正在做完全相反的事情.

最好的问候,汉斯

解决方案

大约两周过去了,我现在似乎可以回答我自己的问题了.

如果您使用 this 装饰器.在撰写本文时,此功能在 C++ 中(尚)不可用,而这正是我所寻找的.一种解决方法是在 C++ 中定义一个普通操作,并使用提到的装饰器将其包装在 python 方法中.如果这些具有相应梯度的函数在 tensorflow 中注册,反向传播将自动"发生.

I'm trying to use a tensorflow op inside a Keras model. I previously tried to wrap it with a Lambda layer but I believe this disables that layers' backpropagation.

More specifically, I'm trying to use the layers from here in a Keras model, without porting it to Keras layers (I hope to deploy to tensorflow later on). I can compile these layers in a shared library form and load these into python. This gives me tensorflow ops and I don't know how to combine this in a Keras model.

A simple example of a Keras MNIST model, where for example one Conv2D layer is replaced by a tf.nn.conv2d op, would be exactly what I'm looking for.

I've seen this tutorial but it appears to do the opposite of what I am looking for. It seems to insert Keras layers into a tensorflow graph. I'm looking to do the exact opposite.

Best regards, Hans

解决方案

Roughly two weeks have passed and it seems I am able to answer my own question now.

It seems like tensorflow can look up gradients if you register them using this decorator. As of writing, this functionality is not (yet) available in C++, which is what I was looking for. A workaround would be to define a normal op in C++ and wrap it in a python method using the mentioned decorator. If these functions with corresponding gradients are registered with tensorflow, backpropagation will happen 'automagically'.

这篇关于Keras 模型中的 Tensorflow op的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆