c++ - Tensorflow 操作文档

标签 c++ tensorflow protocol-buffers

在 Tensorflow 中 source ,我明白了,

REGISTER_OP("BroadcastGradientArgs")
    .Input("s0: T")
    .Input("s1: T")
    .Output("r0: T")
    .Output("r1: T")
    .Attr("T: {int32, int64} = DT_INT32")
    .SetShapeFn([](InferenceContext* c) {
      ... uninteresting details ...
    })
    .Doc(R"doc(                                                                                                              
Return the reduction indices for computing gradients of s0 op s1 with broadcast.                                             

This is typically used by gradient computations for a broadcasting operation.                                                
)doc");

在 Python 中,我可以执行以下操作,

>>> from tensorflow.python.ops import gen_array_ops
>>> gen_array_ops._InitOpDefLibrary()._ops['BroadcastGradientArgs'].op_def
name: "BroadcastGradientArgs"
input_arg {
  name: "s0"
  type_attr: "T"
}
... more stuff ...
attr {
  name: "T"
  type: "type"
  ... uninteresting details ...
}

请注意,我在 Python 中获取了 TF 操作的 Protobuf 定义(为简洁起见,我删除了一些定义)。我想获取我在 C++ 代码中看到的定义的文档部分。我如何获得它?

最佳答案

那是痛苦的。你需要给 TF 和 Protobuf 打补丁

https://github.com/tensorflow/tensorflow/issues/8207 https://github.com/google/protobuf/issues/2798

然后你需要另外注释掉这一行, https://github.com/tensorflow/tensorflow/blob/a3e636c0f561e2ac6d9f8a0044fbe09acb003803/tensorflow/python/framework/python_op_gen.cc#L708

重建和运行给了,

bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package && \
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg && \
sudo pip install --upgrade /tmp/tensorflow_pkg/tensorflow-1.0.0*.whl

并执行我的测试,

$ python -c "from tensorflow.python.ops import gen_array_ops; print gen_array_ops._InitOpDefLibrary()._ops['BroadcastGradientArgs'].op_def"
name: "BroadcastGradientArgs"
input_arg {
  name: "s0"
  type_attr: "T"
}
input_arg {
  name: "s1"
  type_attr: "T"
}
output_arg {
  name: "r0"
  type_attr: "T"
}
output_arg {
  name: "r1"
  type_attr: "T"
}
attr {
  name: "T"
  type: "type"
  default_value {
    type: DT_INT32
  }
  allowed_values {
    list {
      type: DT_INT32
      type: DT_INT64
    }
  }
}
summary: "Return the reduction indices for computing gradients of s0 op s1 with broadcast."
description: "This is typically used by gradient computations for a broadcasting operation."

这用了太多正则表达式......

关于c++ - Tensorflow 操作文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42521166/

相关文章:

python - 属性错误 : 'google.protobuf.pyext._message.RepeatedCompositeCo' object has no attribute 'append'

react-native - React Native 适合使用 TensorFlow.js 构建的跨平台机器学习应用程序吗?

python - protobuf数据解析返回错误结果

python - 反序列化 Google Protobuf 二进制文件

c++ - factoriais 的总和等于给定的数字

c++ - 为什么从两个空类派生出来的空类的大小是2?

C++ 未使用我自己的字符串复制函数按预期打印

tensorflow - 无法使用非浮点dtype建立 `Dense`层错误

c - 与 C 的二进制兼容性

c++ - 使用 native_handle() + pthread_cancel() 取消 std::thread