1

How can I assign a value to a tf Variable inside a function? Based on the link here, it say thats you have to run a sess on the tf tensor. I want to update the tf variable inside the function after few calculations.

Example:

def update(weights):
    value_1 = 0
    value_2 = 2
    ........... some code here ...........
    weights['layer_1'] = tf.multiply(weights['layer_1'],value_1)
    weights['layer_2'] = tf.multiply(weights['layer_2'],value_2)
    ............some code here.............

I can't do the above code. But how do I use assign to make this code work?

Community
  • 1
  • 1
Vimlan.G
  • 193
  • 1
  • 13

2 Answers2

0

You have to use assign, which take a Tensor which has to be exactly the same shape as the original Variable. If you want to have different shape use the validate_shape=False. But you have to keep in mind that you'll get the actual changes on run time, thus you will code the behavior of your variable not assigning values. Here an example that shows variable assignment with variable shapes:

import tensorflow as tf

var = tf.Variable(tf.zeros((1, 3)))
new_v = tf.assign(var, tf.ones((5, 7)), validate_shape=False)

init_op = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init_op)
    print sess.run([var])
    print sess.run([new_v])

For your particular example you could try:

def update(weights):
    value_1 = tf.constant(0)
    value_2 = tf.constant(2)
    ........... some code here ...........
    weights['layer_1'] =  tf.assign(weights['layer_1'], tf.multiply(weights['layer_1'],value_1))
    weights['layer_2'] =  tf.assign(weights['layer_2'], tf.multiply(weights['layer_2'],value_2))
    ............some code here.............
Mohamed Lakhal
  • 466
  • 1
  • 4
  • 11
  • Thanks for your reply. But is there any way I could perform assign in a function with other arithmetic as well? – Vimlan.G Apr 15 '17 at 15:40
  • Yes, you can perform any type of operations on Tensors. Just use the operation that you want, example with Math ops: https://www.tensorflow.org/api_guides/python/math_ops – Mohamed Lakhal Apr 15 '17 at 15:45
  • I can't comment on your question. As a sanity check, the `tf.multiply` expect both `x, y` to be tensors. In your case `values_1`, and `values_2` are `Integer`. `values_1 = tf.constant(0)` and `values_2 = tf.constant(2)` should work for you. – Mohamed Lakhal Apr 15 '17 at 16:04
  • However, even if this code works, when I print the values of the weights, its the values that I expected but after another epoch, it returns back to original weights. – Vimlan.G Apr 15 '17 at 16:06
  • Try to use: `weights['layer_1'] = tf.assign(weights['layer_1'], tf.multiply(weights['layer_1'],value_1))` – Mohamed Lakhal Apr 15 '17 at 16:08
0

This works for me -

import tensorflow as tf  
import numpy as np  

# function to randomly initialize weights for a specific layer
def assign_var(layer_number):  
    weight_value = np.random.rand(5,3) # or any calculations you need
    weight_var = tf.get_variable('weights_layer_'+str(layer_number))
    return tf.assign(weight_var,weight_value)  

with tf.Session() as sess:
    sess.run(assign_var(1))
    sess.run(assign_var(2))

EDIT The problem with the above code is - it keeps adding to the graph every time you call the function.

Alternatively, I think this should be better.

import tensorflow as tf  
import numpy as np  

var_name = tf.placeholder(tf.string)
weight_value = tf.placeholder(tf.float32)
weight_var = tf.get_variable(var_name)
assign_weights = tf.assign(weight_var,weight_value)  

sess = tf.Session()
# function to randomly initialize weights for a specific layer
def assign_var(layer_number):  
    rand_weight_value = np.random.rand(5,3) # or any calculations you need
    sess.run(assign_weights,{var_name:'weights_layer'+str(layer_number),weight_value:rand_weight_value})


assign_var(1) # assigns random weight values to layer 1
madvn
  • 33
  • 7