激活函数的使用

首先导入我们需要的包:
image.png
其中torch.nn.functional中的nn是神经网络模块
通过pytorch计算各个激活函数的值:
image.png
最后使用matpplotlib画出来:
image.png

Regression(回归)

  1. import torch
  2. import torch.nn.functional as F
  3. import matplotlib.pyplot as plt
  4. x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1) # x data (tensor), shape=(100, 1) 自己定义的数据
  5. y = x.pow(2) + 0.2*torch.rand(x.size()) # noisy y data (tensor), shape=(100, 1) 自己定义的函数
  6. # torch can only train on Variable, so convert them to Variable
  7. # The code below is deprecated in Pytorch 0.4. Now, autograd directly supports tensors
  8. # x, y = Variable(x), Variable(y)
  9. # plt.scatter(x.data.numpy(), y.data.numpy())
  10. # plt.show()
  11. class Net(torch.nn.Module): #用class来定义neural network
  12. def __init__(self, n_feature, n_hidden, n_output): #其中包含了初始化层所需要的信息
  13. super(Net, self).__init__()
  14. self.hidden = torch.nn.Linear(n_feature, n_hidden) # hidden layer
  15. self.predict = torch.nn.Linear(n_hidden, n_output) # output layer
  16. def forward(self, x): #network中前一项传递的过程
  17. x = F.relu(self.hidden(x)) # activation function for hidden layer
  18. x = self.predict(x) # linear output
  19. return x
  20. net = Net(n_feature=1, n_hidden=10, n_output=1) # define the network
  21. print(net) # net architecture
  22. optimizer = torch.optim.SGD(net.parameters(), lr=0.2) #使用SGD方法对我们的函数进行优化,net.parameters()是net中所有的参数,lr是learning rate学习效率(不宜太低或太高)
  23. loss_func = torch.nn.MSELoss() # this is for regression mean squared loss 主要用于回归问题,与分类问题不同
  24. plt.ion() # something about plotting
  25. for t in range(200):
  26. prediction = net(x) # input x and predict based on x
  27. loss = loss_func(prediction, y) # must be (1. nn output, 2. target)
  28. optimizer.zero_grad() # clear gradients for next train
  29. loss.backward() # backpropagation, compute gradients
  30. optimizer.step() # apply gradients 更新参数
  31. if t % 5 == 0:
  32. # plot and show learning process
  33. plt.cla()
  34. plt.scatter(x.data.numpy(), y.data.numpy())
  35. plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5)
  36. plt.text(0.5, 0, 'Loss=%.4f' % loss.data.numpy(), fontdict={'size': 20, 'color': 'red'})
  37. plt.pause(0.1)
  38. plt.ioff()
  39. plt.show()

Classification(分类)

  1. import torch
  2. import torch.nn.functional as F
  3. import matplotlib.pyplot as plt
  4. # torch.manual_seed(1) # reproducible
  5. # 自己制作的假的数据
  6. n_data = torch.ones(100, 2)
  7. x0 = torch.normal(2*n_data, 1) # class0 x data (tensor), shape=(100, 2)
  8. y0 = torch.zeros(100) # class0 y data (tensor), shape=(100, 1)
  9. x1 = torch.normal(-2*n_data, 1) # class1 x data (tensor), shape=(100, 2)
  10. y1 = torch.ones(100) # class1 y data (tensor), shape=(100, 1)
  11. x = torch.cat((x0, x1), 0).type(torch.FloatTensor) # shape (200, 2) FloatTensor = 32-bit floating
  12. y = torch.cat((y0, y1), ).type(torch.LongTensor) # shape (200,) LongTensor = 64-bit integer
  13. # The code below is deprecated in Pytorch 0.4. Now, autograd directly supports tensors
  14. # x, y = Variable(x), Variable(y)
  15. # plt.scatter(x.data.numpy()[:, 0], x.data.numpy()[:, 1], c=y.data.numpy(), s=100, lw=0, cmap='RdYlGn')
  16. # plt.show()
  17. class Net(torch.nn.Module):
  18. def __init__(self, n_feature, n_hidden, n_output):
  19. super(Net, self).__init__()
  20. self.hidden = torch.nn.Linear(n_feature, n_hidden) # hidden layer
  21. self.out = torch.nn.Linear(n_hidden, n_output) # output layer
  22. def forward(self, x):
  23. x = F.relu(self.hidden(x)) # activation function for hidden layer
  24. x = self.out(x)
  25. return x
  26. net = Net(n_feature=2, n_hidden=10, n_output=2) # define the network
  27. print(net) # net architecture
  28. optimizer = torch.optim.SGD(net.parameters(), lr=0.02)
  29. loss_func = torch.nn.CrossEntropyLoss() # 主要用于分类问题计算loss
  30. plt.ion() # something about plotting
  31. for t in range(100):
  32. out = net(x) # input x and predict based on x
  33. loss = loss_func(out, y) # must be (1. nn output, 2. target), the target label is NOT one-hotted
  34. optimizer.zero_grad() # clear gradients for next train
  35. loss.backward() # backpropagation, compute gradients
  36. optimizer.step() # apply gradients
  37. if t % 2 == 0:
  38. # plot and show learning process
  39. plt.cla()
  40. prediction = torch.max(out, 1)[1]
  41. pred_y = prediction.data.numpy()
  42. target_y = y.data.numpy()
  43. plt.scatter(x.data.numpy()[:, 0], x.data.numpy()[:, 1], c=pred_y, s=100, lw=0, cmap='RdYlGn')
  44. accuracy = float((pred_y == target_y).astype(int).sum()) / float(target_y.size)
  45. plt.text(1.5, -4, 'Accuracy=%.2f' % accuracy, fontdict={'size': 20, 'color': 'red'})
  46. plt.pause(0.1)
  47. plt.ioff()
  48. plt.show()

