Tensorflow易错点(1)---tensor的“只读“模式

it2025-05-23  9

在使用Encoder-Decoder机制解决文本识别问题时,我想要将Decoder端的每一时间步的预测prediction放入predictions中,让predictions能够记录全部时间步的prediction。

假设prediction形如(N, ), 最大时间步为max_time_steps。

首先说我使用的错误方式,predictions设为全0矩阵 ,大小为(N, max_time_steps), 之后再每个时间步给predictions赋值。代码如下 # 方式1 predictions = np.zeros((N, max_time_steps), dtype=np.int64) for t in range(max_time_steps): ... prediction = tf.argmax(logit, axis=-1) predictions[:, t] = prediction # 方式2 predictions = tf.zeros(shape=(N, max_time_steps), dtype=tf.int64) for t in range(max_time_steps): ... prediction = tf.argmax(logit, axis=-1) predictions[:, t] = prediction

这两种方式错误解析: 方式1:predictions 是numpy array, 而prediction是tensor,tensor不能直接赋值给array。而若要将tensor转化成numpy array,需要到session里面转化,改代码很不方便!那既然这样就自然有了方式2,将predictions用tensor,那这样两者就都是同一种数据类型了,那我自然可以给predictions赋值!事实证明,我错了!原因请看方式2的错误解析。 方式2:predictions是tensor,prediction是tensor,但是,tensor不能直接修改值!类似于"只读"模式,可以读取predictions[:, t]的数据,但是不能修改predictions[:, t]的数据! 没有numpy array 方便阿!

**2. 正确的方法:**使用tensorflow中的tf.stack方法,将prediction一列列的堆叠到predictions上!最终predictions形如(N, max_time_steps)。

for t in range(max_time_steps): ... prediction = tf.argmax(logit, axis=-1) if t == 1: predictions = prediction else: predictions = tf.stack([predictions, prediction], axis=1)
最新回复(0)