-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprocess_opt.py
127 lines (103 loc) · 3.94 KB
/
process_opt.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
import torch
import numpy as np
from pykp.io import SEP_WORD, EOS_WORD
import random
import os
import logging
import json
def process_opt(opt):
if opt.seed > 0:
torch.manual_seed(opt.seed)
np.random.seed(opt.seed)
random.seed(opt.seed)
# if torch.cuda.is_available() and not opt.gpuid:
# opt.gpuid = 0
if opt.delimiter_type == 0:
opt.delimiter_word = SEP_WORD
else:
opt.delimiter_word = EOS_WORD
# my configuration
opt.data = "processed_data/{}/".format(opt.data_tag)
opt.vocab = opt.data
opt.exp = 'trial.' + opt.data_tag if opt.trial else opt.data_tag
# seq2seq setting
if 'Weibo' in opt.data_tag:
opt.vocab_size = 50000
opt.word_vec_size = 150
elif 'Twitter' in opt.data_tag:
opt.vocab_size = 30000
opt.word_vec_size = 150
elif 'StackExchange' in opt.data_tag:
opt.vocab_size = 50000
opt.word_vec_size = 150
else:
print('Wrong data_tag!!')
return
opt.encoder_size = 150
opt.decoder_size = 300
size_tag = ".emb{}".format(opt.word_vec_size) + ".vs{}".format(opt.vocab_size) + ".dec{}".format(opt.decoder_size)
# only train ntm
if opt.only_train_ntm:
assert opt.ntm_warm_up_epochs > 0 and not opt.load_pretrain_ntm
opt.exp += '.topic_num{}'.format(opt.topic_num)
opt.exp += '.ntm_warm_up_%d' % opt.ntm_warm_up_epochs
opt.model_path = opt.model_path % (opt.exp, opt.timemark)
if not os.path.exists(opt.model_path):
os.makedirs(opt.model_path)
print("Only training the ntm for %d epochs and save it to %s" % (opt.ntm_warm_up_epochs, opt.model_path))
return opt
# joint train settings
if opt.joint_train:
opt.exp += '.joint_train'
if opt.add_two_loss:
opt.exp += '.add_two_loss'
if opt.joint_train_strategy != 'p_1_joint':
opt.exp += '.' + opt.joint_train_strategy
opt.p_seq2seq_e = int(opt.joint_train_strategy.split('_')[1])
if opt.joint_train_strategy.split('_')[-1] != 'joint':
opt.iterate_train_ntm = True
# adding topic settings
if opt.use_topic_represent:
opt.exp += '.use_topic'
opt.exp += '.topic_num{}'.format(opt.topic_num)
if opt.topic_type == 'z':
opt.exp += '.z_topic'
if opt.topic_attn:
opt.exp += '.topic_attn'
if not opt.topic_dec:
opt.exp += '.no_topic_dec'
if opt.topic_copy:
opt.exp += '.topic_copy'
if opt.topic_attn_in:
opt.exp += '.topic_attn_in'
if opt.load_pretrain_ntm:
has_topic_num = [t for t in opt.check_pt_ntm_model_path.split('.') if 'topic_num' in t]
if len(has_topic_num) != 0:
assert opt.topic_num == int(has_topic_num[0].replace('topic_num', ''))
ntm_tag = '.'.join(opt.check_pt_ntm_model_path.split('/')[-1].split('.')[:-1])
# opt.exp += '.ntm_%s' % ntm_tag
else:
opt.exp += '.ntm_warm_up_%d' % opt.ntm_warm_up_epochs
if opt.bridge != "copy":
opt.exp += '.{}_bridge'.format(opt.bridge)
if opt.copy_attention:
opt.exp += '.copy'
opt.exp += '.seed{}'.format(opt.seed)
opt.exp += size_tag
# fill time into the name
if opt.model_path.find('%s') > 0:
opt.model_path = opt.model_path % (opt.exp, opt.timemark)
if not os.path.exists(opt.model_path):
os.makedirs(opt.model_path)
logging.info('Model_PATH : ' + opt.model_path)
# dump the setting (opt) to disk in order to reuse easily
if opt.train_from:
opt = torch.load(
open(os.path.join(opt.model_path, 'initial.config'), 'rb')
)
else:
torch.save(opt,
open(os.path.join(opt.model_path, 'initial.config'), 'wb')
)
json.dump(vars(opt), open(os.path.join(opt.model_path, 'initial.json'), 'w'))
return opt