-
Notifications
You must be signed in to change notification settings - Fork 12
/
sequential-multiclass-perceptron-learning.py
102 lines (86 loc) · 3.76 KB
/
sequential-multiclass-perceptron-learning.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Discriminant Functions [week 2]
import numpy as np
def sequential_multiclass_perceptron_learning (N, augmented_matrix, eta, omega, number_of_classes, number_of_features ):
# N is the number of exemplars provided in the question
# augmented_matrix is the augmented feature vector from the question
# eta is the learning rate given in the question
# omega is an array containing all the output classes of the feature vectors
N_counter = 0 # counter which keeps track of cases where winner_class == omega[index]
#Step 2. Initialise aj for each class
at = np.zeros((number_of_classes, number_of_features))
for i in range(0,15):
print ('Iteration: ',i+1)
# Step 3. Find values of g1, g2 and g3 and then select the arg max of g
index = i % 5
#Print updated a^t value
print('a^t:')
print(at)
# Compute g value
g = np.empty([number_of_classes])
for i in range(len(g)):
print('Calculation of g values..........')
print('a^t is:',at[i])
print('Index is:', index)
print('Aug matrix is:', augmented_matrix[:,index] )
g[i] = at[i] @ augmented_matrix[:,index]
print('g1 | g2 | g3')
print(g)
#Step 4. Select the winner
#Logic for 0,0,0 case and similar ones where 2 gs can produce max value
seen = []
bRepeated = False
# Check if there are multiple max values, and assign the winner class accordingly
for number in g:
if number in seen:
bRepeated = True
print ("Number repeated!")
m = max(g)
temp = [index for index, j in enumerate(g) if j == m]
winner_class = max(temp) + 1
else:
seen.append(number)
#If all g values are unique, simply select the max value's class as the winner
if(bRepeated == False):
g = g.tolist()
arg_max = max(g)
winner_class = g.index(arg_max) + 1
print('Winner class = ', winner_class, ', and actual class is:',omega[index])
#Compare winnner to actual class
if(winner_class != omega[index]):
# Step 4. Apply the update rule as per the algorithm
#Increment the actual class value which is incorrectly classified
at[omega[index]-1] = at[omega[index]-1] + eta * augmented_matrix[:,index]
print('New loser value:', at[omega[index]-1])
#Penalize the wrongly predicted Winner class
at[winner_class-1] = at[winner_class-1] - eta * augmented_matrix[:,index]
print('New winner value:', at[winner_class-1])
#Reset counter to 0
N_counter =0
else:
print ('No update is performed!')
N_counter +=1 #Increment convergence counter which keeps track of cases where winner_class == omega[index]
if(N_counter == N): ## check for convergence
print('Value of N = ', N)
print('Value of N_counter = ', N_counter)
print('Learning has converged, so stopping...')
print ('Final values of a^t after update....')
print('at')
print(at)
break
print ('N counter value = ', N_counter)
print('at')
print(at)
print ('=========================================================')
if __name__ == "__main__":
#Set input variables for the sequential_multiclass_perceptron_learning function
N = 5 # N refers to the number of exemplars in the input dataset
eta = 1
input_array = np.array([[ 1, 1, 1, 1],
[ 2, 0, -1, -1],
[ 0, 2, 1, -1]]) # Input matrix from the question
augmented_matrix = np.insert(input_array,0,1,axis=1)
omega = np.array([1,1,2,2,3]) # Class labels from the question
number_of_classes = 3
number_of_features = 3
#Call function
sequential_multiclass_perceptron_learning (N, augmented_matrix, eta, omega, number_of_classes, number_of_features)