欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python查看Tensor尺寸及查看數(shù)據(jù)類型的實現(xiàn)

 更新時間:2022年07月15日 08:39:13   作者:ShuqiaoS  
這篇文章主要介紹了Python查看Tensor尺寸及查看數(shù)據(jù)類型的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

查看Tensor尺寸及查看數(shù)據(jù)類型

Tensor尺寸查看

命令:

  • x.shape

例子:

input = torch.randn(20,16,50,32)
input.shape

輸出為:

在這里插入圖片描述

注意調用的時候不要用x.shape()

否則會報錯:AttributeError: 'Tensor' object has no attribute 'get_shape'

數(shù)據(jù)類型查看

命令:

  • x.type()

例子:

input = torch.randn(20,16,50,32)
input.type()

輸出:

在這里插入圖片描述

Pytorch基本數(shù)據(jù)類型tensor

在Pytorch中必須使用Pytorch特有的張量(tensor)數(shù)據(jù)類型,本文介紹tensor的基本操作

Python和Pytorch數(shù)據(jù)類型對應

在這里插入圖片描述

以上數(shù)據(jù)是存儲在CPU中。

如果使用.cuda(),會返回一個GPU上的引用

import torch
a = torch.tensor([1,2])
c = a.cuda
print(c)
print(type(c))
-----輸出------
D:\Users\Lenovo\anaconda3\python.exe C:/Users/Lenovo/Desktop/pythonProject2/main.py
<built-in method cuda of Tensor object at 0x000002A03434CA00>
<class 'builtin_function_or_method'>
Process finished with exit code 0

創(chuàng)建tensor的方法

import torch
import numpy
a = numpy.array([1,2,3,4])
b = torch.from_numpy(a)  #從numpy中引入
print(b)
c = torch.tensor([1,2,3,4])
print(c)  #直接創(chuàng)建
d = torch.Tensor([4,5,6,7])
print(d)  #直接創(chuàng)建
e = torch.Tensor(2,3)
print(e)  #用shape創(chuàng)建隨機的指定維度的tensor

一些常用的生成tensor方法

import torch
a = torch.rand(3, 3) #生成指定大小的,元素范圍[0,1]的tensor
print(a)
b = torch.rand_like(a) #生成與對象tensor大小一致的tensor
print(b)
c = torch.randint(1,10,(3,3)) #生成(3,3)大小,[1,10)范圍的tensor(包括1,但不包括10)
print(c)
d = torch.randn(2,4) #生成均值為0,方差為1的隨機tensor
print(d)
e = torch.full([2,5],0) #生成全部一樣的tensor
print(e)
f = torch.arange(0,10)  #生成順序tensor
print(f)
g = torch.linspace(0,9,steps=8)  #生成[0,9],等分成8個的tensor
print(g)
print(torch.ones(3,1),
      torch.zeros(4,5),
      torch.eye(6)) #生成全1,全0,單位矩陣tensor

tensor的切片與索引

import torch
a = torch.rand(3, 7) #生成指定大小的,元素范圍[0,1]的tensor
print(a)
print("*"*100)
print(a[0])  #取一行
print("*"*100)
print(a[:2,4:])  #指定一塊子區(qū)域
print("*"*100)
print(a[:,2])  #取一列
print("*"*100)
print(a[:,0:7:2]) #[0,7]行隔2行取樣
print("*"*100)
print(a.index_select(1,torch.tensor([2,6])))  #指定切片位置
print("*"*100)
mask = a.ge(0.8)
print(torch.masked_select(a,mask)) #通過掩碼條件切片(注意切片后會被flatten)
print(mask) #看看mask矩陣

tensor的維度變換(重點)

基本操作:

  • view/reshape
  • squeeze/unsqueeze
import torch
a = torch.rand(3,4,2) #生成指定大小的,元素范圍[0,1]的tensor
print(a)
# view方法:變換tensor的形狀
print(a.view(3, 8))  #壓縮維度
print(a.view(3,2,2,2))  #擴展維度
# unsqueeze方法:在指定地方插入一維(squeeze方法同理會消除一維)
print(a.unsqueeze(1))
print(a.squeeze(1))  #squeeze只有在原有維度為1才有用,試試把上面改成rand(3,1,2)看看效果

tensor的疊加和分割

