PyTorch学习—23.PyTorch的基本使用

本文详细介绍了PyTorch的基本语法和常用功能,包括张量操作、变量、CUDA语义、梯度计算以及nn.Module的使用。讲解了线性层、激活函数、卷积2D层,以及Sequential模块的构建。还涵盖了优化器torch.optim的使用,并通过MNIST数据集实战展示了训练过程。
摘要由CSDN通过智能技术生成

一、 Basic Grammar and common functions

导入包

import torch
import torch.nn as nn
import torch.nn.functional as F
from  torch.autograd import Variable ## Variable概念在新版本已融入Tensor
import numpy as np

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
1.Tensors and relation to numpy
# 创建np array 和 创建 tensor 的方法 类似
x_numpy = np.array([0.1, 0.2, 0.3])
x_torch = torch.tensor([0.1, 0.2, 0.3])
print('x_numpy, x_torch')
print(x_numpy, x_torch)
print()

# Tensor 和 numpy 的转换
print('to and from numpy and pytorch')
print(torch.from_numpy(x_numpy), x_torch.numpy())
print()

# numpy 和 Tensor都可以做 +-*%
y_numpy = np.array([3,4,5.])
y_torch = torch.tensor([3,4,5.])
print("x+y")
print(x_numpy + y_numpy, x_torch + y_torch)
print()

# np 中的方法, pytorch中很多都有. 例如norm ,二阶范数
# 但有部分高级的矩阵计算Tensor并不支持,比如计算特征值特征向量等。
# 因此numpy还是有存在的必要的。
print("norm")
print(np.linalg.norm(x_numpy), torch.norm(x_torch))
print()

# to apply an operation along a dimension,
# we use the dim keyword argument instead of axis
print("mean along the 0th dimension")
x_numpy = np.array([[1,2],[3,4.]])
x_torch = torch.tensor([[1,2],[3,4.]])
print(np.mean(x_numpy, axis=0), torch.mean(x_torch, dim=0)) # dim 3 : [Batch, length, hidden_size]
x_numpy, x_torch
[0.1 0.2 0.3] tensor([0.1000, 0.2000, 0.3000])

to and from numpy and pytorch
tensor([0.1000, 0.2000, 0.3000], dtype=torch.float64) [0.1 0.2 0.3]

x+y
[3.1 4.2 5.3] tensor([3.1000, 4.2000, 5.3000])

norm
0.37416573867739417 tensor(0.3742)

mean along the 0th dimension
[2. 3.] tensor([2., 3.])
2. Tensor.view( )

Reshape the tensor which similar to numpy.shape()

N, C, W, H = 10000, 3, 28, 28
X = torch.randn((N, C, W, H))
 
Y =torch.randn((1,1,1))
# print(Y.shape)

print(28*28)
# print(X.shape)
print(X.view(N, C, 784).shape) # [10000,3,28,28]  -> [10000,3,784]
print(X.view(-1, C, 392).shape) # automatically choose the 0th dimension
784
torch.Size([10000, 3, 784])
torch.Size([20000, 3, 392])
3.Tensor.squeeze( ) / Tensor.unsqueeze( )

维度的变化(增维或者减维)

print(X.size())
X = X.unsqueeze(0)
print(X.size())
X = X.squeeze(0)
print(X.size())
torch.Size([10000, 3, 28, 28])
torch.Size([1, 10000, 3, 28, 28])
torch.Size([10000, 3, 28, 28])
4.Variable( 过去式)

Tensor是Pytorch中非常高效数据格式,但用曾经的 tensor构建神经网络还远远不够,为了构建计算图,所以Variable(以前的数据形式)是不可或缺的数据形式。Variable是对tensor的封装。 Variable有三个属性:

  • .data:tensor本身
  • .grad:对应tensor的梯度
  • .grad_fn:该Variable是通过什么方式获得的
c = Variable(torch.tensor(2.0),requires_grad =True)
a = torch.tensor(2.0, requires_grad=True) # we set requires_grad=True to let PyTorch know to keep the graph
b = torch.tensor(1.0, requires_grad=True)
c = a + b
d = b + 1
e = c * d
print('c', c)
print('d', d)
print('e', e)
c tensor(3., grad_fn=<AddBackward0>)
d tensor(2., grad_fn=<AddBackward0>)
e tensor(6., grad_fn=<MulBackward0>)
5.CUDA SEMANTICS
cpu = torch.device("cpu")
gpu = torch.device("cuda") # ...我的电脑没装cuda

x = torch.rand(10)
print(x)
x = x.to(gpu)
print(x)
x = x.to(cpu)
print(x)
tensor([0.1245, 0.2713, 0.5397, 0.3965, 0.9127, 0.1660, 0.2510, 0.9544, 0.5412,
        0.8402])
tensor([0.1245, 0.2713, 0.5397, 0.3965, 0.9127, 0.1660, 0.2510, 0.9544, 0.5412,
        0.8402], device='cuda:0')
tensor([0.1245, 0.2713, 0.5397, 0.3965, 0.9127, 0.1660, 0.2510, 0.9544, 0.5412,
        0.8402])
6.Gradient
def f(x):
    return (x-2)**2

def fp(x):
    return 2*(x-2)

x = torch.tensor([1.0], requires_grad=True)

y = f(x)
print(y)
y.backward()

print('Analytical f\'(x):', fp(x))
print('PyTorch\'s f\'(x):', x.grad)
tensor([1.], grad_fn=<PowBackward0>)
Analytical f'(x): tensor([-2.], grad_fn=<MulBackward0>)
PyTorch's f'(x): tensor([-2.])
7.Using Gradient

使用梯度反向传播进行梯度下降

x = torch.tensor([5.0], requires_grad=True)
step_size = 0.25

print(x.data)
print(x)

print('iter,\tx,\tf(x),\tf\'(x),\tf\'(x) pytorch')
for i in range(15):
    y = f(x) # loss function
    y.backward() # compute the gradient
    print('{},\t{:.3f},\t{:.3f},\t{:.3f},\t{:.3f}'.format(i, x.item(), f(x).item(), fp(x).item(), x.grad.item()))
    x = x.data - step_size * x.grad # perform a GD update step
    # We need to zero the grad variable since the backward()
    # call accumulates the gradients in .grad instead of overwriting.
    # The detach_() is for efficiency. You do not need to worry too much about it.
    x.grad.detach_()
    # 不清零会造成梯度累加
    x.grad.zero_() 

当我们训练网络的时候可能希望保持一部分的网络参数不变,只对其中一部分的参数进行调整;或者值训练部分分支网络,并不让其梯度对主网络的梯度造成影响。这时候我们就需要使用detach()函数来切断一些分支的反向传播。

tensor([5.])
tensor([5.], requires_grad=True)
iter,	x,	f(x),	f'(x),	f'(x) pytorch
0,	5.000,	9.000,	6.000,	6.000

二、torch.nn.Module

  Module是PyTorch对张量进行运算的方法。模块被实现为torch.nn.Module类的子类。所有模块都是可调用的,可以组合在一起创建复杂的函数。

1.nn.Linear( )
d_in = 3
d_out = 4
linear_module = nn.Linear(d_in, d_out)  # 3*4 

example_tensor = torch.tensor([[1,2,3], [
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值