Skip to content

Commit 31d7ed3

Browse files
committedOct 17, 2022
FFNN-IRIS: complete example of ffnn using rich
1 parent caa5993 commit 31d7ed3

File tree

9 files changed

+218
-103
lines changed

9 files changed

+218
-103
lines changed
 

‎examples/ffnn-iris/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Ignore produced outputs
2+
*.html
3+
*.png

‎examples/ffnn-iris/main.py

+23-10
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,54 @@
55
from nnlearn.network import FFNN, DenseLayer
66
from nnlearn.datasets import load_iris
77
from nnlearn.metrics import accuracy_score
8+
from nnlearn.util import ScriptInformation
89

910
# TODO: replace this with own implentation
1011
from sklearn import preprocessing
1112
from sklearn.model_selection import train_test_split
1213

1314

1415
def test_ffnn_classifier():
15-
16-
# Load and split data
16+
17+
logger = ScriptInformation()
18+
logger.section_start(":herb: FFNN - IRIS")
19+
logger.script_time()
20+
logger.author("Ludek", "Cizinsky")
21+
logger.section_start(":construction: Prepare input for the model")
22+
23+
logger.working_on("Load and split data")
1724
X, y = load_iris()
1825

19-
# Process the data
26+
logger.working_on("Process the data")
2027
y = preprocessing.LabelEncoder().fit_transform(y)
2128
X = preprocessing.StandardScaler().fit_transform(X)
2229

23-
# Train test split
30+
logger.working_on("Train test split")
2431
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
2532
m = X_train.shape[1]
26-
27-
# Define the layers
33+
34+
logger.working_on("Define the layers")
2835
layers = [DenseLayer(m, 3), DenseLayer(3, 4), DenseLayer(4, 3, activation='softmax')]
2936

30-
# Train the model
37+
logger.section_start(":robot: Train the model")
3138
clf = FFNN(layers,
32-
epochs = 1,
39+
logger=logger,
40+
epochs = 50,
3341
loss_func='cross_entropy',
3442
batch_size=.5,
3543
lr=.9,
3644
shuffle=True)
3745
clf.fit(X_train, y_train)
46+
47+
clf.fig.savefig("training.png", bbox_inches='tight')
48+
logger.c.print(clf.report)
3849

39-
# Validate the model
50+
logger.section_start(":crystal_ball: Validate the model")
4051
y_hat = clf.predict(X_test)
41-
print(f'Validation accuracy: {accuracy_score(y_test, y_hat)}')
52+
acc = accuracy_score(y_test, y_hat)
53+
logger.important_metric('Accuracy', acc)
4254

55+
logger.save("ffnn-iris.html")
4356

4457
if __name__ == '__main__':
4558
test_ffnn_classifier()

‎examples/template/main.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
import sys
33
sys.path.insert(0, os.path.abspath('../../'))
44

5-
def your_test():
6-
print("Hello world!")
5+
from nnlearn.util import ScriptInformation
76

7+
def your_test():
8+
s = ScriptInformation()
9+
s.hello()
810

911
if __name__ == '__main__':
1012
your_test()

‎nnlearn/network/_base.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,31 @@
22
import numpy as np
33

44
class Base:
5+
"""
6+
Base for all parametric models which are optimized via Gradient descent.
7+
"""
58

69
def __init__(self):
710
pass
811

912
@staticmethod
1013
def _arr_to_var(X):
11-
1214
"""
1315
Transform given X values into Var objects.
1416
1517
Notes
1618
-----
1719
Assumption made: X is an nd-numpy array with numerical variables.
1820
"""
19-
20-
return np.vectorize(lambda x: Var(x))(X)
21+
22+
f = np.vectorize(lambda x: Var(x))
23+
return f(X)
24+
25+
@staticmethod
26+
def _arr_to_val(X):
27+
"""
28+
Get from given X's Var instances their values.
29+
"""
30+
f = np.vectorize(lambda x: x.v)
31+
return f(X)
2132

0 commit comments

Comments
 (0)