简体   繁体   English

如何在TensorFlow中定义自己的运算符

[英]How do I define my own operators in TensorFlow

In TensorFlow, we can use tf.nn.l2_loss() for doing L2 regularization. 在TensorFlow中,我们可以使用tf.nn.l2_loss()来进行L2正则化。 Let's say I want to define my own regularization operator for L1 regularization (call it tf.nn.l1_loss() ). 假设我想为L1正则化定义我自己的正则化运算符(称之为tf.nn.l1_loss() )。 How would I go about it? 我该怎么办呢? I am having a hard time locating operator definitions in the TensorFlow source code. 我很难在TensorFlow源代码中找到运算符定义。

As the comment suggested, there is a how-to guide for adding an op to TensorFlow . 正如评论所建议的那样,有一个向TensorFlow添加操作的操作指南。 This guide covers adding a new op that is implemented in C++. 本指南介绍了如何添加用C ++实现的新操作。 In general, you should do this in the following situations: 通常,您应该在以下情况下执行此操作:

  • The op cannot be implemented using existing TensorFlow ops (for example, l1_loss could be implemented using the existing element-wise and reduction operators as a Python function). op不能使用现有的TensorFlow操作来实现(例如, l1_loss 可以使用现有的逐元素简化运算符作为Python函数来实现)。
  • A C++ implementation is necessary for performance (or memory consumption) reasons. 出于性能(或内存消耗)的原因,C ++实现是必需的。
  • The op could be implemented as a composition of ops, but it has a gradient that can be computed more efficiently (or with better numerical stability) than computing the gradients op-by-op. op可以实现为ops的组合, 但是它具有可以比逐个运算的梯度计算更有效(或具有更好的数值稳定性)的梯度。 (This is why tf.nn.l2_loss is implemented as a fused op in C++.) (这就是tf.nn.l2_loss在C ++中实现为融合操作的原因。)

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

相关问题 如何在python中定义自己的前缀和后缀运算符 - how do I define my own prefix and postfix operators in python 如何在 Python 中定义自己的范围 function - How do I define my own range function in Python 如何在TensorFlow中使用我自己的图像? - How do i use my own images in TensorFlow? 如何在 TensorFlow 号码识别中使用自己的手绘图像 - How do I use my own Hand-Drawn Image in TensorFlow Number Recognition Pint:如何在 Pint Python 库中定义我自己的单位? - Pint: How do I define my own units in the Pint Python library? 我可以实现自己的卷积函数来替换tensorflow中的tf.nn.conv2d吗? 我该怎么办? - Could I implement my own convolution function to replace tf.nn.conv2d in tensorflow? How do I do? 如何在TensorFlow中使用自己的数据? - How can I use my own data with TensorFlow? 我如何在Tensorflow中准备自己的数据集(数千张图片)? - How i can prepare my own dataset (thousand of images) in Tensorflow? 如何在python中创建自己的数据类型,以便我可以覆盖算术运算符? - How can I create my own datatype in python so that I could overwrite arithmetic operators? 我可以定义自己的格式规范吗? - Can I define my own format specification?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM