def main():
    dict_ = pickle.load(open(Params.data_dir + "dictionary.pkl","r"))
    vocab_size = dict_.vocab_size
    model = Model(is_training = True, vocab_size = vocab_size); print("Built model")
    init = False
    devdata, dev_ind = get_dev()
    if not os.path.isfile(os.path.join(Params.logdir,"checkpoint")):
        init = True
        glove = np.memmap(Params.data_dir + "glove.np", dtype = np.float32, mode = "r")
        glove = np.reshape(glove,(vocab_size,Params.emb_size))
    with model.graph.as_default():
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        sv = tf.train.Supervisor(logdir=Params.logdir,
                        save_model_secs=0,
                        global_step = model.global_step,
                        init_op = model.init_op)
        with sv.managed_session(config = config) as sess:
            if init: sess.run(model.emb_assign, {model.word_embeddings_placeholder:glove})
            pr = Params()
            pr.dump_config(Params.__dict__)
            for epoch in range(1, Params.num_epochs+1):
                train_loss = []
                if sv.should_stop(): break
                for step in tqdm(range(model.num_batch), total = model.num_batch, ncols=70, leave=False, unit='b'):
                    _, loss = sess.run([model.train_op, model.mean_loss],
                                        feed_dict={model.dropout: Params.dropout if Params.dropout is not None else 0.0})
                    train_loss.append(loss)
                    if step % Params.save_steps == 0:
                        gs = sess.run(model.global_step)
                        sv.saver.save(sess, Params.logdir + '/model_epoch_%d_step_%d'%(gs//model.num_batch, gs%model.num_batch))
                    if step % Params.dev_steps == 0:
                        EM_ = []
                        F1_ = []
                        dev = []
                        for i in range(Params.dev_batchs):
                            sample = np.random.choice(dev_ind, Params.batch_size)
                            feed_dict = {data: devdata[i][sample] for i,data in enumerate(model.data)}
                            index, dev_loss = sess.run([model.output_index, model.mean_loss], feed_dict = feed_dict)
                            F1, EM = 0.0, 0.0
                            for batch in range(Params.batch_size):
                                f1, em = f1_and_EM(index[batch], devdata[-1][sample][batch], devdata[0][sample][batch], dict_)
                                F1 += f1
                                EM += em
                            F1 /= float(Params.batch_size)
                            EM /= float(Params.batch_size)
                            EM_.append(EM)
                            F1_.append(F1)
                            dev.append(dev_loss)
                        EM_ = np.mean(EM_)
                        F1_ = np.mean(F1_)
                        dev = np.mean(dev)
                        sess.run(model.metric_assign,{model.F1_placeholder: F1_, model.EM_placeholder: EM_, model.dev_loss_placeholder: dev})
                        print("\nTrain_loss: {}\nDev_loss: {}\nDev_Exact_match: {}\nDev_F1_score: {}".format(np.mean(train_loss),dev,EM_,F1_))
                        train_loss = []
예제 #2
0
파일: model.py 프로젝트: jasontruong/R-net
def main():
	model = Model(is_training = True); print("Built model")
	dict_ = pickle.load(open(Params.data_dir + "dictionary.pkl","r"))
	init = False
	devdata, dev_ind = get_dev()
	if not os.path.isfile(os.path.join(Params.logdir,"checkpoint")):
		init = True
		glove = np.memmap(Params.data_dir + "glove.np", dtype = np.float32, mode = "r")
		glove = np.reshape(glove,(Params.vocab_size,Params.emb_size))
		char_glove = np.memmap(Params.data_dir + "glove_char.np",dtype = np.float32, mode = "r")
		char_glove = np.reshape(char_glove,(Params.char_vocab_size,Params.emb_size))
	with model.graph.as_default():
		config = tf.ConfigProto()
		config.gpu_options.allow_growth = True
		sv = tf.train.Supervisor(logdir=Params.logdir,
						save_model_secs=0,
						global_step = model.global_step,
						init_op = model.init_op)
		with sv.managed_session(config = config) as sess:
			if init: sess.run(model.emb_assign, {model.word_embeddings_placeholder:glove, model.char_embeddings_placeholder:char_glove})
			for epoch in range(1, Params.num_epochs+1):
				if sv.should_stop(): break
				for step in tqdm(range(model.num_batch), total = model.num_batch, ncols=70, leave=False, unit='b'):
					sess.run(model.train_op)
					if step % Params.save_steps == 0:
						gs = sess.run(model.global_step)
						sv.saver.save(sess, Params.logdir + '/model_epoch_%d_step_%d'%(gs//model.num_batch, gs%model.num_batch))
						sample = np.random.choice(dev_ind, Params.batch_size)
						feed_dict = {data: devdata[i][sample] for i,data in enumerate(model.data)}
						logits, dev_loss = sess.run([model.points_logits, model.mean_loss], feed_dict = feed_dict)
						index = np.argmax(logits, axis = 2)
						F1, EM = 0.0, 0.0
						for batch in range(Params.batch_size):
							f1, em = f1_and_EM(index[batch], devdata[8][sample][batch], devdata[0][sample][batch], dict_)
							F1 += f1
							EM += em
						F1 /= float(Params.batch_size)
						EM /= float(Params.batch_size)
						sess.run(model.metric_assign,{model.F1_placeholder: F1, model.EM_placeholder: EM, model.dev_loss_placeholder: dev_loss})
						print("\nDev_loss: {}\nDev_Exact_match: {}\nDev_F1_score: {}".format(dev_loss,EM,F1))