我想在我的代码中找到一种方法避免循环。我需要实现以下公式,起初很简单:
换句话说:索引的列表被解析为 I 。对于 I 中指定的每个索引,需要减去数组 x 中所有后续索引的值。对减去的值进行一些计算。总结一切。完成。
我当前的代码:
def loss(x, indices):
"""
Args:
x: array_like, dtype=float
indices: array_like, dtype=int
Example:
>>> x = np.array([0.3, 0.5, 0.2, 0.1, 1.2, 2.4, 2.8, 1.5, 3.2])
>>> indices = np.array([0, 2, 3, 6])
>>> print(loss(x, indices))
21.81621815885847
"""
total = 0.0
for index in indices:
# Broadcasting here, as all values from all following indices have
# to be subtracted from the value at the given i index.
difference = x[index] - x[index + 1:]
# Sum all up
log_addition = 1.0 + np.log(np.abs(difference))
total += np.sum(log_addition)
return total
具有挑战性的部分是'i'索引在输出范围内随机分布。有什么想法吗?
答案 0 :(得分:4)
这里有一个基于NumPy的矢量化-
mask = indices[:,None] < np.arange(len(x))
v = x[indices,None] - x
vmasked = v[mask]
log_addition = np.log(np.abs(vmasked))
out = log_addition.sum() + mask.sum()
或者,根据对数定律,我们可以将最后两个步骤替换为-
out = np.log(np.prod(np.abs(vmasked))).sum() + mask.sum()
将abs
推出,以使其在标量上运行,它将是-
out = np.log(np.abs(np.prod(vmasked))).sum() + mask.sum()
同样,我们可以将multi-cores
与numexpr
一起使用-
import numexpr as ne
out = np.log(np.abs(ne.evaluate('prod(vmasked)'))) + mask.sum()
如果您发现甚至v
中也有太多不需要的元素,我们可以直接使用-
vmasked
xi = x[indices]
x2D = np.broadcast_to(x, (len(indices),len(x)))
vmasked = np.repeat(xi,mask.sum(1))-x2D[mask]