Zhao
Zhao

Reputation: 2193

How to overwrite a tensorflow variable after it has been initialized?

Assuming codes like this:

    sess.run(tf.initialize_all_variables())

    assign_op_0 = embedding_list[0].assign(tf.random_normal([35019, 32], stddev = 0.0))
    assign_op_1 = embedding_list[1].assign(tf.random_normal([35019, 32], stddev = 0.0))

    sess.run(assign_op_0)
    sess.run(assign_op_1)

embedding_list[0] and embedding_list[1] are two variables which have been initialized in the first line of codes. Now I want to overwrite with some new values, so I have the following four lines of code, however, I don't know if this is correct. And I can not even print the values of the embedding_list[0] and embedding_list[1]. When I do like this:

print(embedding_list[0].eval(session=sess.run))

it has this error:

Traceback (most recent call last):
  File "/home/zhao/DeepQA-master/main.py", line 29, in <module>
    chatbot.main()
  File "/home/zhao/DeepQA-master/chatbot/chatbot.py", line 213, in main
    print(embedding_list[0].eval(session=self.sess.run))
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variables.py", line 437, in eval
    return self._variable.eval(session=session)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 555, in eval
    return _eval_using_default_session(self, feed_dict, self.graph, session)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 3494, in _eval_using_default_session
    if session.graph is not graph:
AttributeError: 'function' object has no attribute 'graph'

Upvotes: 0

Views: 1713

Answers (1)

Phillip Bock
Phillip Bock

Reputation: 1889

Try as follows:

sess.run(tf.initialize_all_variables())
W_0 = tf.Variable(tf.random_normal([35019, 32], stddev = 0.0))
assign_op_0  = W_0.assign(embedding_list[0])
sess.run(assign_op_0)

Upvotes: 1

Related Questions