快速搭建神经网络

  1. class Net(torch.nn.Module):
  2. def __init__(self, n_feature, n_hidden, n_output):
  3. super(Net, self).__init__()
  4. self.hidden = torch.nn.Linear(n_feature, n_hidden) # hidden layer
  5. self.predict = torch.nn.Linear(n_hidden, n_output) # output layer
  6. def forward(self, x):
  7. x = F.relu(self.hidden(x)) # activation function for hidden layer
  8. x = self.predict(x) # linear output
  9. return x
  10. net1 = Net(1, 10, 1)
  11. net2 = torch.nn.Sequential(
  12. torch.nn.Linear(1, 10),
  13. torch.nn.ReLU(),
  14. torch.nn.Linear(10, 1)
  15. )

两种方法的结果,大致相同
image.png

保存和提取神经网络

  1. import torch
  2. import matplotlib.pyplot as plt
  3. # torch.manual_seed(1) # reproducible
  4. # fake data
  5. x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1) # x data (tensor), shape=(100, 1)
  6. y = x.pow(2) + 0.2*torch.rand(x.size()) # noisy y data (tensor), shape=(100, 1)
  7. # The code below is deprecated in Pytorch 0.4. Now, autograd directly supports tensors
  8. # x, y = Variable(x, requires_grad=False), Variable(y, requires_grad=False)
  9. def save():
  10. # save net1
  11. net1 = torch.nn.Sequential(
  12. torch.nn.Linear(1, 10),
  13. torch.nn.ReLU(),
  14. torch.nn.Linear(10, 1)
  15. )
  16. optimizer = torch.optim.SGD(net1.parameters(), lr=0.5)
  17. loss_func = torch.nn.MSELoss()
  18. #训练
  19. for t in range(100):
  20. prediction = net1(x)
  21. loss = loss_func(prediction, y)
  22. optimizer.zero_grad()
  23. loss.backward()
  24. optimizer.step()
  25. # plot result
  26. plt.figure(1, figsize=(10, 3))
  27. plt.subplot(131)
  28. plt.title('Net1')
  29. plt.scatter(x.data.numpy(), y.data.numpy())
  30. plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5)
  31. # 2 ways to save the net
  32. torch.save(net1, 'net.pkl') # 保存整个神经网络
  33. torch.save(net1.state_dict(), 'net_params.pkl') # 保存神经网络中的参数
  34. def restore_net():
  35. # 提取net1并赋给net2
  36. net2 = torch.load('net.pkl')
  37. prediction = net2(x)
  38. # plot result
  39. plt.subplot(132)
  40. plt.title('Net2')
  41. plt.scatter(x.data.numpy(), y.data.numpy())
  42. plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5)
  43. def restore_params():
  44. # 提取net1中的参数赋值给net3,首先要创建一个和net1相同结构的net3,再进行赋值
  45. net3 = torch.nn.Sequential(
  46. torch.nn.Linear(1, 10),
  47. torch.nn.ReLU(),
  48. torch.nn.Linear(10, 1)
  49. )
  50. # 对net3进行赋值
  51. net3.load_state_dict(torch.load('net_params.pkl'))
  52. prediction = net3(x)
  53. # plot result
  54. plt.subplot(133)
  55. plt.title('Net3')
  56. plt.scatter(x.data.numpy(), y.data.numpy())
  57. plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5)
  58. plt.show()
  59. # save net1
  60. save()
  61. # restore entire net (may slow)
  62. restore_net()
  63. # restore only the net parameters
  64. restore_params()

批处理

  1. import torch
  2. import torch.utils.data as Data
  3. torch.manual_seed(1) # reproducible
  4. BATCH_SIZE = 5
  5. # BATCH_SIZE = 8
  6. x = torch.linspace(1, 10, 10) # this is x data (torch tensor)
  7. y = torch.linspace(10, 1, 10) # this is y data (torch tensor)
  8. torch_dataset = Data.TensorDataset(x, y)
  9. loader = Data.DataLoader(
  10. dataset=torch_dataset, # torch TensorDataset format
  11. batch_size=BATCH_SIZE, # 在一个epoch中每次batch读取多少个数据
  12. shuffle=True, # 是否要打乱进行读取
  13. num_workers=2, # subprocesses for loading data
  14. )
  15. def show_batch():
  16. for epoch in range(3): #全部数据共训练3次
  17. for step, (batch_x, batch_y) in enumerate(loader): # for each training step
  18. # train your data...
  19. print('Epoch: ', epoch, '| Step: ', step, '| batch x: ',
  20. batch_x.numpy(), '| batch y: ', batch_y.numpy())
  21. if __name__ == '__main__':
  22. show_batch()

当BATCH_SIZE=5,shuffle = Ture时:
image.png
当BATCH_SIZE=8,shuffle = Fause时:
image.png