text_model/model_skeleton.py
def predict(option_file, model_path, input_file, output_file):
    """
    Predict with pre-trained tf model.

    Args:
        option_file: json option file for model.
        model_path: tf model file path.
        input_file: predict inputs.
        output_file: predict results.

    Returns:
        None
    """
    options = load_options(option_file)
    ## modify options for predict
    options["train_file"] = input_file
    options["need_evaluate"] = False

    gpu_num = 1
    with tf.device("/cpu:0"):
        ## prepare dataset
        dataset = TextModelDataset(options, is_training=False, gpu_num=gpu_num)
        assert options["vocab_size"] == dataset.vocab.size, \
               "vocabulary size not same"

        ## build model
        models = []
        for i in range(gpu_num):
            with tf.variable_scope("model", reuse=(i>0)), tf.device("/gpu:%d" % i):
                model = TextBasedModel(options, dataset=dataset, is_training=False)
                models.append(model)
        model = models[0]

        ## all ops for predict
        pred_ops = { "pred"  : model.model_output["pred"] }

    ## ready to go
    saver  = tf.train.Saver()
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    config.allow_soft_placement = True
    with tf.Session(config=config) as sess, open(output_file, "w") as fout:
        saver.restore(sess, model_path)
        sess.run(dataset.data_init_op)
        batch_cnt = 0
        t0 = time.time()
        avg_speed = 0.
        while True:
            try:
                _pred_ops = sess.run(pred_ops)
                batch_cnt += 1 * gpu_num
                if (batch_cnt % 10) == 0:
                    avg_speed = batch_cnt / (time.time() - t0)
                    sys.stderr.write("\33[2K\r%f batch/sec\tbatch_num=%d" % (batch_cnt, avg_speed))
                ## NOTE output predict results
                fout.write()
            except tf.errors.OutOfRangeError:
                sys.stderr.write("\n")
                break
    sys.stderr.write("Predict finish\n")

    Predict with pre-trained tf model.

    Args:
        option_file: json option file for model.
        model_path: tf model file path.
        input_file: predict inputs.
        output_file: predict results.

    Returns:
        None
    
Predict with pre-trained tf model.
Predict with pre - trained tf model .
def predict(option_file, model_path, input_file, output_file):
    options = load_options(option_file)
    options["train_file"] = input_file
    options["need_evaluate"] = False
    gpu_num = 1
    with tf.device("/cpu:0"):
        dataset = TextModelDataset(options, is_training=False, gpu_num=gpu_num)
        assert options["vocab_size"] == dataset.vocab.size, \
               "vocabulary size not same"
        models = []
        for i in range(gpu_num):
            with tf.variable_scope("model", reuse=(i>0)), tf.device("/gpu:%d" % i):
                model = TextBasedModel(options, dataset=dataset, is_training=False)
                models.append(model)
        model = models[0]
        pred_ops = { "pred"  : model.model_output["pred"] }
    saver  = tf.train.Saver()
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    config.allow_soft_placement = True
    with tf.Session(config=config) as sess, open(output_file, "w") as fout:
        saver.restore(sess, model_path)
        sess.run(dataset.data_init_op)
        batch_cnt = 0
        t0 = time.time()
        avg_speed = 0.
        while True:
            try:
                _pred_ops = sess.run(pred_ops)
                batch_cnt += 1 * gpu_num
                if (batch_cnt % 10) == 0:
                    avg_speed = batch_cnt / (time.time() - t0)
                    sys.stderr.write("\33[2K\r%f batch/sec\tbatch_num=%d" % (batch_cnt, avg_speed))
                fout.write()
            except tf.errors.OutOfRangeError:
                sys.stderr.write("\n")
                break
    sys.stderr.write("Predict finish\n")
def predict ( option_file , model_path , input_file , output_file ) : options = load_options ( option_file ) options [ "train_file" ] = input_file options [ "need_evaluate" ] = False gpu_num = 1 with tf . device ( "/cpu:0" ) : dataset = TextModelDataset ( options , is_training = False , gpu_num = gpu_num ) assert options [ "vocab_size" ] == dataset . vocab . size , "vocabulary size not same" models = [ ] for i in range ( gpu_num ) : with tf . variable_scope ( "model" , reuse = ( i > 0 ) ) , tf . device ( "/gpu:%d" % i ) : model = TextBasedModel ( options , dataset = dataset , is_training = False ) models . append ( model ) model = models [ 0 ] pred_ops = { "pred" : model . model_output [ "pred" ] } saver = tf . train . Saver ( ) config = tf . ConfigProto ( ) config . gpu_options . allow_growth = True config . allow_soft_placement = True with tf . Session ( config = config ) as sess , open ( output_file , "w" ) as fout : saver . restore ( sess , model_path ) sess . run ( dataset . data_init_op ) batch_cnt = 0 t0 = time . time ( ) avg_speed = 0. while True : try : _pred_ops = sess . run ( pred_ops ) batch_cnt += 1 * gpu_num if ( batch_cnt % 10 ) == 0 : avg_speed = batch_cnt / ( time . time ( ) - t0 ) sys . stderr . write ( "\33[2K\r%f batch/sec\tbatch_num=%d" % ( batch_cnt , avg_speed ) ) fout . write ( ) except tf . errors . OutOfRangeError : sys . stderr . write ( "\n" ) break sys . stderr . write ( "Predict finish\n" )
Predict with pre-trained tf model.
Predict with pre - trained tf model .
"""
    Predict with pre-trained tf model.

    Args:
        option_file: json option file for model.
        model_path: tf model file path.
        input_file: predict inputs.
        output_file: predict results.

    Returns:
        None
    """ ## modify options for predict ## prepare dataset ## build model ## all ops for predict ## ready to go ## NOTE output predict results
None
option_file
json option file for model.
json option file for model .
model_path
tf model file path.
tf model file path .
input_file
None
output_file
None