Skip to content

Instantly share code, notes, and snippets.

@touhi99
Created July 13, 2018 11:21
Show Gist options
  • Save touhi99/d61a11c54d651e9d566f5bf25c9f4e7f to your computer and use it in GitHub Desktop.
Save touhi99/d61a11c54d651e9d566f5bf25c9f4e7f to your computer and use it in GitHub Desktop.
def train(self, train_data):
print("In trainer...")
u= np.zeros(self.weights.shape, dtype=np.int32)
q=0
for epoch in range(0,1):
correct=0
print("epoch: ",epoch+1)
i=0
for data in train_data:
q += 1
scores = np.zeros((4,))
feature_vector = np.ones((len(data.featureVector),), dtype = np.float32)
for index in data.featureVector:
for i in range(0,4):
scores[i] += self.weights[i][index]
predicted = np.argmax(scores)
if predicted!= data.label:
for index in data.featureVector:
self.weights[data.label][index]+=1
self.weights[predicted][index]-=1
u[data.label][index]+=q
u[data.label][index]-=q
if predicted==data.label:
correct+=1
i+=1
if i%5000==0:
print("States",i,": ", (correct/i))
print("Accuracy: ", (correct/len(train_data)))
u = u * (1/q)
self.weights -= u
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment