-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
wob_click_train.py
executable file
·138 lines (111 loc) · 5.61 KB
/
wob_click_train.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python3
import os
import gym
import random
import universe
import argparse
import numpy as np
from tensorboardX import SummaryWriter
from lib import wob_vnc, model_vnc, common, vnc_demo
import ptan
import torch
import torch.nn.utils as nn_utils
import torch.nn.functional as F
import torch.optim as optim
REMOTES_COUNT = 8
ENV_NAME = "wob.mini.ClickDialog-v0"
GAMMA = 0.99
REWARD_STEPS = 2
BATCH_SIZE = 16
LEARNING_RATE = 0.0001
ENTROPY_BETA = 0.001
CLIP_GRAD = 0.05
DEMO_PROB = 0.5
CUT_DEMO_PROB_FRAMES = 25000 # After how many frames, demo probability will be dropped to 1%
SAVES_DIR = "saves"
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-n", "--name", required=True, help="Name of the run")
parser.add_argument("--cuda", default=False, action='store_true', help="CUDA mode")
parser.add_argument("--port-ofs", type=int, default=0, help="Offset for container's ports, default=0")
parser.add_argument("--env", default=ENV_NAME, help="Environment name to solve, default=" + ENV_NAME)
parser.add_argument("--demo", help="Demo dir to load. Default=No demo")
parser.add_argument("--host", default='localhost', help="Host with docker containers")
args = parser.parse_args()
device = torch.device("cuda" if args.cuda else "cpu")
env_name = args.env
if not env_name.startswith('wob.mini.'):
env_name = "wob.mini." + env_name
name = env_name.split('.')[-1] + "_" + args.name
writer = SummaryWriter(comment="-wob_click_" + name)
saves_path = os.path.join(SAVES_DIR, name)
os.makedirs(saves_path, exist_ok=True)
demo_samples = None
if args.demo:
demo_samples = vnc_demo.load_demo(args.demo, env_name)
if not demo_samples:
demo_samples = None
else:
print("Loaded %d demo samples, will use them during training" % len(demo_samples))
env = gym.make(env_name)
env = universe.wrappers.experimental.SoftmaxClickMouse(env)
env = wob_vnc.MiniWoBCropper(env)
wob_vnc.configure(env, wob_vnc.remotes_url(port_ofs=args.port_ofs, hostname=args.host, count=REMOTES_COUNT))
net = model_vnc.Model(input_shape=wob_vnc.WOB_SHAPE, n_actions=env.action_space.n).to(device)
print(net)
optimizer = optim.Adam(net.parameters(), lr=LEARNING_RATE, eps=1e-3)
agent = ptan.agent.PolicyAgent(lambda x: net(x)[0], device=device, apply_softmax=True)
exp_source = ptan.experience.ExperienceSourceFirstLast(
[env], agent, gamma=GAMMA, steps_count=REWARD_STEPS, vectorized=True)
best_reward = None
with common.RewardTracker(writer) as tracker:
with ptan.common.utils.TBMeanTracker(writer, batch_size=10) as tb_tracker:
batch = []
for step_idx, exp in enumerate(exp_source):
rewards_steps = exp_source.pop_rewards_steps()
if rewards_steps:
rewards, steps = zip(*rewards_steps)
tb_tracker.track("episode_steps", np.mean(steps), step_idx)
mean_reward = tracker.reward(np.mean(rewards), step_idx)
if mean_reward is not None:
if best_reward is None or mean_reward > best_reward:
if best_reward is not None:
name = "best_%.3f_%d.dat" % (mean_reward, step_idx)
fname = os.path.join(saves_path, name)
torch.save(net.state_dict(), fname)
print("Best reward updated: %.3f -> %.3f" % (best_reward, mean_reward))
best_reward = mean_reward
batch.append(exp)
if len(batch) < BATCH_SIZE:
continue
if step_idx > CUT_DEMO_PROB_FRAMES:
DEMO_PROB = 0.01
if demo_samples and random.random() < DEMO_PROB:
random.shuffle(demo_samples)
demo_batch = demo_samples[:BATCH_SIZE]
model_vnc.train_demo(net, optimizer, demo_batch, writer, step_idx,
preprocessor=ptan.agent.default_states_preprocessor,
device=device)
states_v, actions_t, vals_ref_v = \
common.unpack_batch(batch, net, last_val_gamma=GAMMA ** REWARD_STEPS, device=device)
batch.clear()
optimizer.zero_grad()
logits_v, value_v = net(states_v)
loss_value_v = F.mse_loss(value_v.squeeze(-1), vals_ref_v)
log_prob_v = F.log_softmax(logits_v, dim=1)
adv_v = vals_ref_v - value_v.detach()
log_prob_actions_v = adv_v * log_prob_v[range(BATCH_SIZE), actions_t]
loss_policy_v = -log_prob_actions_v.mean()
prob_v = F.softmax(logits_v, dim=1)
entropy_loss_v = ENTROPY_BETA * (prob_v * log_prob_v).sum(dim=1).mean()
loss_v = loss_policy_v + entropy_loss_v + loss_value_v
loss_v.backward()
nn_utils.clip_grad_norm_(net.parameters(), CLIP_GRAD)
optimizer.step()
tb_tracker.track("advantage", adv_v, step_idx)
tb_tracker.track("values", value_v, step_idx)
tb_tracker.track("batch_rewards", vals_ref_v, step_idx)
tb_tracker.track("loss_entropy", entropy_loss_v, step_idx)
tb_tracker.track("loss_policy", loss_policy_v, step_idx)
tb_tracker.track("loss_value", loss_value_v, step_idx)
tb_tracker.track("loss_total", loss_v, step_idx)