pytorch笔记

中文版官方教程:http://pytorch123.com/

1.一篇总结很好的文章


pytorch学习:https://blog.csdn.net/lantuxin/article/details/87709344

2.查看类型、大小

实例:

import torch

a=torch.tensor([1,2,3],dtype=torch.float32)

print(a)

print(a.type())#查看类型

print(type(a))#查看类型

#size等价于shape

print(a.shape)#查看大小

print(a.size())#查看大小

打印结果:

tensor([1., 2., 3.])

torch.FloatTensor

<class 'torch.Tensor'>

torch.Size([3])

torch.Size([3])

3.转换Torch tensor到numpy

实例:

import numpy as np

import torch

a=torch.tensor([1,2,3],dtype=torch.float32)

print(a)

print(a.type())#查看类型

b=a.numpy()

print(b)

print(type(b))

打印结果:

tensor([1., 2., 3.])

torch.FloatTensor

[1. 2. 3.]

<class 'numpy.ndarray'>

4.转换numpy到Torch tensor

实例:

import numpy as np

import torch

c=np.array([[1,2]])

print(c)

a=torch.from_numpy(c)

print(a)

print(torch.tensor(c))

打印结果:

[[1 2]]

tensor([[1, 2]], dtype=torch.int32)

tensor([[1, 2]], dtype=torch.int32)

5.转置

实例:

import torch

c=torch.tensor([[1,2]])

print(c)

print(c.t())

打印结果:

tensor([[1, 2]])

tensor([[1],

        [2]])

6.dot()

错误1:RuntimeError: dot: Expected 1-D argument self, but got 2-D

    新版本中(>=0.3.0), 关于 tensor.dot() 有了新的改变, 它只能针对于一维的数组. 所以上面的有所改变.输入1维数组就ok

实例:

import torch

a=torch.tensor([[1,2,3],[0,3,6]])

print(a)

b=torch.tensor([[2,1,2],[1,2,3]])

print(b)

c=torch.dot(a.flatten(),b.flatten())

print(c)

打印结果:

tensor([[1, 2, 3],

        [0, 3, 6]])

tensor([[2, 1, 2],

        [1, 2, 3]])

tensor(34)

7.矩阵乘法:@、mm

实例:

import torch

a=torch.tensor([[1,2,3],[0,3,6]])

b=torch.tensor([[2,1,2],[1,2,3]])

print(a@b.t())

print(torch.mm(a,b.t()))

打印结果:

tensor([[10, 14],

        [15, 24]])

tensor([[10, 14],

        [15, 24]])

8.where

实例:

import torch

a=torch.tensor([[1,2,3],[0,3,6]])

a=torch.where(a>0,torch.tensor(0),torch.tensor(-1))

print(a)

打印结果:

tensor([[ 0, 0, 0],

        [-1,  0,  0]])

9.查看系统是否支持CHDA

实例:

import torch

print(torch.cuda.is_available())

打印结果:

True(我自己电脑上装有GPU显卡)

10.使用GPU和CPU计算

很好的原文:https://blog.csdn.net/qq_21578849/article/details/85240797

(1)模型转为cuda

gpus = [0] #使用哪几个GPU进行训练,这里选择0号GPU

cuda_gpu = torch.cuda.is_available()  #判断GPU是否存在可用

net = Net(12288, 25, 16, 6)

if(cuda_gpu):

    net = torch.nn.DataParallel(net, device_ids=gpus).cuda()  #将模型转为cuda类型

(2)数据转化为cuda

(minibatchX, minibatchY) = minibatch

minibatchX = minibatchX.astype(np.float32).T

minibatchY = minibatchY.astype(np.float32).T

if(cuda_gpu):

    b_x = Variable(torch.from_numpy(minibatchX).cuda())    #将数据转为cuda类型

    b_y = Variable(torch.from_numpy(minibatchY).cuda())

