-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
54 lines (42 loc) · 1.79 KB
/
model.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
"""
transition based arc-eager parser with perceptron
"""
import dill
import numpy as np
class Perceptron:
def __init__(self, feature_map, weights):
self.map = feature_map
self.weights = weights
def save_model(self, model, ln):
stream = open('model_5_' + ln, 'wb')
dill.dump(model, stream, -1)
stream.close()
def update_weights(self, data, predicted):
# Update weights based on the difference between predicted and actual labels
for idx in data.feats_vector:
self.weights[data.label][idx] += 1 # add 1 to the correct prediction
self.weights[predicted][idx] -= 1 # subtract 1 from the wrong prediction
def my_model(self, train_data):
epochs = 5
for i in range(epochs):
total = 0
correct = 0
print("epoch: ", i + 1)
for data in train_data:
total += 1
scores = np.zeros((4,))
# Compute scores for each possible label
for index in data.feats_vector:
for j in range(0, 4):
scores[j] += self.weights[j][index]
predicted = np.argmax(scores) # Get the label with the highest score
# Update weights if predicted label is different from actual label
if predicted != data.label:
self.update_weights(data, predicted)
# Keep track of number of correct predictions
if predicted == data.label:
correct += 1
# Print training accuracy every 500 instances
if total % 500 == 0:
accuracy = 100.0 * correct / total
print("total:", total, "accuracy:", accuracy)