Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign tf.Variables using placeholders?

Currently, I have the following code:

x = tf.placeholder(tf.int32, name = "x")
y = tf.Variable(0, name="y")
y = 2*x**2 + 5
for i in range(1,10):
    print("Value of y for x = ",i, " is: ",sess.run(y, feed_dict={x:i}))

However, when I try to display this on tensorboard, this gets messy. Ideally I'd want to do y= tf.Variable(2*x**2 +5) but tensorflow throws an error telling me that x is uninitialized. Or perhaps I shouldn't use tf.Variable and use something else?

like image 790
gust Avatar asked Nov 18 '25 14:11

gust


2 Answers

If you really want to do that with a tf.Variable, you can do that in two ways. You can use the desired expression as the initialization value for the variable. Then, when you initialize the variable, you pass the x value in the feed_dict.

import tensorflow as tf

# Placeholder shape must be specified or use validate_shape=False in tf.Variable
x = tf.placeholder(tf.int32, (), name="x")
# Initialization value for variable is desired expression
y = tf.Variable(2 * x ** 2 + 5, name="y")
with tf.Session() as sess:
    for i in range(1,10):
        # Initialize variable on each iteration
        sess.run(y.initializer, feed_dict={x: i})
        # Show value
        print("Value of y for x =", i , "is:", sess.run(y))

Alternatively, you can do the same thing with a tf.assign operation. In this case, you pass the x value when you run the assignment.

import tensorflow as tf

# Here placeholder shape is not stricly required as tf.Variable already gives the shape
x = tf.placeholder(tf.int32, name="x")
# Specify some initialization value for variable
y = tf.Variable(0, name="y")
# Assign expression value to variable
y_assigned = tf.assign(y, 2 * x** 2 + 5)
# Initialization can be skipped in this case since we always assign new value
with tf.Graph().as_default(), tf.Session() as sess:
    for i in range(1,10):
        # Assign vale to variable in each iteration (and get value after assignment)
        print("Value of y for x =", i , "is:", sess.run(y_assigned, feed_dict={x: i}))

However, as pointed out by Nakor, you may not need a variable if y is simply supposed to be the result of that expression for whatever value x takes. The purpose of a variable is to hold a value that will be maintained in future calls to run. So you would need it only if you want to set y to some value depending on x, and then maintain the same value even if x changes (or even if x is not provided at all).

like image 196
jdehesa Avatar answered Nov 21 '25 03:11

jdehesa


I think you misunderstood was a tf.Variable is. To quote the Tensorflow documentation:

A tf.Variable represents a tensor whose value can be changed by running ops on it. Unlike tf.Tensor objects, a tf.Variable exists outside the context of a single session.run call.

So variables will be your biases and weights in your neural network. These will vary when training your network. In Tensorflow, if you want to use your variables, you need to initialize them (using a constant or random value). That's what your error is about: you're defining y as a tf.Variable so it needs to be initialized.

However, your y is deterministic, it's not a tf.Variable. You can just remove the line where you define y and it works fine:

import tensorflow as tf

x = tf.placeholder(tf.int32, name = "x")
y = 2*x**2 + 5

with tf.Session() as sess:
    for i in range(1,10):
        print("Value of y for x = ",i, " is: ",sess.run(y, feed_dict={x:i}))

It returns:

Value of y for x =  1  is:  7
Value of y for x =  2  is:  13
Value of y for x =  3  is:  23
Value of y for x =  4  is:  37
Value of y for x =  5  is:  55
Value of y for x =  6  is:  77
Value of y for x =  7  is:  103
Value of y for x =  8  is:  133
Value of y for x =  9  is:  167
like image 38
Nakor Avatar answered Nov 21 '25 03:11

Nakor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!