Pytorch框架学习 -1 pytorch入门

Pytorch Learning -1 pytorch入门

安装

  1. 安装CUDA10.2

    1. cuda10.2 安装地址
    2. 根据自己系统选择版本(我这边win10)
    3. 一路next
  2. 安装miniconda

    1. miniconda 镜像地址

    2. 配置conda代理

      conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
      conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
      conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda
      conda config --set show_channel_urls yes
      
  3. 安装pytorch

    1. pytorch 官网,选择版本后会有适合你的链接

    2. 在miniconda环境中使用:

      conda install pytorch torchvision cudatoolkit=10.2 -c pytorch
      

优化算法

  1. 梯度下降

  2. 随机梯度下降

  3. ADAM

使用numpy实现一个多元线性回归

import numpy as np


def compute_error_linear_regression(b, w, x, y):
    mse = 0
    for i in range(len(x)):
        mse = mse + (y[i] - (w * x[i] + b)) ** 2
    return mse


def gradient(b, w, x, y, alpha):
    gb, gw, n = 0, 0, len(x)
    for i in range(len(x)):
        gb += -(2 / n) * (y[i] - (w * x[i] + b))
        gw += -(2 / n) * x[i] * (y[i] - (w * x[i] + b))
    # print('gradient b w', gb, gw)
    return b - alpha * gb, w - alpha * gw


def gd(x, y, b, w, alpha, it_max):
    for i in range(it_max):
        b, w = gradient(b, w, x, y, alpha)
    return b, w


def setup():
    points = np.random.random([100, 2])
    x = points[:, 0] * 10
    b = 7
    w = 5
    y = points[:, 1] + w * x + b
    pb, pw = gd(x, y, 4, 8, 0.0005, 100000)
    print(pb, pw)


if __name__ == '__main__':
    setup()

Pytorch 数据类型

Python 类型 Pytorch 类型
int IntTensor of size()
float FloatTensor of size()
int array IntTensor of size[ d 1 d_1 d1, d 2 d_2 d2,…,]
float array FloatTensorof size[ d 1 d_1 d1, d 2 d_2 d2,…]

标量

torch.tensor()

张量

torch.tensor([np.ndarray|array|pd.*]) # 从数组和numpy数组和pandas数据结构创建
torch.FloatTensor(5)			# 直接创建

随机初始化

torch.rand(int ...)
torch.randint(max, min, [int ...])
torch.randn(int ...)
torch.norm(mean=*,std=*)
torch.norm(mean=torch.full([10],0),std=torch.arange(1,0,0.1))
torch.full([shape],0)
torch.arange(begin,end,step_size)
torch.linspace(begin,end,steps=step_number)
torch.ones(3,3)
torch.zeros(3,3)
torch.eye(3,3)
torch.randperm(5) # 获取0-5不重复的随机数

索引

与numpy类似,不额外赘述

维度变换

# reshape, view
# 注意大小不变!会丢失原始的维度信息
a.reshape()
a.view()
# unsqueeze
# 插入一个维度
a.unsqueeze(index) # 在索引处插入
# squeeze
# 减少维度
a.squeeze(index)
# 维度扩展
expand(dim_1,dim_2,...) # 不主动复制   
repeat(dim_1,dim_2,...) # 主动复制    
a.t 			   # 仅限1,2维度
transpose(a,b) 		# a,b 维度上转置
contiguous()		# 参考https://www.pythonf.cn/read/139488

广播

参考

使用pytorch实现一个多元线性回归

使用pytorch的Linear线性模型:torch.nn.modules.Linear
使用pytorch内置的随机梯度下降算法

随机梯度下降:
每次只随机的选一个方向下降的梯度下降算法
主要用于大规模问题
这里由于pytorch未内置GD算法,使用类似的SGD进行替代

import torch
import torch.nn as nn

device = torch.device('cuda:0' if torch.cuda.is_available() else "cpu")


def regression(input_size, output_size, num_epochs, alpha, x_train, y_train):
    # 线性回归模型
    model = nn.Linear(input_size, output_size)
    # 模型使用GPU
    model.to(device)
    # 损失函数
    criterion = nn.MSELoss()
    # 没有梯度下降,这里使用随机梯度下降法
    optimizer = torch.optim.SGD(model.parameters(), lr=alpha)
    # 按epoch训练
    for epoch in range(num_epochs):
        # 数据转换为torch的数据类型
        # 将数据发往GPU
        inputs = torch.from_numpy(x_train).cuda(device)
        targets = torch.from_numpy(y_train).cuda(device)
        # 隐式调用模型的model.forward()方法
        outputs = model(inputs)
        # 计算本次运算的误差
        loss = criterion(outputs, targets)
        # 把梯度置零,清空上一步的残余更新参数值
        optimizer.zero_grad()
        # 反向传播
        loss.backward()
        # 更新参数
        optimizer.step()
    return model


def setup():
    input_size = 2
    output_size = 1
    epoch = 100000
    alpha = 0.005
    import numpy as np
    x = np.random.random([100, 1]) * 10
    b = 7
    w = 5
    e = np.random.random([100, 1])
    y = x * w + b + e
    model = regression(input_size, output_size, epoch, alpha, x, y)
    for name, param in model.named_parameters():
        print(name, param)

猜你喜欢

转载自blog.csdn.net/goes_on/article/details/109059149