PyTorch學習筆記之Tensors 2
阿新 • • 發佈:2017-07-21
cti sam numpy 所有 隨機 一個 port pre clas
Tensors的一些應用
1 ‘‘‘ 2 Tensors和numpy中的ndarrays較為相似, 因此Tensor也能夠使用GPU來加速運算 3 ‘‘‘ 4 # from _future_ import print_function 5 import torch 6 x = torch.Tensor(5, 3) # 構造一個未初始化的5*3的矩陣 7 8 x2 = torch.rand(5, 3) # 構造一個隨機初始化的矩陣 the same as 9 10 # print(x.size()) # torch.Size([5, 3])11 y = torch.rand(5, 3) 12 13 # x+y 14 # method 1 15 print(torch.add(x, y)) 16 # 輸出tensor 17 result = torch.Tensor(5, 3) 18 19 # method 2 20 print(torch.add(x, y, out=result)) # the same as the above one 21 # print(result) result=torch.add(x, y, out=result) 22 23 # method 3 24print(y.add_(x)) # 任何可以改變tensor內容的操作都會在方法名後加一個下劃線‘_‘,例如:x.copy_(y), x.t_(), 這倆都會改變x的值。 25 # print(y) y=y.add_(x) 26 27 # print(x[:, 1]) # 這一操作會輸出x矩陣的第二列的所有值
PyTorch學習筆記之Tensors 2