PyTorch:开始入门的一些小代码上(from 莫烦)


1.Tensor & numpy & Variable

#torch和numpy的互相转化: .numpy()   .from_numpy()
np_data = np.arange(6).reshape((2,3))
torch_data = torch.from_numpy(np_data)
tensor2array = torch_data.numpy()
print(
      '\nnumpy:',np_data,
      '\ntorch:',torch_data,
      '\ntensor2array:',tensor2array
      )

#运算符:abs
data = [-1,-2,1,2]
tensor = torch.FloatTensor(data)  #32bit
print(
      '\nabs:',
      '\nnumpy:',np.abs(data),
      '\ntorch:',torch.abs(tensor)
      )
#运算符:sin
print(
      '\nsin:',
      '\nnumpy:',np.sin(data),
      '\ntorch:',torch.sin(tensor)
      )
#运算符:mean
print(
      '\nmean:',
      '\nnumpy:',np.mean(data),
      '\ntorch:',torch.mean(tensor)
      )

#矩阵运算
data = [[1,2],[3,4]]
tensor = torch.FloatTensor(data)  #32bit
print(
      '\n矩阵相乘:',                     #Matrix Mutiply
      '\nnumpy:',np.matmul(data,data),  
      '\ntorch:',torch.mm(tensor,tensor)
      )
'''
Tips:
numpy矩阵相乘的另外一种方式:
data = np.array(data)
print(data.dot(data))

但是在torch里面这样做结果就不一样啦:
print('torch:',tensor.dot(tensor))
输出结果是30.0
因为1*1+2*2+3*3+4*4=30

'''

#Variable变量:tensor不能反向传播,但variable可以
var = Variable(tensor,requires_grad=True)
print('tensoe:\n',tensor)
print('Variable:\n',var)

t_out = torch.mean(tensor*tensor)     #x^2
v_out = torch.mean(var*var)
print('tensor mean:\n',t_out)
print('Variable mean:\n',v_out)

v_out.backward()  #backward propagation
print('variable grad:\n',var.grad)  
#因为v_out包含var,v_out = 1/4 * sum(var * var)
#d(v_out)/d(var) = 1/4 * 2 * var = 1/2 * var

#print(var.data)
#print(var.data.numpy())


2.激励函数 Activation Function

#Activation Function:
x = torch.linspace(-5,5,200)  #-5~5之间取200个点
x = Variable(x)
x_np = x.data.numpy()  #torch的数据格式不能被matplotlib识别,需要转化成numpy

y_relu = F.relu(x).data.numpy()
y_sigmoid = F.sigmoid(x).data.numpy()
y_tanh = F.tanh(x).data.numpy()
y_softplus = F.softplus(x).data.numpy()
#softmax计算的是分类问题的概率,对于线图无法做出

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')


3.Regression回归 & Classification分类

#Regression
x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1)  
# x data (tensor), shape=(100, 1)
#将linespace的一维处理成二维,这样才能被torch处理
y = x.pow(2) + 0.2*torch.rand(x.size())  
# noisy y data (tensor), shape=(100, 1)
# x^2 + noise

# torch can only train on Variable, so convert them to Variable
x, y = Variable(x), Variable(y)
#plt.scatter(x.data.numpy(), y.data.numpy())  #打印散点图
#plt.show()

#Define My Neural Network:
class Net(torch.nn.Module):
    #initialization
    def __init__(self, n_feature, n_hidden, n_output):
        super(Net, self).__init__()     #继承(官方步骤)
        self.hidden = torch.nn.Linear(n_feature, n_hidden)   # hidden layer
        self.predict = torch.nn.Linear(n_hidden, n_output)   # output layer

    #forward propagation:input->Linear->ReLU->Linear->output
    def forward(self, x):
        x = F.relu(self.hidden(x))      # activation function for hidden layer
        x = self.predict(x)             # linear output
        return x

#net的实现:
net = Net(n_feature=1, n_hidden=10, n_output=1)     # define the network
print(net)  # net architecture

#net的优化:
optimizer = torch.optim.SGD(net.parameters(), lr=0.5)     #随机梯度下降优化
loss_func = torch.nn.MSELoss()  # 损失函数: mean squared loss ,MSE 均方差

plt.ion()   # something about plotting

