五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

PyTorch Tutorial 02 - Tensor Basics

2023-02-14 19:41 作者:Mr-南喬  | 我要投稿

教程Python代碼如下:


import torch

import numpy as np


"""張量"""

#empty,空張量,值未初始化

x = torch.empty(1)

print(x)


x = torch.empty(3)

print(x)


x = torch.empty(2,3)

print(x)


x = torch.empty(2,2,3)

print(x)


#rand隨機值張量

x = torch.rand(2,2)

print(x)


#zeros,0張量,這里x類型為float

x = torch.zeros(2,2)

print(x)


#ones,1張量

x = torch.ones(2,2)

print(x)

print(x.dtype)#這里x類型為float


#dtype,類型

x = torch.ones(2,2,dtype=torch.int)

print(x)

print(x.dtype)#這里x類型為int


x = torch.ones(2,2,dtype=torch.double)

print(x)

print(x.dtype)#這里x類型為double


#size(),大小

x = torch.ones(2,2,dtype=torch.double)

print(x.size())#torch.Size([2, 2])


#tensor

x = torch.tensor([2.5,0.1])

print(x)



"""加、減、乘運算"""

#加

x = torch.rand(2,2)

y = torch.rand(2,2)

print(x)

print(y)


z = x + y

print(z)


z = torch.add(x,y) #與z = x + y同

print(z)

print("z")


y.add_(x)

print(y)

print("y = y + x")


#減

x = torch.rand(2,2)

y = torch.rand(2,2)

print(x)

print(y)


z = x - y

z = torch.sub(x,y)

print(z)

print("z = x - y")

y.sub_(x)

print(y)

print("y = y - x")


#乘

x = torch.rand(2,2)

y = torch.rand(2,2)

print(x)

print(y)


z = x * y

z = torch.mul(x,y)

print(z)

print("z = x * y")

y.mul_(x)

print(y)

print("y = y * x")


#除

x = torch.rand(2,2)

y = torch.rand(2,2)

print(x)

print(y)


z = x / y

z = torch.div(x,y)

print(z)

print("z = x / y")


y.div_(x)

print(y)

print("y = y / x")


"""切片"""

x = torch.rand(5,3)

print(x)

print(x[0,:])#第一行

print(x[:,0])#第一列

print(x[0,0])#取某下標對應的元素

print(x[0,0].item())#item()的作用是從包含單個元素的張量中取出該元素的值,并保持元素類型不變,只有張量中只有一個元素時才能用item()方法


"""重塑"""

x = torch.rand(4,4)

print(x)


y = x.view(16)

print(y)


y = x.view(-1,8) #y = x.view(2,8),-1自動補齊

print(y)

print(y.size())


"""torch和numpy的轉(zhuǎn)換"""

#torch轉(zhuǎn)numpy

a = torch.ones(5)

print(a) #tensor([1., 1., 1., 1., 1.])

b = a.numpy()

print(b) #[1. 1. 1. 1. 1.]

print(type(b)) #<class 'numpy.ndarray'>


#a,b兩個張量如果在CPU而不是GPU那么ab共享同一個位置,一個值改變另一個也會變,因為ab指向相同的內(nèi)存位置

a.add_(1)

print(a) #tensor([2., 2., 2., 2., 2.])

print(b) #[2. 2. 2. 2. 2.]


#numpy轉(zhuǎn)torch

a = np.ones(5)

print(a)

b = torch.from_numpy(a)

print(b)


a += 1

print(a)

print(b)


"""程序遷移至GPU"""

print("\n" + "程序遷移至GPU")

if torch.cuda.is_available():

device = torch.device("cuda")

#創(chuàng)建一個張量,并將其放在GPU上

x = torch.ones(5,device=device)

#先創(chuàng)建一個張量,再將其遷移到GPU上

y = torch.ones(5)

y = y.to(device)


z = x + y #這將會在GPU上執(zhí)行,而且可能會快很多

print(z)

print("GPU z")

"""z.numpy()會報錯,因為numpy()只能處理CPU張量,而不能處理GPU張量,所以不能把GPU張量轉(zhuǎn)換回numpy,所以我們必須把其遷移回CPU"""

#將張量遷移回CPU

z = z.to("cpu")

print(z)

print("CPU z")

PyTorch Tutorial 02 - Tensor Basics的評論 (共 條)

分享到微博請遵守國家法律
陆丰市| 清丰县| 万州区| 丰都县| 乌拉特中旗| 瑞昌市| 邓州市| 城固县| 昌图县| 虞城县| 波密县| 体育| 庄河市| 平原县| 静宁县| 仙居县| 九台市| 青冈县| 曲阳县| 翁牛特旗| 宁津县| 台江县| 于田县| 辛集市| 忻州市| 兴城市| 砀山县| 安国市| 泊头市| 额济纳旗| 朔州市| 新宾| 博湖县| 吴旗县| 巴中市| 桑植县| 宜州市| 全州县| 沂南县| 万全县| 六枝特区|