Tensorflow 填充实现

Ант*_*ков 2 python tensorflow

我正在尝试在张量流框架中为一维张量实现类似 pandas 的 fill 方法。

我需要传播张量中最后一个非 NaN 值:

[1,nan,nan,0,nan,nan,2,nan,3]   =>   [1,1,1,0,0,0,2,2,3]
Run Code Online (Sandbox Code Playgroud)

我用 tf.scan 尝试了这个实现,但没有运气:

def ffill(previous_output, current_input):
  condition = tf.equal(current_input,np.nan)
  return tf.cond(condition,lambda: previous_output,lambda : current_input)

initializer = tf.constant(0.0)
filled_tensor = tf.scan(ffill, nan_tensor, initializer)
Run Code Online (Sandbox Code Playgroud)

请帮忙

jde*_*esa 6

这是一种方法:

import math
import tensorflow as tf

# Input data
input = tf.placeholder(tf.float32, [None])
# Find non-NaN values
mask = ~tf.is_nan(input)
# Take non-NaN values and precede them with a NaN
values = tf.concat([[math.nan], tf.boolean_mask(input, mask)], axis=0)
# Use cumsum over mask to find the index of the non-NaN value to pick
idx = tf.cumsum(tf.cast(mask, tf.int64))
# Gather values
result = tf.gather(values, idx)
# Test
with tf.Session() as sess:
    input_test = [1, math.nan, math.nan, 0, math.nan, math.nan, 2, math.nan, 3]
    print(sess.run(result, feed_dict={input: input_test}))
Run Code Online (Sandbox Code Playgroud)

输出:

import math
import tensorflow as tf

# Input data
input = tf.placeholder(tf.float32, [None])
# Find non-NaN values
mask = ~tf.is_nan(input)
# Take non-NaN values and precede them with a NaN
values = tf.concat([[math.nan], tf.boolean_mask(input, mask)], axis=0)
# Use cumsum over mask to find the index of the non-NaN value to pick
idx = tf.cumsum(tf.cast(mask, tf.int64))
# Gather values
result = tf.gather(values, idx)
# Test
with tf.Session() as sess:
    input_test = [1, math.nan, math.nan, 0, math.nan, math.nan, 2, math.nan, 3]
    print(sess.run(result, feed_dict={input: input_test}))
Run Code Online (Sandbox Code Playgroud)