国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(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的評論 (共 條)

分享到微博請遵守國家法律
临泽县| 内江市| 西充县| 水城县| 二连浩特市| 鄢陵县| 湛江市| 会理县| 临夏市| 张家口市| 左贡县| 苍南县| 富川| 比如县| 克山县| 塘沽区| 宝清县| 台南市| 天气| 大庆市| 凤庆县| 图片| 塘沽区| 临漳县| 彭山县| 西乌| 曲松县| 乌拉特前旗| 铁岭县| 兴国县| 重庆市| 华亭县| 荣成市| 育儿| 富锦市| 高要市| 平陆县| 江津市| 泗水县| 绥德县| 成都市|