张量流中的广播和降维
我在物理上有以下内容
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
人们可能想到的第一个解决方案可能是
,它将公式直接转换为代码。不过,使用张量流提供的隐式广播效率稍高一些。
微基准测试代码 1(大数据集):
输出:
微基准测试代码 2(小数据集):
输出:
The first solution one might come up with this could be
, which is the direct translation of your formula to code. Though, using the implicit broadcast provided by tensorflow is marginally more efficient.
code 1 for microbenchmark (large dataset):
output:
code 2 for microbenchmark (small dataset):
output: