假设这里有一个二分类问题:
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import seaborn as snsn_data = torch.ones(100,2)
x1 = torch.normal(n_data*2,1)
y1 = torch.ones(100).unsqueeze(1)x2 = torch.normal(-2*n_data,1)
y2 = torch.zeros(100).unsqueeze(1)print(x1.shape,y1.shape) # torch.Size([100, 2]) torch.Size([100, 1])x = torch.cat((x1,x2),0)
y = torch.cat((y1,y2),0)
x, y = Variable(x),Variable(y)
我们画一下图:
# 我们大致画一下图,画图的时候需要将Variable类型转为numpy
import pandas as pd
sns.set()
x_plot = x.data.numpy()[:,0]
y_plot = x.data.numpy()[:,1]
special = y.data.numpy()[:,0]pdata = {"x_plot":x_plot, "y_plot":y_plot,"special":special}
df = pd.DataFrame(pdata)
sns.relplot(x="x_plot", y="y_plot", hue="special",data=df)
下面是定义神经网络:
class Net(nn.Module):def __init__(self):super(Net,self).__init__()self.hidden = nn.Linear(2,10)self.predict = nn.Linear(10,2)def forward(self,x):x = self.hidden(x)x = F.relu(x)x = self.predict(x)return x
net = Net()
print(net)
-------------------------result-----------------------------
Net((hidden): Linear(in_features=2, out_features=10, bias=True)(predict): Linear(in_features=10, out_features=2, bias=True)
)
torch教程,下面是训练过程:
optimizer = torch.optim.SGD(net.parameters(),lr = 0.01)
loss_func = nn.CrossEntropyLoss() # 专门训练多分类问题的
for i in range(500): # 训练500次out = net(x)loss = loss_func(out,y.long().squeeze()) # 这里的y必须转成squeeze,因为200*1 和 200 是不同的optimizer.zero_grad()loss.backward()optimizer.step()if i % 5 == 0:y_pre = torch.max(F.softmax(out),1)[1].squeeze() # F.softmax,转成(0.1,0.9)这种样子的分类,而max返回最大值,[1] 返回最大值的下标,也就是预测出[0.3,0.7],返回0.7的下标1,这样就转化成了一个0,1分类问题sums = sum(y_pre == y.data.squeeze())accu = sums.numpy()/y.shape[0] # 这里亲测,不转成numpy相除,会有问题print("准确率:",accu)
上面的神经网络是通过定义一个类这种形式,下面换一种新的形式来写神经网络:
net2 = torch.nn.Sequential(torch.nn.Linear(2,10),torch.nn.ReLU(),torch.nn.Linear(10,2)
)
print(net2)
------------result-------------
Sequential((0): Linear(in_features=2, out_features=10, bias=True)(1): ReLU()(2): Linear(in_features=10, out_features=2, bias=True)
) 输入为2个chanel,隐藏层为10,输出层为2
-------------------------------
除了定义不一样之外,其他的用法和上面的无异
我们就拿上面训练好的神经网络net1,为例子:
torch.save(net1,"net1.pkl") # 它保存的是整个神经网络
torch.save(net1.state_dict(),"net1_paras.pkl") # 保存的是整个神经网络的参数,这个比上面那种保存方式快那么一点点
下面实现一下简单的分批:
import torch
import torch.utils.data as DataBATCH_SIZE = 5x = torch.linspace(1,10,10)
y = torch.linspace(10,1,10)torch_dataset = Data.TensorDataset(x,y)
loader = Data.DataLoader(dataset=torch_dataset,batch_size=BATCH_SIZE,shuffle=True,
)
for epoch in range(3):for step, (batch_x,batch_y) in enumerate(loader):print(epoch,step,batch_x.numpy(),batch_y.numpy())
------------------------result--------------------------------
0 0 [ 5. 3. 4. 6. 10.] [6. 8. 7. 5. 1.]
0 1 [8. 1. 9. 2. 7.] [ 3. 10. 2. 9. 4.]
1 0 [1. 8. 3. 2. 6.] [10. 3. 8. 9. 5.]
1 1 [ 4. 9. 7. 5. 10.] [7. 2. 4. 6. 1.]
2 0 [4. 2. 7. 5. 3.] [7. 9. 4. 6. 8.]
2 1 [ 8. 6. 9. 1. 10.] [ 3. 5. 2. 10. 1.]
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态