python - 由张量流中的索引张量指定的切片二维张量
问题描述
我有一个二维张量,我想从每一行中提取几个起始元素。
我有必须捕获的元素的列索引(对于每一行)。
请注意,不同行的列索引不同。
以下示例使其创建:
2d 张量是:
[[4 2 4 4 1 1 1 1 1 1 1 1 1 1 1 1]
[4 4 4 4 4 4 4 1 1 1 1 1 1 1 1 1]
[4 4 4 5 4 4 4 1 1 1 1 1 1 1 1 1]
[4 4 1 4 4 4 4 1 1 1 1 1 1 1 1 1]
[4 4 4 4 6 4 4 8 8 1 1 1 1 1 1 1]
[3 9 9 9 9 9 9 1 1 1 1 1 1 1 1 1]
[3 9 9 9 9 9 9 1 1 1 1 1 1 1 1 1]
[1 9 9 9 9 9 9 1 1 1 1 1 1 1 1 1]
[3 9 4 9 9 9 9 1 1 1 1 1 1 1 1 1]
[3 9 9 6 9 9 9 1 1 1 1 1 1 1 1 1]]
并且索引数组是:
[4 7 7 7 9 7 7 7 7 7]
如何从上面的索引数组中获取以下数组:
[[4 2 4 4 ]
[4 4 4 4 4 4 4 ]
[4 4 4 5 4 4 4]
[4 4 1 4 4 4 4]
[4 4 4 4 6 4 4 8 8]
[3 9 9 9 9 9 9 ]
[3 9 9 9 9 9 9 ]
[1 9 9 9 9 9 9 ]
[3 9 4 9 9 9 9 ]
[3 9 9 6 9 9 9 ]]
解决方案
这是一种将其作为稀疏张量的方法:
import tensorflow as tf
# Example data
data = [[4, 2, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[4, 4, 4, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[4, 4, 4, 5, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[4, 4, 1, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[4, 4, 4, 4, 6, 4, 4, 8, 8, 1, 1, 1, 1, 1, 1, 1],
[3, 9, 9, 9, 9, 9, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[3, 9, 9, 9, 9, 9, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 9, 9, 9, 9, 9, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[3, 9, 4, 9, 9, 9, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[3, 9, 9, 6, 9, 9, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
sizes = [4, 7, 7, 7, 9, 7, 7, 7, 7, 7]
with tf.Graph().as_default():
# Input data
data_ph = tf.placeholder(tf.int32, [None, None])
sizes_ph = tf.placeholder(tf.int32, [None])
shape = tf.shape(data_ph)
# Make coordinates grid
ii, jj = tf.meshgrid(tf.range(shape[0]), tf.range(shape[1]), indexing='ij')
# Make mask for values
mask = jj < tf.expand_dims(sizes_ph, 1)
# Take values and coordinates
sp_values = tf.boolean_mask(data_ph, mask)
sp_ii = tf.boolean_mask(ii, mask)
sp_jj = tf.boolean_mask(jj, mask)
# Make sparse index
sp_idx = tf.cast(tf.stack([sp_ii, sp_jj], axis=1), tf.int64)
# Make sparse tensor
sp_tensor = tf.sparse.SparseTensor(sp_idx, sp_values, tf.cast(shape, tf.int64))
# Convert back to dense for testing
sp_to_dense = tf.sparse.to_dense(sp_tensor)
# Test
with tf.Session() as sess:
sp_to_dense_value = sess.run(sp_to_dense, feed_dict={data_ph: data, sizes_ph: sizes})
print(sp_to_dense_value)
输出:
[[4 2 4 4 0 0 0 0 0 0 0 0 0 0 0 0]
[4 4 4 4 4 4 4 0 0 0 0 0 0 0 0 0]
[4 4 4 5 4 4 4 0 0 0 0 0 0 0 0 0]
[4 4 1 4 4 4 4 0 0 0 0 0 0 0 0 0]
[4 4 4 4 6 4 4 8 8 0 0 0 0 0 0 0]
[3 9 9 9 9 9 9 0 0 0 0 0 0 0 0 0]
[3 9 9 9 9 9 9 0 0 0 0 0 0 0 0 0]
[1 9 9 9 9 9 9 0 0 0 0 0 0 0 0 0]
[3 9 4 9 9 9 9 0 0 0 0 0 0 0 0 0]
[3 9 9 6 9 9 9 0 0 0 0 0 0 0 0 0]]
它不是绝对理想的,因为它需要使用完整的坐标网格进行操作。在 NumPy 中,您可能能够先生成索引,然后从密集张量中仅选择您想要的值,但我不确定 TensorFlow 是否可以。
推荐阅读
- google-apps-script - 应用程序脚本 - 自 11 日以来删除表格时遇到问题
- serialization - 无法序列化 IDictionary
在 F# 中的 ASP.Net Core 3.0 中 - python - 在 Altair 图上,您可以更改选择(例如下拉菜单、单选按钮)的显示位置吗?
- mongodb - 将 MongoDB 多个文档输出合并到单个文档
- scala - scala中List [String]的最大大小是多少
- vb.net - 旧 OCR 应用程序无法在新环境中运行
- java - 如何在我的 React-SpringBoot 应用程序中使用 Spring Security
- android - startDragAndDrop 忽略拖动视图是否更改其位置 - 如何在未处理拖放操作后更改它应返回的位置?
- laravel - Bluehost 服务器中的 Laravel 5.8 419 页面过期异常
- android - 如果 RaisedButton.icon() 上的构造函数中的图标值为空,如何保持图标禁用?