pytorch从入门到出家

PyTorch是一个开源的Python机器学习库,基于Torch,应用于人工智能领域,如自然语言处理。它最初由Facebook的人工智能研究团队开发,并且被用于Uber的概率编程软件"Pyro"。
PyTorch主要有两大特征:

  • 类似于NumPy的张量计算,但使用GPU加速
  • 基于带基自动微分系统的深度神经网络

Tensor基本命令学习

import torch
print(torch.__version__)
# construct a 4×4 matrix, uninitialized:
x = torch.empty(4, 4)    
print(x)
1.0.1
tensor([[0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
        [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
        [0.0000e+00, 0.0000e+00, 1.8946e-42, 0.0000e+00],
        [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00]])
# construct a randomly initialized matrix:
x = torch.randn(4, 4)    
print(x)
tensor([[-0.0650, -0.1491, -0.6912, -1.5631],
        [ 0.7346,  0.5579, -0.2857,  0.4296],
        [-0.5296, -0.9600,  1.6074, -1.2324],
        [ 1.2539, -0.2922, -1.1819, -1.0337]])
# construct a matrix filled zeros and of dtype long:
x = torch.zeros(4, 4,dtype=torch.long)     
print(x)
tensor([[0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]])
# tensor类似于numpy的用法
x = torch.tensor([[1, 2], [3, 4] ],dtype=torch.int)    
print(x)
tensor([[1, 2],
        [3, 4]], dtype=torch.int32)
# construct a identity tensor
x = torch.eye(3)
print(x)
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])
# create a tensor based on an existing tensor
# new_* methods take in sizes
x = x.new_zeros(5, 3, dtype=torch.double)
print(x)

# create a tensor whose size is the same as x
x1 = torch.randn_like(x, dtype=torch.float)
print(x1)
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]], dtype=torch.float64)
tensor([[-0.5967,  1.5325, -0.8029],
        [ 0.5554, -1.6352,  1.6666],
        [-0.0776,  0.2526, -1.0336],
        [-0.6302, -0.2482, -1.7461],
        [ 0.6861,  0.6936,  0.4906]])

torch.Tensor是一种包含单一数据类型元素的多维矩阵。

Torch定义了七种CPU tensor类型和八种GPU tensor类型:

Data typeCPU tensorGPU tensor
32-bit floating pointtorch.FloatTensortorch.cuda.FloatTensor
64-bit floating pointtorch.DoubleTensortorch.cuda.DoubleTensor
16-bit floating pointN/Atorch.cuda.HalfTensor
8-bit integer (unsigned)torch.ByteTensortorch.cuda.ByteTensor
8-bit integer (signed)torch.CharTensortorch.cuda.CharTensor
16-bit integer (signed)torch.ShortTensortorch.cuda.ShortTensor
32-bit integer (signed)torch.IntTensortorch.cuda.IntTensor
64-bit integer (signed)torch.LongTensortorch.cuda.LongTensor
torch.Tensor是默认的tensor类型(torch.FlaotTensor)的简称。
注意: 会改变tensor的函数操作会用一个下划线后缀来标示。比如,torch.FloatTensor.abs_()会在原地计算绝对值,并返回改变后的tensor,而tensor.FloatTensor.abs()将会在一个新的tensor中计算结果。
# 加法
x = torch.randn(3, 3)
y = torch.randn(3, 3)
z = x + y 
print(x, y, z)
tensor([[-0.6335, -0.6622, -0.7963],
        [ 1.8275, -0.2682,  0.5084],
        [ 0.5903, -1.1938,  1.3824]]) tensor([[ 1.0473,  0.7805,  0.1164],
        [ 0.7190,  0.0148,  0.9730],
        [ 1.1453,  0.7149, -0.1418]]) tensor([[ 0.4138,  0.1182, -0.6799],
        [ 2.5465, -0.2534,  1.4813],
        [ 1.7356, -0.4789,  1.2406]])
# y的值被改变了,和上面的Z一样
y.add_(x)
print(y)
tensor([[ 0.4138,  0.1182, -0.6799],
        [ 2.5465, -0.2534,  1.4813],
        [ 1.7356, -0.4789,  1.2406]])
x = torch.Tensor([[1, 2, 3],[4, 5, 6],[7, 8, 9],[10, 11, 12]])
v = torch.Tensor([1, 1, 1])
y = torch.empty_like(x)
for i in range(x.size(0)):
    y[i, :] = x[i, :] + v
print(y)
print(v.size())
tensor([[ 2.,  3.,  4.],
        [ 5.,  6.,  7.],
        [ 8.,  9., 10.],
        [11., 12., 13.]])
torch.Size([3])
squeeze中的参数0、1分别代表第一、第二维度,squeeze(0)表示如果第一维度值为1,则去掉,否则不变。故b的维度(1,3),可去掉1成(3),但不可去掉3。unsqueeze()与squeeze()作用相反。参数代表的意思相同。注意: 返回张量与输入张量共享内存,所以改变其中一个的内容会改变另一个。
v = v.unsqueeze(dim=0)
print(v)
print(v.size())
print(x+v)
print(x+1)
tensor([[[1., 1., 1.]]])
torch.Size([1, 1, 3])
tensor([[[ 2.,  3.,  4.],
         [ 5.,  6.,  7.],
         [ 8.,  9., 10.],
         [11., 12., 13.]]])
