我正试图找出如何将序列数据的张量分割成多个部分,这是基于使用二进制数"1"的值对连续掩码进行划分的基础上的.

我已经阅读了官方文件.

我试过tf.衣衫褴褛的boolean\u mask'但它似乎不适合我的情况.

我的解释的可视化示例是:

输入:

# both are tensors, NOT data.
data_tensor = ([3,5,6,2,6,1,3,9,5])
mask_tensor = ([0,1,1,1,0,0,1,1,0])

预期输出:

output_tensor = ([[3],[5,6,2],[6,1],[3,9],[5]])

非常感谢.

推荐答案

我最近发现了一种方法,通过@AloneTogether在this answer中以非常干净的方式完成:

import tensorflow as tf

data_tensor = tf.constant([3,5,6,2,6,1,3,9,5])
mask_tensor = tf.constant([0,1,1,1,0,0,1,1,0])

# Index where the mask changes.
change_idx = tf.concat([tf.where(mask_tensor[:-1] != mask_tensor[1:])[:, 0], [tf.shape(mask_tensor)[0]-1]], axis=0)

# Ranges of indices to gather.
ragged_idx = tf.ragged.range(tf.concat([[0], change_idx[:-1] + 1], axis=0), change_idx + 1)

# Gather ranges into ragged tensor.
output_tensor = tf.gather(data_tensor, ragged_idx)

print(output_tensor)
<tf.RaggedTensor [[3], [5, 6, 2], [6, 1], [3, 9], [5]]>

Python相关问答推荐

try 从网站获取表(ValueRight:如果使用所有纯量值,则必须传递索引)

OdooElectron 商务产品详情页面中add_qty参数动态更新

不同数据类型的Python成员变量不会在具有相同优先级的不同线程中更新

从包含基本数据描述的文本字段中识别和检索特定字符序列

有什么方法可以修复奇怪的y轴Python matplotlib图吗?

KNN分类器中的GridSearchCV

如何使用Tkinter创建两个高度相同的框架(顶部和底部)?

Pydantic 2.7.0模型接受字符串日期时间或无

大Pandas 胚胎中产生组合

如何在虚拟Python环境中运行Python程序?

如何将Docker内部运行的mariadb与主机上Docker外部运行的Python脚本连接起来

如何从数据库上传数据到html?

给定高度约束的旋转角解析求解

提取相关行的最快方法—pandas

SQLAlchemy bindparam在mssql上失败(但在mysql上工作)

在两极中过滤

如何在TensorFlow中分类多个类

基于行条件计算(pandas)

如何获取Python synsets列表的第一个内容?

PYTHON、VLC、RTSP.屏幕截图不起作用