tensorflow - 如何通过张量流中的索引获取元素?
问题描述
data = tf.constant( [ [ [0, 2, 4, 1], [1, 0, 0, 2] ], [ [1, 0, 4, 6], [2, 6, 3, 1] ] ] )
indices = tf.argmax(data, axis=2)
如何在张量流中得到结果[ [4 2], [6 6] ]
?请帮我!!!!
解决方案
tf.reduce_max()
在你的情况下使用
data = tf.constant([[ [0, 2, 4, 1],[1, 0, 0, 2]], [ [1, 0, 4, 6], [2, 6, 3, 1] ]])
maximum_values = tf.reduce_max(data, reduction_indices=[2])
with tf.Session() as sess:
p=sess.run(maximum_values)
print(p)
[[4 2]
[6 6]]
编辑:要访问其他值,您可以按索引切片,然后使用tf.concat
or tf.stack
。例如,如果你想得到[[0 2] [3 1]]
你可以尝试
with tf.Session() as sess:
p=sess.run(data[0][0][0:2])
print(p)
[0 2]
q=sess.run(data[1][1][2:])
print(q)
[3 1]
r=sess.run(tf.stack([p,q],0))
print(r)
[[0 2]
[3 1]]
推荐阅读
- python-3.x - 当我们设置变量started = False时,“if started:”是什么意思?
- inno-setup - 在 Inno Setup 中修改选定的目录访问权限
- discord.js - discord.js - 等待消息并检查 dm 消息
- razor - Program.cs 和 App.razor 的区别
- java - 如何在 netbeans gui builder 中设置 getter 和 setter?
- python - 如何在处理中编码固定数量的半随机间隔且仍适合固定大小图像的行?
- java - 在 HQL 查询中使用 join fetch 和 group by
- erlang - 如何在 Erlang 中使用变量作为引用传递?
- javascript - NodeJS,Express:分叉的子进程无法使用池进行批量 MySQL 查询
- javascript - React 中的条件渲染;这是路吗?