swarm_test.py 4.8 KB

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