python - tf.unique 不重复索引
问题描述
我的输入是例如的张量[8,8,8,2,2,3,1,1,8,8]
。我的输出应该是一个张量,它引用这个张量的每个部分,看起来像这样[0,0,0,1,1,2,3,3,4,4]
:我必须在张量流中计算它。
tf.unique([8,8,8,2,2,3,1,1,8,8])
计算一个[0,0,0,1,1,2,3,3,0,0]
以 0 而不是 4 结尾的张量。
有谁知道如何解决这个问题?
解决方案
你想做的操作其实和 没有太大关系tf.unique
。实现该结果的一种方法是:
import tensorflow as tf
def identify_blocks(a):
neq = tf.not_equal(a[1:], a[:-1])
c = tf.cumsum(tf.dtypes.cast(neq, tf.int32))
return tf.concat([[0], c], axis=0)
a = tf.constant([8, 8, 8, 2, 2, 3, 1, 1, 8, 8])
b = identify_blocks(a)
print(b.numpy())
# [0 0 0 1 1 2 3 3 4 4]
推荐阅读
- pipe - 是否保证 POSIX shell 在执行管道中的任何命令之前始终打开文件进行重定向?
- c# - 如何让玩家忽略代码中与另一个游戏对象的碰撞?
- android - 使用数据绑定(Kotlin)时如何修复自定义 ArrayAdapter 下拉视图剪辑最终项目
- .net - Graphics DrawPath 在渲染文本时产生意外的结果
- python - django 用户更新表单未更新
- python - 无法从python访问redis实例
- sql - 如何使用具有 varchar 列的 unpivot 和 pivot 转置表?
- javascript - 找不到句柄选择反应日期范围
- visual-studio-code - 避免将多个 vscode 实例与图标混淆
- regex - 前导零后匹配所有内容的正则表达式是什么