简体   繁体   中英

How to manipulate Variables in Tensorflow

I want to do something as simple as this a = a + b , example code as follow

sess = tf.InteractiveSession()

embed = tf.Variable(tf.random_uniform([10, 2], -1, 1))

saver = tf.train.Saver([embed])
saver.restore(sess, 'save/model.ckpt')

new_embed = tf.Variable(tf.random_uniform([5, 2], -1, 1))

init = tf.initialize_variables([new_embed])
sess.run(init)

embed = tf.Variable(tf.concat(0, [embed, new_embed]))

However the last line won't execute because embed becomes an uninitialized value.

What I wish to accomplish here is to restore a variable from a file and concat with a new variable, ie make the [10, 2] variable to be a [15, 2] variable, where the first 10 rows are from the stored variable.

I was thinking to restore the [10, 2] variable to a new variable say old_ebmed , but I couldn't find a way to do so.

Any help would be appreciated.

I found a way to restore the variable to a varialbe with a different name

import tensorflow as tf

sess = tf.InteractiveSession()

old_embed = tf.Variable(tf.constant(0.0, shape = [10, 2]))

restorer = tf.train.Saver({'embed': old_embed})
restorer.restore(sess, 'test/d.ckpt')

new_embed = tf.Variable(tf.random_uniform([5, 2], -1, 1))

init_new = tf.initialize_variables([new_embed])
sess.run(init_new)

embed = tf.Variable(tf.concat(0, [old_embed, new_embed]))
init_embed = tf.initialize_variables([embed])
sess.run(init_embed)

saver = tf.train.Saver({'embed': embed})
saver.save(sess, 'test/d.ckpt')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM