-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraining.py
More file actions
executable file
·68 lines (56 loc) · 2.12 KB
/
training.py
File metadata and controls
executable file
·68 lines (56 loc) · 2.12 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
64
65
66
67
68
#!/usr/bin/python3
import argparse as ap
import decimal
import matplotlib.pyplot as plt
import textwrap
from models import linear_regression as lr
def training(
datafile: str, storage: str, iter: int, learning_rate: decimal.Decimal,
):
try:
data = lr.Data(datafile)
except lr.InvalidData as ex:
print(f'\033[91m-> Error: {ex}\033[0m')
return
try:
model = lr.Manager(storage, learning_rate)
except lr.InvalidModel as ex:
print(f'\033[91m-> Error: {ex}\033[0m')
return
model.train(data, iter)
print(f'\033[96m-> Coefficients are dumped to {model.storage}\033[0m')
plt.subplot(311)
data.plot()
model.plot_function(data)
plt.subplot(313)
model.plot_errors()
plt.show()
if __name__ == '__main__':
class CustomFormatter(ap.ArgumentDefaultsHelpFormatter,
ap.RawDescriptionHelpFormatter):
pass
parser = ap.ArgumentParser(
description=textwrap.dedent('''\
Train model on data from [datafile] using linear regression:
Y = theta0 + theta1 * X
Save calculated coefficients theta and precision
to [storage] file in JSON string format.
'''),
formatter_class=CustomFormatter,
)
parser.add_argument('-d', metavar='datafile', type=str, default='data/data.csv',
help='file with data for training')
parser.add_argument('-i', metavar='iterations', type=int, default=10000,
help='number of training iterations')
parser.add_argument('-lr', metavar='rate', type=str, default='0.01',
help='learning rate')
parser.add_argument('-s', metavar='storage', type=str, default='model.json',
help='file to store calculated coefficients')
args = parser.parse_args()
try:
learning_rate = decimal.Decimal(args.lr)
except decimal.InvalidOperation:
print('\033[91m-> Error: Wrong format for learning rate, try again, please\n'
'for example: 0.01\033[0m')
else:
training(args.d, args.s, args.i, learning_rate)