/
algorithms.py
34 lines (31 loc) · 1.46 KB
/
algorithms.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
from __future__ import print_function
import random, collections
from utils import dotProduct, readTrainingExamples
from giveawayFeatures import featureExtractor
def StochasticGradientDescent(trainingSet, featureExtractor, lossFunction=None):
# HYPERPARAMETERS: step size (eta), number of iterations (T)
weights = collections.defaultdict(float)
T = 20
for t in range(T):
eta = float(1/float(math.sqrt(t+1)))
for example in trainingSet:
features = featureExtractor(example[0])
loss = 1 - (example[1] * dotProduct(weights, features))
for feature in features:
weights[feature] -= eta*(-features[feature]*example[1]) if (loss >= 1) else 0
return weights
def MinibatchGradientDescent(trainingSet, featureExtractor, lossFunction=None):
# HYPERPARAMETERS: step size (eta), number of iterations (T), number of batches (numBatches)
weights = collections.defaultdict(float)
T = 20
numBatches = 50
for t in range(T):
eta = float(1/float(math.sqrt(t+1)))
batch = random.sample(trainingSet, numBatches)
features = featureExtractor(batch) #TODO: support batch support in your feat. extractor
loss = sum(example * dotProduct(weights, features) for example in batch)
for feature in features:
weights[feature] -= eta*(-features[feature]*example[1]) if (loss >= 1) else 0
return weights
def KNearestNeighbors():
pass #TODO or import from library