查找张量的质心(tensorflow) [英] Finding centre of mass of tensor (tensorflow)

查看:136
本文介绍了查找张量的质心(tensorflow)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有找到张量质心的有效方法?我正在使用N个堆叠的体积(Nx64x64x64),并希望获得一个Nx3张量,其x,y,z位置分别为每个64x64x64体积的质心

Is there an efficient way to find the centre of mass of a tensor? I'm working with N stacked volumes (Nx64x64x64) and would like to obtain an Nx3 tensor with the x,y,z position of the centre of mass of each 64x64x64 volume

推荐答案

按照公式,您只需要将每个坐标乘以相应的质量,将所有的总和除以总质量即可.

Following the formula, you should just need to multiply each coordinate by the corresponding mass, sum everything and divide by the total mass:

import tensorflow as tf

# Input volumes
volumes = tf.placeholder(tf.float32, [None, 64, 64, 64])
# Make array of coordinates (each row contains three coordinates)
ii, jj, kk = tf.meshgrid(tf.range(64), tf.range(64), tf.range(64), indexing='ij')
coords = tf.stack([tf.reshape(ii, (-1,)), tf.reshape(jj, (-1,)), tf.reshape(kk, (-1,))], axis=-1)
coords = tf.cast(coords, tf.float32)
# Rearrange input into one vector per volume
volumes_flat = tf.reshape(volumes, [-1, 64 * 64 * 64, 1])
# Compute total mass for each volume
total_mass = tf.reduce_sum(volumes_flat, axis=1)
# Compute centre of mass
centre_of_mass = tf.reduce_sum(volumes_flat * coords, axis=1) / total_mass

这篇关于查找张量的质心(tensorflow)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