Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds versioning to the model weight files #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions riverraid.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ def executeKActions(action, prevObservation):
break
return recentKObservations, rewardTotal, done

def getNextModelName():
max = 0
for filename in os.listdir('.'):
if filename.startswith('model') and filename.endswith('.h5'):
i = filename.split('.')[0].split('model')[1]
if max < i:
max = i
return "model{}.h5".format(max+1)

def getMostRecentModel():
max = 0
for filename in os.listdir('.'):
if filename.startswith('model') and filename.endswith('.h5'):
i = filename.split('.')[0].split('model')[1]
if max < i:
max = i
return "model{}.h5".format(max)


if __name__ == '__main__':
Expand All @@ -104,9 +121,10 @@ def executeKActions(action, prevObservation):
Q = initNet()
Q.summary()
#plot_model(Q, to_file='model.png')
if os.path.exists("model.h5"):
recentModel = getMostRecentModel()
if os.path.exists(recentModel):
print "load weights from previous run"
Q.load_weights("model.h5")
Q.load_weights(recentModel)
else :
exit
# TODO: figure out if cnn creation is deterministic
Expand Down Expand Up @@ -219,7 +237,7 @@ def executeKActions(action, prevObservation):
if c == UPDATE_FREQUENCY:
weights = Q.get_weights()
QHat.set_weights(weights)
QHat.save_weights("model.h5")
QHat.save_weights(getNextModelName())
c = 0
print "target NN update={}".format(num_target_updates)
else:
Expand All @@ -230,4 +248,4 @@ def executeKActions(action, prevObservation):


print "average reward={}".format(average/NUM_EPISODES)
#QHat.save_weights("model.h5")
#QHat.save_weights("model0.h5")
17 changes: 13 additions & 4 deletions riverraid_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,26 @@ def executeKActions(action, prevObservation):
break
return recentKObservations, rewardTotal, done


def getMostRecentModel():
max = 0
for filename in os.listdir('.'):
if filename.startswith('model') and filename.endswith('.h5'):
i = filename.split('.')[0].split('model')[1]
if max < i:
max = i
return "model{}.h5".format(max)

if __name__ == '__main__':
env = gym.make('Riverraid-v0')
memory = deque([], REPLAY_MEMORY_SIZE)
Q = initNet()
Q.summary()
#plot_model(Q, to_file='model.png')
if os.path.exists("model.h5"):

recentModel = getMostRecentModel()
if os.path.exists(recentModel):
print "load weights from previous run"
Q.load_weights("model.h5")
Q.load_weights(recentModel)
else :
exit
# TODO: figure out if cnn creation is deterministic
Expand Down Expand Up @@ -153,4 +162,4 @@ def executeKActions(action, prevObservation):
print "average reward={}".format(numpy.mean(episode_rewards))
print "std dev reward={}".format(numpy.std(episode_rewards))
print "median reward={}".format(numpy.median(episode_rewards))
#QHat.save_weights("model.h5")
#QHat.save_weights("model0.h5")