张量流中的广播和降维

发布于 2025-01-10 00:42:09 字数 391 浏览 3 评论 0原文

我在物理上有以下内容

  tensor A with A.shape = (N,2) 
  tensor B with B.shape = (3,2)

,我将 A 可视化为二维中的 N 个数据点。

B 是同一二维中的 3 个中心。

我的目标是计算 A 与 3 个中心中每一个中心的平方距离,然后将它们相加(即系统距 3 个中心的惯性总和)。 我想计算

$$ D = \Sum_{i,j} (A(i,j) - B(1,j))^2 + (A(i,j) - B(2,j))^2 + (A(i,j) - B(3,j))^2 $$ 

有人可以帮我弄清楚如何在tensorflow + python中实现这一点吗?提前致谢

I have the following

  tensor A with A.shape = (N,2) 
  tensor B with B.shape = (3,2)

physically I am visualizing A as N data points in 2 dimension.

B is 3 centers in the same 2 dimension.

My objective is to compute the squared distance of A from each of the 3 centers and then add them up (that is the sum total of inertia of the system from the 3 centers).
I want to compute

$ D = \Sum_{i,j} (A(i,j) - B(1,j))^2 + (A(i,j) - B(2,j))^2 + (A(i,j) - B(3,j))^2 $ 

Can someone please help me figure out how to achieve this in tensorflow + python. Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贱贱哒 2025-01-17 00:42:09

人们可能想到的第一个解决方案可能是

tf.reduce_sum(tf.square(A-B[0])+tf.square(A-B[1])+tf.square(A-B[2]))

,它将公式直接转换为代码。不过,使用张量流提供的隐式广播效率稍高一些。

tf.reduce_sum(tf.square(A[:,None,:]-B[None,:,:]))

微基准测试代码 1(大数据集):

A=tf.random.normal((2**25,2))
B=tf.random.normal((3,2))
%timeit tf.reduce_sum(tf.square(A[:,None,:]-B[None,:,:]))
%timeit tf.reduce_sum(tf.square(A-B[0])+tf.square(A-B[1])+tf.square(A-B[2]))

输出:

13.1 ms ± 38.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
15.7 ms ± 7.83 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

微基准测试代码 2(小数据集):

A=tf.random.normal((3,2))
B=tf.random.normal((3,2))
%timeit tf.reduce_sum(tf.square(A[:,None,:]-B[None,:,:]))
%timeit tf.reduce_sum(tf.square(A-B[0])+tf.square(A-B[1])+tf.square(A-B[2]))

输出:

175 µs ± 731 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)
391 µs ± 18 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

The first solution one might come up with this could be

tf.reduce_sum(tf.square(A-B[0])+tf.square(A-B[1])+tf.square(A-B[2]))

, which is the direct translation of your formula to code. Though, using the implicit broadcast provided by tensorflow is marginally more efficient.

tf.reduce_sum(tf.square(A[:,None,:]-B[None,:,:]))

code 1 for microbenchmark (large dataset):

A=tf.random.normal((2**25,2))
B=tf.random.normal((3,2))
%timeit tf.reduce_sum(tf.square(A[:,None,:]-B[None,:,:]))
%timeit tf.reduce_sum(tf.square(A-B[0])+tf.square(A-B[1])+tf.square(A-B[2]))

output:

13.1 ms ± 38.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
15.7 ms ± 7.83 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

code 2 for microbenchmark (small dataset):

A=tf.random.normal((3,2))
B=tf.random.normal((3,2))
%timeit tf.reduce_sum(tf.square(A[:,None,:]-B[None,:,:]))
%timeit tf.reduce_sum(tf.square(A-B[0])+tf.square(A-B[1])+tf.square(A-B[2]))

output:

175 µs ± 731 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)
391 µs ± 18 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文