-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun.py
More file actions
160 lines (137 loc) · 5.92 KB
/
Copy pathrun.py
File metadata and controls
160 lines (137 loc) · 5.92 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import os
import sys
sys.path.insert(1, './src')
from tagger import CrfModel
from data_loader import BuildDataLoader
from environment import LabelEnv
from agent import ParamRNN, AgentParamRNN
from gensim.models import KeyedVectors
import numpy as np
import random
import argparse
import matplotlib
import matplotlib.pyplot as plt
import pickle
import warnings; warnings.simplefilter('ignore')
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torchvision.transforms as T
from torch.autograd import Variable
# if gpu is to be used
device = torch.device('cuda' if torch.cuda.is_available() else "cpu")
print ("== Cuda: {} ===".format(torch.cuda.is_available()))
# ======================================== setting =====================================================
# parser definition
parser = argparse.ArgumentParser(description='Tune sequence active learning')
parser.add_argument('-b', '--budget', type=int, help='budget size', default=75)
parser.add_argument('-f', '--feature', choices=['all', 'node', 'edge'], help='choose features', default='all') # test/decode TBD
parser.add_argument('-g', '--greedy', help='top m test instances', default='rand')
parser.add_argument('-r', '--reward', help='shifted reward strategy', default='valid2V')
parser.add_argument('-s', '--source', help='dataset', default='sod')
parser.add_argument('-u', '--upLoop', type=int, help='increase loop by N', default=10)
parser.add_argument('-d', '--decay', type=int, help='loop decay', default=3)
parser.add_argument('-n', '--hidden', type=int, help='rnn hidden size', default=64)
parser.add_argument('-w', '--word2vec', type=int, help='use word2vec', default=0)
parser.add_argument('-x', '--replaceX', type=int, help='use x to replace digit', default=1)
parser.add_argument('-e', '--cuda', default='2')
args = parser.parse_args()
os.environ["CUDA_VISIBLE_DEVICES"] = args.cuda
SOURCE = args.source
DATA_PATH = "./datasets/" + SOURCE
NUM_FLAG = True if args.replaceX == 1 else False
EMBED_FLAG = True if args.word2vec == 1 else False
FEAT = args.feature
dataloader = BuildDataLoader(SOURCE, DATA_PATH, NUM_FLAG, EMBED_FLAG)
crf = CrfModel(dataloader, FEAT)
samples = [4]
VALID_N = 200
TEST_N = 200
TRAIN_N = 600
REWEIGHT = args.reward
GEEDY = args.greedy
BUDGET = args.budget
max_len = dataloader.get_max_len()
embedding_size = dataloader.get_embed_size()
parameter_shape = crf.get_para_shape()
print ("max_len is: {}".format(max_len))
print ("crf para size: {}".format(parameter_shape))
# ======================================== active learning =====================================================
qvalue_list = []
action_mark_list = []
prob_list = []
for seed in samples:
env = LabelEnv(dataloader, crf, seed, VALID_N, TEST_N, TRAIN_N, REWEIGHT, BUDGET)
agent = AgentParamRNN(GEEDY, max_len, embedding_size, parameter_shape)
print (">>>> Start play")
step = 0
while env.cost < BUDGET:
env.resume()
observation = env.get_state()
observ = [observation[0], observation[1], observation[3], observation[4], observation[5]]
greedy_flg, action, q_value = agent.get_action(observ)
reward, observation2, terminal = env.feedback(action)
(acc_test, acc_valid) = env.eval_tagger()
print ("cost {}: queried {} with greedy {}:{}, acc=({}, {})".format(env.cost, action, greedy_flg,
q_value.item(), acc_test, acc_valid))
qvalue_list.append(q_value.item())
action_mark_list.append(greedy_flg)
prob_list.append(observation[1][action])
if env.cost % 10 == 0:
step += 10
if env.cost < 3:
continue
for n in range(20 + step):
env.reboot()
while env.terminal == False:
observation = env.get_state()
observ = [observation[0], observation[1], observation[3], observation[4], observation[5]]
greedy_flg, action, q_value = agent.get_action(observ)
reward, observation2, terminal = env.feedback(action)
observ2 = [observation2[0], observation2[1], observation2[3], observation2[4], observation2[5]]
agent.update(observ, action, reward, observ2, terminal)
# ======================================== store result =====================================================
cost_list = sorted(env.acc_trace.keys())
acc_list = [env.acc_trace[i][0] for i in cost_list]
acc_valid_list = [env.acc_trace[i][1] for i in cost_list]
num = "num" if NUM_FLAG else ""
emb = "embed" if EMBED_FLAG else ""
filename = "./results_run/" + SOURCE + num + emb + "_" + str(BUDGET) + "bgt_" + GEEDY + "_" + REWEIGHT + "_" + FEAT
with open(filename + ".bin", "wb") as result:
pickle.dump((cost_list, acc_list, acc_valid_list), result)
# ======================================== sanity check =====================================================
x = range(len(qvalue_list))
fig, axes = plt.subplots(ncols=2, nrows=1)
ax = axes.flatten()
ax[0].plot(x, qvalue_list, color='0.5')
x2 = []
y2 = []
for i in x:
if action_mark_list[i] == 1:
x2.append(i)
y2.append(qvalue_list[i])
l1 = ax[0].scatter(x2, y2, color='r', marker='o')
x2 = []
y2 = []
for i in x:
if action_mark_list[i] == 0:
x2.append(i)
y2.append(qvalue_list[i])
l2 = ax[0].scatter(x2, y2, color='g', marker='x')
ax[0].legend((l1,l2),
('$action > \\epsilon$', '$action < \\epsilon$'))
ax[0].set_title('{} dataset'.format(SOURCE))
# plt.xlim(0, 20)
ax[0].set_ylabel('Q value')
ax[0].set_xlabel('Sequence number')
# print (qvalue_list)
ax[1].scatter(qvalue_list, prob_list)
for i in range(len(qvalue_list)):
ax[1].annotate(i, (qvalue_list[i], prob_list[i]))
ax[1].set_xlabel('Q value')
ax[1].set_ylabel('Likelihood')
plt.subplots_adjust(wspace=0.1)
fig.set_size_inches(15,5)
# plt.show()
plt.savefig(filename + '_check.png')