else:

    b_x = Variable(torch.from_numpy(minibatchX))

    b_y = Variable(torch.from_numpy(minibatchY))

(3)输出数据去cuda,转为numpy

correct_prediction = sum(torch.max(output, 1)[1].data.squeeze() == torch.max(b_y, 1)[1].data.squeeze())

if(cuda_gpu):

    correct_prediction = correct_prediction.cpu().numpy()  #.cpu将cuda转为tensor类型,.numpy将tensor转为numpy类型

else:

    correct_prediction = correct_prediction.numpy()

(4)附加

实例:

import torch

a=torch.tensor([[1,2,3],[0,3,6]])

b=torch.tensor([[2,1,2],[1,2,3]])

print(torch.cuda.is_available())

if torch.cuda.is_available():

a=a.cuda()

b=b.cuda()

else:

a=a.cpu()

b=b.cpu()

print(a+b)#GPU

打印结果:

True(我自己电脑上有GPU)

tensor([[3, 3, 5],

        [1, 5, 9]], device='cuda:0')

11.一个简单的梯度下降实例

(1)~(4):预备工作;(5):小案例

(1)zip

    使用zip()函数来可以把列表合并,并创建一个元组对的列表

    好文链接:https://www.cnblogs.com/wdz1226/p/10181354.html

附加实例:

x=[1,2,3]

y=[4,5,6]

print(zip(x,y))

for i in zip(x,y):

print(i)

打印结果:

<zip object at 0x000002A45F847888>

(1, 4)

(2, 5)

(3, 6)

(2)grad_fn

原文链接:https://blog.csdn.net/duanmuji/article/details/85217338

Varibale包含三个属性:

data:存储了Tensor,是本体的数据

grad:保存了data的梯度,本事是个Variable而非Tensor,与data形状一致

grad_fn:指向Function对象,用于反向传播的梯度计算之用

(3)mean

原文链接:https://blog.csdn.net/sinat_40624829/article/details/91127373

torch.mean(input) 输出input 各个元素的的均值,不指定任何参数就是所有元素的算术平均值,指定参数可以计算每一行或者 每一列的算术平均数

(4)backward()

pytorch中 backward 机制理解:https://blog.csdn.net/baidu_36161077/article/details/81435627

(5)小案例

a)案例1

import torch

from torch.autogradimport Variable

x=Variable(torch.tensor([1.,2,3]),requires_grad=True)#使用Variable()封装变量

z=x+3

y=2*z

print(z)

print(y)

print(y.grad_fn)#每一步的操作。pyThorch根据每一步的操作计算导数。

#backward只能被应用在一个标量上,也就是一个一维tensor,或者传入跟变量相关的梯度。

y=y.mean()#变成标量。

y.backward()

#dx=1/3*2

print(x.grad)#打印导数。对标量求导。

print(x.data)

b=torch.tensor([2.],requires_grad=True)#实际中,在后期版本,将Variable和tensor的东西合并了,不需要使用Variable()封装变量。

b.backward()

print(b.grad )

打印结果:

tensor([4., 5., 6.], grad_fn=<AddBackward0>)

tensor([ 8., 10., 12.], grad_fn=<MulBackward0>)

<MulBackward0 object at 0x0000024AAC36B080>

tensor([0.6667, 0.6667, 0.6667])

tensor([1., 2., 3.])

tensor([1.])

b)案例2

import random

_x=[i/100 for i in range(100)]#i/100:归一化

print(_x)

_y=[3*e+4 for e in _x]

print(_y)

#随机给w、b

w=random.random()

b=random.random()

for i in range(100):

    for x,yin zip(_x,_y):

    #前向传播

        z=w*x+b

        o=2*z-y

        loss=o**2

        #反向求导

        dw=-2*o*x

        db=-2*o

#梯度更新

        w=w+0.1*dw#0.1:步长、学习率

        b=b+0.1*db

print(w,b,loss)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。