类似numpy,pytorch就是在神经网络领域代替numpy的模块
神经网络在做什么?
pytorch类似tensorflow使用tensor表示高维信息
或者看pytorch官方文档
官网命令安装了两个东西
可以进行一些矩阵相关的运算
另外参考pytorch官方网站文档
线性方程
y = Wx
非线性方程
y = AF(Wx) ---掰弯了233
激励函数必须使可微分的,在反向传播back propagation的时候,只有可微分的才可以把误差传递回去
需要注意的是,如果隐藏层比较少,可以随便选择激活函数,在隐藏层比较多的时候,需谨慎考虑,存在梯度爆炸和梯度消失的问题
默认首选
少量隐藏层中可以尝试各种
CNN中推荐relu
RNN 推荐relu 和 tanh
学习资料:
见莫烦python的介绍激励函数视频
几种激活函数图像
另外softmax是一种做出来是概率图,不是线图
关于图像可以看莫烦python教程关于matplotlib,numpy以及pandas的介绍
回归:损失函数一般使用torch.nn.MSELoss()
import torch
import matplotlib.pyplot as plt
x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1) # x data (tensor), shape=(100, 1)
y = x.pow(2) + 0.2*torch.rand(x.size()) # noisy y data (tensor), shape=(100, 1)
# 画图
plt.scatter(x.data.numpy(), y.data.numpy())
plt.show()
分类:损失函数一般使用torch.nn.CrossEntropyLoss()
:比如类别[0,0,1] 计算出的概率[0.1,0.2,0.7]
主要直接采用torch.nn
提供的类来搭建-->对比之前使用import torch.nn.functional as F
之后使用一系列函数如relu()
使用import torch.nn.functional as F
搭建
import torch
import torch.nn.functional as F
class Net(torch.nn.Module):
def __init__(self, n_feature, n_hidden, n_output):
super(Net, self).__init__()
self.hidden = torch.nn.Linear(n_feature, n_hidden)
self.predict = torch.nn,Linear(n_hidden, n_output)
def forward(self, x):
x = F.relu(self.hidden(x))
x = self.predict(x)
return x
使用torch.nn
提供的类搭建
import torch
net = torch.nn.Sequential(
torch.nn.Linear(2,10)
torch.nn.ReLU()
torch.nn.Linear(10,2),
)
主要有两种方式:
保存整个网络
torch.save(net1, ‘net.pkl‘)
只保存网络中的参数(速度快,占内存少)
torch.save(net1.state_dict, ‘net_params.pkl‘)
原文:https://www.cnblogs.com/lonelyisland/p/13085234.html