-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnn_utils.py
More file actions
91 lines (74 loc) · 3.11 KB
/
Copy pathnn_utils.py
File metadata and controls
91 lines (74 loc) · 3.11 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import tensorflow as tf
import numpy as np
from pathlib import Path
#takes a placeholder with input size, and makes hidden layers based off of hidden_size array
def get_FC_model(input, scope, trainable, hidden_size=[64,64], output_size=4):
with tf.variable_scope(scope):
x = input
for size in hidden_size:
x = tf.layers.dense(x, size, activation=tf.nn.tanh, trainable=trainable)
output = tf.layers.dense(x, output_size, trainable=trainable)
return output
def save_most_recent(save_path, saver, sess):
save_name = str(
save_path + "/" + "weights._most_recent_.ckpt"
)
saver.save(sess, save_name)
def save_model(save_path, saver, sess, cost):
save_name = str(
save_path + "/" + "weights.cost_{:.4f}_.ckpt".format(cost)
)
saver.save(sess, save_name)
print("Model Saved with cost {}".format(cost))
def restore_most_recent(save_path, saver, sess):
save_name = str(
save_path + "/" + "weights._most_recent_.ckpt"
)
saver.restore(sess, save_name)
def restore_from_lowest_cost(save_path, saver, sess):
save_path = Path(save_path)
best_cost = 100000000
try:
if not save_path.exists():
save_path.mkdir()
weights_files = [wf for wf in save_path.glob("*.ckpt*") if not "_most_recent_" in str(wf)]
if len(weights_files) == 0:
raise IOError(
"No weights to restore from at {0}".format(str(save_path)))
weights_cost = [float(wf.name.split("_")[1]) for wf in weights_files]
min_idx = np.argmin(weights_cost)
best_cost = weights_cost[min_idx]
best_weights_file = str(weights_files[min_idx])
rmvidx = best_weights_file.index(".ckpt")
best_weights_file = best_weights_file[:rmvidx+5]
saver.restore(sess, best_weights_file)
print("Successfully restored model with cost {:.4f}".format(
best_cost))
except IOError as e:
print(e)
return best_cost
def restore_from_highest_cost(save_path, saver, sess):
save_path = Path(save_path)
best_cost = -100000000
try:
if not save_path.exists():
save_path.mkdir()
weights_files = [wf for wf in save_path.glob("*.ckpt*") if not "_most_recent_" in str(wf)]
if len(weights_files) == 0:
raise IOError(
"No weights to restore from at {0}".format(str(save_path)))
weights_cost = [float(wf.name.split("_")[1]) for wf in weights_files]
max_idx = np.argmax(weights_cost)
best_cost = weights_cost[max_idx]
best_weights_file = str(weights_files[max_idx])
rmvidx = best_weights_file.index(".ckpt")
best_weights_file = best_weights_file[:rmvidx+5]
saver.restore(sess, best_weights_file)
print("Successfully restored model with cost {:.4f}".format(
best_cost))
except IOError as e:
print(e)
return best_cost
if __name__ == "__main__":
# sess = tf.Session()
restore_from_lowest_cost(Path("./weights/"), None, None)