Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.py
More file actions
Latest commit
67 lines (60 loc) · 3.22 KB
/
Copy pathmain.py
File metadata and controls
67 lines (60 loc) · 3.22 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
importtensorflowastf
importos
importnumpyasnp
fromdata_helperimportread_vocab_to_dict, build_glove, dataset_iterator, load_verb_count
frommodelimportModel
fromloggerimportProgbar
os.environ["CUDA_DEVICE_ORDER"] ="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] ="0,1"
glove_path=os.path.join(os.path.expanduser("~"), "data", "embeddings", "glove", "glove.840B.300d.txt")
# read word and verb dict
print("load dictionary...")
word_dict=read_vocab_to_dict("data/word_vocab.txt")
verb_dict=read_vocab_to_dict("data/verb_vocab.txt")
verb_vocab_count=load_verb_count("data/verb_count.txt")
flags=tf.flags
flags.DEFINE_integer("neg_sample", 10, "number of negative samples")
flags.DEFINE_integer("word_dim", 300, "word embedding dimension")
flags.DEFINE_integer("num_units", 100, "number of units for rnn cell and hidden layer of ffn")
flags.DEFINE_integer("output_units", 200, "number of units for output part")
flags.DEFINE_bool("use_pretrained", True, "use pretrained word2vec")
flags.DEFINE_bool("tune_emb", True, "tune pretrained embeddings while training")
flags.DEFINE_string("glove_path", glove_path, "pretrained glove embeddings path")
flags.DEFINE_string("pretrained_context", "data/glove_context.npz", "pretrained context embedding path")
flags.DEFINE_string("pretrained_target", "data/glove_target.npz", "pretrained target embedding path")
flags.DEFINE_integer("vocab_size", len(word_dict), "word vocab size")
flags.DEFINE_integer("verb_size", len(verb_dict), "verb vocab size")
flags.DEFINE_float("lr", 0.001, "learning_rate")
flags.DEFINE_integer("batch_size", 300, "batch size")
flags.DEFINE_string("dataset", "data/dataset.txt", "dataset")
flags.DEFINE_integer("epochs", 3, "epochs")
flags.DEFINE_string("ckpt", "ckpt/", "checkpoint path")
flags.DEFINE_string("model_name", "concept_prime", "model name")
config=flags.FLAGS
# initialize with pretrained glove embeddings
ifnotos.path.exists(config.pretrained_context) ornotos.path.exists(config.pretrained_target):
build_glove(config.glove_path, config.pretrained_context, config.pretrained_target, word_dict, verb_dict,
config.word_dim)
ifnotos.path.exists(config.ckpt):
os.makedirs(config.ckpt)
# training the model
print("start training...")
sess_config=tf.ConfigProto(allow_soft_placement=True)
sess_config.gpu_options.allow_growth=True
withtf.Session(config=sess_config) assess:
# build model
print("build model...")
model=Model(config, verb_vocab_count)
sess.run(tf.global_variables_initializer())
saver=tf.train.Saver(max_to_keep=1)
forepochinrange(config.epochs):
prog=Progbar(target=int(2.663e8) /config.batch_size)
fori, datainenumerate(dataset_iterator(config.dataset, word_dict, verb_dict, config.batch_size)):
feed_dict=model.get_feed_dict(data, is_train=True, lr=config.lr)
_, losses=sess.run([model.train_op, model.loss], feed_dict=feed_dict)
prog.update(i+1, [("train loss", losses)])
# save the model
saver.save(sess, config.ckpt+config.model_name, global_step=config.epochs)
# save the trained target embedding
target_emb=sess.run(model.verb_embeddings)
np.savez_compressed("data/trained_target_emb.npz", embeddings=target_emb)