Pyorch基础:张量

Autograd:Automatic Differentiation

autograd是Pytorch中神经网络的核心
autograd包对所有在Tensor上的操作提供自动微分。是一个按运行定义的框架。这意味着backprop是由代码的运行方式定义的,并且每个迭代可以是不同的

Tensor

torch.Tensor是这个包的核心类。

  • .requires_grad=True可以追踪所有在其的操作。

Pytorch 基础:张量

import torch
print(torch.__version__)
1.1.0

张量(Tensor)

Pytorch里基础运算单位,与Numpy的ndarray相同都是表示一个多维的矩阵。与ndarray的最大区别是,Tensor可以在GPU上运行,而numpy的ndarrary只能在CPU上运行,在GPU上可以加速运算

# 简单张量
x = torch.randn(2, 2)
x
tensor([[ 0.6559, -0.4488],
        [-0.6773,  0.1955]])
# 查看大小 ,可以使用与numpy相同的shape属性
x.shape
torch.Size([2, 2])
x.size()
# 也可以使用size()函数
torch.Size([2, 2])

张量(Tensor)是一个定义在一些向量空间和对偶空间的笛卡尔乘积上的多重线性映射,其座标是n维空间内,有n个分量的一种量,其中每个分量都是座标的函数,在座标变换时,这些分量也按照某些规则作线性变化。r称为该向量的秩或阶

y = torch.rand(2, 3, 4)
y
tensor([[[0.5697, 0.8745, 0.3675, 0.1490],
         [0.0393, 0.9375, 0.8695, 0.9460],
         [0.9790, 0.3922, 0.5406, 0.3504]],

        [[0.5684, 0.1488, 0.7164, 0.7056],
         [0.5746, 0.5168, 0.6269, 0.4023],
         [0.6346, 0.5118, 0.0181, 0.3209]]])

在同构的意义下,第零阶张量(r=0)为标量,第一阶张量(r=1)为向量,第二阶张量(r=2)为矩阵,第三阶及以上统称为多维向量

# 标量
scalar = torch.tensor(3.1415926)
print(scalar)
print(scalar.size())
tensor(3.1416)
torch.Size([])
# 对于标量可以直接使用.item() 从中取出对应的数值
scalar.item()
3.141592502593994
# 张量中只有一个元素的tensor也可以调用.item()方法
tensor = torch.tensor([3.14159])
print(tensor)
print(tensor.shape)
tensor.item()
tensor([3.1416])
torch.Size([1])





3.141590118408203

基本类型

Tensor的基本数据类型:

  • 32位浮点型:torch.FloatTensor (default)
  • 64位浮点型:torch.DoubleTensor
  • 64位整型:torch.LongTensor
  • 32位整型:torch.IntTensor
  • 16位整型:torch.ShortTensor
  • 除以上数字类型外还有byte和chart型
long = torch.LongTensor()
long
tensor([], dtype=torch.int64)
double = torch.DoubleTensor()
double
tensor([], dtype=torch.float64)
Float = torch.FloatTensor()
Float
tensor([])
short = torch.ShortTensor()
short
tensor([], dtype=torch.int16)
Int = torch.IntTensor()
Int
tensor([], dtype=torch.int32)
char = torch.CharTensor()
char
tensor([], dtype=torch.int8)
bt = torch.ByteTensor()
bt
tensor([], dtype=torch.uint8)

Numpy转换

使用numpy方法将tensor转换为ndarray

a = torch.randn(2, 2)
numpy_a = a.numpy()
numpy_a
array([[-0.04118568,  0.83802617],
       [ 0.19688779, -0.8153309 ]], dtype=float32)
# ndarray转换位numpy
torch_a = torch.from_numpy(numpy_a)
torch_a
tensor([[-0.0412,  0.8380],
        [ 0.1969, -0.8153]])

Tensor和Numpy对象共享内存,所以转换他们相互之间转换很快

设备间转换

一般使用.cuda方法将tensor移动到gpu

cpu_a = torch.randn(2, 2)
cpu_a.type()
'torch.FloatTensor'
gpu_a = cpu_a.cuda()
print(gpu_a)
print(gpu_a.type())
tensor([[0.8202, 0.8172],
        [0.1292, 2.1433]], device='cuda:0')
torch.cuda.FloatTensor
# 使用.cpu将tensor移动到cpu
cpu_b = gpu_a.cpu()
cpu_b.type()
'torch.FloatTensor'

如果有多GPU可用,可使用to方法确定使用设备

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)
gpu_b=cpu_b.to(device)
gpu_b.type()
cuda





'torch.cuda.FloatTensor'

初始化

Pytorch中有许多初始化的方法

# 使用[0, 1]均匀分布初始化数组
rand = torch.rand(3, 2)
rand
tensor([[0.5435, 0.6259],
        [0.8157, 0.4474],
        [0.6790, 0.9695]])
# 使用0填充
zero = torch.zeros(2, 2)
zero
tensor([[0., 0.],
        [0., 0.]])
# 使用1填充
one = torch.ones(2, 2)
one
tensor([[1., 1.],
        [1., 1.]])
# 初始化单位矩阵(对角线为1,其余为0)
eye = torch.eye(2, 2)
eye
tensor([[1., 0.],
        [0., 1.]])

Pytorch中对张量的操作类似Numpy操作

x = torch.randn(2, 2)
x
tensor([[ 1.1412, -1.0689],
        [-0.1724, -0.6650]])
# 最大值, 沿行取 指定 dim=0/1
max_value = torch.max(x)
max_value
tensor(1.1412)
# 求和
sum_x = torch.sum(x, dim=1)
sum_x
tensor([ 0.0723, -0.8374])
y = torch.randn(2, 2)
z = x + y 
z
tensor([[ 2.0838, -0.6529],
        [ 1.5526, -0.9550]])
# 以_结尾的方法,均会改变调用的值
x.add_(y)
tensor([[ 2.0838, -0.6529],
        [ 1.5526, -0.9550]])
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章