一、unset函數
1、unset函數是對一個Tensor進行操作,操作的結果是將該Tensor指定位置的元素的值設置為默認值0。
import torch x = torch.tensor([[1, 2], [3, 4]]) x[0, 1] = torch.tensor(0) print(x)
結果為:
tensor([[1, 0], [3, 4]])
2、在unsqueeze函數中,如果在指定維度上執行unset操作,那麼該位置為0,該位置前面的維度值不變,後面的維度需要加1。
import torch x = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) x = torch.unset(x, 1) print(x)
結果為:
tensor([[[0, 0], [0, 0]], [[0, 0], [0, 0]]])
二、squeeze函數是什麼意思
1、squeeze函數是對Tensor進行操作,操作的結果是刪除維數為1的維度。
import torch x = torch.tensor([[[1, 2], [3, 4]]]) x = torch.squeeze(x) print(x.size())
結果為:
torch.Size([2, 2])
2、在unsqueeze函數中,如果在指定維度上執行squeeze操作,那麼該位置的維度被刪除,該位置前面和後面的維度不變。
import torch x = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) x = torch.squeeze(x, 1) print(x.size())
結果為:
torch.Size([2, 2, 2])
三、uniform函數是什麼函數
1、uniform函數是用來生成指定大小的隨機數Tensor,隨機數取值在[0, 1)之間。
import torch x = torch.empty(3, 3) x = torch.nn.init.uniform_(x) print(x)
結果為:
tensor([[0.3481, 0.9646, 0.2879], [0.4404, 0.3296, 0.6972], [0.9313, 0.3676, 0.4409]])
2、在unsqueeze函數中,如果在指定維度上執行uniform操作,那麼該位置的維度上的元素值取隨機數[0, 1),該位置前面和後面的維度不變。
import torch x = torch.ones(2, 3, 4) x = torch.nn.init.uniform_(x[:, 0, :], 0, 1) print(x)
結果為:
tensor([[0.5948, 0.4088, 0.1028, 0.9948], [0.5948, 0.4088, 0.1028, 0.9948]])
四、unsignedchar函數是什麼函數
1、unsignedchar函數是將Tensor的數據類型設置為uint8。
import torch x = torch.tensor([[-1, 2], [3, 4]]) x = x.type(torch.uint8) print(x)
結果為:
tensor([[255, 2], [ 3, 4]], dtype=torch.uint8)
2、在unsqueeze函數中,如果在指定位置上執行unsignedchar操作,那麼該位置的數據類型變為uint8,該位置前面和後面的維度不變。
import torch x = torch.tensor([[1.0, 2.0], [3.0, 4.0]]) x = x.unsqueeze(1) x = x.type(torch.uint8) print(x)
結果為:
tensor([[[1, 2]], [[3, 4]]], dtype=torch.uint8)
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/207080.html