swarm_test.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # Import modules
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from sklearn.datasets import load_iris
  5. from sklearn.metrics import accuracy_score
  6. from IPython.display import Image
  7. # Import PySwarms
  8. import pyswarms as ps
  9. import misc
  10. from pyswarms.utils.functions import single_obj as fx
  11. from pyswarms.utils.plotters import (plot_cost_history, plot_contour, plot_surface)
  12. def logits_function(p):
  13. """ Calculate roll-back the weights and biases
  14. Inputs
  15. ------
  16. p: np.ndarray
  17. The dimensions should include an unrolled version of the
  18. weights and biases.
  19. Returns
  20. -------
  21. numpy.ndarray of logits for layer 2
  22. """
  23. # Neural network architecture
  24. n_inputs = 2 ** n
  25. n_hidden = 2 ** (n + 1)
  26. n_classes = 2 ** n
  27. # Roll-back the weights and biases
  28. W1 = p[0:n_inputs * n_hidden].reshape((n_inputs, n_hidden))
  29. b1 = p[n_inputs * n_hidden:n_inputs * n_hidden + n_hidden].reshape((n_hidden,))
  30. W2 = p[n_inputs * n_hidden + n_hidden:n_inputs * n_hidden + n_hidden + n_hidden * n_classes].reshape(
  31. (n_hidden, n_classes))
  32. b2 = p[
  33. n_inputs * n_hidden + n_hidden + n_hidden * n_classes:n_inputs * n_hidden + n_hidden + n_hidden * n_classes + n_classes].reshape(
  34. (n_classes,))
  35. # Perform forward propagation
  36. z1 = x_train_ho.dot(W1) + b1 # Pre-activation in Layer 1
  37. a1 = np.tanh(z1) # Activation in Layer 1
  38. z2 = a1.dot(W2) + b2 # Pre-activation in Layer 2
  39. logits = z2 # Logits for Layer 2
  40. return logits
  41. # Forward propagation
  42. def forward_prop(params):
  43. """Forward propagation as objective function
  44. This computes for the forward propagation of the neural network, as
  45. well as the loss.
  46. Inputs
  47. ------
  48. params: np.ndarray
  49. The dimensions should include an unrolled version of the
  50. weights and biases.
  51. Returns
  52. -------
  53. float
  54. The computed negative log-likelihood loss given the parameters
  55. """
  56. logits = logits_function(params)
  57. # Compute for the softmax of the logits
  58. exp_scores = np.exp(logits)
  59. probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True)
  60. # Compute for the negative log likelihood
  61. corect_logprobs = -np.log(probs[range(int(samples/n)), hot_one_indices])
  62. loss = np.sum(corect_logprobs) / samples/n
  63. return loss
  64. def f(x):
  65. """Higher-level method to do forward_prop in the
  66. whole swarm.
  67. Inputs
  68. ------
  69. x: numpy.ndarray of shape (n_particles, dimensions)
  70. The swarm that will perform the search
  71. Returns
  72. -------
  73. numpy.ndarray of shape (n_particles, )
  74. The computed loss for each particle
  75. """
  76. n_particles = x.shape[0]
  77. j = [forward_prop(x[i]) for i in range(n_particles)]
  78. return np.array(j)
  79. def predict(X, pos, n):
  80. """
  81. Use the trained weights to perform class predictions.
  82. Inputs
  83. ------
  84. X: numpy.ndarray
  85. Input Iris dataset
  86. pos: numpy.ndarray
  87. Position matrix found by the swarm. Will be rolled
  88. into weights and biases.
  89. """
  90. # Neural network architecture
  91. n_inputs = 2 ** n
  92. n_hidden = 2 ** (n + 1)
  93. n_classes = 2 ** n
  94. # Roll-back the weights and biases
  95. W1 = pos[0:n_inputs * n_hidden].reshape((n_inputs, n_hidden))
  96. b1 = pos[n_inputs * n_hidden:n_inputs * n_hidden + n_hidden].reshape((n_hidden,))
  97. W2 = pos[n_inputs * n_hidden + n_hidden:n_inputs * n_hidden + n_hidden + n_hidden * n_classes].reshape(
  98. (n_hidden, n_classes))
  99. b2 = pos[
  100. n_inputs * n_hidden + n_hidden + n_hidden * n_classes:n_inputs * n_hidden + n_hidden + n_hidden * n_classes + n_classes].reshape(
  101. (n_classes,))
  102. # Perform forward propagation
  103. z1 = X.dot(W1) + b1 # Pre-activation in Layer 1
  104. a1 = np.tanh(z1) # Activation in Layer 1
  105. z2 = a1.dot(W2) + b2 # Pre-activation in Layer 2
  106. logits = z2 # Logits for Layer 2
  107. y_pred = np.argmax(logits, axis=1)
  108. y_pred_ho = np.zeros(X.shape, dtype=bool)
  109. sample = 0
  110. for entry in y_pred:
  111. y_pred_ho[sample][entry] = True
  112. sample += 1
  113. return y_pred_ho
  114. n = 4
  115. n_inputs = 2 ** n
  116. n_hidden = 2 ** (n + 1)
  117. n_classes = 2 ** n
  118. samples = 1e4
  119. x_train = misc.generate_random_bit_array(samples).reshape((-1, n))
  120. x_train_ho = misc.bit_matrix2one_hot(x_train)
  121. x_test_array = misc.generate_random_bit_array(samples * 0.3)
  122. x_test = x_test_array.reshape((-1, n))
  123. x_test_ho = misc.bit_matrix2one_hot(x_test)
  124. # Initialize swarm
  125. options = {'c1': 0.5, 'c2': 0.3, 'w':0.9}
  126. hot_one_indices = []
  127. for item in x_train_ho:
  128. count = 0
  129. for digit in item:
  130. if digit:
  131. hot_one_indices.append(count)
  132. count += 1
  133. # Call instance of PSO
  134. dimensions = (n_inputs * n_hidden) + (n_hidden * n_classes) + n_hidden + n_classes
  135. optimizer = ps.single.GlobalBestPSO(n_particles=100, dimensions=dimensions, options=options)
  136. # Perform optimization
  137. cost, pos = optimizer.optimize(f, iters=200)
  138. plot_cost_history(cost_history=optimizer.cost_history)
  139. #plt.rcParams.update({'font.size': 18})
  140. plt.title('Cost vs Number of Epochs using Particle Swarm Optimisation')
  141. plt.ylabel('Cost')
  142. plt.xlabel('Number of Epochs')
  143. plt.legend(['test'], loc='upper left')
  144. plt.show()
  145. results = predict(x_test_ho, pos, n)
  146. print("Accuracy: %.4f" % accuracy_score(x_test_ho, results))