最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PyTorch一小時(shí)掌握之基本操作篇

 更新時(shí)間:2021年09月08日 09:37:51   作者:我是小白呀  
這篇文章主要介紹了PyTorch一小時(shí)掌握之基本操作篇,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

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

在這里插入圖片描述

torch.empty()

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

格式:

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)建一個(gè)形狀為[2, 2]的矩陣
a = torch.empty(2, 2)
print(a)

# 創(chuàng)建一個(gè)形狀為[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)建一個(gè)全零矩陣.

格式:

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)建一個(gè)形狀為[2, 2]的全零數(shù)組
a = torch.zeros([2, 2], dtype=torch.float32)
print(a)

# 創(chuàng)建一個(gè)形狀為[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)建一個(gè)全一矩陣.

格式:

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)建一個(gè)形狀為[2, 2]的全一數(shù)組
a = torch.ones([2, 2], dtype=torch.float32)
print(a)

# 創(chuàng)建一個(gè)形狀為[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)建一個(gè) 0~1 隨機(jī)數(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]的隨機(jī)數(shù)矩陣
rand = torch.rand(2, 2)
print(rand)

輸出結(jié)果:

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

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

在這里插入圖片描述

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]])

注: 相加的張量形狀必須一致, 否則會(huì)報(bào)錯(cuò).

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一小時(shí)掌握之基本操作篇的文章就介紹到這了,更多相關(guān)PyTorch基本操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python實(shí)現(xiàn)批量轉(zhuǎn)換圖片為黑白

    python實(shí)現(xiàn)批量轉(zhuǎn)換圖片為黑白

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)批量轉(zhuǎn)換圖片為黑白,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • python 遞歸遍歷文件夾,并打印滿足條件的文件路徑實(shí)例

    python 遞歸遍歷文件夾,并打印滿足條件的文件路徑實(shí)例

    下面小編就為大家?guī)硪黄猵ython 遞歸遍歷文件夾,并打印滿足條件的文件路徑實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • python實(shí)現(xiàn)的文件同步服務(wù)器實(shí)例

    python實(shí)現(xiàn)的文件同步服務(wù)器實(shí)例

    這篇文章主要介紹了python實(shí)現(xiàn)的文件同步服務(wù)器,實(shí)例分析了文件同步服務(wù)器的原理及客戶端、服務(wù)端的實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2015-06-06
  • pycharm新建Vue項(xiàng)目的方法步驟(圖文)

    pycharm新建Vue項(xiàng)目的方法步驟(圖文)

    這篇文章主要介紹了pycharm新建Vue項(xiàng)目的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • ?分享Python?中的?7?種交叉驗(yàn)證方法

    ?分享Python?中的?7?種交叉驗(yàn)證方法

    這篇文章主要給大家分享的是Python?中的?7?種交叉驗(yàn)證方法,交叉驗(yàn)證是一種用于估計(jì)機(jī)器學(xué)習(xí)模型性能的統(tǒng)計(jì)方法,它是一種評(píng)估統(tǒng)計(jì)分析結(jié)果如何推廣到獨(dú)立數(shù)據(jù)集的方法,下文相關(guān)介紹,需要的朋友可以參考一下
    2022-03-03
  • Python?Pandas中布爾索引的用法詳解

    Python?Pandas中布爾索引的用法詳解

    布爾索引是一種使用?DataFrame?中數(shù)據(jù)的實(shí)際值的索引。本文將通過一些示例為大家詳細(xì)講講Python中布爾索引的用法,需要的可以參考一下
    2022-08-08
  • python構(gòu)建基礎(chǔ)的爬蟲教學(xué)

    python構(gòu)建基礎(chǔ)的爬蟲教學(xué)

    在本篇內(nèi)容里小編給大家分享的是關(guān)于python構(gòu)建基礎(chǔ)的爬蟲教學(xué)內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2018-12-12
  • OpenCV半小時(shí)掌握基本操作之角點(diǎn)檢測(cè)

    OpenCV半小時(shí)掌握基本操作之角點(diǎn)檢測(cè)

    這篇文章主要介紹了OpenCV基本操作之角點(diǎn)檢測(cè),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • 簡潔的十分鐘Python入門教程

    簡潔的十分鐘Python入門教程

    這篇文章主要介紹了簡潔的十分鐘Python入門教程,Python語言本身的簡潔也使得網(wǎng)絡(luò)上各種Python快門入門教程有著很高的人氣,本文是國內(nèi)此類其中的一篇,需要的朋友可以參考下
    2015-04-04
  • Python dict的使用誤區(qū)你知道嗎

    Python dict的使用誤區(qū)你知道嗎

    這篇文章主要為大家介紹了Python dict的使用誤區(qū),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01

最新評(píng)論

乐清市| 巴彦淖尔市| 上杭县| 出国| 安顺市| 遵义市| 七台河市| 信丰县| 平湖市| 清原| 成安县| 永胜县| 合阳县| 米脂县| 无为县| 陇南市| 法库县| 芦山县| 前郭尔| 辛集市| 上栗县| 师宗县| 新巴尔虎右旗| 沛县| 大方县| 宜城市| 内江市| 边坝县| 南投市| 吉木乃县| 将乐县| 南召县| 泸州市| 谢通门县| 华阴市| 娄底市| 三穗县| 五指山市| 福贡县| 周口市| 新沂市|