1. 以_結(jié)尾操作
import torch
# adds x to y
y.add_(x) # 任何以_結(jié)尾的操作會(huì)用結(jié)果替換原變量
#例如x.copy_(y)震糖, x.t_(y)
2. .size()和.view()方法類似與numpy里面的.shape和.reshape()
x = x.new_ones(5, 3, dtype=torch.double) # new_* 方法來(lái)創(chuàng)建對(duì)象
print(x)
x = torch.randn_like(x, dtype=torch.float) # 覆蓋 dtype!
print(x) # 對(duì)象的size 是相同的,只是值和類型發(fā)生了變化
# tensor([[1., 1., 1.],
# [1., 1., 1.],
# [1., 1., 1.],
# [1., 1., 1.],
# [1., 1., 1.]], dtype=torch.float64)
# tensor([[ 0.5691, -2.0126, -0.4064],
# [-0.0863, 0.4692, -1.1209],
# [-1.1177, -0.5764, -0.5363],
# [-0.4390, 0.6688, 0.0889],
# [ 1.3334, -1.1600, 1.8457]])
print(x.size())
# torch.Size([5, 3])
3.以結(jié)尾的操作都會(huì)用結(jié)果替換原來(lái)變量梨水, 例如x.copy(y), x.t_()都會(huì)改變x.
y.add_(x)
print(y)
# tensor([[ 0.7808, -1.4388, 0.3151],
# [-0.0076, 1.0716, -0.8465],
# [-0.8175, 0.3625, -0.2005],
# [ 0.2435, 0.8512, 0.7142],
# [ 1.4737, -0.8545, 2.4833]])
4 torch.view和numpy的reshape類似
y = x.view(16)
z = x.view(-1, 8) # size -1 從其他維度推斷
print(x.size(), y.size(), z.size())
# torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
5. 如果只有一個(gè)元素的張量,使用.item()來(lái)得到python數(shù)據(jù)類型的數(shù)值
x = torch.randn(1)
print(x)
print(x.item())
# tensor([-0.2368])
# -0.23680149018764496
6.numpy和Torch Tensor轉(zhuǎn)換
Torch Tensor與NumPy數(shù)組共享底層內(nèi)存地址,修改一個(gè)會(huì)導(dǎo)致另一個(gè)的變化。
將一個(gè)Torch Tensor轉(zhuǎn)換為NumPy數(shù)組
a = torch.ones(5)
print(a)
# tensor([1., 1., 1., 1., 1.])
b = a.numpy()
print(b)
# [1. 1. 1. 1. 1.]
a.add_(1)
print(a)
print(b)
# tensor([2., 2., 2., 2., 2.])
# [2. 2. 2. 2. 2.]
7. numpy Array轉(zhuǎn)化成Torch tensor
使用from_numpy自動(dòng)轉(zhuǎn)化
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)
#[2. 2. 2. 2. 2.]
#tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
8.CUDA張量
所有的 Tensor 類型默認(rèn)都是基于CPU在跳, CharTensor 類型不支持到 NumPy 的轉(zhuǎn)換.
使用.to 方法 可以將Tensor移動(dòng)到任何設(shè)備中
# ``torch.device``將張量移動(dòng)到指定的設(shè)備中
if torch.cuda.is_available():
device = torch.device("cuda") # a CUDA 設(shè)備對(duì)象
y = torch.ones_like(x, device=device) # 直接從GPU創(chuàng)建張量
x = x.to(device) # 或者直接使用``.to("cuda")``將張量移動(dòng)到cuda中
z = x + y
print(z)
print(z.to("cpu", torch.double)) # ``.to`` 也會(huì)對(duì)變量的類型做更改
# tensor([0.7632], device='cuda:0')
# tensor([0.7632], dtype=torch.float64)
9. Tensor基本數(shù)據(jù)類型:
Tensor的基本數(shù)據(jù)類型有五種:
32位浮點(diǎn)型:torch.FloatTensor。 (默認(rèn))
64位整型:torch.LongTensor隐岛。
32位整型:torch.IntTensor。
16位整型:torch.ShortTensor瓷翻。
64位浮點(diǎn)型:torch.DoubleTensor聚凹。
除以上數(shù)字類型外,還有 byte和chart型
一般情況下齐帚,可以使用.cuda方法將tensor移動(dòng)到gpu妒牙,這步操作需要cuda設(shè)備支持
cpu_a = torch.rand(4,3)
cpu_a.type()
使用.cpu方法將tensor移動(dòng)到cpu
cpu_b = gpu_a.cpu()
cpu_b.type()
Tensor初始化方法
##初始化,使用1填充
one = torch.ones(2, 2)
one
##初始化对妄,使用0填充
zero=torch.zeros(2,2)
zero
#初始化一個(gè)單位矩陣湘今,即對(duì)角線為1 其他為0
eye=torch.eye(2,2)
eye
#tensor([[1., 0.],
# [0., 1.]])
x = torch.randn(3, 3)
print(x)
# tensor([[ 0.6922, -0.4824, 0.8594],
# [ 0.4509, -0.8155, -0.0368],
# [ 1.3533, 0.5545, -0.0509]])
# 沿著行取最大值
max_value, max_idx = torch.max(x, dim=1)
print(max_value, max_idx)
# tensor([0.8594, 0.4509, 1.3533]) tensor([2, 0, 0])
# 每行 x 求和
sum_x = torch.sum(x, dim=1)
print(sum_x)
# tensor([ 1.0692, -0.4014, 1.8568])
y=torch.randn(3, 3)
z = x + y
print(z)
# tensor([[-0.3821, -2.6932, -1.3884],
# [ 0.7468, -0.7697, -0.0883],
# [ 0.7688, -1.3485, 0.7517]])
# add 完成后x的值改變了
x.add_(y)
print(x)
# tensor([[-0.3821, -2.6932, -1.3884],
# [ 0.7468, -0.7697, -0.0883],
# [ 0.7688, -1.3485, 0.7517]])