Skip to content

Instantly share code, notes, and snippets.

View tarlanahad's full-sized avatar

Tarlan Ahad tarlanahad

View GitHub Profile
class SVM:
def __init__(self, X, y, num_of_epochs, lr, C):
self.X = X
self.y = y
self.num_of_epochs = num_of_epochs
self.lr = lr
self.C = C
# Add column vector of ones for computational convenience
pso = PSO(dims=2, numOfBoids=30, numOfEpochs=500)
pso.optimize()
Epoch: 23 | Best position: [1.802829810222999,-2.7203300431518134] | Best known error: 29.24505754053026
Epoch: 24 | Best position: [1.802829810222999,-2.7203300431518134] | Best known error: 29.24505754053026
Epoch: 25 | Best position: [1.9678556451385256,-1.6042436666836852] | Best known error: 24.579905728771077
Epoch: 26 | Best position: [2.1199768747964054,-1.1332450655810595] | Best known error: 11.792438949384607
Epoch: 27 | Best position: [2.1199768747964054,-1.1332450655810595] | Best known error: 11.792438949384607
Epoch: 28 | Best position: [2.1199768747964054,-1.1332450655810595] | Best known error: 11.792438949384607
Epoch: 29 | Best position: [2.1199768747964054,-1.1332450655810595] | Best known error: 11.792438949384607
Epoch: 30 | Best position: [2.1199768747964054,-1.1332450655810595] | Best known error: 11.792438949384607
Epoch: 31 | Best position: [2.1199768747964054,-1.1332450655810595] | Best known error: 11.792438949384607
Epoch: 32 | Best position: [1.0839903018536725,-1.167888659397816
def optimize(self):
for i in range(self.numOfEpochs):
for j in range(len(self.swarm_list)):
current_particle = self.swarm_list[j] # get current particle
Vcurr = grad_error(current_particle.position) # calculate current velocity of the particle
deltaV = self.w * Vcurr \
class PSO:
w = 0.729
c1 = 1.49445
c2 = 1.49445
lr = 0.01
def __init__(self, dims, numOfBoids, numOfEpochs):
self.swarm_list = [self.Particle(dims, -500, 500) for i in range(numOfBoids)]
self.numOfEpochs = numOfEpochs
class Particle:
def __init__(self, dim, minx, maxx):
self.position = np.random.uniform(low=minx, high=maxx, size=dim)
self.velocity = np.random.uniform(low=minx, high=maxx, size=dim)
self.best_part_pos = self.position.copy()
self.error = error(self.position)
self.best_part_err = self.error.copy()
import numpy as np
def error(current_pos):
x = current_pos[0]
y = current_pos[1]
return (x ** 2 - 10 * np.cos(2 * np.pi * x)) + \
(y ** 2 - 10 * np.cos(2 * np.pi * y)) + 20
data = np.load('files/mnist_data.npy').astype(float)
train_y = data[0:5000, 0]
train_x = data[0:5000, 1:]
test_y = data[0:1000, 0]
test_x = data[0:1000, 1:]
RBF_CLASSIFIER = RBF(train_x, train_y, test_x, test_y, num_of_classes=10,
k=500, std_from_clusters=False)
def fit(self):
self.centroids, self.std_list = kmeans(self.X, self.k, max_iters=1000)
if not self.std_from_clusters:
dMax = np.max([get_distance(c1, c2) for c1 in self.centroids for c2 in self.centroids])
self.std_list = np.repeat(dMax / np.sqrt(2 * self.k), self.k)
RBF_X = self.rbf_list(self.X, self.centroids, self.std_list)
class RBF:
def __init__(self, X, y, tX, ty, num_of_classes,
k, std_from_clusters=True):
self.X = X
self.y = y
self.tX = tX
self.ty = ty