mofanpy-Pytorch神经网络学习笔记(一):基础知识

Pytorch神经网络基础

1 用 Numpy 还是 Torch

Torch 自称为神经网络界的 Numpy, 因为他能将 torch 产生的 tensor 放在 GPU 中加速运算 (前提是你有合适的 GPU), 就像 Numpy 会把 array 放在 CPU 中加速运算.

Tensor译为张量,表示多维矩阵,可以把tensor理解成numpy中的array。在神经网络中, 当然是用 Torch 的 tensor 形式数据最好咯. 就像 Tensorflow 当中的 tensor 一样.

当然, 我们对 Numpy 还是爱不释手的, 因为我们太习惯 numpy 的形式了. 不过 torch 看出来我们的喜爱, 他把 torch 做的和 numpy 能很好的兼容. 比如这样就能自由地转换 numpy array 和 torch tensor 了:

import torch
import numpy as np

# numpy创建array
np_data = np.arange(6).reshape((2, 3))
# numpy-->torch
torch_data = torch.from_numpy(np_data)
# torch--->numpy
tensor2array = torch_data.numpy()

print(
    '\nnumpy array:\n', np_data,          
    '\ntorch tensor:\n', torch_data,      
    '\ntensor to array:\n', tensor2array, 
)
numpy array:
 [[0 1 2]
 [3 4 5]] 
torch tensor:
 tensor([[0, 1, 2],
        [3, 4, 5]], dtype=torch.int32) 
tensor to array:
 [[0 1 2]
 [3 4 5]]

torch中的数学运算

  • 简单的计算
# abs 绝对值计算
data = [-1, -2, 1, 2]
tensor = torch.FloatTensor(data)  # 转换成32位浮点 tensor
print(
    '\nabs',
    '\nnumpy: ', np.abs(data),          # [1 2 1 2]
    '\ntorch: ', torch.abs(tensor)      # [1 2 1 2]
)

# sin   三角函数 sin
print(
    '\nsin',
    '\nnumpy: ', np.sin(data),      # [-0.84147098 -0.90929743  0.84147098  0.90929743]
    '\ntorch: ', torch.sin(tensor)  # [-0.8415 -0.9093  0.8415  0.9093]
)

# mean  均值
print(
    '\nmean',
    '\nnumpy: ', np.mean(data),         # 0.0
    '\ntorch: ', torch.mean(tensor)     # 0.0
)
abs 
numpy:  [1 2 1 2] 
torch:  tensor([1., 2., 1., 2.])

sin 
numpy:  [-0.84147098 -0.90929743  0.84147098  0.90929743] 
torch:  tensor([-0.8415, -0.9093,  0.8415,  0.9093])

mean 
numpy:  0.0 
torch:  tensor(0.)

tips:tensor的数据类型
* torch.FloatTensor():用于生成数据类型为浮点型的Tensor;
* torch.IntTensor:用于生成数据类型为整形的Tensor;
* torch.rand:用于生成数据类型为浮点型且维度指定的随机Tensor,随机生成的浮点数据在0-1区间均匀分布;
* torch.randn:用于生成数据类型为浮点型且维度指定的随机Tensor,随机生成的浮点数的取值满足均值为0,方差为1的正态分布;
* torch.range:用于生成数据类型为浮点型且自定义起始范围和结束范围的Tensor,传递给torch.range的参数有三个,分别是范围的起始值,范围的结束值和步长,其中,步长用于指定从起始值到结束值得每步得数据间隔。

  • 矩阵运算
# matrix multiplication 矩阵点乘
data = [[1,2], [3,4]]
tensor = torch.FloatTensor(data)  # 转换成32位浮点 tensor
# correct method
print(
    '\nmatrix multiplication (matmul)',
    '\nnumpy:\n ', np.matmul(data, data),     # [[7, 10], [15, 22]]
    '\ntorch: \n', torch.mm(tensor, tensor)   # [[7, 10], [15, 22]]
)
matrix multiplication (matmul) 
numpy:
  [[ 7 10]
 [15 22]] 
torch: 
 tensor([[ 7., 10.],
        [15., 22.]])

2 变量 (Variable)

(1) 创建变量

import torch
from torch.autograd import Variable # 记得import torch 中 Variable 模块

# 创建一个tensor
tensor = torch.FloatTensor([[1,2],[3,4]])
# 创建一个变量,把tensor赋值进去
variable = Variable(tensor, requires_grad=True)

