Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

How to assign values to variables in Tensorflow graphs using the C API

I am attempting to assign a value to a variable in a Tensorflow graph loaded using the C API. When I export a graph using Tensorflow 2.x it contains the following ops related to a particular variable:

variable_name type: VarHandleOp device:  number inputs: 0 number outputs: 1
Number inputs: 0
Number outputs: 1
 dims: 0 []

variable_name/Read/ReadVariableOp type: ReadVariableOp device:  number inputs: 1 number outputs: 1
Number inputs: 1
Number outputs: 1
 dims: 0 []

According to this video (https://www.youtube.com/watch?v=uaRO0AV6Tto) variables in TF 2.x now use VarHandleOps in order to mediate write access to them. The TF test suite for the C API is the only example I've found so far of using a VarHandleOp to access a variable but it is created from scratch using the Eager API (https://github.com/OAID/TensorFlow-HRT/blob/master/tensorflow/c/eager/c_api_test.cc#L409).

I would like to be able to load a graph and assign to a variable already contained within it, is this even possible with the Eager API? When I try using the older graph API I can fetch the VarHandleOp like so:

TF_Operation* op = TF_GraphOperationByName(graph, "variable_name");

But then I can't figure out how to run this op to return a TFE_TensorHandle (like here https://github.com/OAID/TensorFlow-HRT/blob/master/tensorflow/c/eager/c_api_test.cc#L418), a type which doesn't even seem to exist in the graph API. TF_SessionRun()'s inputs and outputs deal in TF_Output and TFE_Execute expects a TFE_Op rather than the TF_Operation I have access to.

Any tips or pointers gratefully received!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...