import torch x = torch.empty(5, 3) # 生成空的矩陣 print(x) x = torch.rand(5, 4) # 生成隨機矩陣 print(x) x = torch.zeros(5, 4, dtype=torch.long) # 生成空矩陣 print(x) x = torch.tensor([5, 3]) # 將列表轉換維tensor類型 print(x) x = x.new_ones([5, 3], dtype=torch.double) # 構造全1的數組 print(x) x = torch.rand_like(x, dtype=torch.float) # 生成相同維度的隨機矩陣 print(x.size()) y = torch.rand(5, 3) # 構造隨機的矩陣 print(x + y) torch.add(x, y) # 進行矩陣的相加操作 # 索引 print(x[:, 1]) # view操作可以改變矩陣維度 x = torch.randn(4, 4) y = x.view(16) z = x.view(-1, 8) a = torch.ones(5) # tensor b = a.numpy() # 將tensor轉換為numpy格式 import numpy as np a = np.ones([3, 2]) b = torch.from_numpy(a) # 將a轉換為tensor格式 print(b)