對運營網(wǎng)站有什么見解百度推廣年費多少錢
???????
目錄
1. reshape 函數(shù)的用法
2. transpose 和 permute 函數(shù)的使用
4. squeeze 和 unsqueeze 函數(shù)的用法
5. 小節(jié)
個人主頁:Icomi
專欄地址:PyTorch入門
在深度學(xué)習(xí)蓬勃發(fā)展的當(dāng)下,PyTorch 是不可或缺的工具。它作為強大的深度學(xué)習(xí)框架,為構(gòu)建和訓(xùn)練神經(jīng)網(wǎng)絡(luò)提供了高效且靈活的平臺。神經(jīng)網(wǎng)絡(luò)作為人工智能的核心技術(shù),能夠處理復(fù)雜的數(shù)據(jù)模式。通過 PyTorch,我們可以輕松搭建各類神經(jīng)網(wǎng)絡(luò)模型,實現(xiàn)從基礎(chǔ)到高級的人工智能應(yīng)用。接下來,就讓我們一同走進(jìn) PyTorch 的世界,探索神經(jīng)網(wǎng)絡(luò)與人工智能的奧秘。本系列為PyTorch入門文章,若各位大佬想持續(xù)跟進(jìn),歡迎與我交流互關(guān)。
?????????咱們已經(jīng)學(xué)習(xí)了張量的花式索引操作,它就像一把精巧的工具,讓我們能夠在數(shù)據(jù)的 “寶藏庫” 里精準(zhǔn)地提取和修改信息。我們接下來要學(xué)習(xí)—— 掌握對張量形狀的操作。
????????想象一下,我們即將搭建的網(wǎng)絡(luò)模型就像一座宏偉而復(fù)雜的建筑,而數(shù)據(jù)則是構(gòu)建這座建筑的基石。這些數(shù)據(jù)在我們的深度學(xué)習(xí)世界里,都是以張量的形式存在。在這座 “建筑” 中,不同的樓層(網(wǎng)絡(luò)層)有著不同的功能和設(shè)計,它們之間的數(shù)據(jù)傳遞和運算就如同建筑中不同樓層之間的物資運輸和協(xié)作。
????????每一層網(wǎng)絡(luò)對數(shù)據(jù)的處理方式都不盡相同,這就導(dǎo)致數(shù)據(jù)在網(wǎng)絡(luò)層與層之間流動時,會以不同的形狀(shape)進(jìn)行表現(xiàn)和運算。比如說,有的層可能接收的是二維的張量數(shù)據(jù),經(jīng)過處理后輸出一個三維的張量,就像把方形的積木經(jīng)過加工變成了一個立體的模型。
????????如果我們不掌握對張量形狀的操作,就好比一個建筑工人不熟悉不同建筑材料的尺寸和拼接方式,那么在搭建這座 “網(wǎng)絡(luò)建筑” 時,各層之間的數(shù)據(jù)連接就會出現(xiàn)問題,就像積木無法正確拼接,最終導(dǎo)致整個建筑搖搖欲墜。
????????為了能夠更好地處理網(wǎng)絡(luò)各層之間的數(shù)據(jù)連接,順利搭建出穩(wěn)固而強大的網(wǎng)絡(luò)模型,掌握對張量形狀的操作就顯得尤為重要。接下來,我們就一同深入學(xué)習(xí)如何巧妙地調(diào)整和管理張量的形狀,讓我們在深度學(xué)習(xí)的建筑之路上穩(wěn)步前行。
1. reshape 函數(shù)的用法
reshape 函數(shù)可以在保證張量數(shù)據(jù)不變的前提下改變數(shù)據(jù)的維度,將其轉(zhuǎn)換成指定的形狀,在后面的神經(jīng)網(wǎng)絡(luò)學(xué)習(xí)時,會經(jīng)常使用該函數(shù)來調(diào)節(jié)數(shù)據(jù)的形狀,以適配不同網(wǎng)絡(luò)層之間的數(shù)據(jù)傳遞。
import torch
import numpy as npdef tensor_shape_operations():# 創(chuàng)建一個二維張量tensor = torch.tensor([[10, 20, 30], [40, 50, 60]])# 1. 使用 shape 屬性或者 size 方法都可以獲得張量的形狀print(f"使用 shape 屬性獲取的形狀: {tensor.shape},第 0 維大小: {tensor.shape[0]},第 1 維大小: {tensor.shape[1]}")print(f"使用 size 方法獲取的形狀: {tensor.size()},第 0 維大小: {tensor.size(0)},第 1 維大小: {tensor.size(1)}")# 2. 使用 reshape 函數(shù)修改張量形狀reshaped_tensor = tensor.reshape(1, 6)print(f"修改形狀后的張量形狀: {reshaped_tensor.shape}")if __name__ == '__main__':tensor_shape_operations()
需要注意的是,轉(zhuǎn)換前后的兩個形狀元素個數(shù)要相同
import torchdef test():torch.manual_seed(0)data = torch.randint(0, 10, [4, 5])# 查看張量的形狀print(data.shape, data.shape[0], data.shape[1])print(data.size(), data.size(0), data.size(1))# 修改張量的形狀new_data = data.reshape(2, 10)print(new_data)# 注意: 轉(zhuǎn)換之后的形狀元素個數(shù)得等于原來張量的元素個數(shù)# new_data = data.reshape(1, 10)# print(new_data)# 使用-1代替省略的形狀new_data = data.reshape(5, -1)print(new_data)new_data = data.reshape(-1, 2)print(new_data)if __name__ == '__main__':test()
2. transpose 和 permute 函數(shù)的使用
transpose 函數(shù)可以實現(xiàn)交換張量形狀的指定維度, 例如: 一個張量的形狀為 (2, 3, 4) 可以通過 transpose 函數(shù)把 3 和 4 進(jìn)行交換, 將張量的形狀變?yōu)?(2, 4, 3)
permute 函數(shù)可以一次交換更多的維度。
import torch
import numpy as npdef test():data = torch.tensor(np.random.randint(0, 10, [3, 4, 5]))print('data shape:', data.size())# 1. 交換1和2維度new_data = torch.transpose(data, 1, 2)print('data shape:', new_data.size())# 2. 將 data 的形狀修改為 (4, 5, 3)new_data = torch.transpose(data, 0, 1)new_data = torch.transpose(new_data, 1, 2)print('new_data shape:', new_data.size())# 3. 使用 permute 函數(shù)將形狀修改為 (4, 5, 3)new_data = torch.permute(data, [1, 2, 0])print('new_data shape:', new_data.size())if __name__ == '__main__':test()
4. squeeze 和 unsqueeze 函數(shù)的用法
squeeze 函數(shù)用刪除 shape 為 1 的維度,unsqueeze 在每個維度添加 1, 以增加數(shù)據(jù)的形狀
import torch
import numpy as npdef test():data = torch.tensor(np.random.randint(0, 10, [1, 3, 1, 5]))print('data shape:', data.size())# 1. 去掉值為1的維度new_data = data.squeeze()print('new_data shape:', new_data.size()) # torch.Size([3, 5])# 2. 去掉指定位置為1的維度,注意: 如果指定位置不是1則不刪除new_data = data.squeeze(2)print('new_data shape:', new_data.size()) # torch.Size([3, 5])# 3. 在2維度增加一個維度new_data = data.unsqueeze(-1)print('new_data shape:', new_data.size()) # torch.Size([3, 1, 5, 1])if __name__ == '__main__':test()
5. 小節(jié)
本小節(jié)我們學(xué)習(xí)了經(jīng)常使用的關(guān)于張量形狀的操作,我們用到的主要函數(shù)有:
- reshape 函數(shù)可以在保證張量數(shù)據(jù)不變的前提下改變數(shù)據(jù)的維度.
- transpose 函數(shù)可以實現(xiàn)交換張量形狀的指定維度, permute 可以一次交換更多的維度.
- view 函數(shù)也可以用于修改張量的形狀, 但是它要求被轉(zhuǎn)換的張量內(nèi)存必須連續(xù),所以一般配合 contiguous 函數(shù)使用.
- squeeze 和 unsqueeze 函數(shù)可以用來增加或者減少維度.