[work]TypeError: 'Tensor' object does not support item assignment in TensorFlow

10人阅读 评论(0) 收藏 举报
分类:

I try to run this code:

outputs, states = rnn.rnn(lstm_cell, x, initial_state=initial_state, sequence_length=real_length)

tensor_shape = outputs.get_shape()
for step_index in range(tensor_shape[0]):
    word_index = self.x[:, step_index]
    word_index = tf.reshape(word_index, [-1,1])
    index_weight = tf.gather(word_weight, word_index)
    outputs[step_index,  :,  :]=tf.mul(outputs[step_index,  :,  :] , index_weight)

But I get error on last line: TypeError: 'Tensor' object does not support item assignment It seems I can not assign to tensor, how can I fix it?


In general, a TensorFlow tensor object is not assignable*, so you cannot use it on the left-hand side of an assignment.

The easiest way to do what you're trying to do is to build a Python list of tensors, and tf.stack()them together at the end of the loop:

outputs, states = rnn.rnn(lstm_cell, x, initial_state=initial_state,
                          sequence_length=real_length)

output_list = []

tensor_shape = outputs.get_shape()
for step_index in range(tensor_shape[0]):
    word_index = self.x[:, step_index]
    word_index = tf.reshape(word_index, [-1,1])
    index_weight = tf.gather(word_weight, word_index)
    output_list.append(tf.mul(outputs[step_index, :, :] , index_weight))

outputs = tf.stack(output_list)

 * With the exception of tf.Variable objects, using the Variable.assign() etc. methods. However, rnn.rnn() likely returns a tf.Tensor object that does not support this method.


查看评论

EJB商业探讨的白皮书

1 业界支持  任何一个商业化的技术要想拥有良好的市场态势,业界内重要企业的大量参与是十分重要的。J2EE是由SUN引导,各厂商共同发起的,并得到广泛认可的工业标准。业内企业计算领域的大企业如:IBM...
  • DrunkenLion
  • DrunkenLion
  • 2001-06-20 08:49:00
  • 666

theano 中出现 TypeError: 'TensorVariable' object does not support item assignment

theano 中出现 TypeError: 'TensorVariable' object does not support item assignment 在使用TensorVariabl...
  • u011276025
  • u011276025
  • 2017-02-28 09:31:52
  • 1116

pyhon学习笔记(1): 数据类型之普通类型

写在前边:      python是有名的简单,易用,强大的语言。前两天需要用了,花了一点时间去学习。然后写了下边这个学习笔记。目的就是想让没有看过的而有C基础的人一目十行的看过。如果说一个标明自己简...
  • xwjbs
  • xwjbs
  • 2007-09-02 13:32:00
  • 16002

python object does not support item assignment

先看以下代码:class Animal(object): def __init__(self, name): self.name = name a1 =...
  • lanyang123456
  • lanyang123456
  • 2017-05-30 21:59:32
  • 3381

Python新手错误自查

Python新手常犯的错误 当初学 Python 时,想要弄懂 Python 的错误信息的含义可能有点复杂。这里列出了常见的的一些让你程序 crash 的运行时错误。 1)忘记...
  • Alpha5
  • Alpha5
  • 2014-01-01 16:26:43
  • 5656

TypeError: 'tuple' object does not support item assignment解决方案

def change(tupleTest): tupleTest[0] = 2 tupleTest = (1, 2, 3) change(tupleTest) print(tupleTest)...
  • a19990412
  • a19990412
  • 2018-02-14 21:47:42
  • 425

python 报错TypeError: 'range' object does not support item assignment,解决方法

1. 例子如下:from math import sqrt if __name__ == '__main__': N = 100 a = range(0,N) for i in...
  • wanglin_lin
  • wanglin_lin
  • 2016-03-07 15:23:53
  • 13933

TypeError: 'str' object does not support item assignment 的解决方法

最近在写高精度减法时,遇到如下的错误提示: TypeError: ‘str’ object does not support item assignmentdef sub(n11,n21): ...
  • pjzxweb
  • pjzxweb
  • 2017-10-22 19:26:05
  • 527

Insert python object in mongodb

插入python对象到mongo中转json 报错 from pymongo import Connection import json conn = Connection() db = conn...
  • jazywoo123
  • jazywoo123
  • 2013-12-31 16:18:51
  • 1376

python3中报错:TypeError: 'range' object doesn't support item deletion

通过range构造数组trainingSet = range(50);在执行以下操作时del(trainingSet[randIndex]) 报错:TypeError: 'range' object ...
  • Dillon2015
  • Dillon2015
  • 2016-10-31 22:24:22
  • 4235
    友情链接
    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 118万+
    积分: 1万+
    排名: 772
    博客专栏
    最新评论