茱莉亚:跨观测值张量的对偶距离计算广播。

问题描述 投票:2回答:1

我正在尝试使用 Distances 包来执行距离矩阵的广播计算。

我知道如何计算单个 N x N 对某个矩阵的距离矩阵 X (附尺寸 D x N),其中每列 X[:,i] 储存 D-观察的维度特征向量 i. 代码为

using Distances

dist_matrix = pairwise(Euclidean(), X, dims = 2)

dist_matrix 包含了每一对欧几里得的距离 D-二维列,例如 dist_matrix[m,n] 之间的欧几里得距离。X[:,m]X[:,n].

现在想象一下我的阵列 X 其实是一个整体 张量 或 "量 "的 D-观测值,因此 X[:,i,j] 储存的 j-我的 "片子"。D x N 观察结果。整个阵列 X 因此,有尺寸 D x N x T,其中 T 是切片的数量。

因此,我想计算一个 张量 或距离矩阵的 "体积",所以 dist_matrix 将有尺寸 N x N x T.

是否有办法在一条线上通过广播的方式来实现这一点。pairwise() 茱莉亚中的函数?最快的方法是什么?下面用一个基本的for循环来展示这个想法。

using Distances

dist_matrix_tensor = zeros(N,N,T);

for t = 1:T
        dist_matrix_tensor[:,:,t] = pairwise(Euclidean(), X[:,:,t], dims = 2)
end

EDIT: 我想出了如何使用... mapslices但仍然不确定这是否是最好的方法。

using Distances

dist_function(x)  = pairwise(Euclidean(), x, dims = 2) # define a function that gets the N x N distance matrix for a single 'slice'

dist_matrix_tensor = mapslices(dist_function, X, dims = [1,2]) # map your matrix-operating function across the slices of the main tensor X

当然,这也可以并行化,因为在这个计算中,X的每个 "片 "都是独立的,所以我基本上只是在寻找最快的方法来完成这个任务。我也对你在广播方面的具体操作感兴趣。

julia distance array-broadcasting distance-matrix
1个回答
2
投票

你的解决方案与 mapslices 是合理的性能,如果 X 为大。下面是一个使用JuliennedArrays的例子,它对小尺寸的 X的性能,但具有与 mapslices 当两个第一维度的大小为100时,返回一个JuliennedArrays。

using Distances, JuliennedArrays, BenchmarkTools

dist_function(x)  = pairwise(Euclidean(), x, dims = 2) # define a function that gets the N x N distance matrix for a single 'slice'

X = randn(10,10,20);
dist_matrix_tensor = @btime mapslices(dist_function, X, dims = [1,2]); # 61.172 μs (198 allocations: 42.28 KiB)
dist_matrix_tensor2 = @btime map(dist_function, Slices(X, 1, 2)); # 41.529 μs (62 allocations: 21.67 KiB)

但请注意,JuliennedArrays会返回一個 VectorMatrix 而不是三维阵列。

© www.soinside.com 2019 - 2024. All rights reserved.