swarm_test.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Import modules
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from sklearn.datasets import load_iris
  5. # Import PySwarms
  6. import pyswarms as ps
  7. import misc
  8. data = load_iris()
  9. # Store the features as X and the labels as y
  10. #X = data.data
  11. #y = data.target
  12. n = 4
  13. n_inputs = 2 ** n
  14. n_hidden = 2 ** (n + 1)
  15. n_classes = 2
  16. samples = 1000
  17. x_train = misc.generate_random_bit_array(samples).reshape((-1, n))
  18. x_train_ho = misc.bit_matrix2one_hot(x_train)
  19. x_test_array = misc.generate_random_bit_array(samples * 0.3)
  20. x_test = x_test_array.reshape((-1, n))
  21. x_test_ho = misc.bit_matrix2one_hot(x_test)
  22. def logits_function(p):
  23. """ Calculate roll-back the weights and biases
  24. Inputs
  25. ------
  26. p: np.ndarray
  27. The dimensions should include an unrolled version of the
  28. weights and biases.
  29. Returns
  30. -------
  31. numpy.ndarray of logits for layer 2
  32. """
  33. # Neural network architecture
  34. n_inputs = 2 ** n
  35. n_hidden = 2 ** (n + 1)
  36. n_classes = 2
  37. # Roll-back the weights and biases
  38. W1 = p[0:n_inputs * n_hidden].reshape((n_inputs, n_hidden))
  39. b1 = p[n_inputs * n_hidden:n_inputs * n_hidden + n_hidden].reshape((n_hidden,))
  40. W2 = p[n_inputs * n_hidden + n_hidden:n_inputs * n_hidden + n_hidden + n_hidden * n_classes].reshape(
  41. (n_hidden, n_classes))
  42. b2 = p[
  43. n_inputs * n_hidden + n_hidden + n_hidden * n_classes:n_inputs * n_hidden + n_hidden + n_hidden * n_classes + n_classes].reshape(
  44. (n_classes,))
  45. # Perform forward propagation
  46. z1 = x_train_ho.dot(W1) + b1 # Pre-activation in Layer 1
  47. a1 = np.tanh(z1) # Activation in Layer 1
  48. z2 = a1.dot(W2) + b2 # Pre-activation in Layer 2
  49. logits = z2 # Logits for Layer 2
  50. return logits
  51. # Forward propagation
  52. def forward_prop(params):
  53. """Forward propagation as objective function
  54. This computes for the forward propagation of the neural network, as
  55. well as the loss.
  56. Inputs
  57. ------
  58. params: np.ndarray
  59. The dimensions should include an unrolled version of the
  60. weights and biases.
  61. Returns
  62. -------
  63. float
  64. The computed negative log-likelihood loss given the parameters
  65. """
  66. logits = logits_function(params)
  67. # Compute for the softmax of the logits
  68. exp_scores = np.exp(logits)
  69. probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True)
  70. # Compute for the negative log likelihood
  71. corect_logprobs = -np.log(probs[range(samples), x_train_ho])
  72. loss = np.sum(corect_logprobs) / samples
  73. return loss
  74. def f(x):
  75. """Higher-level method to do forward_prop in the
  76. whole swarm.
  77. Inputs
  78. ------
  79. x: numpy.ndarray of shape (n_particles, dimensions)
  80. The swarm that will perform the search
  81. Returns
  82. -------
  83. numpy.ndarray of shape (n_particles, )
  84. The computed loss for each particle
  85. """
  86. n_particles = x.shape[0]
  87. j = [forward_prop(x[i]) for i in range(n_particles)]
  88. return np.array(j)
  89. # Initialize swarm
  90. options = {'c1': 0.5, 'c2': 0.3, 'w':0.9}
  91. # Call instance of PSO
  92. dimensions = (n_inputs * n_hidden) + (n_hidden * n_classes) + n_hidden + n_classes
  93. optimizer = ps.single.GlobalBestPSO(n_particles=100, dimensions=dimensions, options=options)
  94. # Perform optimization
  95. cost, pos = optimizer.optimize(f, iters=1000)
  96. def predict(pos):
  97. """
  98. Use the trained weights to perform class predictions.
  99. Inputs
  100. ------
  101. pos: numpy.ndarray
  102. Position matrix found by the swarm. Will be rolled
  103. into weights and biases.
  104. """
  105. logits = logits_function(pos)
  106. y_pred = np.argmax(logits, axis=1)
  107. return y_pred
  108. print((predict(pos) == x_train_ho).mean())