-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_dqn_pong.py
More file actions
101 lines (74 loc) · 2.83 KB
/
Copy pathrun_dqn_pong.py
File metadata and controls
101 lines (74 loc) · 2.83 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
92
93
94
95
from torch import save
from Wrapper.layers import *
from Wrapper.wrappers import make_atari, wrap_deepmind, wrap_pytorch
import math, random
import gym
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.optim as optim
import torch.autograd as autograd
import torch.nn.functional as F
import pickle
USE_CUDA = torch.cuda.is_available()
from dqn import QLearner, compute_td_loss, ReplayBuffer
env_id = "PongNoFrameskip-v4"
env = make_atari(env_id)
env = wrap_deepmind(env)
env = wrap_pytorch(env)
num_frames = 1000000
batch_size = 32
gamma = 0.99
record_idx = 10000
replay_initial = 10000
replay_buffer = ReplayBuffer(100000)
model = QLearner(env, num_frames, batch_size, gamma, replay_buffer)
model.load_state_dict(torch.load("name.pth", map_location='cpu'))
target_model = QLearner(env, num_frames, batch_size, gamma, replay_buffer)
target_model.copy_from(model)
optimizer = optim.Adam(model.parameters(), lr=0.00001)
if USE_CUDA:
model = model.cuda()
target_model = target_model.cuda()
print("Using cuda new")
epsilon_start = 1.0
epsilon_final = 0.01
epsilon_decay = 30000
epsilon_by_frame = lambda frame_idx: epsilon_final + (epsilon_start - epsilon_final) * math.exp(-1. * frame_idx / epsilon_decay)
losses = []
all_rewards = []
episode_reward = 0
state = env.reset()
for frame_idx in range(1, num_frames + 1):
#print("Frame: " + str(frame_idx))
epsilon = epsilon_by_frame(frame_idx)
action = model.act(state, epsilon)
next_state, reward, done, _ = env.step(action)
replay_buffer.push(state, action, reward, next_state, done)
state = next_state
episode_reward += reward
if done:
state = env.reset()
all_rewards.append((frame_idx, episode_reward))
episode_reward = 0
if len(replay_buffer) > replay_initial:
loss = compute_td_loss(model, target_model, batch_size, gamma, replay_buffer)
optimizer.zero_grad()
loss.backward()
optimizer.step()
losses.append((frame_idx, loss.data.cpu().numpy()))
if frame_idx % 10000 == 0 and len(replay_buffer) <= replay_initial:
print('#Frame: %d, preparing replay buffer' % frame_idx)
if frame_idx % 10000 == 0 and len(replay_buffer) > replay_initial:
print('#Frame: %d, Loss: %f' % (frame_idx, np.mean(losses, 0)[1]))
print('Last-10 average reward: %f' % np.mean(all_rewards[-10:], 0)[1])
#with open("frames.pkl", "wb") as frames_file:
# pickle.dump(frame_idx, frames_file)
if frame_idx % 50000 == 0:
target_model.copy_from(model)
torch.save(model.state_dict(), 'name.pth')
with open("losses_file.pkl", "wb") as losses_file:
pickle.dump(losses, losses_file)
with open("all_rewards_file.pkl", "wb") as all_rewards_file:
pickle.dump(all_rewards, all_rewards_file)