TypeError:'Tensor'对象不支持TensorFlow中的项目分配

高指甲:

我尝试运行以下代码:

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)

但是我在最后一行得到了错误:TypeError: 'Tensor' object does not support item assignment似乎我无法分配张量,如何解决?

mrry:

通常,TensorFlow张量对象不可分配*,因此您不能在分配的左侧使用它。

做您想做的事情的最简单方法是构建张量的Python列表,并tf.stack()在循环结束时将它们在一起:

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)

 *除tf.Variable对象外,使用Variable.assign()etc.方法。但是,rnn.rnn()可能返回tf.Tensor不支持此方法对象。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何解决Keras中的TypeError:Tensor对象不支持项目分配的问题

TypeError:“ int”对象不支持项目分配错误

TypeError /数组索引;'int'对象不支持项目分配

Python 脚本... TypeError: 'method' 对象不支持项目分配

TypeError:“ NodeView”对象不支持项目分配-NetworkX

TypeError:“范围”对象不支持项目分配

Python TypeError:“类型”对象不支持项目分配

Python,TypeError:“ int”对象不支持项目分配”

Python:TypeError:'int' 对象不支持 MinMaxScaler 的项目分配

Python:TypeError:“ str”对象不支持项目分配

TypeError:'str'对象不支持项目分配,熊猫操作

TypeError:“类型”对象不支持项目分配

如何修复:TypeError'tuple'对象不支持项目分配

如何修复“ TypeError:'NoneType'对象不支持项目分配”

“ TypeError:'功能'对象不支持项目分配”

TypeError:“ str”对象不支持项目分配

TypeError'set'对象不支持项目分配

在Featuretools中创建实体集错误TypeError:'str'对象不支持项目分配

TypeError:“ int”对象不支持项目分配,在线程中

通过刮擦管道刮擦时,字典中的“ TypeError:'unicode'对象不支持项目分配”

在交换 array[i] = array[j] TypeError: 'tuple' 对象不支持项目分配

TypeError:'str'对象不支持项目分配,带有json文件的python

Lambda错误“ TypeError:'str'对象不支持项目分配回溯”

TypeError:交换值时,“ tuple”对象不支持项目分配

添加边缘属性会导致TypeError:“ AtlasView”对象不支持项目分配

NLTK感知器标记器“ TypeError:“ LazySubsequence”对象不支持项目分配”

Python:使make_class TypeError:对象不支持项目分配

跟踪:sys.argv [:] = args; TypeError:“元组”对象不支持项目分配

“ TypeError:'NoneType'对象不支持项目分配”如何解决此问题?