使用tf.assign来代替直接使用“=”,举例:
import tensorflow as tf
import numpy as np
a = tf.placeholder(tf.float32, shape=[2,2])
b = tf.Variable(tf.zeros(shape=[1, 1]))
sess = tf.Session()
b[0,0] = a[0,0]
sess.run(tf.initialize_all_variables())
这里会出现报错"TypeError: ‘RefVariable’ object does not support item assignment"。
解决方案是使用tf.assign,进行Variable的赋值。
assg = tf.assign(b, tf.reshape(a[0,0],shape=[1,1]))