for t in range(100):        #训练步数:100
    prediction = net(x)     # input x and predict based on x
    loss = loss_func(prediction, y)     # must be (1. nn output, 2. target)
    
    optimizer.zero_grad()   # clear gradients for next train
    loss.backward()         # backpropagation, compute gradients
    optimizer.step()        # apply and optimize gradients
    
    if t % 5 == 0:
        # plot and show learning process
        plt.cla()
        plt.scatter(x.data.numpy(), y.data.numpy())
        plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5)
        plt.text(0.5, 0, 'Loss=%.4f' % loss.data[0], fontdict={'size': 20, 'color':  'red'})
        plt.pause(0.5)

plt.ioff()
plt.show()

#Classification :

torch.manual_seed(1)    # reproducible 设定生成随机数的种子,返回一个 torch._C.Generator 对象.

# make fake data
n_data = torch.ones(100, 2)
#class 0 :
x0 = torch.normal(2*n_data, 1)      # class0 x data (tensor), shape=(100, 2)
y0 = torch.zeros(100)               # class0 y data (tensor), shape=(100, 1)
#class 1 :
x1 = torch.normal(-2*n_data, 1)     # class1 x data (tensor), shape=(100, 2)
y1 = torch.ones(100)                # class1 y data (tensor), shape=(100, 1)
#两类数据分别以(2,2)、(-2,-2)为中心正态分布,标签分别为0和1

#需要修改到torch可以运行的数据形式
x = torch.cat((x0, x1), 0).type(torch.FloatTensor)  # shape (200, 2) FloatTensor = 32-bit floating
y = torch.cat((y0, y1), ).type(torch.LongTensor)    # shape (200,) LongTensor = 64-bit integer

# torch can only train on Variable, so convert them to Variable
x, y = Variable(x), Variable(y)

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

#Define My Neural Network:
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)   # hidden layer
        self.out = torch.nn.Linear(n_hidden, n_output)   # output layer

    def forward(self, x):
        x = F.relu(self.hidden(x))      # activation function for hidden layer
        x = self.out(x)
        return x

net = Net(n_feature=2, n_hidden=10, n_output=2)     # define the network
#二分类输出属于每一类的概率,例如[1,0]表示是第一类,[0,1]表示是第二类
print(net)  # net architecture

optimizer = torch.optim.SGD(net.parameters(), lr=0.02)
loss_func = torch.nn.CrossEntropyLoss()  # the target label is NOT an one-hotted

plt.ion()   # something about plotting
#Train:
for t in range(100):
    out = net(x)                 # input x and predict based on x
    loss = loss_func(out, y)     # must be (1. nn output, 2. target), the target label is NOT one-hotted
    
    optimizer.zero_grad()   # clear gradients for next train
    loss.backward()         # backpropagation, compute gradients
    optimizer.step()        # apply and optimize gradients
    
    if t % 2 == 0:
        # plot and show learning process
        plt.cla()
        prediction = torch.max(F.softmax(out), 1)[1]  #因为输出的是每一类的可能,所以用softmax转换成概率,然后取最大
        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, cmap='RdYlGn')
        accuracy = sum(pred_y == target_y)/200.
        plt.text(1.5, -4, 'Accuracy=%.2f' % accuracy, fontdict={'size': 20, 'color':  'red'})
        plt.pause(1.0)

plt.ioff()
plt.show()



快速定义网络:

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

# replace following class code with an easy sequential network

#Method 1:定义类的方法
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)   # hidden layer
        self.predict = torch.nn.Linear(n_hidden, n_output)   # output layer

    def forward(self, x):
        x = F.relu(self.hidden(x))      # activation function for hidden layer
        x = self.predict(x)             # linear output
        return x

net1 = Net(1, 10, 1)


# easy and fast way to build your network
#Method 2:
net2 = torch.nn.Sequential(
    torch.nn.Linear(1, 10),
    torch.nn.ReLU(),
    torch.nn.Linear(10, 1)
)

#2种方法是等价的,只不过在print的时候输出有所不同

print(net1)     # net1 architecture

"""
Net (
  (hidden): Linear (1 -> 10)
  (predict): Linear (10 -> 1)
)
"""

print(net2)     # net2 architecture

"""
Sequential (
  (0): Linear (1 -> 10)
  (1): ReLU ()
  (2): Linear (10 -> 1)
)
"""

神经网络的储存和提取:
torch.manual_seed(1)    # reproducible

# fake data
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)

x, y = Variable(x, requires_grad=False), Variable(y, requires_grad=False)