tensor([[ 2.,  3.,  4.],
        [ 5.,  6.,  7.],
        [ 8.,  9., 10.],
        [11., 12., 13.]])
# 点乘,得到一个结果
x = torch.Tensor([2, 3, 3])
y = torch.Tensor([2, 2, 4])
# print x1 * y1 + x2 * y2 + x3 * y3
print(torch.dot(x, y))
tensor(22.)
x = torch.Tensor([[1, 2, 3],[1, 2, 3]])
y = torch.Tensor([[2, 2, 2],[2, 2, 2]])

# multiply elements respectively
print(x * y)

# divide elements respectively
print(x / y)
tensor([[2., 4., 6.],
        [2., 4., 6.]])
tensor([[0.5000, 1.0000, 1.5000],
        [0.5000, 1.0000, 1.5000]])
'''
It is matrix multiplication
If mat1 is a (n×m) tensor, mat2 is a (m×p) tensor, 
out will be a (n×p) tensor.
'''
x = torch.Tensor([[2, 3], [1, 1]])
y = torch.Tensor([[2, 2, 1], [1, 1, 1]])
print(torch.mm(x, y))
tensor([[7., 7., 5.],
        [3., 3., 2.]])
x = torch.Tensor([[1, 2], [3, 4],[5, 6]])
print(x)
# sum all elements
print(x.sum())

# sum elements for each column
# dim=0 refers to row
print(x.sum(dim=0))

# sum elements for each row
# dim=1 refers to column
print(x.sum(dim=1))
tensor([[1., 2.],
        [3., 4.],
        [5., 6.]])
tensor(21.)
tensor([ 9., 12.])
tensor([ 3.,  7., 11.])
# matrix transpose,第一个参数为tensor,后面俩是要交换的维度,如果填同一个数什么也不会发生,而填0,1和填1,0的效果是一样的。
print(torch.transpose(x,1,1))
print(torch.transpose(x,0,1))
tensor([[1., 2.],
        [3., 4.],
        [5., 6.]])
tensor([[1., 3., 5.],
        [2., 4., 6.]])
'''
view 可以将Tensor拼接成一行,当输入几个参数时,效果跟reshape差不多
'''
x = torch.randn(4, 4, dtype=torch.float)
y = x.view(-1)
print(x)
print(y)
print(y.size())
print(x.reshape(8,-1))
tensor([[ 0.2907,  0.4242, -0.5006, -0.4336],
        [-0.2107,  0.2024,  2.2346, -0.0815],
        [-0.4340, -0.2916, -0.5340, -0.2336],
        [ 1.3421,  0.6721,  0.0072,  0.4988]])
tensor([ 0.2907,  0.4242, -0.5006, -0.4336, -0.2107,  0.2024,  2.2346, -0.0815,
        -0.4340, -0.2916, -0.5340, -0.2336,  1.3421,  0.6721,  0.0072,  0.4988])
torch.Size([16])
tensor([[ 0.2907,  0.4242],
        [-0.5006, -0.4336],
        [-0.2107,  0.2024],
        [ 2.2346, -0.0815],
        [-0.4340, -0.2916],
        [-0.5340, -0.2336],
        [ 1.3421,  0.6721],
        [ 0.0072,  0.4988]])
# 将一个Tensor值转化为number,注意大小必须是1
x = torch.randn(1)
print(x)
# get numbers in x
y = x.item()
print(y)
tensor([-1.8933])
-1.8933312892913818

Numpy与Tensor

import numpy as np
a = np.ones(5)
# numpy convert into tensor
b = torch.from_numpy(a)    
print(b)
print(b.numpy())
tensor([1., 1., 1., 1., 1.], dtype=torch.float64)
[1. 1. 1. 1. 1.]

Tensor调用cuda

# define a gpu device
device1 = torch.device('cuda:0')
# define a cpu device
device2 = torch.device('cpu')  
# generate a random tensor
x = torch.randn(5,3)
# show the random tensor on cpu
print(x.device)
cpu
# judge current environment whether can use cuda
if torch.cuda.is_available():
    # copy tensor x to gpu (x on cpu and x on gpu don't share the same address)
    x = x.to(device1)   
    print(x.device)
x = torch.randn([2, 2]).cuda()
print(x.device)
---------------------------------------------------------------------------

AssertionError                            Traceback (most recent call last)

<ipython-input-54-9cff28824f41> in <module>
----> 1 x = torch.randn([2, 2]).cuda()
      2 print(x.device)


E:\train\anaconda\envs\torch\lib\site-packages\torch\cuda\__init__.py in _lazy_init()
    159         raise RuntimeError(
    160             "Cannot re-initialize CUDA in forked subprocess. " + msg)
--> 161     _check_driver()
    162     torch._C._cuda_init()
    163     _cudart = _load_cudart()


E:\train\anaconda\envs\torch\lib\site-packages\torch\cuda\__init__.py in _check_driver()
     89 Alternatively, go to: https://pytorch.org to install
     90 a PyTorch version that has been compiled with your version
---> 91 of the CUDA driver.""".format(str(torch._C._cuda_getDriverVersion())))
     92 
     93 


AssertionError: 
The NVIDIA driver on your system is too old (found version 9010).
Please update your GPU driver by downloading and installing a new
version from the URL: http://www.nvidia.com/Download/index.aspx
Alternatively, go to: https://pytorch.org to install
a PyTorch version that has been compiled with your version
of the CUDA driver.

用不了,看来只能用CPU了。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

羊城迷鹿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值