张量流自定义运算梯度

2024-04-14

我们想要在张量流中创建一个自定义层。因此,我们决定简单地从一个玩具示例开始:复制层。经过一番尝试和错误后,我们发现梯度似乎会传递正确的值。然而,在第二次迭代中,特征得到了 NAN。 这可能是一个简单的错误,但目前我看不到它。

总的来说,我有两个问题:

  1. 有人可以发现这里的问题以及如何解决它吗?
  2. 调试张量流会话的好方法是什么?

复制操作.cc

#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/op_kernel.h"
#include <stdio.h>

namespace tensorflow {



typedef Eigen::ThreadPoolDevice CPUDevice;
typedef Eigen::GpuDevice GPUDevice;

template<typename Device, typename T>
class MyCopyOp: public OpKernel {
public:
    explicit MyCopyOp(OpKernelConstruction* context) :
            OpKernel(context) {
    }

    void Compute(OpKernelContext* context) override {
        const Tensor& input = context->input(0);
        auto in_flat = input.flat<T>();

        printf("Debug MyCopyOp Features: %s \n",input.DebugString().c_str());

        Tensor* output = nullptr;
        OP_REQUIRES_OK(context,
                context->allocate_output(0, input.shape(), &output));

        auto out_flat = output->flat<T>();
        out_flat.setZero();

        for (int d = 0; d < input.dims(); ++d) {
            for (int i = 0; i < input.dim_size(d); ++i) {
                out_flat(d * input.dim_size(d) + i) = in_flat(
                        d * input.dim_size(d) + i);
            }
        }

        printf("Debug MyCopyOp Output: %s \n",output->DebugString().c_str());
    }

};


template<typename Device, typename T>
class MyCopyGradOp: public OpKernel {
public:
    explicit MyCopyGradOp(OpKernelConstruction* context) :
            OpKernel(context) {

    }

    void Compute(OpKernelContext* context) override {
        printf("called MyCopyGradOp.Compute() \n");
        const Tensor& gradients = context->input(0);
        const Tensor& features = context->input(1);
        printf("Debug MyCopyOpGrad Gradients: %s \n",gradients.DebugString().c_str());
        printf("Debug MyCopyOpGrad Features: %s \n",features.DebugString().c_str());

        TensorShape output_shape = features.shape();

        Tensor* output = nullptr;
        OP_REQUIRES_OK(context,
                context->allocate_output(0, output_shape, &output));
        output->flat<T>().setZero();

        const T* btm_ptr = gradients.flat<T>().data();
        T* top_ptr = output->flat<T>().data();

        for (int i = 0; i < gradients.NumElements(); ++i) {
            top_ptr[i] = btm_ptr[i];
        }

        printf("Debug MyCopyOpGrad Output: %s \n",output->DebugString().c_str());
        printf("---------------------------------- \n");
    }

};


REGISTER_OP("MyCopy")
.Input("features: T")
.Output("output: T")
.Attr("T: realnumbertype")
.Doc(R"doc(
Copies all input values to the output
)doc");

REGISTER_OP("MyCopyGrad")
.Input("gradients: T")
.Input("features: T")
.Output("backprops: T")
.Attr("T: realnumbertype")
.Doc(R"doc(
TODO!!
)doc");


#define REGISTER_MYCOPY_KERNELS(type)                                           \
  REGISTER_KERNEL_BUILDER(                                                      \
      Name("MyCopy").Device(DEVICE_CPU).TypeConstraint<type>("T"),              \
      MyCopyOp<Eigen::ThreadPoolDevice, type>);                                 \
  REGISTER_KERNEL_BUILDER(                                                      \
      Name("MyCopyGrad").Device(DEVICE_CPU).TypeConstraint<type>("T"),          \
      MyCopyGradOp<Eigen::ThreadPoolDevice, type>);                             //  \
  // REGISTER_KERNEL_BUILDER(                                                      \
  //     Name("MyCopy").Device(DEVICE_GPU).TypeConstraint<type>("T"),              \
  //     MyCopyOp<Eigen::GpuDevice, type>);                                        \
  // REGISTER_KERNEL_BUILDER(                                                      \
  //     Name("MyCopyGrad").Device(DEVICE_GPU).TypeConstraint<type>("T"),          \
  //     MyCopyGradOp<Eigen::GpuDevice, type>);                                


REGISTER_MYCOPY_KERNELS(float); 
REGISTER_MYCOPY_KERNELS(int);
REGISTER_MYCOPY_KERNELS(double);


}

