cppflow icon indicating copy to clipboard operation
cppflow copied to clipboard

How to write variables

Open remaininlight opened this issue 5 years ago • 7 comments

First off thanks for all the work you guys have put into CppFlow - it's great and way better than messing about with Bazel!

I would like to be able to write values to variables in graphs loaded in CppFlow but haven't been able to find any information about how to do this with the C API . I noticed a function in raw_ops.h called read_variable_op (https://github.com/serizba/cppflow/blob/cppflow2/include/cppflow/raw_ops.h#L16462) which I thought might have something to do with reading variables, calling the C API op ReadVariableOp. This led me to AssignVariableOp (https://www.tensorflow.org/api_docs/python/tf/raw_ops/AssignVariableOp), which looks promising, but I can't find any examples of how to use this.

If anyone has any pointers/advice as to how to use the CppFlow function read_variable_op (how do you get the correct variable tensor to pass into it?) or how I might adapt it to assign to the variable instead, they would be gratefully received.

remaininlight avatar Nov 29 '20 16:11 remaininlight

Hi @remaininlight,

Could you be a bit more explicit on what you really want to do? Do you want to assign a specific value to a cppflow::tensor? Maybe you can add an example of what you want to do in python, so we can understand it better :)

serizba avatar Dec 01 '20 18:12 serizba

Hi @serizba,

Thanks for your quick response :) I'm trying to load a graph and change the values of variables within it. In Python, something like this:

import tensorflow as tf

model = tf.saved_model.load('/path/to/model')

# Then variables are accessible
names = [model.name for model in model.variables]

# And can be assigned to
model.variable_name.assign(5)

I was trying to work out if it were possible to assign to a variable in a graph loaded in CppFlow but haven't been able to find any examples of even doing it with the C API. If I find a way I would be happy to write it up as a PR for CppFlow (if that's useful), just drawing blank at the moment!

remaininlight avatar Dec 04 '20 20:12 remaininlight

I've been digging further into this and it seems that maybe the introduction of VarHandleOp in Tensorflow 2.x, to mediate write access to variables, has not been matched by a way to use it from the older TF graph API.

Opened a TF issue here (https://github.com/tensorflow/tensorflow/issues/46077) and a SO question here (https://stackoverflow.com/questions/65509638/how-to-assign-values-to-variables-in-tensorflow-graphs-using-the-c-api).

remaininlight avatar Dec 30 '20 15:12 remaininlight

I just came across what looks like a new C API for loading models: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/c/experimental/saved_model

Haven't dug into how to use it yet but it looks like their are functions for assigning values to variables

remaininlight avatar Apr 10 '21 14:04 remaininlight

Hi @remaininlight,

Could you be a bit more explicit on what you really want to do? Do you want to assign a specific value to a cppflow::tensor?

Hi @serizba

I was wondering exactly this, how can we assign a specific value to a tensor ? something similar as: my_tensor[idx, idx] = 3

I noticed that there is an assign() function but cannot get around how to target a specific position within the tensor

michaelStettler avatar Aug 19 '21 14:08 michaelStettler

Hey @ljn917 ,

Thanks for your answer, well then I suppose it makes sense that it wasn't straight forward... I modified my pipeline and I am now simply setting the tensor with the array as explained here: https://github.com/serizba/cppflow/issues/114

michaelStettler avatar Aug 20 '21 13:08 michaelStettler

Closing due to inactivity.

serizba avatar Sep 23 '22 11:09 serizba