2016-12-07 31 views
1

我正在尝试对序列的实际长度进行均值运算。 (掩蔽零向量)使用动态长度的TensorFlow平均值

我的输入sequence_outpus是(的batch_size,MAX_LEN,尺寸)

我有存储在批次中的每个序列的实际长度的张量。我从https://danijar.com/variable-sequence-lengths-in-tensorflow/

def length(sequence): 
    used = tf.sign(tf.reduce_max(tf.abs(sequence), reduction_indices=2)) 
    length = tf.reduce_sum(used, reduction_indices=1) 
    length = tf.cast(length, tf.int64) 
    return length 

使用该函数我这样做:

lengths = length(sequence_outputs) 
lengths = tf.cast(length, tf.float32) 
lengths = tf.expand_dims(lengths,1) 
sentence_outputs = tf.reduce_sum(sentence_outputs,1)/lengths 

图表编译但我得到NaN的损耗值。此外,当使用eval()进行调试时,我的长度变为负值。

这似乎是一个简单的问题,但我一直坚持这一段时间,并会感谢一些帮助!

谢谢!

回答

0

我没有看到任何问题。你的代码有点过于复杂。下面的代码

import numpy as np 
import tensorflow as tf 

# creating data 
B = 15 
MAX_LEN = 4 
data = np.zeros([B, MAX_LEN], dtype=np.float32) 

for b in range(B): 
    current_len = np.random.randint(2, MAX_LEN) 
    current_vector = np.concatenate([np.random.randn(current_len), np.zeros(MAX_LEN - current_len)], axis=-1) 
    print("{}\t\t{}".format(current_vector, current_vector.shape)) 
    data[b, ...] = current_vector 

data_op = tf.convert_to_tensor(data) 


def tf_length(x): 
    assert len(x.get_shape().as_list()) == 2 
    length = tf.count_nonzero(x, axis=1, keepdims=True) 
    return length 


x = tf.reduce_sum(data_op, axis=1)/tf_length(data_op) 

# test gradients 
grads = tf.gradients(tf.reduce_mean(x), [data_op]) 

with tf.Session() as sess: 
    print sess.run(grads) 

运行完全正常这里没有任何的NaN。你确定,你真的使用这个代码吗?如果我需要猜测,我敢打赌你在序列长度计算中忘记了tf.abs

请注意:您的长度函数,以及tf_length在这篇文章中,假设序列中非零值!计算序列长度应该是数据生产者的任务,并将其输入到计算图中。其他的一切,我认为是一个hacky的解决方案。