我们使用简单的 MNIST 示例作为基础:

层测试.py

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

import tensorflow as tf
from tensorflow.python.framework import ops
copy_op_module = tf.load_op_library('copy_op.so')

@ops.RegisterGradient("MyCopy")
def _CopyOpGrad(op, grad):
  return copy_op_module.my_copy_grad(grad,op.inputs[0])

sess = tf.InteractiveSession()

x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])

W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

sess.run(tf.initialize_all_variables())

y1 = tf.nn.softmax(tf.matmul(x,W) + b)
y = copy_op_module.my_copy(y1)            //Here: MyCopy Layer is inserted

cross_entropy = -tf.reduce_sum(y_*tf.log(y))

train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

for i in range(2):
  batch = mnist.train.next_batch(50)
  train_step.run(feed_dict={x: batch[0], y_: batch[1]})

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

compile

TF_INC=$(python -c 'import tensorflow as tf; print(tf.sysconfig.get_include())')
TF_LIB=$(python -c 'import tensorflow as tf; print(tf.sysconfig.get_lib())')
g++ -std=c++11 -shared copy_op.cc -o copy_op.so -I $TF_INC -L $TF_LIB -fPIC -Wl,-rpath $TF_LIB

output:

Debug MyCopyOp Features: Tensor<type: float shape: [50,10] values: 0.1 0.1 0.1...> 
Debug MyCopyOp Output: Tensor<type: float shape: [50,10] values: 0.1 0.1 0.1...> 
called MyCopyGradOp.Compute() 
Debug MyCopyOpGrad Gradients: Tensor<type: float shape: [50,10] values: -0 -0 -0...> 
Debug MyCopyOpGrad Features: Tensor<type: float shape: [50,10] values: 0.1 0.1 0.1...> 
Debug MyCopyOpGrad Output: Tensor<type: float shape: [50,10] values: -0 -0 -0...> 
---------------------------------- 
Debug MyCopyOp Features: Tensor<type: float shape: [50,10] values: nan nan nan...> 
Debug MyCopyOp Output: Tensor<type: float shape: [50,10] values: nan nan nan...> 
called MyCopyGradOp.Compute() 
Debug MyCopyOpGrad Gradients: Tensor<type: float shape: [50,10] values: nan nan nan...> 
Debug MyCopyOpGrad Features: Tensor<type: float shape: [50,10] values: nan nan nan...> 
Debug MyCopyOpGrad Output: Tensor<type: float shape: [50,10] values: nan nan nan...> 
---------------------------------- 
Debug MyCopyOp Features: Tensor<type: float shape: [10000,10] values: nan nan nan...> 
Debug MyCopyOp Output: Tensor<type: float shape: [10000,10] values: nan nan nan...> 
0.098

预先非常感谢!


来自 mrry 的评论:使用 - 存在已知的稳定性问题tf.reduce_sum(y_ * tf.log(y))计算交叉熵(使用tf.nn.softmax_cross_entropy_with_logits(y, y_)相反),并初始化你的W变量为零通常会导致比随机初始化更糟糕的结果。这个答案 https://stackoverflow.com/questions/36127436/tensorflow-predicts-always-the-same-result/36134261#36134261有关权重初始化问题的更多详细信息。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

张量流自定义运算梯度 的相关文章

