【TensorFlow】tf.where

2024-08-27 18:32
文章标签 tf tensorflow

本文主要是介绍【TensorFlow】tf.where,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

官方API

TensorFlow中文社区tf.where

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]]

官方API的解释是返回输入矩阵中true的位置,但看到第2个例子就有点看不懂了。

返回tensor的第一个维度为input中true的数量,这个比较好理解。第二个维度为矩阵的阶,这里我在看的时候出现了理解误差,我以为是矩阵数学里面的阶,后来才发现Tensorflow中的阶是指矩阵的维数,所以第二个例子的返回值是3列(input的维数为3)。

再看返回tensor的值:

对于第一个例子来说,返回的刚好就是true所在的位置(二维坐标),

对于第二个例子就不怎么好理解了。经过各种实验,发现是这样解读的,对于返回值的第一个[0,0,0]表示在第一个维度(编号从0开始,input第一个维度为[[true,false],[true,false]],注意,这是个二维矩阵),true的坐标为[0,0],第三条数据表示在第二个维度且坐标为[0,1]的位置为true。

折腾了这么久,终于搞明白这个函数了!文档写的有点模糊。

另一用法

官方文档中只有tf.where(input, name=None)一种用法,在实际应用中发现了另外一种使用方法tf.where(input, a,b),其中a,b均为尺寸一致的tensor,作用是将a中对应input中true的位置的元素值不变,其余元素进行替换,替换成b中对应位置的元素值,下面使用代码来说明:

输出: 

print(sess.run(tf.where(tf.equal(a,1),a1,1-a1)))

输出:

print(sess.run(tf.where(tf.equal(a,0),a1,1-a1)))

输出: 

对比两行代码的不同可以发现该函数的作用。

不同之处为tf.equal(a,0)和tf.equal(a,1) 
tf.equal()返回tensor中满足条件的位置

这篇关于【TensorFlow】tf.where的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1112440

相关文章

win10不用anaconda安装tensorflow-cpu并导入pycharm

记录一下防止忘了 一、前提:已经安装了python3.6.4,想用tensorflow的包 二、在pycharm中File-Settings-Project Interpreter点“+”号导入很慢,所以直接在cmd中使用 pip install -i https://mirrors.aliyun.com/pypi/simple tensorflow-cpu下载好,默认下载的tensorflow

稀疏自编码器tensorflow

自编码器是一种无监督机器学习算法,通过计算自编码的输出与原输入的误差,不断调节自编码器的参数,最终训练出模型。自编码器可以用于压缩输入信息,提取有用的输入特征。如,[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]四比特信息可以压缩成两位,[0,0],[1,0],[1,1],[0,1]。此时,自编码器的中间层的神经元个数为2。但是,有时中间隐藏层的神经元

Tensorflow实现与门感知机

感知机是最简单的神经网络,通过输入,进行加权处理,经过刺激函数,得到输出。通过输出计算误差,调整权重,最终,得到合适的加权函数。 今天,我通过tensorflow实现简单的感知机。 首先,初始化变量:     num_nodes = 2     output_units = 1     w = tf.Variable(tf.truncated_normal([num_nodes,output

Tensorflow lstm实现的小说撰写预测

最近,在研究深度学习方面的知识,结合Tensorflow,完成了基于lstm的小说预测程序demo。 lstm是改进的RNN,具有长期记忆功能,相对于RNN,增加了多个门来控制输入与输出。原理方面的知识网上很多,在此,我只是将我短暂学习的tensorflow写一个预测小说的demo,如果有错误,还望大家指出。 1、将小说进行分词,去除空格,建立词汇表与id的字典,生成初始输入模型的x与y d

Deepin Linux安装TensorFlow

Deepin Linux安装TensorFlow 1.首先检查是否有Python,一般deepin系统都自带python的。   2.安装pip Sudo appt-get install pip来安装pip,如果失败就先更新一下sudo apt-get updata,然后再sudo apt-get install pip,如果定位失败,就sudo apt-get install pyth

终止distributed tensorflow的ps进程

1.直接终止: $ ps -ef | grep python | grep 文件名 | awk {'print $2'} | xargs kill文件名为当前运行的程序,名称如:distribute.py 2.查找pid,后kill: $ ps -ef | grep python | grep 文件名 | awk {'print $2'}$ kill -9 <pid>

Python(TensorFlow和PyTorch)两种显微镜成像重建算法模型(显微镜学)

🎯要点 🎯受激发射损耗显微镜算法模型:🖊恢复嘈杂二维和三维图像 | 🖊模型架构:恢复上下文信息和超分辨率图像 | 🖊使用嘈杂和高信噪比的图像训练模型 | 🖊准备半合成训练集 | 🖊优化沙邦尼尔损失和边缘损失 | 🖊使用峰值信噪比、归一化均方误差和多尺度结构相似性指数量化结果 | 🎯训练荧光显微镜模型和对抗网络图形转换模型 🍪语言内容分比 🍇Python图像归一化

【tensorflow CNN】构建cnn网络,识别mnist手写数字识别

#coding:utf8"""构建cnn网络,识别mnistinput conv1 padding max_pool([2,2],strides=[2,2]) conv2 x[-1,28,28,1] 卷积 [5,5,1,32] -> [-1,24,24,32]->[-1,28,

【tensorflow 全连接神经网络】 minist 手写数字识别

主要内容: 使用tensorflow构建一个三层全连接传统神经网络,作为字符识别的多分类器。通过字符图片预测对应的数字,对mnist数据集进行预测。 # coding: utf-8from tensorflow.examples.tutorials.mnist import input_dataimport tensorflow as tfimport matplotlib.pyplot

【tensorflow 使用错误】tensorflow2.0 过程中出现 Error : Failed to get convolution algorithm

如果在使用 tensorflow 过程中出现 Error : Failed to get convolution algorithm ,这是因为显卡内存被耗尽了。 解决办法: 在代码的开头加入如下两句,动态分配显存 physical_device = tf.config.experimental.list_physical_devices("GPU")tf.config.experiment