-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkerasrl_utils.py
32 lines (27 loc) · 1.15 KB
/
kerasrl_utils.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
import re
from gym import error
import glob
# checkpoints/KerasDDPG-InvertedPendulum-v0-20170701190920_actor.h5
weight_save_re = re.compile(r'^(?:\w+\/)+?(\w+-v\d+)-(\w+-v\d+)-(\d+)(?:_\w+)?\.(\w+)$')
def get_fields(weight_save_name):
match = weight_save_re.search(weight_save_name)
if not match:
raise error.Error(
'Attempted to read a malformed weight save: {}. (Currently all weight saves must be of the form {}.)'
.format(id, weight_save_re.pattern))
return match.group(1), match.group(2), int(match.group(3))
def get_latest_save(file_folder, agent_name, env_name, version_number):
"""
Returns the properties of the latest weight save. The information can be used to generate the loading path
:return:
"""
path = "%s%s" % (file_folder, "*.h5")
file_list = glob.glob(path)
latest_file_properties = []
file_properties = []
for f in file_list:
file_properties = get_fields(f)
if file_properties[0] == agent_name and file_properties[1] == env_name and (
latest_file_properties == [] or file_properties[2] > latest_file_properties[2]):
latest_file_properties = file_properties
return latest_file_properties