3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

tensorflowのvariableに特定の値を入力する

Last updated at Posted at 2016-08-18

tensorflowのVariableを初期化した後に特定の値を代入するにはassign()を使う.例えばcheckpointからvariableの値を読み込んだ後に,一部のみを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()
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?