理解C ++中新Tensorflow操作符的定义

时间:2017-09-11 09:24:42

标签: tensorflow operators

我正在尝试遵循在tensorflow中定义新运算符的官方指南。 https://www.tensorflow.org/extend/adding_an_op

#include <cstdlib>
#include <iostream>

using namespace std;

/*
 * 
 */
int x = 10; // This x is global
int main() {
    int n;
    { // The pair of braces introduces a scope  
    int m = 10;    // The variable m is only accessible within the scope
    cout << m << "\n";

    int x = 25; // This local variable x hides the global x
    int y = ::x; // y = 10, ::x refers to the global variable x
    cout << "Global: " << y << "\n" << "Local: " << x << "\n";
    {
        int z = x; // z = 25; x is taken from the previous scope
        int x = 28; // it hides local variable x = 25 above
        int t = ::x; // t = 10, ::x refers to the global variable x
        cout << "(in scope, before assignment) t = " << t << "\n";
        t = x; // t = 38, treated as a local variableout was not declared in this scope
        cout << "This is another hidden scope! \n";
        cout << "z = " << z << "\n";
        cout << "x = " << x << "\n";
        cout << "(in scope, after re assignment) t = " << t << "\n";
    } 
    int z = x; // z = 25, has the same scope as y
    cout << "Same scope of y. Look at code! z = " << z;
    }
    //i = m; // Gives an error, since m is only accessible WITHIN the scope

    int m = 20; // This is OK, since it defines a NEW VARIABLE m
    cout << m;

    return 0;
}

但是我找不到这段代码的逐行解释,特别是我不明白.SetShapeFn([](:: tensorflow :: shape_inference :: InferenceContext * c)的作用及其语法另外我对InferenceContext感到困惑,我猜它是一种连续传递任何数组元素的方法..我无法在任何地方找到明确的定义,也许我在错误的地方寻找,有人可以帮助我有解释或参考吗? 我想深入了解这段代码在幕后做了什么。

1 个答案:

答案 0 :(得分:1)

您是否在此处发现了形状推断功能部分? https://www.tensorflow.org/extend/adding_an_op#shape_functions_in_c

对ShapeInferenceContext类和编写自己的函数的机制有很多讨论。如果这不能涵盖您感兴趣的内容,您能提供更多详细信息吗?