tensorflow 的tf.where详解

最近在用到数据筛选,观看代码中有tf.where()的用法,不是很常用,也不是很好理解。在这里记录一下

1 tf.where(
2     condition,
3     x=None,
4     y=None,
5     name=None
6 )

Return the elements, either from x or y, depending on the condition.

理解:where嘛,就是要根据条件找到你要的东西。

condition:条件,是一个boolean

x:数据

y:同x维度的数据。

返回,返回符合条件的数据。当条件为真,取x对应的数据;当条件为假,取y对应的数据

举例子。

复制代码
 1 def test_where():
 2     # 定义一个tensor,表示condition,内部数据随机产生
 3     condition = tf.convert_to_tensor(np.random.random([5]), dtype=tf.float32)
 4 
 5     # 定义两个tensor,表示原数据
 6     a = tf.ones(shape=[5, 3], name='a')
 7 
 8     b = tf.zeros(shape=[5, 3], name='b')
 9 
10     # 选择大于0.5的数值的坐标,并根据condition信息在a和b中选取数据
11     result = tf.where(condition > 0.5, a, b)
12 
13     with tf.Session() as sess:
14         print("condition:\n", sess.run([condition, result]))
复制代码

结果:

 

posted @   今夜无风  阅读(5432)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示