如何在 Pytorch 中切片 3D 張量?
Python 中的 3D 張量以其矩陣陣列而聞名,而 1D 和 2D 張量分別表示向量和矩陣。Pytorch 是基於 Python 程式語言的機器學習框架的開源模組,它提供了名為 torch 的模組。在 Python 中,我們有一些內建函式,如 randn()、rand() 和 split(),可用於表示 Pytorch 中 3D 張量的切片。
語法
以下語法在示例中使用:
import torch
這是一個 Python 模組,它儲存所有神經網路的基類。它有助於構建和訓練神經網路,例如輸入、輸出和隱藏層。
randn()
這是一個 NumPy 庫內建函式,可用於從陣列中設定標準正態分佈的隨機樣本。
rand()
這是 Python 的一個內建函式,用於生成偽隨機數。
split()
這是 Python 中的內建方法,用於將字串轉換為列表。
安裝要求
pip install torch
此模組有助於基於 1D、2D 和 3D 張量執行程式。
示例 1
在以下示例中,我們將透過匯入 torch 模組來啟動程式。然後使用名為 **randn()** 的內建方法,該方法遵循 **torch** 模組,接受一些整數作為引數以列表的形式分配陣列的平均標準,並將其儲存在變數 **a** 中。然後連續初始化三個變數 - b、c 和 d,它們將透過保留其元素來切片維度。接下來,使用 print 函式依次設定所有變數並獲取結果。
import torch a = torch.randn(1,2,3) # Slicing of the first dimension by keeping the first element b = x[0:1,:,:] # Slicing of the second dimension by keeping the second element c = x[:,0:2,:] # Slicing of the third dimension by keeping the third element d = x[:,:,-2:] print(a) print(b) print(c) print(d)
輸出
tensor([[[ 0.1352, -0.8739, -1.5743],
[-0.4853, 0.7519, 0.9749]]])
tensor([[[-0.7441, 0.3385, -1.0671, 1.0431],
[ 0.2104, -0.3083, 1.1756, 0.3387],
[ 0.8722, 0.4534, 0.5528, -0.3567]]])
tensor([[[-0.7441, 0.3385, -1.0671, 1.0431],
[ 0.2104, -0.3083, 1.1756, 0.3387]],
[[ 0.6623, 1.3858, -1.0033, -0.5977],
[ 1.1141, -0.8058, 1.5914, 0.6476]]])
tensor([[[-1.0671, 1.0431],
[ 1.1756, 0.3387],
[ 0.5528, -0.3567]],
[[-1.0033, -0.5977],
[ 1.5914, 0.6476],
[ 1.6480, -0.9212]]])
示例 2
在以下示例中,透過匯入 torch 模組來開始程式。然後將三個陣列列表的總和儲存在變數 tensor_3d 中。接下來,將列表的第 0 個索引儲存在變數 tensor 中。同樣,將 list1 和 list2 分別儲存在變數 tensor1 和 tensor2 中。然後在所有張量變數的幫助下列印結果。
import torch
tensor_3d = ([[[10, 20, 30, 40],[50, 60, 70, 80],[1, 2, 3, 4]],
[[13, 14, 15, 16],[21, 22, 23, 24], [3, 4, 5, 6]],
[[61, 62, 63, 64],[71, 72, 73, 74],[7, 8, 9, 10]]])
tensor = tensor_3d[0]
tensor1 = tensor_3d[1]
tensor2 = tensor_3d[2]
print("The first dimension:",tensor)
print("The second dimension:",tensor1)
print("The third dimension:",tensor2)
輸出
The first dimension: [[10, 20, 30, 40], [50, 60, 70, 80], [1, 2, 3, 4]] The second dimension: [[13, 14, 15, 16], [21, 22, 23, 24], [3, 4, 5, 6]] The third dimension: [[61, 62, 63, 64], [71, 72, 73, 74], [7, 8, 9, 10]]
示例 3
在以下示例中,透過匯入必要的名為 torch 和 random 的模組來開始程式,該模組將生成具有隨機值的矩陣陣列。然後 torch.rand((3, 4, 8)) 建立 0 到 1 之間的隨機值並將其儲存在變數 x 中。接下來,torch.split() 接受三個引數 - x、3 和 2,它們將張量 x 沿第三維度 (2) 分成塊,每個塊的大小為 3,並將其儲存在變數 slices 中。最後,我們使用變數 **slices** 列印結果。
請注意,塊是指透過沿特定維度劃分原始張量而建立的較小張量。
import torch import random # generate the random values between 0 and 1 x = torch.rand((3,4,8)) slices = torch.split(x, 3, 2) print(slices)
輸出
(tensor([[[0.3747, 0.4710, 0.4233, 0.2445],
[0.9414, 0.6634, 0.6091, 0.8761],
[0.7911, 0.8687, 0.8468, 0.8766],
[0.8192, 0.1498, 0.7685, 0.4718]],
[[0.4160, 0.7172, 0.4647, 0.4860],
[0.7074, 0.9610, 0.4967, 0.7411],
[0.2269, 0.8565, 0.8671, 0.3461],
[0.0397, 0.0809, 0.2017, 0.1106]],
[[0.6528, 0.7044, 0.7829, 0.1844],
[0.4297, 0.2802, 0.4159, 0.4485],
[0.5843, 0.6958, 0.8991, 0.3918],
[0.2549, 0.9363, 0.3098, 0.7053]]]), tensor([[[0.0095, 0.4314, 0.6110, 0.4594],
[0.8541, 0.7622, 0.3550, 0.7715],
[0.5951, 0.9786, 0.3598, 0.2587],
[0.5516, 0.6523, 0.5176, 0.1267]],
[[0.8616, 0.3667, 0.0145, 0.9252],
[0.5995, 0.3094, 0.0483, 0.1996],
[0.1004, 0.1373, 0.3303, 0.0982],
[0.2414, 0.7782, 0.8850, 0.1027]],
[[0.0559, 0.8675, 0.7974, 0.6309],
[0.3900, 0.4914, 0.4957, 0.5973],
[0.6249, 0.8075, 0.6700, 0.7789],
[0.6211, 0.9158, 0.6633, 0.2306]]]))
結論
我們討論了 Pytorch 中所有型別的張量。Pytorch 簡稱為 N 維或高維張量。以上輸出使用了各種內建函式,如 split()、randn() 和 rand(),這些函式有助於以 3D 張量的形式獲取結果。張量的各種應用被用於物理學、工程學和計算機科學領域。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP