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

pytorch使用-tensor的基本操作解讀

 更新時(shí)間:2022年12月13日 10:53:46   作者:大蝦飛哥哥  
這篇文章主要介紹了pytorch使用-tensor的基本操作解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

一、tensor加減乘除

加法操作

import torch

x = torch.randn(2, 3)
y = torch.randn(2, 3)

z = x + y
print(z)

z = torch.add(x, y)
print(z)

y.add_(x)
print(y)

其他操作類似:減法:sub(-), 乘法:mul(*), 除法:div(/)

二、tensor矩陣運(yùn)算

# 二維矩陣相乘
a = torch.full([2, 2], 3, dtype=torch.long)
b = torch.ones(2, 2, dtype=torch.long)

print(a)
print(b)
print(torch.mm(a, b))

#  matmul 和 @ 可以用于二維矩陣計(jì)算,也可以是多維
# 四維,計(jì)算的時(shí)候就是前兩維不變,后兩維進(jìn)行計(jì)算。
a = torch.rand(1, 1, 3, 2)
b = torch.rand(1, 1, 2, 4)

c = torch.matmul(a, b)
print(a)
print(b)
print(c)

pow

a = torch.full([2, 2], 6)
print(a.pow(3))

a = torch.full([2, 2], 6)
print(a**2)
  • sqrt: 平方根
  • rsqrt: 平方根倒數(shù)
a = torch.full([2, 2], 1024)

print(a.sqrt())
print(a.rsqrt())
print(a**0.5)

exp log

a = torch.ones(2, 2)

print(torch.exp(a))
print(torch.log(a))
print(torch.log2(a))
  • .floor()——往下近似
  • .ceil()——往上近似
  • .trunc()——裁剪為整數(shù)部分
  • .frac()——裁剪成小數(shù)部分
a = torch.tensor(3.1415926)

print(a.floor())
print(a.ceil())
print(a.trunc())
print(a.frac())

torch.round()——四舍五入

a = torch.tensor(3.1415926)

print(a.round())

.item() 轉(zhuǎn)化為python number

x = torch.randn(1)

print(x)
print(x.item())

四、tensor切片操作

a = torch.randn(4, 3)
print(a)

# 取第二列
print(a[:, 1])

# 取前兩列
print(a[:, :2])

五、tensor改變形狀

x = torch.randn(4, 4)
y = x.view(16)

# -1, 自動(dòng)匹配個(gè)數(shù)
z = x.view(-1, 8)

print(x)
print(y)
print(z)

六、tensor 和 numpy.array相互轉(zhuǎn)換

# 底層內(nèi)存共享
x = torch.ones(5)
print(x)

y = x.numpy()
print(y)

x.add_(1)
print(y)

import numpy as np
x = np.ones(5)
y = torch.from_numpy(x)
print(y)

七、tensor 轉(zhuǎn)到GPU上

if torch.cuda.is_available():
    
    device = torch.device("cuda")
    x = torch.randn(2, 3)
    print(x)

    y = x.to(device)
    print(y)

    z = torch.randn(2, 3, device="cuda")
    print(z)
    
    # 同時(shí)在GPU上才能相加
    print(y + z)

    # 轉(zhuǎn)換會(huì)cpu
    print(z.to("cpu"))

總結(jié)

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

相關(guān)文章

  • python文件編寫好后如何實(shí)踐

    python文件編寫好后如何實(shí)踐

    在本篇文章里小編給大家分享了關(guān)于python文件編寫好后如何實(shí)踐的相關(guān)內(nèi)容,需要的朋友們可以參考下。
    2020-07-07
  • Python Numpy 實(shí)現(xiàn)交換兩行和兩列的方法

    Python Numpy 實(shí)現(xiàn)交換兩行和兩列的方法

    今天小編就為大家分享一篇Python Numpy 實(shí)現(xiàn)交換兩行和兩列的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • python協(xié)程用法實(shí)例分析

    python協(xié)程用法實(shí)例分析

    這篇文章主要介紹了python協(xié)程用法,實(shí)例分析Python中協(xié)議的概念、功能及使用方法,需要的朋友可以參考下
    2015-06-06
  • Python如何按單元格讀取復(fù)雜電子表格(Excel)的數(shù)據(jù)

    Python如何按單元格讀取復(fù)雜電子表格(Excel)的數(shù)據(jù)

    這篇文章主要介紹了Python如何按單元格讀取復(fù)雜電子表格(Excel)的數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Python中random.shuffle()函數(shù)用法代碼案例

    Python中random.shuffle()函數(shù)用法代碼案例

    random.shuffle方法,對(duì)元素進(jìn)行重新排序,打亂原有的順序,返回一個(gè)隨機(jī)序列,該方法的作用類似洗牌,本文重點(diǎn)給大家介紹Python中random.shuffle()函數(shù)用法代碼案例,感興趣的朋友跟隨小編一起看看吧
    2022-11-11
  • 在python中解決死鎖的問題

    在python中解決死鎖的問題

    這篇文章主要介紹了在python中解決死鎖的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • 對(duì)Python 字典元素進(jìn)行刪除的方法

    對(duì)Python 字典元素進(jìn)行刪除的方法

    這篇文章主要介紹了對(duì)Python 字典元素進(jìn)行刪除的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 詳解Python在七牛云平臺(tái)的應(yīng)用(一)

    詳解Python在七牛云平臺(tái)的應(yīng)用(一)

    這篇文章主要介紹了詳解Python在七牛云平臺(tái)的應(yīng)用(一),涉及Python通過官方庫對(duì)空間的操作,上傳的步驟,操作方法等相關(guān)內(nèi)容,以及完整的操作代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • python中random隨機(jī)函數(shù)詳解

    python中random隨機(jī)函數(shù)詳解

    大家好,本篇文章主要講的是python中random隨機(jī)函數(shù)詳解,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-02-02
  • python?pandas?數(shù)據(jù)排序的幾種常用方法

    python?pandas?數(shù)據(jù)排序的幾種常用方法

    這篇文章主要介紹了python?pandas數(shù)據(jù)排序的幾種常用方法,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09

最新評(píng)論