-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
206 lines (142 loc) · 5.27 KB
/
app.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import tensorflow as tf
from tensorflow import keras
import numpy as np
import time
from datetime import datetime as dt
import os
from threading import Thread
from flask import Flask, render_template
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
np.random.seed(time.time_ns() % ((1 << 32) - 1))
tf.random.set_seed(time.time_ns())
N = 6400
a, b = 0.3, 5
x = np.linspace(0, 32, N)
y = a * x + b + np.random.uniform(-1, 1, N)
m,c = 50,90
LEARNING_RATE = 0.001
TRAINING_EPOCHS = 500
total_epochs = None
training_result = None
model = None
weights=None
biases = None
class TrainingCallback(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
global total_epochs, TRAINING_EPOCHS
print(f"epoch {total_epochs + epoch}/{TRAINING_EPOCHS}", end="\r", flush=True)
def perform_training():
global training, total_epochs, training_start_time
training_start_time = time.time()
while training and total_epochs < TRAINING_EPOCHS:
model.fit(
x,
y,
epochs=TRAINING_EPOCHS // 100,
callbacks=[TrainingCallback()],
verbose=1
)
total_epochs += TRAINING_EPOCHS // 100
print(f"epoch {total_epochs}/{TRAINING_EPOCHS}")
weights, biases = model.get_weights()
cost = model.evaluate(x, y, verbose=0)
training = False
finalize_training()
def finalize_training():
global training_end_time, training_result, weights, biases
training_end_time = time.time()
print(f"[{dt.time(dt.now())}]: training completed!")
weights, biases = model.get_weights()
cost = model.evaluate(x, y, verbose=0)
training_result = '\n'.join([
f"It took {training_end_time - training_start_time: .3f} seconds",
f"We used {m = }, {c = } and some noise",
f"After training, we have m = {weights[0][0]: .3f}, c = {biases[0]: .3f}"
])
print(training_result)
def init_model():
model = keras.Sequential([
keras.layers.Dense(1)
])
optimizer = tf.keras.optimizers.RMSprop(learning_rate=LEARNING_RATE)
model.compile(loss="mean_squared_error", optimizer=optimizer)
return model
app = Flask(__name__)
@app.route("/", endpoint="inner", methods=['GET','POST'])
def index():
return render_template('index.html')
total_epochs = 0
@app.route("/start", endpoint="start_training", methods=['GET'])
def start_training():
global model, training, training_end_time, training_thread
if model is None:
model = init_model()
training = True
training_thread = Thread(target=perform_training)
print(f"[{dt.time(dt.now())}]: Model training has started!")
training_start_time = time.time()
training_thread.start()
return f"""
[{dt.time(dt.now())}]: training successfully started!
"""
elif training:
return """
Training is already in progress!
See <a href="/status">/status</a>
"""
return """
Model is already trained!
"""
@app.route("/pause", endpoint="stop_training", methods=['GET','POST'])
def pause_training():
global model, training, training_end_time, training_thread
if model is not None:
if training:
training = False
training_thread.join()
return f"Training paused! After training, we have m = {weights[0][0]: .3f}, c = {biases[0]: .3f}"
else:
return f"Training Already completed! After training, we have m = {weights[0][0]: .3f}, c = {biases[0]: .3f}"
return """
Model is not initiliazed!
See <a href="/start">/start</a>
"""
@app.route("/status", endpoint="status_training", methods=['GET','POST'])
def status_training():
global model, training, training_end_time, training_thread
if model is not None:
if training:
return f"""
Training is in progress...
Epochs completed: {total_epochs}
Time spent: {time.time() - training_start_time: .3f} seconds
"""
return f"""
Training completed!
Total epochs completed: {total_epochs}
Time taken: {training_end_time - training_start_time: .3f} seconds
After training, we have m = {weights[0][0]: .3f}, c = {biases[0]: .3f}
"""
return """
Model is not initiliazed!
Click <a href="/start">/start</a>
"""
@app.route("/resume", endpoint="resume_training", methods=['GET'])
def resume_training():
global model, training, training_end_time, training_thread
if model is None:
return "Model is not initiliazed. Please start training first"
elif training is False:
training = True
m = weights[0][0]
c = biases[0]
training_thread = Thread(target=perform_training)
print(f"[{dt.time(dt.now())}]: Model training is resumed!")
training_thread.start()
return """
Training is Resumed!
See <a href="/status">/status</a>
"""
return "Training is already ongoing!"
if __name__ == "__main__":
app.run()