def save():
    # save net1
    net1 = torch.nn.Sequential(
        torch.nn.Linear(1, 10),
        torch.nn.ReLU(),
        torch.nn.Linear(10, 1)
    )
    optimizer = torch.optim.SGD(net1.parameters(), lr=0.5)
    loss_func = torch.nn.MSELoss()

    for t in range(100):
        prediction = net1(x)
        loss = loss_func(prediction, y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    # plot result
    plt.figure(1, figsize=(10, 3))
    plt.subplot(131)
    plt.title('Net1')
    plt.scatter(x.data.numpy(), y.data.numpy())
    plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5)

    # 2 ways to save the net
    torch.save(net1, 'net.pkl')  # save entire net
    torch.save(net1.state_dict(), 'net_params.pkl')   # save only the parameters

def restore_net():
    # restore entire net1 to net2
    net2 = torch.load('net.pkl')  #提取整个网络
    prediction = net2(x)

    # plot result
    plt.subplot(132)
    plt.title('Net2')
    plt.scatter(x.data.numpy(), y.data.numpy())
    plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5)


def restore_params():
    # restore only the parameters in net1 to net3
    net3 = torch.nn.Sequential(
        torch.nn.Linear(1, 10),
        torch.nn.ReLU(),
        torch.nn.Linear(10, 1)
    )

    # copy net1's parameters into net3
    #首先要建立一个和net1一样结构的网络,才能成功提取它的参数
    net3.load_state_dict(torch.load('net_params.pkl'))
    prediction = net3(x)

    # plot result
    plt.subplot(133)
    plt.title('Net3')
    plt.scatter(x.data.numpy(), y.data.numpy())
    plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5)
    plt.show()

# save net1
save()

# restore entire net (may slow)
restore_net()

# restore only the net parameters
restore_params()

#据说提取参数的方法会比提取整个网络快一点


批数据训练:
import torch
import torch.utils.data as Data

torch.manual_seed(1)    # reproducible

BATCH_SIZE = 5
# BATCH_SIZE = 8 如果取8但总共数据集只有10个点,则第一个batch8个,第二个2个

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(data_tensor=x, target_tensor=y)

#将数据分批
#shuffile如果不定义的话(即DataLoader的()内为空),则默认按顺序提取batch数据
#shuffile为True则会打乱顺序进行提取
#num_workers定义每次提取batch用的线程数
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
)

#epoch:表示一个批处理周期,在这个周期内,
#将数据分成batch_size的大小,全部训练完毕为一个epoch
for epoch in range(3):   # train entire dataset 3 times

    for step, (batch_x, batch_y) in enumerate(loader):  # for each training step
        # train your data...
        print('Epoch: ', epoch, '| Step: ', step, '| batch x: ',
              batch_x.numpy(), '| batch y: ', batch_y.numpy())


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


torch.manual_seed(1)    # reproducible

#一些超参数,常用全大写的变量来命名
LR = 0.01
BATCH_SIZE = 32
EPOCH = 12

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

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

# put dateset into torch dataset
torch_dataset = Data.TensorDataset(data_tensor=x, target_tensor=y)
loader = Data.DataLoader(dataset=torch_dataset, batch_size=BATCH_SIZE, shuffle=True) #这里本来还有个参数,但是不知道为什么一加上这个程序就运行不了

# default network
class Net(torch.nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.hidden = torch.nn.Linear(1, 20)   # hidden layer
        self.predict = torch.nn.Linear(20, 1)   # output layer

    def forward(self, x):
        x = F.relu(self.hidden(x))      # activation function for hidden layer
        x = self.predict(x)             # linear output
        return x

# different nets
net_SGD         = Net()
net_Momentum    = Net()
net_RMSprop     = Net()
net_Adam        = Net()

#将4个神经网络定义在一个list中,以便之后在for循环中循环训练
nets = [net_SGD, net_Momentum, net_RMSprop, net_Adam]

# different optimizers
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)
opt_Adam        = torch.optim.Adam(net_Adam.parameters(), lr=LR, betas=(0.9, 0.99))

optimizers = [opt_SGD, opt_Momentum, opt_RMSprop, opt_Adam]


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

# training
for epoch in range(EPOCH):
    print('Epoch: ', epoch)
    for step, (batch_x, batch_y) in enumerate(loader):          # for each training step
        #此前的类型是tensor,需要封装在Variable中才能被nn处理
        b_x = Variable(batch_x) 
        b_y = Variable(batch_y)
        
        for net, opt, l_his in zip(nets, optimizers, losses_his):
            output = net(b_x)              # get output for every net
            loss = loss_func(output, b_y)  # compute loss for every net
            opt.zero_grad()                # clear gradients for next train
            loss.backward()                # backpropagation, compute gradients
            opt.step()                     # apply gradients
            l_his.append(loss.data[0])     # loss recoder

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()





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值