-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassification_example.py
More file actions
63 lines (51 loc) · 2.07 KB
/
classification_example.py
File metadata and controls
63 lines (51 loc) · 2.07 KB
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
55
56
57
58
59
60
61
62
63
import numpy as np
import pandas as pd
import neural_network_backbone as nnb
from sklearn.metrics import accuracy_score
SILENT = False
SEED = 10101
EPOCHS = 300
LEARNING_RATE = 0.1
INPUTS_DIRECTORY = './inputs/classification/'
TRAIN_FILE = 'data.simple.train.100.csv'
TEST_FILE = 'data.simple.test.100.csv'
def get_progress(Y_hat, Y):
cost = get_cost_value(Y_hat, Y)
accuracy = get_accuracy_value(Y_hat, Y)
return "cost: {:.5f} - accuracy: {:.5f}".format(cost, accuracy)
def one_hot(Y, num_classes):
return np.squeeze(np.eye(num_classes)[Y.reshape(-1)])
def convert_prob_into_class(probs):
return np.array([[1. if prob == max(v) else 0. for prob in v] for v in probs]).reshape(probs.shape)
def get_accuracy_value(Y_hat, Y):
Y = one_hot(Y, n_outputs)
Y_hat_ = convert_prob_into_class(Y_hat.T)
return accuracy_score(Y, Y_hat_)
def get_cost_value(Y_hat, Y, derivative = False):
Y = one_hot(Y, n_outputs)
if not derivative:
eps = 1e-15
Y_hat = np.clip(Y_hat, eps, 1. - eps)
return -np.mean(Y * np.log(Y_hat.T) + (1. - Y) * np.log(1. - Y_hat.T))
else:
return Y_hat.T - Y
dataset_train = pd.read_csv(INPUTS_DIRECTORY + TRAIN_FILE, sep=',').values
dataset_test = pd.read_csv(INPUTS_DIRECTORY + TEST_FILE, sep=',').values
n_inputs = len(dataset_train[0]) - 1
n_outputs = len(set([row[-1] for row in dataset_train]))
X_train = dataset_train[:,0:2]
y_train = dataset_train[:,2].astype(int) - 1
X_test = dataset_test[:,0:2]
y_test = dataset_test[:,2].astype(int) - 1
network_layers = [
{"nodes": n_inputs},
{"nodes": 5, "activation": nnb.relu},
{"nodes": n_outputs, "activation": nnb.softmax}
]
nnb.SILENT = SILENT
nnb.COST_FUNC = get_cost_value
nnb.PROGRESS_FUNC = get_progress
params_values = nnb.train(X_train, y_train.reshape((y_train.shape[0], 1)),
network_layers, EPOCHS, LEARNING_RATE, SEED)
Y_test_hat, _ = nnb.full_forward_propagation(np.transpose(X_test), params_values, network_layers)
print("Test set: " + get_progress(Y_test_hat, np.transpose(y_test.reshape((y_test.shape[0], 1)))))