五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動態(tài)

PyTorch一小時掌握之基本操作篇

發(fā)布日期:2022-01-18 09:36 | 文章來源:源碼中國

創(chuàng)建數(shù)據(jù)

torch.empty()

創(chuàng)建一個空張量矩陣.

格式:

torch.empty(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False, memory_format=torch.contiguous_format) → Tensor

參數(shù):

  • size: 生成矩陣的形狀, 必選
  • dtype: 數(shù)據(jù)類型, 默認(rèn)為 None

例子:

# 創(chuàng)建一個形狀為[2, 2]的矩陣
a = torch.empty(2, 2)
print(a)
# 創(chuàng)建一個形狀為[3, 3]的矩陣
b = torch.empty(3, 3)
print(b)

輸出結(jié)果:

tensor([[0., 0.],
[0., 0.]])
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])

torch.zeros()

創(chuàng)建一個全零矩陣.

格式:

torch.zeros(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

參數(shù):

  • size: 生成矩陣的形狀, 必選
  • dtype: 數(shù)據(jù)類型, 默認(rèn)為 None

例子:

# 創(chuàng)建一個形狀為[2, 2]的全零數(shù)組
a = torch.zeros([2, 2], dtype=torch.float32)
print(a)
# 創(chuàng)建一個形狀為[3, 3]的全零數(shù)組
b = torch.zeros([3, 3], dtype=torch.float32)
print(b)

輸出結(jié)果:

tensor([[0., 0.],
[0., 0.]])
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])

torch.ones()

創(chuàng)建一個全一矩陣.

格式:

torch.ones(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

參數(shù):

  • size: 生成矩陣的形狀, 必選
  • dtype: 數(shù)據(jù)類型, 默認(rèn)為 None

例子:

# 創(chuàng)建一個形狀為[2, 2]的全一數(shù)組
a = torch.ones([2, 2], dtype=torch.float32)
print(a)
# 創(chuàng)建一個形狀為[3, 3]的全一數(shù)組
b = torch.ones([3, 3], dtype=torch.float32)
print(b)

輸出結(jié)果:

tensor([[1., 1.],
[1., 1.]])
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])

torch.tensor()

通過數(shù)據(jù)創(chuàng)建張量.

格式:

torch.tensor(data, *, dtype=None, device=None, requires_grad=False, pin_memory=False) → Tensor

參數(shù):

  • data: 數(shù)據(jù) (數(shù)組, 元組, ndarray, scalar)
  • dtype: 數(shù)據(jù)類型, 默認(rèn)為 None

例子:

# 通過數(shù)據(jù)創(chuàng)建張量
array = np.arange(1, 10).reshape(3, 3)
print(array)
print(type(array))
tensor = torch.tensor(array)
print(tensor)
print(type(tensor))

輸出結(jié)果:

[[1 2 3]
[4 5 6]
[7 8 9]]
<class 'numpy.ndarray'>
tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], dtype=torch.int32)
<class 'torch.Tensor'>

torch.rand()

創(chuàng)建一個 0~1 隨機數(shù)的張量矩陣.

格式:

torch.rand(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

參數(shù):

  • size: 生成矩陣的形狀, 必選
  • dtype: 數(shù)據(jù)類型, 默認(rèn)為 None

例子:

# 創(chuàng)建形狀為[2, 2]的隨機數(shù)矩陣
rand = torch.rand(2, 2)
print(rand)

輸出結(jié)果:

tensor([[0.6209, 0.3424],
[0.3506, 0.7986]])

數(shù)學(xué)運算

torch.add()

返回相加的張量.

格式:

torch.add(input, other, *, out=None) → Tensor

例子:

# 張量相加
input1 = torch.tensor([[1, 2], [3, 4]])
print(input1)
input2 = torch.tensor([[4, 3], [2, 1]])
print(input2)
output = torch.add(input1, input2)
print(output)

輸出結(jié)果:

tensor([[1, 2],
[3, 4]])
tensor([[4, 3],
[2, 1]])
tensor([[5, 5],
[5, 5]])

注: 相加的張量形狀必須一致, 否則會報錯.

torch.sub()

返回相減的張量.

例子:

# 張量相減
input1 = torch.tensor([[1, 2], [3, 4]])
print(input1)
input2 = torch.tensor([[4, 3], [2, 1]])
print(input2)
output = torch.sub(input1, input2)
print(output)

輸出結(jié)果:

tensor([[1, 2],
[3, 4]])
tensor([[4, 3],
[2, 1]])
tensor([[-3, -1],
[ 1, 3]])

torch.matmul()

例子:

# 張量矩陣相乘
input1 = torch.tensor([[1, 1, 1]])
print(input1)
input2 = torch.tensor([[3], [3], [3]])
print(input2)
output = torch.matmul(input1, input2)
print(output)

輸出結(jié)果:

tensor([[1, 1, 1]])
tensor([[3],
[3],
[3]])
tensor([[9]])

索引操作

索引 (index) 可以幫助我們快速的找到張量中的特定信息.

例子:

# 簡單的索引操作
ones = torch.ones([3, 3])
print(ones[: 2])
print(ones[:, : 2])

調(diào)試輸出:

tensor([[1., 1., 1.],
[1., 1., 1.]])
tensor([[1., 1.],
[1., 1.],
[1., 1.]])

到此這篇關(guān)于PyTorch一小時掌握之基本操作篇的文章就介紹到這了,更多相關(guān)PyTorch基本操作內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部