zoukankan      html  css  js  c++  java
  • Tensorflow 学习笔记 -----tf.where

    TensorFlow函数:tf.where

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

    官方解释:

     1 tf.where(input, name=None)`
     2 Returns locations of true values in a boolean tensor.
     3 
     4 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.
     5 
     6 For example:
     7 # 'input' tensor is [[True, False]
     8 #                    [True, False]]
     9 # 'input' has two true values, so output has two coordinates.
    10 # 'input' has rank of 2, so coordinates have two indices.
    11 where(input) ==> [[0, 0],
    12                   [1, 0]]
    13 
    14 # `input` tensor is [[[True, False]
    15 #                     [True, False]]
    16 #                    [[False, True]
    17 #                     [False, True]]
    18 #                    [[False, False]
    19 #                     [False, True]]]
    20 # 'input' has 5 true values, so output has 5 coordinates.
    21 # 'input' has rank of 3, so coordinates have three indices.
    22 where(input) ==> [[0, 0, 0],
    23                   [0, 1, 0],
    24                   [1, 0, 1],
    25                   [1, 1, 1],
    26                   [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]]

  • 相关阅读:
    同一根域名的多站点登录共享
    mysql忘记管理员密码
    使用Cacti监控你的网络(四) Cacti脚本及模板
    使用Cacti时常见的问题集
    SQL Server:SQL Like 通配符特殊用法:Escape
    IS6.0 应用程序池Web园导致Session丢失
    VMware建立虚拟机无法上网
    SqlServer 添加用户 添加角色 分配权限
    教你如何在SQL Server数据库中加密数据
    sendmail邮件服务器搭载smtp和pop3认证的配置方法
  • 原文地址:https://www.cnblogs.com/lyc-seu/p/8565997.html
Copyright © 2011-2022 走看看