PyTorch实战代码

线性拟合

import torch
from torch.autograd import Variable
import torch.nn.functional as F
import matplotlib.pyplot as plt

x = torch.unsqueeze(torch.linspace(-1,1,100),dim=1)
y=x.pow(2)+0.2*torch.rand(x.size())

x,y=Variable(x),Variable(y)

class Net(torch.nn.Module):
    def __init__(self,n_features,n_hidden,n_output):
        super(Net, self).__init__()
        self.hidden = torch.nn.Linear(n_features,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

net=Net(1,10,1)

plt.ion()#实时打印
plt.show()

optimizer=torch.optim.SGD(net.parameters(),lr=0.5)
loss_func=torch.nn.MSELoss()

for t in range(500):
    prediction=net(x)

    loss = loss_func(prediction,y)

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    if t%5==0:
        plt.cla()     # Clear axis即清除当前图形中的当前活动轴。其他轴不受影响。
        plt.scatter(x.data.numpy(),y.data.numpy())
        plt.plot(x.data.numpy(),prediction.data.numpy())
        plt.text(0.5,0,'Loss=%.4f'%loss.data)
        plt.pause(0.1)
plt.ioff()
plt.show()

Classification分类

import torch
import torch.nn.functional as F
from torch.autograd import Variable
import matplotlib.pyplot as plt

n_data=torch.ones(100,2)
x0=torch.normal(2*n_data,1)  #torch.normal(a,b)  表示随机数是从均值为a,标准差为b的正态分布中随机生成的
y0=torch.zeros(100)
x1=torch.normal(-2*n_data,1)
y1=torch.ones(100)
x=torch.cat((x0,x1),0).type(torch.FloatTensor)
y=torch.cat((y0,y1),).type(torch.LongTensor)

x ,y =Variable(x),Variable(y)
# plt.scatter(x.data.numpy()[:,0],x.data.numpy()[:,1],c=y.data.numpy(),s=100,lw=0)
# plt.show()

class Net(torch.nn.Module):
    def __init__(self,n_features,n_hidden,n_output):
        super(Net, self).__init__()
        self.hidden = torch.nn.Linear(n_features,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

net=Net(2,10,2)#二分类任务(one-hot编码)一个向量只有一个为1,其他为0


plt.ion()
plt.show()

optimizer=torch.optim.SGD(net.parameters(),lr=0.02)
loss_func=torch.nn.CrossEntropyLoss()

for t in range(100):
    out=net(x)

    loss=loss_func(out,y)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    if t%2==0:
        plt.cla()
        prediction=torch.max(F.softmax(out),1)[1]
        pred_y=prediction.data.numpy().squeeze()
        target_y=y.data.numpy()
        plt.scatter(x.data.numpy()[:,0],x.data.numpy()[:,1],c=pred_y,s=100,lw=0)
        accuracy=sum(pred_y == target_y)/200
        plt.text(1.5,-4,'Accuracy=%.2f'%accuracy)
        plt.pause(0.1)
plt.ioff()
plt.show()

批训练

import torch
import torch.utils.data as Data

BATCH_SIZE = 5

x = torch.linspace(1, 10, 10)       # this is x data (torch tensor)
y = torch.linspace(10, 1, 10)       # this is y data (torch tensor)

torch_dataset = Data.TensorDataset(x, y)
loader = Data.DataLoader(
    dataset=torch_dataset,      # torch TensorDataset format
    batch_size=BATCH_SIZE,      # mini batch size
    shuffle=True,               # random shuffle for training
    num_workers=2,              # subprocesses for loading data
)

def show_batch():
    for epoch in range(3):
        for step,(batch_x,batch_y) in enumerate(loader):
            #training
            print('Epoch: ', epoch, '| Step: ', step, '| batch x: ',
                  batch_x.numpy(), '| batch y: ', batch_y.numpy())

if __name__ == '__main__':#使用多批处理要有主函数
    show_batch()

加速神经网络训练

SGD 随机梯度下降
Momentum 收敛速度更快,永远向着梯度下降的方向移动
Adam 在学习率上做调整,使每个参数的更新都可以拥有自己与众不同的学习率(推荐)
RMSProp Momentum+Adam

优化器比较

import torch
import torch.utils.data as Data
import torch.nn.functional as F
from torch.autograd import Variable
import matplotlib.pyplot as plt

LR=0.01
BATCH_SIZE = 32
EPOCH = 12

x = torch.unsqueeze(torch.linspace(-1,1,1000),dim=1)
y=x.pow(2)+0.1*torch.normal(torch.zeros(*x.size()))

# plt.scatter(x.numpy(),y.numpy())
# plt.show()

torch_dataset=Data.TensorDataset(x,y)
loader=Data.DataLoader(dataset=torch_dataset,batch_size=BATCH_SIZE,shuffle=True)

class Net(torch.nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.hidden=torch.nn.Linear(1,20)
        self.predict=torch.nn.Linear(20,1)
    def forward(self, x):
        x=F.relu(self.hidden(x))
        x=self.predict(x)
        return x
net_SGD=Net()
net_Momentum=Net()
net_RMSProp=Net()
net_Adam=Net()
nets=[net_SGD,net_Momentum,net_RMSProp,net_Adam]

opt_SGD=torch.optim.SGD(net_SGD.parameters(),lr=LR)
opt_Momentum=torch.optim.SGD(net_Momentum.parameters(),lr=LR,momentum=0.8)
opt_RMSprop=torch.optim.RMSprop(net_RMSProp.parameters(),lr=LR,alpha=0.9)#alpha是平滑常数
opt_Adam=torch.optim.Adam(net_Adam.parameters(),lr=LR,betas=(0.9,0.99))# betas用于计算梯度以及梯度平方的运行平均值的系数(默认:0.9,0.999)
optimizers=[opt_SGD,opt_Momentum,opt_RMSprop,opt_Adam]

loss_func=torch.nn.MSELoss()
losses_his=[[],[],[],[]]

for epoch in range(EPOCH):
    print(epoch)
    for step,(batch_x,batch_y) in enumerate(loader):
        b_x,b_y=Variable(batch_x),Variable(batch_y)
        for net,opt,l_his in zip(nets,optimizers,losses_his):
            output=net(b_x)
            loss=loss_func(output,b_y)
            opt.zero_grad()
            loss.backward()
            opt.step()
            l_his.append(loss.data)

labels=['SGD','Momentum','RMSprop','Adam']
for i,l_his in enumerate(losses_his):
    plt.plot(l_his,label=labels[i])
plt.legend(loc='best')
plt.xlabel('Steps')
plt.ylabel('Loss')
plt.ylim((0,0.2))
plt.show()

CNN

import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.utils.data as Data
import torchvision
import matplotlib.pyplot as plt

EPOCH = 1
BATCH_SIZE = 50
LR = 0.001
DOWNLOAD_MNIST = False

train_data=torchvision.datasets.MNIST(
    root='./data',
    train=True,#trainning data
    transform=torchvision.transforms.ToTensor(),#(0,1)
    download=DOWNLOAD_MNIST,
)
# print(train_data.train_data.size())
# print(train_data.train_labels.size())
# plt.imshow(train_data.train_data[0].numpy(),cmap='gray')
# plt.title('%i'%train_data.train_labels[0])
# plt.show()

train_loader=Data.DataLoader(dataset=train_data,batch_size=BATCH_SIZE,shuffle=True,num_workers=2)
test_data=torchvision.datasets.MNIST(root='./data/',train=False)
with torch.no_grad():
    test_x=Variable(torch.unsqueeze(test_data.data,dim=1)).type(torch.FloatTensor)[0:2000]/255.#标准化
test_y=test_data.targets[:2000]

class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.conv1 = nn.Sequential(
            nn.Conv2d(
                in_channels=1,
                out_channels=16,
                kernel_size=5,
                stride=1,
                padding=2  #if stride=1,padding=(kernel_size-1)/2
            ),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2,stride=2),
        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(16,32,5,1,2),
            nn.ReLU(),
            nn.MaxPool2d(2),
        )
        self.out = nn.Linear(32*7*7,10)
    def forward(self, x):
        x=self.conv1(x)
        x=self.conv2(x)  #(batch,32,7,7)
        x=x.view(x.size(0),-1)
        output=self.out(x)
        return output
if __name__ == '__main__':

    cnn=CNN()

    optimizer=torch.optim.Adam(cnn.parameters(),lr=LR)
    loss_func=nn.CrossEntropyLoss()

    for epoch in range(EPOCH):
        for step,(x,y) in enumerate(train_loader):
            b_x,b_y=Variable(x),Variable(y)

            output=cnn(b_x)
            loss=loss_func(output,b_y)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            if step%50==0:
                test_output = cnn(test_x)
                pred_y=torch.max(test_output,1)[1].data.squeeze()
                accuracy=float((pred_y==test_y).sum())/test_y.size(0)
                print('Epoch: ',epoch,'|train loss:%.4f'%loss.data,
                      '|test accuracy: ',accuracy)
    test_output=cnn(test_x[:10])
    pred_y=torch.max(test_output,1)[1].data.numpy().squeeze()
    print(pred_y,'prediction number')
    print(test_y[:10].numpy(),'real number')

RNN

分类

import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.utils.data as Data
import torchvision
import matplotlib.pyplot as plt

EPOCH = 1
BARCH_SIZE = 64
TIME_STEP = 28  #考虑多少个时间点的数据(28*28的图片,一次读取一行)
INPUT_SIZE = 28
LR = 0.01
DOWNLOAD_MNIST = False

train_data = torchvision.datasets.MNIST(
    root = '/data',
    train = True,
    transform = torchvision.transforms.ToTensor(),
    download = DOWNLOAD_MNIST,
)
train_loader=Data.DataLoader(dataset=train_data,batch_size=BARCH_SIZE,shuffle=True)
test_data=torchvision.datasets.MNIST(root='./data/',train=False,transform=torchvision.transforms.ToTensor())
with torch.no_grad():
    test_x=Variable(torch.unsqueeze(test_data.data,dim=1)).type(torch.FloatTensor)[0:2000]/255.#标准化
test_y=test_data.targets[:2000]

class RNN(nn.Module):
    def __init__(self):
        super(RNN, self).__init__()

        self.rnn=nn.LSTM(
            input_size=INPUT_SIZE,
            hidden_size=64,
            num_layers=1,
            batch_first=True,
        )
        self.out=nn.Linear(64,10)
    def forward(self, x):
        r_out, (h_n,h_c)=self.rnn(x,None)
        out =self.out(r_out[:,-1,:])
        return out
rnn = RNN()

optimizer = torch.optim.Adam(rnn.parameters(),lr=LR)
loss_func = nn.CrossEntropyLoss()

for epoch in range(EPOCH):
    for step,(x,y) in enumerate(train_loader):
        b_x=Variable(x.view(-1,28,28))
        b_y=Variable(y)
        output=rnn(b_x)
        loss=loss_func(output,b_y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        if step % 50 == 0:
            test_output = rnn(test_x.view(-1,28,28))
            pred_y = torch.max(test_output, 1)[1].data.squeeze()
            accuracy = float((pred_y == test_y).sum()) / test_y.size(0)
            print('Epoch: ', epoch, '|train loss:%.4f' % loss.data,
                  '|test accuracy: ', accuracy)
test_output = rnn(test_x[:10].view(-1,28,28))
pred_y = torch.max(test_output, 1)[1].data.numpy().squeeze()
print(pred_y, 'prediction number')
print(test_y[:10].numpy(), 'real number')

回归

import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.utils.data as Data
import torchvision
import numpy as np
import matplotlib.pyplot as plt

torch.manual_seed(1)  #为cpu设置种子用于生成随机数
TIME_STEP = 10
INPUR_SIZE = 1
LR = 0.02


class RNN(nn.Module):
    def __init__(self):
        super(RNN, self).__init__()
        self.rnn = nn.RNN(
            input_size=1,
            hidden_size=32,
            num_layers= 1,
            batch_first=True,
        )
        self.out=nn.Linear(32,1)
    def forward(self, x ,h_state):
        r_out,h_state=self.rnn(x,h_state)#只保存最后一步的h_state相当于记忆
        outs =[]
        for time_step in range(r_out.size(1)):
            outs.append(self.out(r_out[:,time_step,:]))  #把每个时间点的time_step放到out层中过滤一次

        return torch.stack(outs,dim=1) , h_state
rnn=RNN()

optimizer = torch.optim.Adam(rnn.parameters(),lr=LR)
loss_func = nn.MSELoss()
h_state=None

plt.ion()
plt.show()

for step in range(60):
    start,end=step*np.pi , (step+1)*np.pi
    steps=np.linspace(start,end,TIME_STEP,dtype=np.float)
    x_np=np.sin(steps)
    y_np=np.cos(steps)

    x = Variable(torch.from_numpy(x_np[np.newaxis,:,np.newaxis])).float()
    y = Variable(torch.from_numpy(y_np[np.newaxis,:,np.newaxis])).float()

    prediction, h_state = rnn(x,h_state)
    h_state = Variable(h_state.data)

    loss=loss_func(prediction,y)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    if step%2==0:
        plt.cla()
        pred_y = prediction.data.numpy().squeeze().flatten()
        target_y = y.data.numpy().flatten()
        x_axis=np.arange(target_y.size)
        plt.plot(x_axis, target_y,c='green')
        plt.plot(x_axis, pred_y,c='red')
        accuracy = sum(pred_y == target_y) / len(pred_y)
        plt.text(1.5, -4, 'Accuracy=%.2f' % accuracy)
        plt.pause(0.1)
plt.ioff()
plt.show()

数据预测LSTM

建立模型

import torch.nn as nn

class LSTM(nn.Module):
    def __init__(self):
        super(LSTM, self).__init__()
        self.layer1=nn.LSTM(2,32,num_layers=2)
        self.layer2=nn.Linear(32,1)
    def forward(self, x):
        x,_=self.layer1(x)
        s,b,h=x.size()
        x=x.view(s*b,h)
        x=self.layer2(x)
        return x

创建数据

import pandas as pd
import random
import csv

data_list=pd.date_range('2017-11-15','2020-11-15')

with open('data.csv','w') as f:
    fcsv=csv.writer(f)
    for i in data_list:
        fcsv.writerow([i,random.randint(1,100)])

训练并保存数据

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

data_csv=pd.read_csv('data.csv',usecols=[1])
data=np.array(data_csv.values).reshape(-1)
data_csv = data_csv.dropna()  # 滤除缺失数据
dataset = data_csv.values   # 获得csv的值
dataset = dataset.astype('float32')
max_value = np.max(dataset)  # 获得最大值
min_value = np.min(dataset)  # 获得最小值
scalar = max_value - min_value  # 获得间隔数量
dataset = list(map(lambda x: x / scalar, dataset)) # 归一化

def create_dataset(dataset, look_back=2):
    dataX, dataY = [], []
    for i in range(len(dataset) - look_back):
        a = dataset[i:(i + look_back)]
        dataX.append(a)
        dataY.append(dataset[i + look_back])
    return np.array(dataX), np.array(dataY)

# 创建好输入输出
data_X, data_Y = create_dataset(dataset)

train_size = int(len(data_X) * 0.7)
test_size = len(data_X) - train_size
train_X = data_X[:train_size]
train_Y = data_Y[:train_size]
test_X = data_X[train_size:]
test_Y = data_Y[train_size:]

import torch
import torch.nn as nn
from LSTM_Test.LSTM import LSTM
from torch.autograd import Variable

train_X = train_X.reshape(-1, 1, 2)
train_Y = train_Y.reshape(-1, 1)
test_X = test_X.reshape(-1, 1, 2)

train_x = torch.from_numpy(train_X)
train_y = torch.from_numpy(train_Y)
test_x = torch.from_numpy(test_X)

net=LSTM()
net.load_state_dict(torch.load('lstm.pkl'))

criterion = nn.MSELoss()
optimizer=torch.optim.Adam(net.parameters(),lr=1e-3)

if torch.cuda.is_available():
    net=net.cuda()

for e in range(1000):
    if torch.cuda.is_available():
        var_x=Variable(train_x).cuda()
        var_y=Variable(train_y).cuda()
    else:
        var_x = Variable(train_x)
        var_y = Variable(train_y)

    out=net(var_x)
    loss=criterion(out,var_y)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    if (e+1)%100 ==0:
        print('Epoch:{},Loss:{:5f}'.format(e+1,loss.data))

torch.save(net.state_dict(),'lstm.pkl')

测试数据

from LSTM_Test.LSTM import LSTM
import torch
import numpy as np
import pandas as pd
from torch.autograd import Variable
import matplotlib.pyplot as plt

data_csv=pd.read_csv('data.csv',usecols=[1])
data=np.array(data_csv.values).reshape(-1)
data_csv = data_csv.dropna()  # 滤除缺失数据
dataset = data_csv.values   # 获得csv的值
dataset = dataset.astype('float32')
max_value = np.max(dataset)  # 获得最大值
min_value = np.min(dataset)  # 获得最小值
scalar = max_value - min_value  # 获得间隔数量
dataset = list(map(lambda x: x / scalar, dataset)) # 归一化

def create_dataset(dataset, look_back=2):
    dataX, dataY = [], []
    for i in range(len(dataset) - look_back):
        a = dataset[i:(i + look_back)]
        dataX.append(a)
        dataY.append(dataset[i + look_back])
    return np.array(dataX), np.array(dataY)

# 创建好输入输出
data_X, data_Y = create_dataset(dataset)


net=LSTM()
net.load_state_dict(torch.load('lstm.pkl'))

net=net.eval()
data_X=data_X.reshape(-1,1,2)
data_X=torch.from_numpy(data_X)
var_data=Variable(data_X)
pred_y=net(var_data )
pred_y=pred_y.view(-1).data.numpy()

plt.plot(pred_y, 'r', label='prediction')
plt.plot(dataset, 'b', label='real')
plt.legend(loc='best')
plt.show()

自编码

import torch.utils.data as Data
import torchvision
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np

torch.manual_seed(1)

#超参数
EPOCH = 10
BATCH_SIZE = 64
LR = 0.005
DOWNLOAD_MNIST = False
N_TEST_IMG = 5

train_data=torchvision.datasets.MNIST(
    root='/data',
    train=True,
    transform=torchvision.transforms.ToTensor(),
    download=DOWNLOAD_MNIST,
)
train_loader = Data.DataLoader(dataset=train_data,
                               batch_size=BATCH_SIZE,
                               shuffle=True)

class AutoEncoder(nn.Module):
    def __init__(self):
        super(AutoEncoder, self).__init__()
        self.encoder=nn.Sequential(
            nn.Linear(28*28,128),
            nn.Tanh(),
            nn.Linear(128,64),
            nn.Tanh(),
            nn.Linear(64,12),
            nn.Tanh(),
            nn.Linear(12,3),
        )
        self.decoder=nn.Sequential(
            nn.Linear(3,12),
            nn.Tanh(),
            nn.Linear(12,64),
            nn.Tanh(),
            nn.Linear(64,128),
            nn.Tanh(),
            nn.Linear(128,28*28),
            nn.Sigmoid(),#将输出范围控制到0-1之间
        )
    def forward(self,x):
        encoded=self.encoder(x)
        decoded=self.decoder(encoded)
        return encoded,decoded

autoencoder = AutoEncoder()
optimizer=torch.optim.Adam(autoencoder.parameters(),lr=LR)
loss_func = nn.MSELoss()

f , a =plt.subplots(2,N_TEST_IMG,figsize=(5,2))
plt.ion()
plt.show()

view_data = Variable(train_data.data[:N_TEST_IMG].view(-1,28*28).type(torch.FloatTensor))
for i in range(N_TEST_IMG):
    a[0][i].imshow(np.reshape(view_data.data.numpy()[i],(28,28)),cmap='gray')
    a[0][i].set_xticks(())
    a[0][i].set_yticks(())

for epoch in range(EPOCH):
    for step,(x,y) in enumerate(train_loader):
        b_x = Variable(x.view(-1,28*28))
        b_y = Variable(x.view(-1,28*28))
        b_label = Variable(y)

        encoded,decoded=autoencoder(b_x)

        loss = loss_func(decoded,b_y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        if step %100 ==0 :
            print('Epoch: ',epoch,'|train loss:%.4f'%loss.data)
            _,decoded_data=autoencoder(view_data)
            for i in range(N_TEST_IMG):
                a[1][i].clear()
                a[1][i].imshow(np.reshape(decoded_data.data.numpy()[i], (28, 28)), cmap='gray')
                a[1][i].set_xticks(())
                a[1][i].set_yticks(())
            plt.draw()
            plt.pause(0.5)
plt.pause(0)
plt.ioff()
plt.show()

生成对抗网络

import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np
import matplotlib.pyplot as plt

torch.manual_seed(1)
np.random.seed(1)

BATCH_SIZE = 64
LR_G = 0.0001 #生成网络的学习效率
LR_D = 0.0001 #对抗网络的学习效率
N_IDEAS = 5 #
ART_COMPONENTS = 15
PAINT_POINTS = np.vstack([np.linspace(-1,1,ART_COMPONENTS) for _ in range(BATCH_SIZE)])

# plt.plot(PAINT_POINTS[0],2*np.power(PAINT_POINTS[0],2)+1,c='#74BCFF',lw=3,label='L')
# plt.plot(PAINT_POINTS[0],1*np.power(PAINT_POINTS[0],2)+0,c='#FF9359',lw=3,label='R')
# plt.legend(loc='upper right')
# plt.show()

def artist_works():
    a=np.random.uniform(1,2,size=BATCH_SIZE)[:,np.newaxis]
    paintings = a * np.power(PAINT_POINTS, 2)+ (a - 1)
    paintings = torch.from_numpy(paintings).float()
    return Variable(paintings)

G = nn.Sequential(
    nn.Linear(N_IDEAS,128),#生成器的随机想法
    nn.ReLU(),
    nn.Linear(128,ART_COMPONENTS),
)

D = nn.Sequential(
    nn.Linear(ART_COMPONENTS,128),
    nn.ReLU(),
    nn.Linear(128,1),
    nn.Sigmoid(),
)

opt_D=torch.optim.Adam(D.parameters(),lr=LR_D)
opt_G=torch.optim.Adam(G.parameters(),lr=LR_G)

for step in range(10000):
    artist_paintings = artist_works()
    G_ideas = Variable(torch.randn(BATCH_SIZE,N_IDEAS))
    G_paintings = G(G_ideas)

    prob_artist0 = D(artist_paintings)
    prob_artist1 = D(G_paintings)

    D_loss = - torch.mean(torch.log(prob_artist0)+torch.log(1-prob_artist1))#对数几率近似
    G_loss = torch.mean(torch.log(1-prob_artist1))

    opt_D.zero_grad()
    D_loss.backward(retain_graph=True)#保留之前网络参数
    opt_D.step()

    opt_G.zero_grad()
    G_loss.backward()
    opt_G.step()

    if step % 50 == 0:
        plt.cla()
        plt.plot(PAINT_POINTS[0],G_paintings.data.numpy()[0],c='#4AD631',lw=3,label='G')
        plt.plot(PAINT_POINTS[0],2*np.power(PAINT_POINTS[0],2)+0,c='#74BCFF',lw=3,label='R')
        plt.plot(PAINT_POINTS[0],1*np.power(PAINT_POINTS[0],2)+0,c='#FF9359',lw=3,label='R')
        plt.ylim((0.3))
        plt.legend(loc='upper right',fontsize=12)
        plt.draw()
        plt.pause(0.01)
plt.ioff()
plt.show()

过拟合

import torch
from torch.autograd import Variable
import matplotlib.pyplot as plt

torch.manual_seed(1)

N_SAMPLES = 20
N_HIDDEN = 300

x = torch.unsqueeze(torch.linspace(-1,1,N_SAMPLES),1)
y = x + 0.3*torch.normal(torch.zeros(N_SAMPLES,1),torch.ones(N_SAMPLES,1))
x,y=Variable(x),Variable(y)

test_x = torch.unsqueeze(torch.linspace(-1,1,N_SAMPLES),1)
test_y = test_x + 0.3*torch.normal(torch.zeros(N_SAMPLES,1),torch.ones(N_SAMPLES,1))
with torch.no_grad():
    test_x,test_y=Variable(test_x),Variable(test_y)

# plt.scatter(x.data.numpy(),y.data.numpy(),c='magenta',s=50,alpha=0.5,label='train_data')
# plt.scatter(test_x.data.numpy(),test_y.data.numpy(),c='cyan',s=50,alpha=0.5,label='test_data')
# plt.legend(loc='upper left')
# plt.ylim((-2.5,2.5))
# plt.show()

net_overfitting = torch.nn.Sequential(
    torch.nn.Linear(1,N_HIDDEN),
    torch.nn.ReLU(),
    torch.nn.Linear(N_HIDDEN,N_HIDDEN),
    torch.nn.ReLU(),
    torch.nn.Linear(N_HIDDEN,1),
)

net_dropped = torch.nn.Sequential(
    torch.nn.Linear(1,N_HIDDEN),
    torch.nn.Dropout(0.5),
    torch.nn.ReLU(),
    torch.nn.Linear(N_HIDDEN,N_HIDDEN),
    torch.nn.Dropout(0.5),
    torch.nn.ReLU(),
    torch.nn.Linear(N_HIDDEN,1),
)

optimizer_ofit = torch.optim.Adam(net_overfitting.parameters(),lr=0.01)
optimizer_drop = torch.optim.Adam(net_dropped.parameters(),lr=0.01)

loss_func = torch.nn.MSELoss()

for t in range(500):
    pred_ofit = net_overfitting(x)
    pred_drop = net_dropped(x)
    loss_ofit = loss_func(pred_ofit, y)
    loss_drop = loss_func(pred_drop, y)

    optimizer_ofit.zero_grad()
    optimizer_drop.zero_grad()
    loss_ofit.backward()
    loss_drop.backward()
    optimizer_drop.step()
    optimizer_ofit.step()

    if t % 10 == 0:
        net_overfitting.eval()
        net_dropped.eval()

        test_pred_ofit = net_overfitting(test_x)
        test_pred_drop = net_dropped(test_x)

        plt.scatter(x.data.numpy(),y.data.numpy(),c='magenta',s=50,alpha=0.5)
        plt.scatter(test_x.data.numpy(),test_y.data.numpy(),c='cyan',s=50,alpha=0.5)
        plt.plot(test_x.data.numpy(),test_pred_ofit.data.numpy(),'r--')
        plt.plot(test_x.data.numpy(),test_pred_drop.data.numpy(),'b--')
        plt.text(0,-1.2,'overfitting loss=%.4f'%loss_func(test_pred_ofit,test_y))
        plt.text(0,-1.5,'dropout loss=%.4f'%loss_func(test_pred_drop,test_y))
        plt.legend(loc='upper left')
        plt.ylim((-2.5,2.5))
        plt.pause(0.1)
plt.ioff()
plt.show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值