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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37697747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:49:09  来源:igfitidea点击:

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

pythontensorflow

提问by Nils Cao

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 assignmentIt seems I can not assign to tensor, how can I fix it?

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

回答by mrry

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

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

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:

做你想做的最简单的方法是构建一个张量的 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)


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

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

回答by xiangshu lin

Another way you can do it like this.

另一种方法你可以这样做。

aa=tf.Variable(tf.zeros(3, tf.int32))
aa=aa[2].assign(1)

then the output is:

那么输出是:

array([0, 0, 1], dtype=int32)

数组([0, 0, 1], dtype=int32)

ref:https://www.tensorflow.org/api_docs/python/tf/Variable#assign

参考:https: //www.tensorflow.org/api_docs/python/tf/Variable#assign

回答by yuvaraj8blr

When you have a tensor already, convert the tensor to a list using tf.unstack(TF2.0) and then use tf.stack like @mrry has mentioned. (when using a multi-dimensional tensor, be aware of the axis argument in unstack)

当您已经有了张量时,使用tf.unstack(TF2.0)将张量转换为列表,然后像 @mrry 提到的那样使用 tf.stack。(使用多维张量时,注意 unstack 中的轴参数)

a_list = tf.unstack(a_tensor)

a_list[50:55] = [np.nan for i in range(6)]

a_tensor = tf.stack(a_list)