Skip to content

Commit feaa836

Browse files
committed
Initial branch
0 parents  commit feaa836

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+13304
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2016 Artem Golubin
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Machine learning algorithms
2+
Minimal and clean examples of machine learning algorithms implemented in numpy.
3+
4+
5+
6+
Implemented:
7+
* Deep learning (MLP, CNN, RNN, LSTM)
8+
* Linear regression, logistic regression
9+
* Random Forests
10+
* SVM with kernels
11+
* K-Means
12+
* PCA
13+
14+

examples/kmeans.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
3+
import numpy as np
4+
from sklearn.datasets import make_blobs
5+
6+
from mla.kmeans import KMeans
7+
8+
9+
def kmeans_example(plot=False):
10+
X, y = make_blobs(centers=4, n_samples=500, n_features=2, shuffle=True, random_state=42)
11+
clusters = len(np.unique(y))
12+
k = KMeans(K=clusters, max_iters=150, init='++')
13+
k.fit(X)
14+
k.predict()
15+
16+
if plot:
17+
k.plot()
18+
19+
20+
if __name__ == '__main__':
21+
kmeans_example(plot=True)

examples/linear_models.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
import logging
3+
4+
from sklearn.cross_validation import train_test_split
5+
from sklearn.datasets import make_classification
6+
from sklearn.datasets import make_regression
7+
8+
from mla.linear_models import LinearRegression, LogisticRegression
9+
from mla.metrics.metrics import *
10+
11+
# Change to DEBUG to see convergence
12+
logging.basicConfig(level=logging.ERROR)
13+
14+
15+
def regression():
16+
X, y = make_regression(n_samples=10000, n_features=100, n_informative=75, n_targets=1,
17+
noise=0.05, random_state=1111, bias=0.5)
18+
19+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1111)
20+
model = LinearRegression(lr=0.01, max_iters=2000, penalty='l2', C=0.03)
21+
model.fit(X_train, y_train)
22+
predictions = model.predict(X_test)
23+
print('regression mse', mean_squared_error(y_test, predictions))
24+
25+
26+
def classification():
27+
X, y = make_classification(n_samples=1000, n_features=100, n_informative=75, random_state=1111, n_classes=2,
28+
class_sep=2.5, )
29+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=1111)
30+
model = LogisticRegression(lr=0.01, max_iters=500, penalty='l1', C=0.01)
31+
model.fit(X_train, y_train)
32+
predictions = model.predict(X_test)
33+
print('classification accuracy', accuracy(y_test, predictions))
34+
35+
36+
if __name__ == '__main__':
37+
regression()
38+
classification()

examples/nnet_convnet_mnist.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import logging
2+
3+
from mla.datasets import load_mnist
4+
from mla.metrics import accuracy
5+
from mla.neuralnet import NeuralNet
6+
from mla.neuralnet.layers import Activation, Convolution, MaxPooling, Flatten, Dropout, Parameters
7+
from mla.neuralnet.layers import Dense
8+
from mla.neuralnet.optimizers import Adadelta
9+
from mla.utils import one_hot
10+
11+
logging.basicConfig(level=logging.DEBUG)
12+
13+
X_train, X_test, y_train, y_test = load_mnist()
14+
15+
# Normalization
16+
X_train /= 255.
17+
X_test /= 255.
18+
19+
y_train = one_hot(y_train.flatten())
20+
y_test = one_hot(y_test.flatten())
21+
print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)
22+
23+
# Approx. 15-20 min. per epoch
24+
model = NeuralNet(
25+
layers=[
26+
Convolution(n_filters=32, filter_shape=(3, 3), padding=(1, 1), stride=(1, 1)),
27+
Activation('relu'),
28+
Convolution(n_filters=32, filter_shape=(3, 3), padding=(1, 1), stride=(1, 1)),
29+
Activation('relu'),
30+
MaxPooling(pool_shape=(2, 2), stride=(2, 2)),
31+
Dropout(0.5),
32+
33+
Flatten(),
34+
Dense(128),
35+
Activation('relu'),
36+
Dropout(0.5),
37+
Dense(10),
38+
Activation('softmax'),
39+
],
40+
loss='categorical_crossentropy',
41+
optimizer=Adadelta(),
42+
metric='accuracy',
43+
batch_size=128,
44+
max_epochs=3,
45+
)
46+
47+
model.fit(X_train, y_train)
48+
predictions = model.predict(X_test)
49+
print accuracy(y_test, predictions)

