def build_metric(self):
with tf.variable_scope('fc', reuse=tf.AUTO_REUSE):
response_m = self.response
shape = response_m.get_shape().as_list()[1:3]
output_list = []
for i in range(shape[0]):
for j in range(shape[1]):
t1 = self.instance_embeds[:,i:i+6,j:j+6,:]
t2 = self.templates
t1, t2 = logit(t1, t2)
f = gsml(t1, t2)
for s in range(8):
response_m[s, i, j] = f[s]
output_list.append(f)
self.response_m = response_m
response_m [s,i,j] = f [s]
TypeError:“张量”对象不支持项目分配
我该怎么办?
答案 0 :(得分:0)
假设您的响应变量是张量流变量:
您可以为此目的使用屁股:
def build_metric(self):
with tf.variable_scope('fc', reuse=tf.AUTO_REUSE):
response_m = self.response
shape = response_m.get_shape().as_list()[1:3]
output_list = []
for i in range(shape[0]):
for j in range(shape[1]):
t1 = self.instance_embeds[:,i:i+6,j:j+6,:]
t2 = self.templates
t1, t2 = logit(t1, t2)
f = gsml(t1, t2)
for s in range(8):
response_m=tf.assign(response[s,i,j],f[s]) #change I have made
output_list.append(f)
self.response_m = response_m
一个更简单的理解示例可以是:
one=tf.Variable(tf.zeros(shape=[1,10]))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(one),"\n")
new_one=tf.assign(one[0,2],0.33) #using index to assign values
with tf.Session() as sess_2:
sess_2.run(tf.global_variables_initializer()) #initialize variables with zero values
print(sess_2.run(new_one))
代码的输出将是:
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
[[0. 0. 0.33 0. 0. 0. 0. 0. 0. 0. ]]