亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Pytorch中的torch.where函數(shù)使用

 更新時間:2024年02月26日 09:47:38   作者:煙雨風(fēng)渡  
這篇文章主要介紹了Pytorch中的torch.where函數(shù)使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

使用torch.where函數(shù)

首先我們看一下Pytorch中torch.where函數(shù)是怎樣定義的:

@overload
def where(condition: Tensor) -> Union[Tuple[Tensor, ...], List[Tensor]]: ...

torch.where函數(shù)的功能如下:

torch.where(condition, x, y)

  • condition:判斷條件
  • x:若滿足條件,則取x中元素
  • y:若不滿足條件,則取y中元素

以具體實例看一下torch.where函數(shù)的效果:

import torch
 
# 條件
condition = torch.rand(3, 2)
print(condition)
# 滿足條件則取x中對應(yīng)元素
x = torch.ones(3, 2)
print(x)
# 不滿足條件則取y中對應(yīng)元素
y = torch.zeros(3, 2)
print(y)
# 條件判斷后的結(jié)果
result = torch.where(condition > 0.5, x, y)
print(result)

結(jié)果如下:

tensor([[0.3224, 0.5789],
        [0.8341, 0.1673],
        [0.1668, 0.4933]])
tensor([[1., 1.],
        [1., 1.],
        [1., 1.]])
tensor([[0., 0.],
        [0., 0.],
        [0., 0.]])
tensor([[0., 1.],
        [1., 0.],
        [0., 0.]])

可以看到torch.where函數(shù)會對condition中的元素逐一進行判斷,根據(jù)判斷的結(jié)果選取x或y中的值,所以要求x和y應(yīng)該與condition形狀相同。

torch.where(),np.where()兩種用法,及np.argwhere()尋找張量(tensor)和數(shù)組中為0的索引

1.torch.where()

torch.where()有兩種用法,

  • 當輸入?yún)?shù)為三個時,即torch.where(condition, x, y),返回滿足 x if condition else y的tensor,注意x,y必須為tensor
  • 當輸入?yún)?shù)為一個時,即torch.where(condition),返回滿足condition的tensor索引的元組(tuple)

代碼示例

torch.where(condition, x, y)

代碼

import torch
import numpy as np
 
# 初始化兩個tensor
x = torch.tensor([
    [1,2,3,0,6],
    [4,6,2,1,0],
    [4,3,0,1,1]
])
y = torch.tensor([
    [0,5,1,4,2],
    [5,7,1,2,9],
    [1,3,5,6,6]
])
 
# 尋找滿足x中大于3的元素,否則得到y(tǒng)對應(yīng)位置的元素
arr0 = torch.where(x>=3, x, y) #輸入?yún)?shù)為3個
 
print(x, '\n', y)
print(arr0, '\n', type(arr0))

結(jié)果

>>> x
tensor([[1, 2, 3, 0, 6],
        [4, 6, 2, 1, 0],
        [4, 3, 0, 1, 1]])
>>> y
tensor([[0, 5, 1, 4, 2],
        [5, 7, 1, 2, 9],
        [1, 3, 5, 6, 6]])
 
>>> arr0
tensor([[0, 5, 3, 4, 6],
        [4, 6, 1, 2, 9],
        [4, 3, 5, 6, 6]])
 
>>> type(arr0)
<class 'torch.Tensor'>

arr0的類型為<class 'torch.Tensor'>

torch.where(condition)

以尋找tensor中為0的索引為例

代碼

import torch
import numpy as np
x = torch.tensor([
    [1,2,3,0,6],
    [4,6,2,1,0],
    [4,3,0,1,1]
])
y = torch.tensor([
    [0,5,1,4,2],
    [5,7,1,2,9],
    [1,3,5,6,6]
])
 
# 返回x中0元素的索引
index0 = torch.where(x==0) # 輸入?yún)?shù)為1個
 
print(index0,'\n', type(index0))

結(jié)果

>>> index0
(tensor([0, 1, 2]), tensor([3, 4, 2])) 
 
>>> type(index0)
<class 'tuple'>

其中[0, 1, 2]是0元素坐標的行索引,[3, 4, 2]是0元素坐標的列索引,注意,最終得到的是tuple類型的返回值,元組中包含了tensor

2.np.where()

