pytorch tensor(張量)

import torch
x = torch.empty([5, 3]) #構造一個5x3矩陣,不初始化。
print(x)

x = torch.rand(5, 3) #構造一個隨機初始化的矩陣
print(x)

x = torch.zeros(5, 3, dtype=torch.long) #構造一個矩陣全爲 0,而且數據類型是 long.
print(x)

x = torch.tensor([5.5, 3]) #構造一個張量,直接使用數據
print(x)

#創建一個 tensor 基於已經存在的 tensor。
x = x.new_ones(5, 3, dtype=torch.double)
# new_* methods take in sizes
print(x)
x = torch.rand_like(x, dtype=torch.float)
# override dtype!
print(x)
# result has the same size
print(x.size()) #獲取它的維度信息

#加法操作:  加法: 方式 1
y = torch.rand(5, 3)
print(x + y)
#加法: 方式2
print(torch.add(x, y))
#加法: 提供一個輸出 tensor 作爲參數
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
#加法: in-place
y.add_(x) # adds x to y
print(y)

#你可以使用標準的 NumPy 類似的索引操作
print('你可以使用標準的 NumPy 類似的索引操作')
print(x)
print(x[:, 1])

#改變大小:如果你想改變一個 tensor 的大小或者形狀,你可以使用 torch.view
x = torch.rand(4, 4)
y = x.view(16)
z = x.view(-1, 8) #the size -1 is inferred from other dimensions
print(x.size(), y.size(), z.size())

#如果你有一個元素 tensor ,使用 .item() 來獲得這個 value 
print('如果你有一個元素 tensor ,使用 .item() 來獲得這個 value ')
x = torch.rand(1)
print(x)
print(x.item())
x = torch.rand(2,2)
print(x)
print(x[1, 1].item())


結果:

tensor([[1.0286e-38, 1.0653e-38, 1.0194e-38],
        [8.4490e-39, 1.0469e-38, 9.3674e-39],
        [9.9184e-39, 8.7245e-39, 9.2755e-39],
        [8.9082e-39, 9.9184e-39, 8.4490e-39],
        [9.6429e-39, 1.0653e-38, 1.0469e-38]])
tensor([[0.7646, 0.9651, 0.3421],
        [0.0723, 0.6261, 0.8424],
        [0.5608, 0.2591, 0.9499],
        [0.0916, 0.0446, 0.4608],
        [0.4475, 0.9349, 0.4968]])
tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])
tensor([5.5000, 3.0000])
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)
tensor([[0.0322, 0.5927, 0.9057],
        [0.2867, 0.8064, 0.6671],
        [0.4588, 0.9307, 0.6803],
        [0.1403, 0.9681, 0.6943],
        [0.1115, 0.1651, 0.6864]])
torch.Size([5, 3])
tensor([[0.1692, 1.0217, 1.8799],
        [0.3839, 1.1857, 1.1743],
        [0.9833, 1.7841, 1.3485],
        [0.3881, 1.1007, 0.8834],
        [0.8782, 1.0215, 0.7617]])
tensor([[0.1692, 1.0217, 1.8799],
        [0.3839, 1.1857, 1.1743],
        [0.9833, 1.7841, 1.3485],
        [0.3881, 1.1007, 0.8834],
        [0.8782, 1.0215, 0.7617]])
tensor([[0.1692, 1.0217, 1.8799],
        [0.3839, 1.1857, 1.1743],
        [0.9833, 1.7841, 1.3485],
        [0.3881, 1.1007, 0.8834],
        [0.8782, 1.0215, 0.7617]])
tensor([[0.1692, 1.0217, 1.8799],
        [0.3839, 1.1857, 1.1743],
        [0.9833, 1.7841, 1.3485],
        [0.3881, 1.1007, 0.8834],
        [0.8782, 1.0215, 0.7617]])
你可以使用標準的 NumPy 類似的索引操作
tensor([[0.0322, 0.5927, 0.9057],
        [0.2867, 0.8064, 0.6671],
        [0.4588, 0.9307, 0.6803],
        [0.1403, 0.9681, 0.6943],
        [0.1115, 0.1651, 0.6864]])
tensor([0.5927, 0.8064, 0.9307, 0.9681, 0.1651])
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
如果你有一個元素 tensor ,使用 .item() 來獲得這個 value 
tensor([0.0557])
0.05566328763961792
tensor([[0.9741, 0.3063],
        [0.5104, 0.9453]])
0.9453043341636658

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章