```python
import torch
#cat操作
a = torch.rand(4,1,3)
b = torch.rand(3,1,3)
# print(a)
# print(b)
c = torch.cat((a,b))  #合并tensor(只能在其他dimension一致的情況下才能合并)
# print(c)
# print(c.shape)
#stack操作
d = torch.rand(4,1)
e = torch.rand(4,1)
# print(d)
# print(e)
f = torch.stack((d,e))  #合并tensor,與cat不同的是,stack會增加一個更高的維度
# print(f)
# print(f.shape)
#split操作
g = torch.rand(5,2,1)
h,i = g.split([1,4])
# print(g)
# print(h)
# print(i)

tensor的數(shù)學運算

import torch
#基本加減乘除
a = torch.zeros(4,3)
b = torch.ones(3)
# print(a+b)  #這樣會報錯,正確做法如下
# print(torch.add(a, b))  #結果全是1,broadcast運算邏輯
c = torch.tensor([[1,2,3],[4,5,6],[7,8,9]])
d = torch.eye(3)
# print(torch.add(c,d))
# print(c+d)  #不采用broadcast邏輯時,這樣也可以
# print(c*d)
# print(c/d)
# print(c**2) #平方
# print(c**0.5) #開平方
#矩陣相乘
# print(torch.matmul(torch.ones(3,3),torch.ones(3,3)))
# print(torch.ones(3,3)@torch.ones(3,3))  #用@的效果是一樣的
#高維度的矩陣相乘
e = torch.rand(4,3,2,3)
f = torch.rand(4,3,3,5)
g = e@f
# print(g)
# print(g.shape)  #可以看到,本質上也是二維矩陣相乘的規(guī)律
#clamp算法
print(c.clamp(4))  #把小于4的值全部都替換成4

tensor的統(tǒng)計相關操作

import torch
#norm方法(求范數(shù))
a = torch.arange(10,dtype=float)
b = a.view(2,5)
print(a.norm(1))
print(b.norm(1))
print(a.norm(2,dtype=float))  #同理求二范數(shù)
print(b)
print(b.norm(1,dim=1,dtype=float))  #求指定維度的范數(shù)
#求最大、最小、平均、求和
print(a.sum())
print(a.min())
print(a.max())
print(a.mean())
#top N的值
c = torch.tensor([1,2,3,3,4,4,4,5,5,5,5,6,8,8],dtype=float)
print(c.topk(3))
print(c.topk(3, largest=False))  #找到前N最小的值
print(c.kthvalue(4))  #找到第k小的值

#比較
print(a>4)
print(a!=8)
#where
cond = torch.tensor([[1,2],[3,4]],dtype=float)  #用where組合2個tensor
d = torch.zeros(2,2)
e = torch.ones(2,2)
print(torch.where(cond>2,d,e))

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

相關文章

  • Python求導數(shù)的方法

    Python求導數(shù)的方法

    這篇文章主要介紹了Python求導數(shù)的方法,涉及Python數(shù)學運算的相關技巧,需要的朋友可以參考下
    2015-05-05
  • OpenCV實現(xiàn)人臉識別

    OpenCV實現(xiàn)人臉識別

    本文主要介紹了python使用opencv實現(xiàn)人臉識別的相關資料。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-04-04
  • python 3利用Dlib 19.7實現(xiàn)攝像頭人臉檢測特征點標定

    python 3利用Dlib 19.7實現(xiàn)攝像頭人臉檢測特征點標定

    這篇文章主要為大家詳細介紹了python 3利用Dlib 19.7實現(xiàn)攝像頭人臉檢測特征點標定,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • python發(fā)送byte數(shù)據(jù)組到tcp的server問題

    python發(fā)送byte數(shù)據(jù)組到tcp的server問題

    這篇文章主要介紹了python發(fā)送byte數(shù)據(jù)組到tcp的server問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • python輸出決策樹圖形的例子

    python輸出決策樹圖形的例子

    今天小編就為大家分享一篇python輸出決策樹圖形的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • python腳本之一鍵移動自定格式文件方法實例

    python腳本之一鍵移動自定格式文件方法實例

    這篇文章主要給大家介紹了關于python腳本之一鍵移動自定格式文件的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用python具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-09-09
  • python內置HTTP Server如何實現(xiàn)及原理解析

    python內置HTTP Server如何實現(xiàn)及原理解析

    這篇文章主要為大家介紹了python內置HTTP Server如何實現(xiàn)及原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • Python 快速實現(xiàn)CLI 應用程序的腳手架

    Python 快速實現(xiàn)CLI 應用程序的腳手架

    本篇文章主要介紹了Python 快速實現(xiàn)CLI 應用程序的腳手架,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • python支付寶支付示例詳解

    python支付寶支付示例詳解

    這篇文章主要為大家詳細介紹了python支付寶支付示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • Python實現(xiàn)解析Bit Torrent種子文件內容的方法

    Python實現(xiàn)解析Bit Torrent種子文件內容的方法

    這篇文章主要介紹了Python實現(xiàn)解析Bit Torrent種子文件內容的方法,結合實例形式分析了Python針對Torrent文件的讀取與解析相關操作技巧與注意事項,需要的朋友可以參考下
    2017-08-08

最新評論