我在张量流中有一个脚本,其中包含自定义张量流操作。我想将代码移植到 keras,但我不确定如何在 keras 代码中调用自定义操作。
我想在 keras 中使用tensorflow,所以到目前为止我发现的教程描述了与我想要的相反的内容:https://blog.keras.io/keras-as-a-simplified-interface-to-tensorflow-tutorial.html https://blog.keras.io/keras-as-a-simplified-interface-to-tensorflow-tutorial.html.
我还阅读了有关可以包装任意自定义函数的 Lambda 层的信息,但我没有看到 tf.ops 的示例。
如果您能提供代码片段和最简单的示例,我将非常感激。例如假设 tf.ops 为:
outC = my_custom_op(inA, inB)
- -编辑:
类似的问题已在here https://stackoverflow.com/questions/45067108/tensorflow-op-in-keras-model- 本质上是打电话this https://github.com/zplizzi/tensorflow-fast-rcnn/tree/master/tensorflow/core/user_opskeras 中的自定义操作,但是我无法掌握如何将其应用到我想要的另一个示例上的解决方案,例如this https://github.com/charlesq34/pointnet2/tree/master/tf_ops/sampling一。这个自定义的 tf 操作首先被编译(对于 GPU),然后到目前为止在张量流中使用here https://github.com/charlesq34/pointnet2/blob/master/utils/pointnet_util.py,参见@第40行。我很清楚如何使用包装在Lambda层中的自定义(lambda)函数,我想了解的是如果我使用keras,如何使用已编译的自定义操作。
您可以将任意张量流函数包装在 keras 中Lambda
层并将它们添加到您的模型中。最小工作示例从这个答案 https://stackoverflow.com/questions/51523305/custom-linear-transformation-in-keras/51523393#51523393:
import tensorflow as tf
from keras.layers import Dense, Lambda, Input
from keras.models import Model
W = tf.random_normal(shape=(128,20))
b = tf.random_normal(shape=(20,))
inp = Input(shape=(10,))
x = Dense(128)(inp)
# Custom linear transformation
y = Lambda(lambda x: tf.matmul(x, W) + b, name='custom_layer')(x)
model = Model(inp, y)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)