TensorFlow函数:tf.where

在之前版本对应函数tf.select

官方解释:

 tf.where(input, name=None)`
Returns locations of true values in a boolean tensor. This operation returns the coordinates of true elements in input. The coordinates are returned in a 2-D tensor where the first dimension (rows) represents the number of true elements, and the second dimension (columns) represents the coordinates of the true elements. Keep in mind, the shape of the output tensor can vary depending on how many true values there are in input. Indices are output in row-major order. For example:
# 'input' tensor is [[True, False]
# [True, False]]
# 'input' has two true values, so output has two coordinates.
# 'input' has rank of 2, so coordinates have two indices.
where(input) ==> [[0, 0],
[1, 0]] # `input` tensor is [[[True, False]
# [True, False]]
# [[False, True]
# [False, True]]
# [[False, False]
# [False, True]]]
# 'input' has 5 true values, so output has 5 coordinates.
# 'input' has rank of 3, so coordinates have three indices.
where(input) ==> [[0, 0, 0],
[0, 1, 0],
[1, 0, 1],
[1, 1, 1],
[2, 1, 1]]

有两种用法:

1、tf.where(tensor)

tensor 为一个bool 型张量,where函数将返回其中为true的元素的索引。如上图官方注释

2、tf.where(tensor,a,b)

a,b为和tensor相同维度的tensor,将tensor中的true位置元素替换为a中对应位置元素,false的替换为b中对应位置元素。

例:

import tensorflow as tf
import numpy as np
sess=tf.Session() a=np.array([[1,0,0],[0,1,1]])
a1=np.array([[3,2,3],[4,5,6]]) print(sess.run(tf.equal(a,1)))
print(sess.run(tf.where(tf.equal(a,1),a1,1-a1)))

>>[[true,false,false],[false,true,true]]

>>[[3,-1,-2],[-3,5,6]]

Tensorflow 学习笔记 -----tf.where的更多相关文章

  1. tensorflow 学习笔记-- tf.reduce_max、tf.sequence_mask

    1.tf.reduce_max函数的作用:计算张量的各个维度上的元素的最大值.例子: import tensorflow as tfmax_value = tf.reduce_max([1, 3, 2 ...

  2. Tensorflow学习笔记2:About Session, Graph, Operation and Tensor

    简介 上一篇笔记:Tensorflow学习笔记1:Get Started 我们谈到Tensorflow是基于图(Graph)的计算系统.而图的节点则是由操作(Operation)来构成的,而图的各个节 ...

  3. Tensorflow学习笔记2019.01.22

    tensorflow学习笔记2 edit by Strangewx 2019.01.04 4.1 机器学习基础 4.1.1 一般结构: 初始化模型参数:通常随机赋值,简单模型赋值0 训练数据:一般打乱 ...

  4. Tensorflow学习笔记2019.01.03

    tensorflow学习笔记: 3.2 Tensorflow中定义数据流图 张量知识矩阵的一个超集. 超集:如果一个集合S2中的每一个元素都在集合S1中,且集合S1中可能包含S2中没有的元素,则集合S ...

  5. TensorFlow学习笔记之--[compute_gradients和apply_gradients原理浅析]

    I optimizer.minimize(loss, var_list) 我们都知道,TensorFlow为我们提供了丰富的优化函数,例如GradientDescentOptimizer.这个方法会自 ...

  6. 深度学习-tensorflow学习笔记(1)-MNIST手写字体识别预备知识

    深度学习-tensorflow学习笔记(1)-MNIST手写字体识别预备知识 在tf第一个例子的时候需要很多预备知识. tf基本知识 香农熵 交叉熵代价函数cross-entropy 卷积神经网络 s ...

  7. 深度学习-tensorflow学习笔记(2)-MNIST手写字体识别

    深度学习-tensorflow学习笔记(2)-MNIST手写字体识别超级详细版 这是tf入门的第一个例子.minst应该是内置的数据集. 前置知识在学习笔记(1)里面讲过了 这里直接上代码 # -*- ...

  8. tensorflow学习笔记(4)-学习率

    tensorflow学习笔记(4)-学习率 首先学习率如下图 所以在实际运用中我们会使用指数衰减的学习率 在tf中有这样一个函数 tf.train.exponential_decay(learning ...

  9. tensorflow学习笔记(3)前置数学知识

    tensorflow学习笔记(3)前置数学知识 首先是神经元的模型 接下来是激励函数 神经网络的复杂度计算 层数:隐藏层+输出层 总参数=总的w+b 下图为2层 如下图 w为3*4+4个   b为4* ...

随机推荐

  1. POJ1338 & POJ2545 & POJ2591 & POJ2247 找给定规律的数

    POJ1338 2545 2591 2247都是一个类型的题目,所以放到一起来总结 POJ1338:Ugly Numbers Time Limit: 1000MS   Memory Limit: 10 ...

  2. Metasploit的armitage初步使用

      armitage的启动 root@kali:~# armitage 别急,过会儿就好了 .  等扫描完会弹出一个框框然后会多出目标的图标比如目标是打印机

  3. 1570. [POJ3461]乌力波

    ★☆   输入文件:oulipo.in   输出文件:oulipo.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 法国作家乔治·佩雷克(Georges Perec,1 ...

  4. GPU开发笔记(一)

    首先我想到的是把安装好的CUDA下的programdata里面的demo都找一找,看看有没有自己需要的demo程序. 然后去CSDN或者pudn上去找找开源的代码. 至于GITHUB还没找过. 其次是 ...

  5. python import windows文件路经

    import sys sys.path.append("E:\\python\\workspacepython\\PY001\\src\\testpy01") import str ...

  6. MySQL服务正在启动或停止中,请稍候片刻后再试一次【解决方案】

    相信有些小伙伴在使用数据库的过程中会经常频繁的启动和停止MySQL服务,有时候会出现“服务正在启动或停止中,请稍候片刻后再试一次.”这样的提示,如下图所示. 于是乎想办法去解决这个问题,但是发现连强制 ...

  7. [USACO08DEC]拍头Patting Heads 水题

    类似素数筛,暴力可过,不需要太多的优化 Code: #include<cstdio> #include<algorithm> #include<string> us ...

  8. Redis特点以及安装

       Mysql 的数据 是以"文件形式存储在硬盘"里边.硬盘运行速度相比较CPU.内存是排在第三的.而 Redis 是内存高速缓存数据库,运行速度比 Mysql 速度快,也支持数 ...

  9. NodeJS学习笔记 (24)本地路径处理-path(ok)

    模块概览 在nodejs中,path是个使用频率很高,但却让人又爱又恨的模块.部分因为文档说的不够清晰,部分因为接口的平台差异性. 将path的接口按照用途归类,仔细琢磨琢磨,也就没那么费解了. 获取 ...

  10. iOS开发——打包报错error: linker command failed with exit code 1

    真机运行没问题,打包报错: clang: error: linker command failed with exit code 1 (use -v to see invocation) 原因:在Xc ...