-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
34 lines (29 loc) · 1.07 KB
/
helpers.py
File metadata and controls
34 lines (29 loc) · 1.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
import os, shutil
# save train or validation loss
def log_loss(loss_val : float, path_to_save_loss : str, train : bool = True):
if train:
file_name = "train_loss.txt"
else:
file_name = "val_loss.txt"
path_to_file = path_to_save_loss+file_name
os.makedirs(os.path.dirname(path_to_file), exist_ok=True)
with open(path_to_file, "a") as f:
f.write(str(loss_val)+"\n")
f.close()
# Exponential Moving Average, https://en.wikipedia.org/wiki/Moving_average
def EMA(values, alpha=0.1):
ema_values = [values[0]]
for idx, item in enumerate(values[1:]):
ema_values.append(alpha*item + (1-alpha)*ema_values[idx])
return ema_values
# Remove all files from previous executions and re-run the model.
def clean_directory():
if os.path.exists('save_loss'):
shutil.rmtree('save_loss')
if os.path.exists('save_model'):
shutil.rmtree('save_model')
if os.path.exists('save_predictions'):
shutil.rmtree('save_predictions')
os.mkdir("save_loss")
os.mkdir("save_model")
os.mkdir("save_predictions")