我是靠谱客的博主 精明可乐,最近开发中收集的这篇文章主要介绍[work]TypeError: 'Tensor' object does not support item assignment in TensorFlow,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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.


最后

以上就是精明可乐为你收集整理的[work]TypeError: 'Tensor' object does not support item assignment in TensorFlow的全部内容,希望文章能够帮你解决[work]TypeError: 'Tensor' object does not support item assignment in TensorFlow所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部