본문 바로가기
파이썬

파이썬 TypeError : 'Tensor'개체는 TensorFlow에서 항목 할당을 지원하지 않습니다.

by º기록 2020. 11. 3.
반응형

이 코드를 실행하려고합니다.

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 텐서에 할당 할 수없는 것 같습니다. 어떻게 고칠 수 있습니까?

 

해결 방법

 

일반적으로 TensorFlow 텐서 객체는 할당 할 수 없기 때문에 * 할당의 왼쪽에서 사용할 수 없습니다.


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)


 

참조 페이지 https://stackoverflow.com/questions/37697747

 

 

반응형

댓글