swarm_test.py 3.7 KB

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