浏览代码

"Completed operational version of swarm optimisation"

an 5 年之前
父节点
当前提交
a089bc6543
共有 2 个文件被更改,包括 67 次插入46 次删除
  1. 65 44
      models/swarm_test.py
  2. 2 2
      models/tutorial.py

+ 65 - 44
models/swarm_test.py

@@ -2,37 +2,12 @@
 import numpy as np
 import matplotlib.pyplot as plt
 from sklearn.datasets import load_iris
-
+from sklearn.metrics import accuracy_score
 
 # Import PySwarms
 import pyswarms as ps
 import misc
 
-data = load_iris()
-
-# Store the features as X and the labels as y
-X = data.data
-y = data.target
-
-n = 4
-print(y.shape)
-n_inputs = 2 ** n
-n_hidden = 2 ** (n + 1)
-n_classes = 2 ** n
-
-samples = 100000
-
-
-x_train = misc.generate_random_bit_array(samples).reshape((-1, n))
-x_train_ho = misc.bit_matrix2one_hot(x_train)
-x_test_array = misc.generate_random_bit_array(samples * 0.3)
-x_test = x_test_array.reshape((-1, n))
-x_test_ho = misc.bit_matrix2one_hot(x_test)
-print(x_train.shape)
-
-print(x_train_ho.shape)
-
-
 def logits_function(p):
     """ Calculate roll-back the weights and biases
 
@@ -96,7 +71,7 @@ def forward_prop(params):
 
     # Compute for the negative log likelihood
 
-    corect_logprobs = -np.log(probs[range(int(samples/n)), x_train_ho.shape[1]-1])
+    corect_logprobs = -np.log(probs[range(int(samples/n)), hot_one_indices])
     loss = np.sum(corect_logprobs) / samples/n
 
     return loss
@@ -119,31 +94,77 @@ def f(x):
     j = [forward_prop(x[i]) for i in range(n_particles)]
     return np.array(j)
 
-# Initialize swarm
-options = {'c1': 0.5, 'c2': 0.3, 'w':0.9}
-
-# Call instance of PSO
-dimensions = (n_inputs * n_hidden) + (n_hidden * n_classes) + n_hidden + n_classes
-optimizer = ps.single.GlobalBestPSO(n_particles=100, dimensions=dimensions, options=options)
-
-# Perform optimization
-cost, pos = optimizer.optimize(f, iters=2)
-
-def predict(pos):
+def predict(X, pos, n):
     """
     Use the trained weights to perform class predictions.
 
     Inputs
     ------
+    X: numpy.ndarray
+        Input Iris dataset
     pos: numpy.ndarray
         Position matrix found by the swarm. Will be rolled
         into weights and biases.
     """
-    logits = logits_function(pos)
-    print(logits.shape)
+    # Neural network architecture
+    n_inputs = 2 ** n
+    n_hidden = 2 ** (n + 1)
+    n_classes = 2 ** n
+
+    # Roll-back the weights and biases
+    W1 = pos[0:n_inputs * n_hidden].reshape((n_inputs, n_hidden))
+    b1 = pos[n_inputs * n_hidden:n_inputs * n_hidden + n_hidden].reshape((n_hidden,))
+    W2 = pos[n_inputs * n_hidden + n_hidden:n_inputs * n_hidden + n_hidden + n_hidden * n_classes].reshape(
+        (n_hidden, n_classes))
+    b2 = pos[
+         n_inputs * n_hidden + n_hidden + n_hidden * n_classes:n_inputs * n_hidden + n_hidden + n_hidden * n_classes + n_classes].reshape(
+        (n_classes,))
+
+    # Perform forward propagation
+    z1 = X.dot(W1) + b1  # Pre-activation in Layer 1
+    a1 = np.tanh(z1)     # Activation in Layer 1
+    z2 = a1.dot(W2) + b2 # Pre-activation in Layer 2
+    logits = z2          # Logits for Layer 2
+
     y_pred = np.argmax(logits, axis=1)
-    print(y_pred)
-    return y_pred
+    y_pred_ho = np.zeros(X.shape, dtype=bool)
+    sample = 0
+    for entry in y_pred:
+        y_pred_ho[sample][entry] = True
+        sample += 1
+    return y_pred_ho
+
+
+n = 4
+n_inputs = 2 ** n
+n_hidden = 2 ** (n + 1)
+n_classes = 2 ** n
+
+samples = 1e4
+
+x_train = misc.generate_random_bit_array(samples).reshape((-1, n))
+x_train_ho = misc.bit_matrix2one_hot(x_train)
+x_test_array = misc.generate_random_bit_array(samples * 0.3)
+x_test = x_test_array.reshape((-1, n))
+x_test_ho = misc.bit_matrix2one_hot(x_test)
+
+# Initialize swarm
+options = {'c1': 0.5, 'c2': 0.3, 'w':0.9}
+
+hot_one_indices = []
+for item in x_train_ho:
+    count = 0
+    for digit in item:
+        if digit:
+            hot_one_indices.append(count)
+        count += 1
+
+# Call instance of PSO
+dimensions = (n_inputs * n_hidden) + (n_hidden * n_classes) + n_hidden + n_classes
+optimizer = ps.single.GlobalBestPSO(n_particles=100, dimensions=dimensions, options=options)
+
+# Perform optimization
+cost, pos = optimizer.optimize(f, iters=80)
 
-print((predict(pos) == x_train_ho))
-    #.mean()
+results = predict(x_test_ho, pos, n)
+print("Accuracy: %.4f" % accuracy_score(x_test_ho, results))

+ 2 - 2
models/tutorial.py

@@ -105,7 +105,7 @@ dimensions = (n_inputs * n_hidden) + (n_hidden * n_classes) + n_hidden + n_class
 optimizer = ps.single.GlobalBestPSO(n_particles=100, dimensions=dimensions, options=options)
 
 # Perform optimization
-cost, pos = optimizer.optimize(f, iters=1000)
+cost, pos = optimizer.optimize(f, iters=100)
 
 def predict(pos):
     """
@@ -121,4 +121,4 @@ def predict(pos):
     y_pred = np.argmax(logits, axis=1)
     return y_pred
 
-(predict(pos) == y).mean()
+print((predict(pos) == y).mean())