1. 以_结尾操作
import torch
# adds x to y
y.add_(x) # 任何以_结尾的操作会用结果替换原变量
#例如x.copy_(y), x.t_(y)
2. .size()和.view()方法类似与numpy里面的.shape和.reshape()
x = x.new_ones(5, 3, dtype=torch.double) # new_* 方法来创建对象
print(x)
x = torch.randn_like(x, dtype=torch.float) # 覆盖 dtype!
print(x) # 对象的size 是相同的,只是值和类型发生了变化
# 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.以结尾的操作都会用结果替换原来变量, 例如x.copy(y), x.t_()都会改变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. 如果只有一个元素的张量,使用.item()来得到python数据类型的数值
x = torch.randn(1)
print(x)
print(x.item())
# tensor([-0.2368])
# -0.23680149018764496
6.numpy和Torch Tensor转换
Torch Tensor与NumPy数组共享底层内存地址,修改一个会导致另一个的变化。
将一个Torch Tensor转换为NumPy数组
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转化成Torch tensor
使用from_numpy自动转化
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 类型默认都是基于CPU, CharTensor 类型不支持到 NumPy 的转换.
使用.to 方法 可以将Tensor移动到任何设备中
# ``torch.device``将张量移动到指定的设备中
if torch.cuda.is_available():
device = torch.device("cuda") # a CUDA 设备对象
y = torch.ones_like(x, device=device) # 直接从GPU创建张量
x = x.to(device) # 或者直接使用``.to("cuda")``将张量移动到cuda中
z = x + y
print(z)
print(z.to("cpu", torch.double)) # ``.to`` 也会对变量的类型做更改
# tensor([0.7632], device='cuda:0')
# tensor([0.7632], dtype=torch.float64)
9. Tensor基本数据类型:
Tensor的基本数据类型有五种:
32位浮点型:torch.FloatTensor。 (默认)
64位整型:torch.LongTensor。
32位整型:torch.IntTensor。
16位整型:torch.ShortTensor。
64位浮点型:torch.DoubleTensor。
除以上数字类型外,还有 byte和chart型
一般情况下,可以使用.cuda方法将tensor移动到gpu,这步操作需要cuda设备支持
cpu_a = torch.rand(4,3)
cpu_a.type()
使用.cpu方法将tensor移动到cpu
cpu_b = gpu_a.cpu()
cpu_b.type()
Tensor初始化方法
##初始化,使用1填充
one = torch.ones(2, 2)
one
##初始化,使用0填充
zero=torch.zeros(2,2)
zero
#初始化一个单位矩阵,即对角线为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]])