-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathrun_model.py
64 lines (52 loc) · 1.77 KB
/
run_model.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
import curses
import sys
import os
import torch
import time
from engine import TetrisEngine
from dqn_agent import BasicFF, ReplayMemory, Transition
from torch.autograd import Variable
use_cuda = torch.cuda.is_available()
FloatTensor = torch.cuda.FloatTensor if use_cuda else torch.FloatTensor
LongTensor = torch.cuda.LongTensor if use_cuda else torch.LongTensor
width, height = 10, 20 # standard tetris friends rules
engine = TetrisEngine(width, height)
def load_model(filename):
model = BasicFF()
if use_cuda:
model.cuda()
checkpoint = torch.load(filename)
model.load_state_dict(checkpoint['state_dict'])
return model
def run(model):
state = FloatTensor(engine.clear()[None,None,:,:])
score = 0
while True:
action = model(Variable(state,
volatile=True).type(FloatTensor)).data.max(1)[1].view(1,1).type(LongTensor)
print( model(Variable(state,
volatile=True).type(FloatTensor)).data)
state, reward, done = engine.step(action.item())
state = FloatTensor(state[None,None,:,:])
# Accumulate reward
score += int(reward)
stdscr = curses.initscr()
stdscr.clear()
stdscr.addstr(str(engine))
stdscr.addstr('\ncumulative reward: ' + str(score))
stdscr.addstr('\nreward: ' + str(reward))
time.sleep(.1)
if done:
print('score {0}'.format(score))
break
if len(sys.argv) <= 1:
print('specify a filename to load the model')
sys.exit(1)
if __name__ == '__main__':
filename = sys.argv[1]
if os.path.isfile(filename):
print("=> loading model '{}'".format(filename))
model = load_model(filename).eval()
run(model)
else:
print("=> no file found at '{}'".format(filename))