-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample.py
53 lines (40 loc) · 1.45 KB
/
sample.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
r'''
python sample.py --load runs/deal_response/model.pkl \
--eval_file runs/response_train.jsons \
--device gpu1
'''
import pickle
import sys
import json
from stanza.research import config
from stanza.research.instance import Instance
import thutils
import run_experiment
from seq2seq import SimpleSeq2SeqLearner
def sample(model_pkl_file, device, insts_file):
dev_insts = []
with open(insts_file, 'r') as infile:
for line in infile:
line = line.strip()
if line:
dev_insts.append(Instance(**json.loads(line)))
with thutils.device_context(device):
with open(model_pkl_file, 'rb') as infile:
model = pickle.load(infile)
import pdb; pdb.set_trace()
samples = model.predict(dev_insts, random=True, verbosity=0)
for inst, sample in zip(dev_insts, samples):
print(json.dumps(sample))
def test_selection(model, inst, verbose=False, target='<disagree> <disagree> <disagree>'):
samps = []
for _ in range(1000):
s = model.predict([inst], random=True, verbosity=0)[0]
samps.append(s)
if verbose:
print(s)
return samps.count(target)
def test_response(model, inst, verbose=False, target='<selection>'):
return test_selection(model, inst, verbose=verbose, target=target)
if __name__ == '__main__':
options = config.options()
sample(options.load, options.device, options.eval_file)