【问题标题】:problem in my code :'Tensor' object does not support item assignment我的代码中的问题:'Tensor' 对象不支持项目分配
【发布时间】:2019-03-08 12:52:40
【问题描述】:
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: 'Tensor' 对象不支持项目分配

我能做什么?

【问题讨论】:

  • self.response 是变量张量???

标签: python-2.7 tensorflow


【解决方案1】:

假设你的响应变量是一个张量流变量:

您可以为此目的使用 assing:

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.  ]]

【讨论】:

  • @ahuxyy_1 不确定我是否遵循您的意思 如果这是代码请考虑添加问题并解释我提出的建议有什么问题
猜你喜欢
  • 2015-03-09
  • 2019-01-03
  • 2016-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-12
  • 2023-02-06
相关资源
最近更新 更多