Python 形状为b*n*3的张量流张量T1。形状为b*n->;一个布尔张量,指示T1中的哪些行
我正在尝试提取Python 形状为b*n*3的张量流张量T1。形状为b*n->;一个布尔张量,指示T1中的哪些行,python,tensorflow,Python,Tensorflow,我正在尝试提取n点三维坐标和b批次中的特定行。本质上,我的张量T1的形状是b*n*3。我还有另一个布尔张量T2,形状为b*n,指示需要使用的n行。 基本上,我的输出应该是b*?*3,因为T2在每个批次中可以有不同数量的1 我已经使用布尔掩码实现了以下内容,但输出不是预期的,输出形状是(?,),但不是(b*?*3) 由于每个示例所选元素的数量可能不同,因此不能用适当的张量表示。一个选择可能是使用一个。它们不能做正常张量所能做的一切,但可以实现您想要的,例如: import tensorflow a
n
点三维坐标和b
批次中的特定行。本质上,我的张量T1
的形状是b*n*3
。我还有另一个布尔张量T2
,形状为b*n
,指示需要使用的n
行。
基本上,我的输出应该是b*?*3
,因为T2
在每个批次中可以有不同数量的1
我已经使用布尔掩码实现了以下内容,但输出不是预期的,输出形状是(?,)
,但不是(b*?*3)
由于每个示例所选元素的数量可能不同,因此不能用适当的张量表示。一个选择可能是使用一个。它们不能做正常张量所能做的一切,但可以实现您想要的,例如:
import tensorflow as tf
with tf.Graph().as_default(), tf.Session() as sess:
# Input data
t1 = tf.constant([
[
[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
],
[
[10, 11, 12],
[13, 14, 15],
[16, 17, 18],
],
])
t2 = tf.constant([
[1, 0, 1],
[0, 1, 0],
])
# Count the number of ones for each row in T2
c = tf.reduce_sum(t2, axis=1)
# Ragged ranges for each row
r = tf.ragged.range(c)
# Sorting indices so indices with a one are first
s = tf.argsort(t2, axis=1, direction='DESCENDING', stable=True)
# First axis dimension index
idx0 = tf.expand_dims(tf.range(tf.shape(t1)[0]), 1) * tf.ones_like(r)
# 2D index for getting indices of ones on each row
idx_s = tf.stack([idx0, r], axis=-1)
# Get indices of ones
idx1 = tf.gather_nd(s, idx_s)
# 2D index to get indices of selected vectors in T1
idx = tf.stack([idx0, idx1], axis=-1)
# Get selected vectors
result = tf.gather_nd(t1, idx)
# Print result
print(sess.run(result))
# <tf.RaggedTensorValue [[[1, 2, 3], [7, 8, 9]], [[13, 14, 15]]]>
将tensorflow导入为tf
将tf.Graph()作为默认值(),将tf.Session()作为sess:
#输入数据
t1=tf.常数([
[
[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
],
[
[10, 11, 12],
[13, 14, 15],
[16, 17, 18],
],
])
t2=tf常数([
[1, 0, 1],
[0, 1, 0],
])
#计算T2中每行的1个数
c=tf.减少_和(t2,轴=1)
#每行的不规则范围
r=tf.参差不齐范围(c)
#对索引进行排序,以使带有1的索引位于第一位
s=tf.argsort(t2,轴=1,方向='下降',稳定=真)
#第一轴维度索引
idx0=tf.扩展尺寸(tf.范围(tf.形状(t1)[0]),1)*tf.形状(r)
#2D索引,用于获取每行上的索引
idx_s=tf.stack([idx0,r],轴=-1)
#获取1的索引
idx1=tf.gather\u nd(s,idx\u s)
#2D索引以获取T1中选定向量的索引
idx=tf.stack([idx0,idx1],轴=-1)
#获取选定向量
结果=tf.聚集(t1,idx)
#打印结果
打印(sess.run(结果))
#
由于每个示例所选元素的数量可能不同,因此不能用适当的张量表示。一个选择可能是使用一个。它们不能做正常张量所能做的一切,但可以实现您想要的,例如:
import tensorflow as tf
with tf.Graph().as_default(), tf.Session() as sess:
# Input data
t1 = tf.constant([
[
[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
],
[
[10, 11, 12],
[13, 14, 15],
[16, 17, 18],
],
])
t2 = tf.constant([
[1, 0, 1],
[0, 1, 0],
])
# Count the number of ones for each row in T2
c = tf.reduce_sum(t2, axis=1)
# Ragged ranges for each row
r = tf.ragged.range(c)
# Sorting indices so indices with a one are first
s = tf.argsort(t2, axis=1, direction='DESCENDING', stable=True)
# First axis dimension index
idx0 = tf.expand_dims(tf.range(tf.shape(t1)[0]), 1) * tf.ones_like(r)
# 2D index for getting indices of ones on each row
idx_s = tf.stack([idx0, r], axis=-1)
# Get indices of ones
idx1 = tf.gather_nd(s, idx_s)
# 2D index to get indices of selected vectors in T1
idx = tf.stack([idx0, idx1], axis=-1)
# Get selected vectors
result = tf.gather_nd(t1, idx)
# Print result
print(sess.run(result))
# <tf.RaggedTensorValue [[[1, 2, 3], [7, 8, 9]], [[13, 14, 15]]]>
将tensorflow导入为tf
将tf.Graph()作为默认值(),将tf.Session()作为sess:
#输入数据
t1=tf.常数([
[
[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
],
[
[10, 11, 12],
[13, 14, 15],
[16, 17, 18],
],
])
t2=tf常数([
[1, 0, 1],
[0, 1, 0],
])
#计算T2中每行的1个数
c=tf.减少_和(t2,轴=1)
#每行的不规则范围
r=tf.参差不齐范围(c)
#对索引进行排序,以使带有1的索引位于第一位
s=tf.argsort(t2,轴=1,方向='下降',稳定=真)
#第一轴维度索引
idx0=tf.扩展尺寸(tf.范围(tf.形状(t1)[0]),1)*tf.形状(r)
#2D索引,用于获取每行上的索引
idx_s=tf.stack([idx0,r],轴=-1)
#获取1的索引
idx1=tf.gather\u nd(s,idx\u s)
#2D索引以获取T1中选定向量的索引
idx=tf.stack([idx0,idx1],轴=-1)
#获取选定向量
结果=tf.聚集(t1,idx)
#打印结果
打印(sess.run(结果))
#
如果T2
在每个示例中可以有不同的1s数,那么结果就不可能是一个合适的张量,因为在每种情况下,第二维度的大小都会不同。这可能有点像一个例子,但这不是很受支持。对于正则张量,最好的目标是使用一个张量,其中示例需要“填充”,以便所有示例都具有相同的大小(另一个张量表示每个示例的有效条目数)。谢谢jdehesa。你能提供一个只有参差不齐张量的例子吗?如果T2
在每个例子中可以有不同数量的1,那么结果就不能是一个合适的张量,因为在每种情况下,第二维度的大小都会不同。这可能有点像一个例子,但这不是很受支持。对于正则张量,最好的目标是使用一个张量,其中示例需要“填充”,以便所有示例都具有相同的大小(另一个张量表示每个示例的有效条目数)。谢谢jdehesa。你能提供一个只有不规则张量的例子吗?