examples/nnet_mlp.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
import logging
3+
4+
from sklearn.cross_validation import train_test_split
5+
from sklearn.datasets import make_classification
6+
from sklearn.datasets import make_regression
7+
from sklearn.metrics import roc_auc_score
8+
9+
from mla.datasets import *
10+
from mla.metrics.metrics import root_mean_squared_log_error, mean_squared_error
11+
from mla.neuralnet import NeuralNet
12+
from mla.neuralnet.constraints import MaxNorm, UnitNorm
13+
from mla.neuralnet.layers import Activation, Dense, Dropout
14+
from mla.neuralnet.optimizers import SGD, RMSprop, Adagrad, Adadelta, Adam
15+
from mla.neuralnet.parameters import Parameters
16+
from mla.neuralnet.regularizers import *
17+
from mla.utils import one_hot
18+
19+
logging.basicConfig(level=logging.DEBUG)
20+
21+
22+
def classification():
23+
X, y = make_classification(n_samples=1000, n_features=100, n_informative=75, random_state=1111, n_classes=2,
24+
class_sep=2.5, )
25+
y = one_hot(y)
26+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=1111)
27+
28+
model = NeuralNet(
29+
layers=[
30+
Dense(256, Parameters(init='uniform', regularizers={'W': L2(0.05)})),
31+
Activation('relu'),
32+
Dropout(0.5),
33+
Dense(128, Parameters(init='normal', constraints={'W': MaxNorm()})),
34+
Activation('relu'),
35+
Dense(2),
36+
Activation('softmax'),
37+
],
38+
loss='categorical_crossentropy',
39+
optimizer=Adadelta(),
40+
metric='accuracy',
41+
batch_size=64,
42+
max_epochs=25,
43+
44+
)
45+
model.fit(X_train, y_train)
46+
predictions = model.predict(X_test)
47+
print('classification accuracy', roc_auc_score(y_test[:, 0], predictions[:, 0]))
48+
49+
50+
def regression():
51+
X, y = make_regression(n_samples=5000, n_features=25, n_informative=25, n_targets=1, random_state=100, noise=0.05)
52+
y *= 0.01
53+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=1111)
54+
55+
model = NeuralNet(
56+
layers=[
57+
Dense(64, Parameters(init='normal')),
58+
Activation('linear'),
59+
Dense(32, Parameters(init='normal')),
60+
Activation('linear'),
61+
Dense(1),
62+
],
63+
loss='mse',
64+
optimizer=Adam(),
65+
metric='mse',
66+
batch_size=256,
67+
max_epochs=15,
68+
)
69+
model.fit(X_train, y_train)
70+
predictions = model.predict(X_test)
71+
print("regression mse", mean_squared_error(y_test, predictions.flatten()))
72+
73+
74+
if __name__ == '__main__':
75+
classification()
76+
regression()

