pytorch view函式用法

2021-10-12 05:42:03 字數 1075 閱讀 1850

在pytorch中view函式的作用為重構張量的維度,相當於numpy中resize()的功能

>>> import torch

>>> t1 = torch.tensor([1,2,3,4,5,6])

>>> result = tt1.view(3,2)

>>> result

tensor([[1, 2],

[3, 4],

[5, 6]])

建立乙個6維的向量,view(3,2)將其轉化為3x2規模的矩陣

>>> t2 = torch.tensor([1,2,3],[4,5,6],[7,8,9])

>>> result = t2.view(-1)

tensor([1, 2, 3, 4, 5, 6, 7, 8, 9])

當view(-1)時,其作用是將乙個矩陣展開為乙個向量

>>> t3 = torch.tensor([1,2,3],[4,5,6],[7,8,9])

>>> result = t2.view(3,-1)

>>> result

tensor([[1, 2, 3],

[4, 5, 6],

[7, 8, 9]])

>>> result.size()

torch.size([3, 3])

>>> t4 = torch.tensor([[1,2,3],[4,5,6]])

>>> t4.size()

torch.size([2,3])

>>> result = t4.view(3,-1)

>>> result

tensor([[1, 2],

[3, 4],

[5, 6]])

>>> result.size()

torch.size([3, 2])

當view()引數不止乙個,且其中乙個為-1時,則-1的作用相當於佔位符,其大小由torch根據矩陣元素個數和其他維大小來自動計算

注意:torch中的矩陣順序為[batch_size, channels, x, y]

PyTorch view和reshape的區別

相同之處 都可以用來重新調整 tensor 的形狀。不同之處 view 函式只能用於 contiguous 後的 tensor 上,也就是只能用於記憶體中連續儲存的 tensor。如果對 tensor 呼叫過 transpose,permute 等操作的話會使該 tensor 在記憶體中變得不再連續...

函式的用法 CELL函式高階用法,

資料多時看的眼花繚亂,使用聚光燈功能突出顯示當前的行和列,在檢視資料時非常方便,wps是自帶該功能的。選中要設定的資料區域,在 開始 選項卡下點 條件格式 選擇 新建規則 在彈出的 新建格式規則 視窗中選擇 使用公式確定要設定格式的單元格 在 為符合此公式的值設定格式 下輸入公式 or cell r...

函式的用法 INDEXC函式的用法

1 製作小 箱 如何利用excel製作簡單的小 箱?這裡與大家分享借用index函式來實現。在單元格中輸入公式 index a2 a17,randbetween 2,counta a2 a17 公式說明 counta函式表示統計非空單元格格式 randbetween函式表示隨機生成整數 2 隔行提取...