np.where()用法與torch.where()用法類似,也包括兩種用法,但是不同的是輸入值類型和返回值的類型

代碼示例

np.where(condition, x, y)和np.where(condition),輸入x,y可以為非tensor

代碼

import torch
import numpy as np
x = torch.tensor([
    [1,2,3,0,6],
    [4,6,2,1,0],
    [4,3,0,1,1]
])
y = torch.tensor([
    [0,5,1,4,2],
    [5,7,1,2,9],
    [1,3,5,6,6]
])
 
arr1 = np.where(x>=3, x, y) # 輸入?yún)?shù)為3個
 
index0 = torch.where(x==0) # 輸入?yún)?shù)為1個
 
print(arr1,'\n',type(arr1))
print(index1,'\n', type(index1))
 

結(jié)果

>>> arr1
[[0 5 3 4 6]
 [4 6 1 2 9]
 [4 3 5 6 6]]
 
>>> type(arr1)
<class 'numpy.ndarray'>
 
>>> index1
(array([0, 1, 2]), array([3, 4, 2])) 
 
>>> type(index1)
<class 'tuple'>

注意,np.where()和torch.where()的返回值類型不同

3.np.argwhere(condition)

尋找符合contion的元素索引

代碼示例

代碼

import torch
import numpy as np
x = torch.tensor([
    [1,2,3,0,6],
    [4,6,2,1,0],
    [4,3,0,1,1]
])
y = torch.tensor([
    [0,5,1,4,2],
    [5,7,1,2,9],
    [1,3,5,6,6]
])
 
 
index2 = np.argwhere(x==0) # 尋找元素為0的索引
 
print(index2,'\n', type(index2))

結(jié)果

>>> index2
tensor([[0, 1, 2],
        [3, 4, 2]]) 
 
>>> type(index2)
<class 'torch.Tensor'>

注意返回值的類型

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 10個python爬蟲入門實例(小結(jié))

    10個python爬蟲入門實例(小結(jié))

    這篇文章主要介紹了10個python爬蟲入門實例(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Python實現(xiàn)企業(yè)微信通知機器人的方法詳解

    Python實現(xiàn)企業(yè)微信通知機器人的方法詳解

    這篇文章主要為大家詳細介紹了如何使用Python實現(xiàn)對企業(yè)微信進行群通知的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2023-02-02
  • 利用 Monkey 命令操作屏幕快速滑動

    利用 Monkey 命令操作屏幕快速滑動

    Monkey測試是Android平臺自動化測試的一種手段,通過Monkey程序模擬用戶觸摸屏幕、滑動Trackball、按鍵等操作來對設(shè)備上的程序進行壓力測試,檢測程序多久的時間會發(fā)生異常
    2016-12-12
  • python django入門

    python django入門

    這篇文章主要為大家介紹了python django的入門,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-11-11
  • pytorch實現(xiàn)好萊塢明星識別的示例代碼

    pytorch實現(xiàn)好萊塢明星識別的示例代碼

    本文主要介紹了pytorch實現(xiàn)好萊塢明星識別,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Python類反射機制使用實例解析

    Python類反射機制使用實例解析

    這篇文章主要介紹了Python類反射機制使用實例解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • Python中l(wèi)oguru日志庫的使用

    Python中l(wèi)oguru日志庫的使用

    本文主要介紹了Python中l(wèi)oguru日志庫的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Python中__init__的用法和理解示例詳解

    Python中__init__的用法和理解示例詳解

    在Python中定義類經(jīng)常會用到__init__函數(shù)(方法),首先需要理解的是,兩個下劃線開頭的函數(shù)是聲明該屬性為私有,不能在類的外部被使用或訪問,從文字理解比較困難,下面通過示例代碼幫助大家理解__init__在python中用法,感興趣的朋友一起看看吧
    2023-02-02
  • 從np.random.normal()到正態(tài)分布的擬合操作

    從np.random.normal()到正態(tài)分布的擬合操作

    這篇文章主要介紹了從np.random.normal()到正態(tài)分布的擬合操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Python如何保留float類型小數(shù)點后3位

    Python如何保留float類型小數(shù)點后3位

    這篇文章主要介紹了Python如何保留float類型小數(shù)點后3位,具有很好的參考價值,希望對的大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05

最新評論