examples/nnet_rnn_binary_add.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import logging
2+
from itertools import combinations, islice
3+
4+
import numpy as np
5+
from sklearn.cross_validation import train_test_split
6+
7+
from mla.metrics import accuracy
8+
from mla.neuralnet import NeuralNet
9+
from mla.neuralnet.constraints import SmallNorm
10+
from mla.neuralnet.layers import Activation, TimeDistributedDense, Parameters
11+
from mla.neuralnet.layers.recurrent import RNN, LSTM
12+
from mla.neuralnet.optimizers import Adam
13+
14+
logging.basicConfig(level=logging.DEBUG)
15+
16+
17+
def addition_dataset(dim=10, n_samples=10000, batch_size=64):
18+
combs = list(islice(combinations(range(2 ** (dim - 1)), 2), n_samples))
19+
binary_format = '{:0' + str(dim) + 'b}'
20+
X = np.zeros((len(combs), dim, 2), dtype=np.uint8)
21+
y = np.zeros((len(combs), dim, 1), dtype=np.uint8)
22+
23+
for i, (a, b) in enumerate(combs):
24+
X[i, :, 0] = list(reversed([int(x) for x in binary_format.format(a)]))
25+
X[i, :, 1] = list(reversed([int(x) for x in binary_format.format(b)]))
26+
y[i, :, 0] = list(reversed([int(x) for x in binary_format.format(a + b)]))
27+
28+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1111)
29+
30+
train_b = (X_train.shape[0] // batch_size) * batch_size
31+
test_b = (X_test.shape[0] // batch_size) * batch_size
32+
X_train = X_train[0:train_b]
33+
y_train = y_train[0:train_b]
34+
35+
X_test = X_test[0:test_b]
36+
y_test = y_test[0:test_b]
37+
return X_train, X_test, y_train, y_test
38+
39+
40+
def addition_nlp(ReccurentLayer):
41+
X_train, X_test, y_train, y_test = addition_dataset(8, 5000)
42+
43+
print(X_train.shape, X_test.shape)
44+
model = NeuralNet(
45+
layers=[
46+
ReccurentLayer,
47+
TimeDistributedDense(1),
48+
Activation('sigmoid'),
49+
],
50+
loss='mse',
51+
optimizer=Adam(),
52+
metric='mse',
53+
batch_size=64,
54+
max_epochs=15,
55+
)
56+
# print X_train.shape
57+
model.fit(X_train, y_train)
58+
predictions = np.round(model.predict(X_test))
59+
predictions = np.packbits(predictions.astype(np.uint8))
60+
y_test = np.packbits(y_test.astype(np.int))
61+
print(accuracy(y_test, predictions))
62+
63+
64+
65+
# RNN
66+
# addition_nlp(RNN(16, parameters=Parameters(constraints={'W': SmallNorm(), 'U': SmallNorm()})))
67+
# LSTM
68+
addition_nlp(LSTM(16))

examples/nnet_rnn_text_generation.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from __future__ import print_function
2+
3+
import logging
4+
import random
5+
6+
import numpy as np
7+
import sys
8+
9+
from mla.datasets import load_nietzsche
10+
from mla.neuralnet import NeuralNet
11+
from mla.neuralnet.constraints import SmallNorm
12+
from mla.neuralnet.layers import Activation, Dense, Parameters
13+
from mla.neuralnet.layers.recurrent import LSTM, RNN
14+
from mla.neuralnet.optimizers import Adam, RMSprop
15+
16+
logging.basicConfig(level=logging.DEBUG)
17+
18+
19+
def sample(preds, temperature=1.0):
20+
# helper function to sample an index from a probability array
21+
preds = np.asarray(preds).astype('float64')
22+
preds = np.log(preds) / temperature
23+
exp_preds = np.exp(preds)
24+
preds = exp_preds / np.sum(exp_preds)
25+
probas = np.random.multinomial(1, preds, 1)
26+
return np.argmax(probas)
27+
28+
29+
X, y, text, chars, char_indices, indices_char = load_nietzsche()
30+
# Round the number of sequences for batch processing
31+
items_count = X.shape[0] - (X.shape[0] % 64)
32+
maxlen = X.shape[1]
33+
X = X[0:items_count]
34+
y = y[0:items_count]
35+
36+
print(X.shape, y.shape)
37+
# LSTM OR RNN
38+
# rnn_layer = RNN(128, return_sequences=False)
39+
rnn_layer = LSTM(128,return_sequences=False,)
40+
41+
model = NeuralNet(
42+
layers=[
43+
rnn_layer,
44+
# Flatten(),
45+
# TimeStepSlicer(-1),
46+
Dense(X.shape[2]),
47+
Activation('softmax'),
48+
],
49+
loss='categorical_crossentropy',
50+
optimizer=RMSprop(learning_rate=0.01),
51+
metric='accuracy',
52+
batch_size=64,
53+
max_epochs=1,
54+
shuffle=False,
55+
56+
)
57+
58+
for i in xrange(25):
59+
model.fit(X, y)
60+
start_index = random.randint(0, len(text) - maxlen - 1)
61+
62+
generated = ''
63+
sentence = text[start_index: start_index + maxlen]
64+
generated += sentence
65+
print('----- Generating with seed: "' + sentence + '"')
66+
sys.stdout.write(generated)
67+
for i in range(100):
68+
x = np.zeros((64, maxlen, len(chars)))
69+
for t, char in enumerate(sentence):
70+
x[0, t, char_indices[char]] = 1.
71+
preds = model.predict(x)[0]
72+
next_index = sample(preds, 0.5)
73+
next_char = indices_char[next_index]
74+
75+
generated += next_char
76+
sentence = sentence[1:] + next_char
77+
78+
sys.stdout.write(next_char)
79+
sys.stdout.flush()
80+
print()

examples/pca.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from sklearn.cross_validation import train_test_split
2+
from sklearn.datasets import make_classification
3+
4+
from mla.linear_models import LogisticRegression
5+
from mla.metrics import accuracy
6+
from mla.pca import PCA
7+
8+
# logging.basicConfig(level=logging.DEBUG)
9+
10+
X, y = make_classification(n_samples=1000, n_features=100, n_informative=75, random_state=1111, n_classes=2,
11+
class_sep=2.5, )
12+
13+
for s in ['svd', 'eigen']:
14+
p = PCA(15, solver=s)
15+
p.fit(X)
16+
X = p.transform(X)
17+
print(X.shape)
18+
19+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1111)
20+
model = LogisticRegression(lr=0.001, max_iters=2500)
21+
model.fit(X_train, y_train)
22+
predictions = model.predict(X_test)
23+
print('Classification accuracy for %s PCA: %s' % (s, accuracy(y_test, predictions)))

0 commit comments

Comments
 (0)