pytorch基础知识

参考pytorch官方教程,撸pytorch基础知识,记录学习的过程
主要参考DEEP LEARNING WITH PYTORCH: A 60 MINUTE BLITZ,再加上一些numpy的基础知识,这算是一篇对pytorch和numpy的基础知识的总结吧,共分5个方面,这里主要记录1,2部分的学习,英语较好的同学可以直接看参考[1]。
在这里插入图片描述
pytorch是基于python的一个科学计算包,主要面向两类同学:
(1)替代numpy从而可以使用GPU来进行计算
(2)作为深度学习平台,提供开发灵活性和速度
1.pytorch基础运算
生成torch数组
torch类型,维度
torch运算(加减)

from __future__ import print_function
import torch
import numpy as np
x_zero = torch.zeros(5, 3, dtype = torch.long)
x_rand2 = torch.rand(4, 5, dtype = torch.double)
x_rand3 = torch.rand(4, 5, dtype = torch.float)
x_rand_add1 = x_rand1 + x_rand3 #同类型才能相加
x_rand_add2 = torch.add(x_rand1, x_rand3)   #torch提供的加法
print(x_rand1.size())           #torch.Size([4, 5])

(2)resize/reshape tensor
进行分类时,需将tensor转成一维,这里可以用view来进行reshape

x_rand = torch.rand(4, 4)
x_flat1 = x_rand.view(-1, 16)
x_flat2 = x_rand.view(-1, 8)
x_flat3 = x_rand.view(16)
print(x_rand, x_flat1, x_flat2, x_flat3)
print(x_rand.size(), x_flat1.size(), x_flat2.size(), x_flat3.size())

(3)与numpy相互转换
使用.item()获取只有一个元素的tensor的数值,这个经常用来打印当前的loss值
tensor转numpy可使用.numpy()来实现,numpy转tensor使用torch.from_numpy(np_array)来实现

x_rand = torch.rand(1)
x_val = x_rand.item()               #.item() to get the value as a Python number
print(x_rand, x_val)
x_rand1 = torch.rand(4, 5)
x_rand_val = x_rand1.numpy()        #tensor转numpy
x_numpy = np.random.rand(4, 5)      #create rand numpy array
print(x_numpy, type(x_numpy))
x_rand2 = torch.from_numpy(x_numpy) #numpy转tensor
print(x_rand1, x_rand_val, x_rand2)

(4)梯度更新

print("----test4 automatic differentiation-------")
x = torch.ones(2, 2, requires_grad=True)
print(x)
y = x + 2
print(y)
z = y * y * 3
out = z.mean()
print(z, out)
out.backward()
print(x.grad)

f = 3 * (x + 2)^2 / 4,在x = 1处的导数为4.5
在这里插入图片描述

from __future__ import print_function
import torch
import numpy as np

#test1
print("---------------test1 start------------\n")
x = torch.empty(4, 5)           #产生4x5的空矩阵
x_rand1 = torch.rand(4, 5)      #默认是torch.float型
x_zero = torch.zeros(5, 3, dtype = torch.long)
x_rand2 = torch.rand(4, 5, dtype = torch.double)
x_rand3 = torch.rand(4, 5, dtype = torch.float)
x_rand_add1 = x_rand1 + x_rand3 #同类型才能相加
x_rand_add2 = torch.add(x_rand1, x_rand3)   #torch提供的加法
print(x_rand1.size())           #torch.Size([4, 5])
print(x_rand_add1, x_rand_add1.type())
print(x_rand_add2, type(x_rand_add2))
print(x_rand1.type(), x_rand2.type(), x_rand3.type())

x1 = torch.tensor([5.5, 3])
x2 = np.array([5.5, 3])
print(x1)
print(type(x1), type(x2))
print(x_rand_add1[:, 2], x_rand_add1[:, 2].size())  #借numpy一样索引
print("---------------test1 end--------------\n")

#test2:.view
print("---------------test2 start-------------\n")
x_rand = torch.rand(4, 4)
x_flat1 = x_rand.view(-1, 16)
x_flat2 = x_rand.view(-1, 8)
x_flat3 = x_rand.view(16)
print(x_rand, x_flat1, x_flat2, x_flat3)
print(x_rand.size(), x_flat1.size(), x_flat2.size(), x_flat3.size())
print("---------------test2 end-------------\n")

#test3:.item() to get the value as a Python number
print("---------------test3 start-------------\n")
x_rand = torch.rand(1)
x_val = x_rand.item()               #.item() to get the value as a Python number
print(x_rand, x_val)
x_rand1 = torch.rand(4, 5)
x_rand_val = x_rand1.numpy()        #tensor转numpy
x_numpy = np.random.rand(4, 5)      #create rand numpy array
print(x_numpy, type(x_numpy))
x_rand2 = torch.from_numpy(x_numpy) #numpy转tensor
print(x_rand1, x_rand_val, x_rand2)
print("---------------test3 end-------------\n")


#AUTOGRAD: AUTOMATIC DIFFERENTIATION
print("----test4 automatic differentiation-------")
x = torch.ones(2, 2, requires_grad=True)
print(x)
y = x + 2
print(y)
z = y * y * 3
out = z.mean()
print(z, out)
out.backward()
print(x.grad)

x1 = torch.rand(3, requires_grad=False)
x2 = torch.randn(3, requires_grad=True)
print(x1, x2)
x1_numpy = x1.numpy()
x1_norm = np.sqrt(np.sum(np.square(x1_numpy)))
print(x1_norm)
print(x1.data.norm())

references
[1] https://pytorch.org/tutorials/beginner/blitz/tensor_tutorial.html#sphx-glr-beginner-blitz-tensor-tutorial-py

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值