Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the current value of a Variable?

Suppose we have a variable:

x = tf.Variable(...) 

This variable can be updated during the training process using the assign() method.

What is the best way to get the current value of a variable?

I know we could use this:

session.run(x) 

But I'm afraid this would trigger a whole chain of operations.

In Theano, you could just do

y = theano.shared(...) y_vals = y.get_value() 

I'm looking for the equivalent thing in TensorFlow.

like image 320
Denis L Avatar asked Nov 12 '15 19:11

Denis L


People also ask

What is the value of a variable?

The value of the variable, which is the value the variable name represents, might be categorized as follows: A constant, which is a number that is expressed as: An integer (12) A decimal (12.5) A floating point number (1.25E2)

How do you get the value of a TF variable?

To get the current value of a variable x in TensorFlow 2, you can simply print it with print(x) . This prints a representation of the tf. Variable object that also shows you its current value.

How do you set the value of a variable?

Assigning values to variables is achieved by the = operator. The = operator has a variable identifier on the left and a value on the right (of any value type). Assigning is done from right to left, so a statement like var sum = 5 + 3; will assign 8 to the variable sum .

How to get the value of a variable in Excel using VBA?

For the defined variable, put an equal sign and mention the cell address. Once again, put a dot to see the IntelliSense list. From the VBA IntelliSense list, choose “Value” property to get the value from the mentioned cell. Now the variable “CellValue” holds the value from the cell A1. Show this variable value in the message box in VBA.

How do I get the value of a variable in PowerShell?

Gets the variables in the current console. The Get-Variable cmdlet gets the PowerShell variables in the current console. You can retrieve just the values of the variables by specifying the ValueOnly parameter, and you can filter the variables returned by name. This command gets variables with names that begin with the letter m.

How do I retrieve just the values of the variables?

You can retrieve just the values of the variables by specifying the ValueOnly parameter, and you can filter the variables returned by name. This command gets variables with names that begin with the letter m.

How to get only the values of variables with names that begin m?

This command gets only the values of the variables that have names that begin with m. This command gets information about the variables that begin with either the letter M or the letter P. The first command gets only the variables that are defined in the local scope. It is equivalent to Get-Variable -Scope Local and can be abbreviated as gv -s 0.


2 Answers

The only way to get the value of the variable is by running it in a session. In the FAQ it is written that:

A Tensor object is a symbolic handle to the result of an operation, but does not actually hold the values of the operation's output.

So TF equivalent would be:

import tensorflow as tf  x = tf.Variable([1.0, 2.0])  init = tf.global_variables_initializer()  with tf.Session() as sess:     sess.run(init)     v = sess.run(x)     print(v)  # will show you your variable. 

The part with init = global_variables_initializer() is important and should be done in order to initialize variables.

Also, take a look at InteractiveSession if you work in IPython.

like image 178
Salvador Dali Avatar answered Sep 22 '22 18:09

Salvador Dali


In general, session.run(x) will evaluate only the nodes that are necessary to compute x and nothing else, so it should be relatively cheap if you want to inspect the value of the variable.

Take a look at this great answer https://stackoverflow.com/a/33610914/5543198 for more context.

like image 26
Rafał Józefowicz Avatar answered Sep 20 '22 18:09

Rafał Józefowicz