第一種神經網絡的寫法
假設這裏有一個二分類問題:
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import seaborn as sns
n_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)
)
下面是訓練過程:
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 Data
BATCH_SIZE = 5
x = 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.]