当已经有张量时,使用tf.unstack (TF2.0)将张量转换为列表,然后使用@mrry提到的tf.stack。 (使用多维张量时,请注意unstack中的axis参数)
a_list = tf.unstack(a_tensor)
a_list[50:55] = [np.nan for i in range(6)]
a_tensor = tf.stack(a_list)
0
当已经有张量时,使用tf.unstack (TF2.0)将张量转换为列表,然后使用@mrry提到的tf.stack。 (使用多维张量时,请注意unstack中的axis参数)
a_list = tf.unstack(a_tensor)
a_list[50:55] = [np.nan for i in range(6)]
a_tensor = tf.stack(a_list)
0
通常,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()
等方法。但是, rnn.rnn()
可能返回不支持此方法的tf.Tensor
对象。
0
您可以这样做的另一种方式。
aa=tf.Variable(tf.zeros(3, tf.int32))
aa=aa[2].assign(1)
那么输出是:
数组([0,0,1],dtype = int32)
参考: https : //www.tensorflow.org/api_docs/python/tf/Variable#assign
0
我尝试运行以下代码:
但是我在最后一行收到错误:
TypeError: 'Tensor' object does not support item assignment
似乎我无法分配给张量,如何解决呢?