[英]Tensorflow / Keras Batch normalization with NaN
In a time sequence classification task, I used np.nan
as an indicator for missing values (ie, end of sequence). 在时间序列分类任务中,我使用
np.nan
作为缺失值的指示符(即序列结束)。 I can calculate the length of the sequence by my own and use tf.nn.dynamic_rnn
to build the RNN layers (I know it is deprecated, it is just a POC). 我可以自己计算序列的长度,并使用
tf.nn.dynamic_rnn
来构建RNN层(我知道它已被弃用,它只是一个POC)。
Is there a way to use the out-of-the-box batch normalization to do the normalization excluding the NANs? 有没有办法使用开箱即用的批量标准化来进行除NAN之外的标准化? In a similar way to
np.nanmean
? 以类似于
np.nanmean
? Or do I need to implement it from scratch? 或者我需要从头开始实施吗?
For example, the following code outputs np.nan
s: 例如,以下代码输出
np.nan
s:
import tensorflow as tf
import numpy as np
inp = np.array([
# Sequence 1
[
[0.0, 0.1],
[1.0, 0.2],
[2.0, 0.3],
[np.nan, np.nan],
[np.nan, np.nan]
],
# Sequence 2
[
[1.0, 0.2],
[2.0, 0.3],
[3.0, 0.4],
[4.0, 0.5],
[5.0, 0.6]
]
])
in_training_mode = tf.placeholder(tf.bool)
tf_inp = tf.placeholder(tf.float32, shape=inp.shape)
tf_bn = tf.keras.layers.BatchNormalization(axis=2)(
tf_inp, training=in_training_mode
)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(tf_bn, feed_dict={tf_inp : inp, in_training_mode: True}))
# Output:
# [[[nan nan]
# [nan nan]
# [nan nan]
# [nan nan]
# [nan nan]]
# [[nan nan]
# [nan nan]
# [nan nan]
# [nan nan]
# [nan nan]]]
While the following equivalent code, we get the desired result: 在以下等效代码中,我们得到了所需的结果:
print((inp - np.nanmean(inp, axis=(0,1))) / np.nanstd(inp, axis=(0,1)))
# Output:
# [[[-1.44115338 -1.44115338]
# [-0.80064077 -0.80064077]
# [-0.16012815 -0.16012815]
# [ nan nan]
# [ nan nan]]
# [[-0.80064077 -0.80064077]
# [-0.16012815 -0.16012815]
# [ 0.48038446 0.48038446]
# [ 1.12089708 1.12089708]
# [ 1.76140969 1.76140969]]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.