Use assign () to assign a specific value after initializing Variable of tensorflow. For example, after reading the variable value from checkpoint, only a part can be rewritten with numpy.array.
assign_test.py
import tensorflow as tf
import numpy as np
x = tf.Variable(tf.zeros([5]))
y = tf.Variable(tf.ones([5]))
sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())
print 'x original = ', x.eval()
input_placeholder = tf.placeholder(tf.float32, shape=[5])
assign_op = x.assign(input_placeholder)
sess.run(assign_op, feed_dict={input_placeholder: np.ones(5).astype(np.float32)})
print 'x <- numpy array ...', x.eval()
assign_op = x.assign(y)
sess.run(assign_op)
print 'x <- another variable ...', x.eval()
Recommended Posts