gpt4 book ai didi

python - 如何在张量中仅平均非零条目?

转载 作者:行者123 更新时间:2023-12-04 12:42:20 24 4
gpt4 key购买 nike

我遇到过一个平均包含填充值的情况。给定一个张量 X某种形状 (batch_size, ..., features) ,可能有零填充特征来获得相同的形状。

如何平均 X 的最终尺寸(特征)但只有非零条目?因此,我们将总和除以非零条目的数量。

示例输入:

x = [[[[1,2,3], [2,3,4], [0,0,0]],
[[1,2,3], [2,0,4], [3,4,5]],
[[1,2,3], [0,0,0], [0,0,0]],
[[1,2,3], [1,2,3], [0,0,0]]],
[[[1,2,3], [0,1,0], [0,0,0]],
[[1,2,3], [2,3,4], [0,0,0]],
[[1,2,3], [0,0,0], [0,0,0]],
[[1,2,3], [1,2,3], [1,2,3]]]]
# Desired output
y = [[[1.5 2.5 3.5]
[2. 2. 4. ]
[1. 2. 3. ]
[1. 2. 3. ]]
[[0.5 1.5 1.5]
[1.5 2.5 3.5]
[1. 2. 3. ]
[1. 2. 3. ]]]

最佳答案

纯 Keras 解决方案计算非零条目的数量,然后相应地除以总和。这是一个自定义层:

import keras.layers as L
import keras.backend as K

class NonZeroMean(L.Layer):
"""Compute mean of non-zero entries."""
def call(self, x):
"""Calculate non-zero mean."""
# count the number of nonzero features, last axis
nonzero = K.any(K.not_equal(x, 0.0), axis=-1)
n = K.sum(K.cast(nonzero, 'float32'), axis=-1, keepdims=True)
x_mean = K.sum(x, axis=-2) / n
return x_mean

def compute_output_shape(self, input_shape):
"""Collapse summation axis."""
return input_shape[:-2] + (input_shape[-1],)

我想需要添加一个条件来检查所有特征是否为零并返回零,否则我们会得到除以零错误。当前示例测试:
# Dummy data
x = [[[[1,2,3], [2,3,4], [0,0,0]],
[[1,2,3], [2,0,4], [3,4,5]],
[[1,2,3], [0,0,0], [0,0,0]],
[[1,2,3], [1,2,3], [0,0,0]]],
[[[1,2,3], [0,1,0], [0,0,0]],
[[1,2,3], [2,3,4], [0,0,0]],
[[1,2,3], [0,0,0], [0,0,0]],
[[1,2,3], [1,2,3], [1,2,3]]]]
x = np.array(x, dtype='float32')

# Example run
x_input = K.placeholder(shape=x.shape, name='x_input')
out = NonZeroMean()(x_input)
s = K.get_session()
print("INPUT:", x)
print("OUTPUT:", s.run(out, feed_dict={x_input: x}))

关于python - 如何在张量中仅平均非零条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53303724/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com