随机推荐

  • 姜戈。复杂的注释需要别名。这里的别名是什么?

    我试图通过以下查询获取模型的最大值和最小值 max min price MyModel objects annotate Min price Max price 但我收到错误 复杂的注释需要别名 我不确定别名在这里意味着什么 并且我认为文档
  • 测试助手类的正确位置在哪里? (phpunit/最佳实践)

    我想使用 PHPUnit 测试我的应用程序 所以我像往常一样有我的应用程序类和第二棵带有测试类的树 现在我需要进行一些测试 一种虚拟 模拟对象 我想知道应该将它们放在哪里 它是一个不同的用例 它应该放在公共 lib 文件夹中还是更喜欢什么
  • 如何使用 FirefoxProfile 在 FireFox Selenium Webdriver 中启用 Adob​​e Flash

    我需要一种自动化方法来在 Firefox Selenium Webdriver 中启用 Flash 而无需用户交互 我努力了 FirefoxProfile profile new FirefoxProfile As 0 is to disa
  • Azure 移动应用自定义身份验证

    我正在尝试使用来实现自定义身份验证LoginAsync string provider JObject token MobileServiceClient 的重载 我有一个像这样的自定义身份验证控制器 MobileAppController
  • 如何检查事件是否已存在一天 - fullcalendar

    如何在使用时检查事件是否已经存在一天renderEvent method 我发现可能在堆栈中回答dayClick with clientEvents 我不确定它如何用于renderEvent var diffDay 5 for var i
  • 如何选择博客模型中的最后一个和倒数第二个条目?

    我有一个模型 blog posts 其中有一个字段 published at 我想从该模型中选择最新的两个博客以显示在我的主页上 但不确定如何构建它 目前 我有一个解决方法 可以获取一部分数据 但当我表中没有任何内容时 它会不断失败 而不是
  • ARM架构中不同处理器模式下如何使用内核堆栈?

    据我了解 每个进程都有一个用户堆栈和内核堆栈 除此之外 ARM 架构中的每种模式都有一个堆栈 所以我想知道不同的堆栈和堆栈指针在 ARM 模式下如何工作 另外 何时会使用与进程关联的内核堆栈 何时会使用与进程关联的内核堆栈 当您进行系统调用
  • Python 中枚举的枚举?

    Python 中是否可以有枚举的枚举 例如 我想要 enumA enumB elementA elementB enumC elementC elementD 供我参考elementA as enumA enumB elementA 或参考
  • 设置 GLEW 窗口?

    我有 Visual Studio 2010 我想在其上设置 glew h 我执行了这一步 但仍然出现链接器错误 1 下载glew包 2 将 h文件复制到C Program Files x86 Microsoft SDKs Windows v
  • Parsley.js - 更改错误容器

    我想改变每个错误消息的位置 即在相应的位置显示错误消息 div class errorBlock div 通过使用文档代码 错误消息显示在元素 输入 之前 而不是按预期显示 有任何想法吗 根据文档 errors container func
  • Knex 连接 Heroku Postgres 时出现错误?

    我正在尝试将 Heroku Postgres 与 Knex 连接 它在本地运行良好 但是当我推 Heroku 时 并尝试注册一个帐户 我收到这个错误 code DEPTH ZERO SELF SIGNED CERT 但我推 Heroku 我
  • Windows 服务错误 1053

    我目前正在编写一个 Windows 服务 它连接到 crm 系统以拉下一个计划 然后运行各种数据源等 我已经一切正常 除了当我安装所有内容并尝试运行启动服务时 我收到以下错误 错误 1053 服务未响应启动或控制请求 及时时尚 这是我在 S
  • 在 JavaScript 中,await 是否可以保证执行顺序而不需要赋值?

    主题 我可以说下面的两段代码是相等的吗 await someFunc no assignment here doSomethingAfterSomeFunc and someFunc then gt doSomethingAfterSome
  • 如何在android http POST中添加参数?

    friends 我正在尝试使用以下教程将文件上传到 php 服务器http getablogger blogspot com 2008 01 android how to post file to php server html http
  • 添加带有多个小部件链接的右键单击上下文菜单?

    我的问题是一种后续行动这个问题 https stackoverflow com questions 12014210 python tkinter app adding a right click context menu 23834156
  • 如何使用标准 MVC Core 依赖注入解析未注册类型

    有没有办法得到IServiceProvider GetService
  • 如何在 matplotlib 中的另一个图上添加一个图?

    我有两个包含数据的文件 datafile1 和 datafile2 第一个始终存在 第二个仅有时存在 因此 datafile2 上的数据图被定义为我的 python 脚本中的函数 geom macro 在 datafile1 上的数据绘制代
  • Visual Studio 忽略 try catch - 仅调试

    我认为错误处理是个好主意 调试时它可能会妨碍 特别是对于用户友好的消息 在 VB6 中 我只需选中编译器的一个框即可忽略我的错误处理 我found https stackoverflow com questions 893277 is th
  • Mediator/EventAggregator 差异

    另外 当我需要在松散耦合的对象之间进行通信时 例如 MVVM的 ViewModel 有关最佳编程实践的不同书籍和博客建议使用 Mediator EventAggregator 模式 我的问题是关于这些模式之间的差异 关系 谁能为我描述一下它
  • 张量流自定义运算梯度

    我们想要在张量流中创建一个自定义层 因此 我们决定简单地从一个玩具示例开始 复制层 经过一番尝试和错误后 我们发现梯度似乎会传递正确的值 然而 在第二次迭代中 特征得到了 NAN 这可能是一个简单的错误 但目前我看不到它 总的来说 我有两个