trainer.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Improve performance of neural network
  2. import numpy as np
  3. from lab import neural, io_mnist
  4. def train(net, learnRate = 0.05, epochs = 2, batchSize = 10):
  5. """
  6. Create an improved network
  7. net : the network to be trained
  8. learnRate : speed of training, lower is slower but more precise
  9. epochs : the number of iterations
  10. return : the trained network
  11. """
  12. # Load data
  13. np_images, np_expected = io_mnist.load_training_samples()
  14. nbSamples = np_images.shape[1]
  15. # Prepare variables
  16. a0 = np.empty((net.inputLength, batchSize))
  17. z1 = np.empty((net.hiddenLength, batchSize))
  18. a1 = np.empty((net.hiddenLength, batchSize))
  19. z2 = np.empty((net.outputLength, batchSize))
  20. a2 = np.empty((net.outputLength, batchSize))
  21. y = np.empty((net.outputLength, batchSize))
  22. g = net.activationFunction
  23. g_ = net.activationDerivative
  24. d2 = np.empty(a2.shape)
  25. d1 = np.empty(a1.shape)
  26. permut = np.arange(nbSamples)
  27. for epoch in range(epochs):
  28. # Create mini batches
  29. np.random.shuffle(permut)
  30. # Iterate over batches
  31. for batchIndex in range(0, nbSamples, batchSize):
  32. # Update modified weights
  33. w1 = net.layer1
  34. b1 = np.stack([net.bias1] * batchSize).transpose() # stack
  35. w2 = net.layer2
  36. b2 = np.stack([net.bias2] * batchSize).transpose() # stack
  37. # Capture batch
  38. batchEndIndex = batchIndex + batchSize
  39. batchSelection = permut[batchIndex : batchEndIndex]
  40. a0 = np_images[:, batchSelection]
  41. y = np_expected[:, batchSelection]
  42. # Forward computation
  43. z1 = w1 @ a0 + b1
  44. a1 = g(z1)
  45. z2 = w2 @ a1 + b2
  46. a2 = g(z2)
  47. # Backward propagation
  48. d2 = a2 - y
  49. d1 = w2.transpose() @ d2 * g_(z1)
  50. # Weight correction
  51. net.layer2 -= learnRate * d2 @ a1.transpose() / batchSize
  52. net.layer1 -= learnRate * d1 @ a0.transpose() / batchSize
  53. net.bias2 -= learnRate * d2 @ np.ones(batchSize) / batchSize
  54. net.bias1 -= learnRate * d1 @ np.ones(batchSize) / batchSize
  55. return net