Tensorflow操作文档

时间:2017-03-01 00:26:58

标签: 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 ++代码中看到的定义的文档部分。我怎么得到它?

1 个答案:

答案 0 :(得分:1)

那很痛苦。你需要修补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."

这让Regex太过分了......