def dev_by_ckpt(): model = MyModel(embedding_dim=768, hidden_dim=300, vocab_size_char=len(char2id), vocab_size_bio=len(tag2id), use_crf=True, embeddings=char_embeddings, ) sess = tf.Session() sess.run(tf.global_variables_initializer()) saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state('D:/Expriment/model_output/ner_tool/lstm_crf/single_task/clue_ner/runs/1620802544/checkpoints') if ckpt is None: print ('Model not found, please train your model first') else: path = ckpt.model_checkpoint_path print ('loading pre-trained model from %s.....' % path) saver.restore(sess, path) return sess, model
"./data/train/output.seq.bioattr", w2i_char, w2i_bio, shuffling=True) data_processor_valid = DataProcessor("./data/test/input.seq.char", "./data/test/output.seq.bioattr", w2i_char, w2i_bio, shuffling=True) logger.info("building model...") model = MyModel(embedding_dim=300, hidden_dim=300, vocab_size_char=len(w2i_char), vocab_size_bio=len(w2i_bio), use_crf=True) logger.info("model params:") params_num_all = 0 for variable in tf.trainable_variables(): params_num = 1 for dim in variable.shape: params_num *= dim params_num_all += params_num logger.info("\t {} {} {}".format(variable.name, variable.shape, params_num)) logger.info("all params num: " + str(params_num_all)) logger.info("start training...")
logger.info("Writing to {}\n".format(checkpoint_out_dir)) # Training # ================================================== with tf.Graph().as_default(): session_conf = tf.compat.v1.ConfigProto( allow_soft_placement=FLAGS.allow_soft_placement, log_device_placement=FLAGS.log_device_placement) sess = tf.Session(config=session_conf) with sess.as_default(): logger.info("building model...") model = MyModel( embedding_dim=768, hidden_dim=300, vocab_size_char=len(char2id), vocab_size_bio=len(tag2id), use_crf=True, embeddings=char_embeddings, ) # Define Training procedure global_step = tf.Variable(0, name="global_step", trainable=False) optimizer = tf.train.AdamOptimizer(FLAGS.learning_rate) grads_and_vars = optimizer.compute_gradients(model.loss) if FLAGS.use_clip_grad: grads_and_vars = [[ tf.clip_by_value(g, FLAGS.clip_grad, FLAGS.clip_grad), v ] for g, v in grads_and_vars] train_op = optimizer.apply_gradients(grads_and_vars, global_step=global_step)