print(tensor)
print(variable)
tensor([[1., 2.],
        [3., 4.]])
tensor([[1., 2.],
        [3., 4.]], requires_grad=True)

tips:Variable VS Tensor
Variable 计算时, 它在背景幕布后面一步步默默地搭建着一个庞大的系统, 叫做计算图, computational graph. 这个图是将所有的计算步骤 (节点) 都连接起来, 最后进行误差反向传递的时候, 一次性将所有 variable 里面的修改幅度 (梯度) 都计算出来, 而 tensor 就没有这个能力.

(2) 计算梯度

t_out = torch.mean(tensor*tensor)       
v_out = torch.mean(variable*variable)   # mean是求均值
print("variable:",variable)
print('v-out:',v_out)   

# 模拟 v_out 的误差反向传递
v_out.backward()    
print("variable.grad:",variable.grad)    # 初始 Variable 的梯度
variable: tensor([[1., 2.],
        [3., 4.]], requires_grad=True)
v-out: tensor(7.5000, grad_fn=<MeanBackward0>)
variable.grad: tensor([[0.5000, 1.0000],
        [1.5000, 2.0000]])

tips:怎么算出来的呢?
在这里插入图片描述

(3) 获取变量的数据

print(variable)     #  Variable 形式
print(variable.data)    # tensor 形式
print(variable.data.numpy())    # numpy 形式
tensor([[1., 2.],
        [3., 4.]], requires_grad=True)
tensor([[1., 2.],
        [3., 4.]])
[[1. 2.]
 [3. 4.]]

第一种直接输出Variale形式的数据,很多情况下直接用不了(比如想用plt画图);可以转化成tensor、numpy形式输出。

3 激励函数(Activation Function)

激励函数是为了解决我们日常生活中不能用线性方程所概括的问题:y=Wx----->y=AF(Wx),把线性变成非线性,这里的AF()就是激励函数,实际就是一个非线性函数,比如说relu, sigmoid, tanh,你甚至可以创造自己的激励函数来处理自己的问题, 不过要确保的是这些激励函数必须是可微分的, 因为在 backpropagation 误差反向传递的时候, 只有这些可微分的激励函数才能把误差传递回去.

在卷积神经网络 Convolutional neural networks 的卷积层中, 推荐的激励函数是 relu. 在循环神经网络中 recurrent neural networks, 推荐的是 tanh 或者是 relu.

(1) torch中的激励函数

import torch
import torch.nn.functional as F   # 激励函数都在这
from torch.autograd import Variable
import matplotlib.pyplot as plt

# 做一些假数据来观看图像
x = torch.linspace(-5, 5, 200)  # x data (tensor), shape=(100, 1)
x = Variable(x)
x_np = x.data.numpy()   # 换成 numpy array, 出图时用

# 几种常用的 激励函数
y_relu = torch.relu(x).data.numpy()
y_sigmoid = torch.sigmoid(x).data.numpy()
y_tanh = torch.tanh(x).data.numpy()
y_softplus = F.softplus(x).data.numpy() # there's no softplus in torch
# y_softmax = torch.softmax(x, dim=0).data.numpy() softmax 比较特殊, 不能直接显示, 不过他是关于概率的, 用于分类
  • 记得import torch.nn.functional,激励函数都在这儿
  • Torch 中的激励函数有很多, 不过我们平时要用到的就这几个. relu, sigmoid, tanh, softplus.

(2) 可视化激励函数

import matplotlib.pyplot as plt  # python 的可视化模块, 莫烦教程 (https://mofanpy.com/tutorials/data-manipulation/plt/)

plt.figure(1, figsize=(8, 6))
plt.subplot(221)
plt.plot(x_np, y_relu, c='red', label='relu')
plt.ylim((-1, 5))
plt.legend(loc='best')

plt.subplot(222)
plt.plot(x_np, y_sigmoid, c='red', label='sigmoid')
plt.ylim((-0.2, 1.2))
plt.legend(loc='best')

plt.subplot(223)
plt.plot(x_np, y_tanh, c='red', label='tanh')
plt.ylim((-1.2, 1.2))
plt.legend(loc='best')

plt.subplot(224)
plt.plot(x_np, y_softplus, c='red', label='softplus')
plt.ylim((-0.2, 6))
plt.legend(loc='best')

plt.show()

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值