-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
138 lines (109 loc) · 5.1 KB
/
main.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
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import argparse
import sys, os
path = os.path.join(os.path.dirname(__file__), '..', '')
sys.path.insert(1, path)
from multilayer_perceptron.Model import Model
from multilayer_perceptron.NeuralNet import NeuralNet
from multilayer_perceptron.ModelPlotter import ModelPlotter
from multilayer_perceptron.ModelTrainer import ModelTrainer
from multilayer_perceptron.srcs.utils import load_topology, load_split_data, load_parameters, split_dataset_save
from multilayer_perceptron.srcs.metrics import accuracy_score
import multilayer_perceptron.config as config
import multilayer_perceptron.srcs.losses as losses
import multilayer_perceptron.srcs.optimizers as optimizers
def prediction(filename):
parameters_path = config.parameters_dir + filename
topology_path = config.topologies_dir + filename + ".json"
test_path = config.data_dir + config.test_path
x, y = load_split_data(test_path)
parameters = load_parameters(parameters_path)
topology = load_topology(topology_path)
model = NeuralNet()
# print("Net doc:", model.__doc__)
model.create_network(topology)
model.set_parameters(list(parameters))
y_pred = model.predict(x)
y = model.one_hot_encode_labels(y)
accuracy = accuracy_score(y, y_pred)
print('\nAccuracy:', accuracy)
def set_argparse():
parser = argparse.ArgumentParser(description=description)
parser.add_argument("--topology", type=str, default=None, required=True,
help="Path of model topology")
parser.add_argument("-s", "--split", type=str, default=None,
help="Split dataset into train and validation sets.")
parser.add_argument("-t", "--train", action="store_true", default=False,
help="Train with dataset.")
parser.add_argument("-p", "--predict", action="store_true", default=False,
help="Predict using saved model.")
parser.add_argument("-c", "--compare", type=str, default=None, nargs='?', choices=["optimizers"],
help="Compare models by plotting learning curves.")
# for model compiling
parser.add_argument('--optimizer', type=str, default='sgd',
help='Gradient descent optimizer')
parser.add_argument('--epochs', type=int, default=10,
help='Number of epochs to train')
parser.add_argument('--loss', type=str, default='binary_crossentropy',
help='Loss function')
parser.add_argument('--batch_size', type=int,
help='Batch size for training')
parser.add_argument('--learning_rate', type=float, default=1e-3,
help='Learning rate for optimizer')
return parser
if __name__ == "__main__":
train_path = config.data_dir + config.train_path
valid_path = config.data_dir + config.valid_path
try:
with open('description.txt', 'r') as file:
description = file.read()
except:
description = "multilayer perceptron"
parser = set_argparse()
args = parser.parse_args()
x_train, y_train = load_split_data(train_path)
x_val, y_val = load_split_data(valid_path)
trainer = ModelTrainer()
plotter = ModelPlotter()
histories, model_names = [], []
topology = None
if args.topology:
topology = load_topology(args.topology)
filename = os.path.basename(args.topology)
filename = os.path.splitext(filename)[0]
if args.split:
split_dataset_save(args.split, train_path, valid_path, train_size=0.8, random_state=42)
elif args.train:
model = trainer.create(topology)
optimizer_list = []
print(len(trainer.model_list))
# parameters = load_parameters("./parameters/" + filename)
# model.set_parameters(list(parameters))
# print(model.get_parameters())
for _ in range(len(trainer.model_list)):
optimizer_list.append(optimizers.SGD(learning_rate=1e-3))
histories, model_names = trainer.train(
trainer.model_list,
x_train,
y_train,
optimizer_list,
loss=args.loss,
# loss=losses.CrossEntropyLoss(),
# metrics=['accuracy', 'Precision', 'Recall'],
metrics=['accuracy'],
batch_size=args.batch_size,
epochs=args.epochs,
validation_data=(x_val, y_val),
)
if isinstance(model, Model):
# model.save_topology(config.topologies_dir + filename)
model.save_parameters(config.parameters_dir + filename)
elif args.predict:
prediction(filename)
elif args.compare == "optimizers":
histories, model_names = trainer.optimizer_test()
else:
print(f"Usage: python {sys.argv[0]} -h")
if histories and model_names:
plotter.set_model_histories(histories)
plotter.set_model_names(model_names)
plotter.plot()