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中對應元素 x = torch.ones(3, 2) print(x) # 不滿足條件則取y中對應元素 y = torch.zeros(3, 2) print(y) # 條件判斷后的結果 result = torch.where(condition > 0.5, x, y) print(result)
結果如下:
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ù)判斷的結果選取x或y中的值,所以要求x和y應該與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)對應位置的元素
arr0 = torch.where(x>=3, x, y) #輸入?yún)?shù)為3個
print(x, '\n', y)
print(arr0, '\n', type(arr0))結果
>>> 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))結果
>>> 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))
結果
>>> 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))結果
>>> index2
tensor([[0, 1, 2],
[3, 4, 2]])
>>> type(index2)
<class 'torch.Tensor'>
注意返回值的類型
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- PyTorch中torch.load()的用法和應用
- python中torch.load中的map_location參數(shù)使用
- Pytorch中的torch.nn.Linear()方法用法解讀
- python中的List sort()與torch.sort()
- 關于torch.scatter與torch_scatter庫的使用整理
- PyTorch函數(shù)torch.cat與torch.stac的區(qū)別小結
- pytorch.range()和pytorch.arange()的區(qū)別及說明
- 使用with torch.no_grad():顯著減少測試時顯存占用
- PyTorch中torch.save()的用法和應用小結
相關文章
對sklearn的使用之數(shù)據(jù)集的拆分與訓練詳解(python3.6)
今天小編就為大家分享一篇對sklearn的使用之數(shù)據(jù)集的拆分與訓練詳解(python3.6),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
python requests 庫請求帶有文件參數(shù)的接口實例
今天小編就為大家分享一篇python requests 庫請求帶有文件參數(shù)的接口實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
python中l(wèi)xml.etree 和 ElementTree 的區(qū)別解析
lxml.etree 提供了更多的功能,例如 XPath、XSLT、Relax NG、 和 XML 模式支持,etree 對 Python unicode 字符串的想法與 ElementTree 不同,本文給大家介紹python中l(wèi)xml.etree 和 ElementTree 的區(qū)別,感興趣的朋友一起看看吧2024-01-01
使用Python快樂學數(shù)學Github萬星神器Manim簡介
這篇文章主要介紹了使用Python快樂學數(shù)學Github萬星神器Manim簡介,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08
python?tkinter自定義實現(xiàn)Expander控件
和其他成熟的GUI庫相比,tkinter的組件并不是太多,但在自定義組件這一點上,并不遜色于其他框架,下面小編就教大家如何自定義一個Expander控件吧2023-08-08
python的pygal模塊繪制反正切函數(shù)圖像方法
在本篇文章中我們給大家整理了關于如何用python的pygal模塊繪制反正切函數(shù)圖像的知識點內容,有需要的朋友們可以學習下。2019-07-07

