def main(_):
    if len(sys.argv) < 3:
        print(
            'Usage: ucdoc_saved_model.py [--model_version=y] --data_dir=xxx --ckpt_dir=xxx --saved_dir=xxx'
        )
        sys.exit(-1)
    if FLAGS.training_iteration <= 0:
        print('Please specify a positive value for training iteration.')
        sys.exit(-1)
    if FLAGS.model_version <= 0:
        print('Please specify a positive value for version number.')
        sys.exit(-1)

    # create deploy model first
    with tf.variable_scope('input') as inp_scope:
        with tf.device("/cpu:0"):
            #inp = VarFeeder.read_vars("data/vars")
            inp = VarFeeder.read_vars(FLAGS.data_dir)
            pipe = InputPipe(inp,
                             ucdoc_features(inp),
                             inp.hits.shape[0],
                             mode=ModelMode.PREDICT,
                             batch_size=FLAGS.batch_size,
                             n_epoch=1,
                             verbose=False,
                             train_completeness_threshold=0.01,
                             predict_window=FLAGS.predict_window,
                             predict_completeness_threshold=0.0,
                             train_window=FLAGS.train_window,
                             back_offset=FLAGS.predict_window + 1)

    asgd_decay = 0.99 if FLAGS.asgd else None

    if FLAGS.n_models == 1:
        model = Model(pipe,
                      build_from_set(FLAGS.hparam_set),
                      is_train=False,
                      seed=1,
                      asgd_decay=asgd_decay)
    else:
        models = []
        for i in range(FLAGS.n_models):
            prefix = f"m_{i}"
            with tf.variable_scope(prefix) as scope:
                models.append(
                    Model(pipe,
                          build_from_set(FLAGS.hparam_set),
                          is_train=False,
                          seed=1,
                          asgd_decay=asgd_decay,
                          graph_prefix=prefix))
        model = models[FLAGS.target_model]

    # load checkpoint model from training
    #ckpt_path = FLAGS.ckpt_dir
    print('loading checkpoint model...')
    ckpt_file = tf.train.latest_checkpoint(FLAGS.ckpt_dir)
    #graph = tf.Graph()
    graph = model.predictions.graph

    saver = tf.train.Saver(name='deploy_saver', var_list=None)
    with tf.Session(config=tf.ConfigProto(gpu_options=tf.GPUOptions(
            allow_growth=True))) as sess:
        pipe.load_vars(sess)
        pipe.init_iterator(sess)
        saver.restore(sess, ckpt_file)
        print('Done loading checkpoint model')
        export_path_base = FLAGS.saved_dir
        export_path = os.path.join(
            tf.compat.as_bytes(export_path_base),
            tf.compat.as_bytes(str(FLAGS.model_version)))
        print('Exporting trained model to', export_path)
        if os.path.isdir(export_path):
            shutil.rmtree(export_path)
        builder = tf.saved_model.builder.SavedModelBuilder(export_path)

        true_x = tf.saved_model.utils.build_tensor_info(model.inp.true_x)
        time_x = tf.saved_model.utils.build_tensor_info(model.inp.time_x)
        norm_x = tf.saved_model.utils.build_tensor_info(model.inp.norm_x)
        lagged_x = tf.saved_model.utils.build_tensor_info(model.inp.lagged_x)
        true_y = tf.saved_model.utils.build_tensor_info(model.inp.true_y)
        time_y = tf.saved_model.utils.build_tensor_info(model.inp.time_y)
        norm_y = tf.saved_model.utils.build_tensor_info(model.inp.norm_y)
        norm_mean = tf.saved_model.utils.build_tensor_info(model.inp.norm_mean)
        norm_std = tf.saved_model.utils.build_tensor_info(model.inp.norm_std)
        pg_features = tf.saved_model.utils.build_tensor_info(
            model.inp.ucdoc_features)
        page_ix = tf.saved_model.utils.build_tensor_info(model.inp.page_ix)

        pred = tf.saved_model.utils.build_tensor_info(model.predictions)

        labeling_signature = (
            tf.saved_model.signature_def_utils.build_signature_def(
                inputs={
                    "truex": true_x,
                    "timex": time_x,
                    "normx": norm_x,
                    "laggedx": lagged_x,
                    "truey": true_y,
                    "timey": time_y,
                    "normy": norm_y,
                    "normmean": norm_mean,
                    "normstd": norm_std,
                    "page_features": pg_features,
                    "pageix": page_ix,
                },
                outputs={"pred": pred},
                method_name="tensorflow/serving/predict"))

        legacy_init_op = tf.group(tf.tables_initializer(),
                                  name='legacy_init_op')

        builder.add_meta_graph_and_variables(
            sess, [tf.saved_model.tag_constants.SERVING],
            signature_def_map={
                tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
                labeling_signature
            },
            main_op=tf.tables_initializer(),
            strip_default_attrs=True)

        builder.save()
        print("Build Done")
Exemple #2
0
def predict(checkpoints, hparams, return_x=False, verbose=False, predict_window=10, back_offset=10, n_models=1,
            target_model=0, asgd=False, seed=1, batch_size=1024):
    with tf.variable_scope('input') as inp_scope:
        with tf.device("/cpu:0"):
            inp = VarFeeder.read_vars("data/vars")
            pipe = InputPipe(inp, ucdoc_features(inp), inp.n_pages, mode=ModelMode.PREDICT, batch_size=batch_size,
                             n_epoch=1, verbose=verbose,
                             train_completeness_threshold=0.01,
                             predict_window=predict_window,
                             predict_completeness_threshold=0.0, train_window=hparams.train_window,
                             back_offset=back_offset)
    asgd_decay = 0.99 if asgd else None
    if n_models == 1:
        model = Model(pipe, hparams, is_train=False, seed=seed, asgd_decay=asgd_decay)
    else:
        models = []
        for i in range(n_models):
            prefix = f"m_{i}"
            with tf.variable_scope(prefix) as scope:
                models.append(Model(pipe, hparams, is_train=False, seed=seed, asgd_decay=asgd_decay, graph_prefix=prefix))
        model = models[target_model]

    if asgd:
        var_list = model.ema.variables_to_restore()
        prefix = f"m_{target_model}"
        for var in list(var_list.keys()):
            if var.endswith('ExponentialMovingAverage') and not var.startswith(prefix):
                del var_list[var]
    else:
        var_list = None
    saver = tf.train.Saver(name='eval_saver', var_list=var_list)
    x_buffer = []
    predictions = None
    with tf.Session(config=tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))) as sess:
        pipe.load_vars(sess)
        for checkpoint in checkpoints:
            pred_buffer = []
            pipe.init_iterator(sess)
            saver.restore(sess, checkpoint)
            cnt = 0
            e = []
            while True:
                try:
                    if return_x:
                        #truex, timex, normx, laggedx, truey, timey, normy, normmean, normstd, pgfeatures, pageix
                        pgfeatures, y ,pred, x, pname, truex , timex, timey= sess.run([model.inp.ucdoc_features, model.inp.true_y, model.predictions, model.inp.true_x, model.inp.page_ix, model.inp.true_x, model.inp.time_x, model.inp.time_y ])
                        # print(pname, '\n', "true_y:", '\n', np.round(np.expm1(y)), '\n', "prediction:", '\n',
                        #       np.round(np.expm1(pred)), np.average(np.divide(np.abs(np.subtract(np.round(np.expm1(pred)),np.round(np.expm1(y)))),np.round(np.expm1(y)))))
                        error = np.average(np.divide(np.abs(np.subtract(np.round(np.expm1(pred)),np.round(np.expm1(y)))),np.round(np.expm1(y))))
                        e.append(error)
                        # if pname == b'magazinelock,1,3G,g_f,2,pt,1004,icc,2,11':
                        #     print(pgfeatures, y ,pred, x, pname, truex , timex, timey)
                        #     raise NotImplementedError()

                              # ,np.average(np.divide(np.abs(np.subtract(np.round(np.expm1(y)),np.round(np.expm1(pred)))),np.round(np.expm1(y)) )),
                              #  "time_y:",timey, "time_x:" , timex)
                        # print(np.average(np.divide(np.abs(np.subtract(np.round(np.expm1(y)),np.round(np.expm1(pred)))),np.round(np.expm1(y)) )))
                    else:
                        pred, pname = sess.run([model.predictions, model.inp.page_ix])
                    utf_names = [str(name, 'utf-8') for name in pname]
                    pred_df = pd.DataFrame(index=utf_names, data=np.expm1(pred))
                    pred_buffer.append(pred_df)
                    if return_x:
                        # noinspection PyUnboundLocalVariable
                        x_values = pd.DataFrame(index=utf_names, data=np.round(np.expm1(x)).astype(np.int64))
                        x_buffer.append(x_values)
                    newline = cnt % 80 == 0
                    if cnt > 0:
                        log.info('.') #, end='\n' if newline else '' , flush=True
                    if newline:
                        log.info(cnt) #, end='\n'
                    cnt += 1
                except tf.errors.OutOfRangeError:
                    log.info('🎉')
                    break
            print(np.average(e))
            cp_predictions = pd.concat(pred_buffer)
            if predictions is None:
                predictions = cp_predictions
            else:
                predictions += cp_predictions
    predictions /= len(checkpoints)
    offset = pd.Timedelta(back_offset, 'D')
    start_prediction = inp.data_end + pd.Timedelta('1D') - offset
    end_prediction = start_prediction + pd.Timedelta(predict_window - 1, 'D')
    predictions.columns = pd.date_range(start_prediction, end_prediction)
    if return_x:
        x = pd.concat(x_buffer)
        start_data = inp.data_end - pd.Timedelta(hparams.train_window - 1, 'D') - back_offset
        end_data = inp.data_end - back_offset
        x.columns = pd.date_range(start_data, end_data)
        return predictions, x
    else:
        return predictions
Exemple #3
0
def main(_):
    if not FLAGS.server:
        print('please specify server host:port')
        return

    channel = grpc.insecure_channel(FLAGS.server)
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
    request = predict_pb2.PredictRequest()

    request.model_spec.name = "ucdoc"
    request.model_spec.signature_name = "serving_default"

    with tf.variable_scope('input') as inp_scope:
        with tf.device("/cpu:0"):
            inp = VarFeeder.read_vars("data/vars")
            pipe = InputPipe(inp,
                             ucdoc_features(inp),
                             inp.n_pages,
                             mode=ModelMode.PREDICT,
                             batch_size=FLAGS.batch_size,
                             n_epoch=1,
                             verbose=FLAGS.verbose,
                             train_completeness_threshold=0.01,
                             predict_window=FLAGS.predict_window,
                             predict_completeness_threshold=0.0,
                             train_window=FLAGS.train_window,
                             back_offset=FLAGS.predict_window + 1)
    with tf.Session(config=tf.ConfigProto(gpu_options=tf.GPUOptions(
            allow_growth=True))) as sess:
        pipe.load_vars(sess)
        pipe.init_iterator(sess)

        while True:
            try:
                truex, timex, normx, laggedx, truey, timey, normy, normmean, normstd, pgfeatures, pageix = \
                    sess.run([pipe.true_x, pipe.time_x, pipe.norm_x, pipe.lagged_x, pipe.true_y, pipe.time_y,
                              pipe.norm_y, pipe.norm_mean, pipe.norm_std, pipe.ucdoc_features, pipe.page_ix])

                request.inputs["truex"].CopyFrom(tf.make_tensor_proto(truex))
                request.inputs["timex"].CopyFrom(tf.make_tensor_proto(timex))
                request.inputs["normx"].CopyFrom(tf.make_tensor_proto(normx))
                request.inputs["laggedx"].CopyFrom(
                    tf.make_tensor_proto(laggedx))
                request.inputs["truey"].CopyFrom(tf.make_tensor_proto(truey))
                request.inputs["timey"].CopyFrom(tf.make_tensor_proto(timey))
                request.inputs["normy"].CopyFrom(tf.make_tensor_proto(normy))
                request.inputs["normmean"].CopyFrom(
                    tf.make_tensor_proto(normmean))
                request.inputs["normstd"].CopyFrom(
                    tf.make_tensor_proto(normstd))
                request.inputs["page_features"].CopyFrom(
                    tf.make_tensor_proto(pgfeatures))
                request.inputs["pageix"].CopyFrom(tf.make_tensor_proto(pageix))

                response = stub.Predict(request, 10)
                tensor_proto = response.outputs['pred']
                if not 'pred_result' in locals():
                    pred_result = tf.contrib.util.make_ndarray(tensor_proto)
                else:
                    pred_result = np.concatenate([
                        pred_result,
                        tf.contrib.util.make_ndarray(tensor_proto)
                    ])
            except tf.errors.OutOfRangeError:
                print('done with prediction')
                break
        pred_result = np.expm1(pred_result) + 0.5
        pred_result = pred_result.astype(int)
        if not os.path.exists(FLAGS.result_dir):
            os.mkdir(FLAGS.result_dir)
        result_file = os.path.join(FLAGS.result_dir, "predict.pkl")
        pickle.dump(pred_result, open(result_file, "wb"))
        print('finished prediction')
Exemple #4
0
def train(name, hparams, multi_gpu=False, n_models=1, train_completeness_threshold=0.01,
          seed=None, logdir='data/logs', max_epoch=100, patience=2, train_sampling=1.0,
          eval_sampling=1.0, eval_memsize=5, gpu=0, gpu_allow_growth=False, save_best_model=False,
          forward_split=False, write_summaries=False, verbose=False, asgd_decay=None, tqdm=True,
          side_split=True, max_steps=None, save_from_step=None, do_eval=True, predict_window=63):

    eval_k = int(round(2621 * eval_memsize / n_models))
    eval_batch_size = int(
        eval_k / (hparams.rnn_depth * hparams.encoder_rnn_layers))  # 128 -> 1024, 256->512, 512->256
    eval_pct = 0.2
    batch_size = hparams.batch_size
    train_window = hparams.train_window
    tf.reset_default_graph()
    if seed:
        tf.set_random_seed(seed)

    with tf.device("/cpu:0"):
        inp = VarFeeder.read_vars("data/vars")
        if side_split:
           splitter = Splitter(ucdoc_features(inp),inp.page_map,  3, train_sampling=train_sampling,
                                test_sampling=eval_sampling, seed=seed)
        else:
            splitter = FakeSplitter(ucdoc_features(inp), 3, seed=seed, test_sampling=eval_sampling)

    real_train_pages = splitter.splits[0].train_size
    real_eval_pages = splitter.splits[0].test_size

    items_per_eval = real_eval_pages * eval_pct
    eval_batches = int(np.ceil(items_per_eval / eval_batch_size))
    steps_per_epoch = real_train_pages // batch_size
    eval_every_step = int(round(steps_per_epoch * eval_pct))
    # eval_every_step = int(round(items_per_eval * train_sampling / batch_size))

    global_step = tf.train.get_or_create_global_step()
    inc_step = tf.assign_add(global_step, 1)


    all_models: List[ModelTrainerV2] = []

    def create_model(scope, index, prefix, seed):

        with tf.variable_scope('input') as inp_scope:
            with tf.device("/cpu:0"):
                split = splitter.splits[index]
                pipe = InputPipe(inp, features=split.train_set, n_pages=split.train_size,
                                 mode=ModelMode.TRAIN, batch_size=batch_size, n_epoch=None, verbose=verbose,
                                 train_completeness_threshold=train_completeness_threshold,
                                 predict_completeness_threshold=train_completeness_threshold, train_window=train_window,
                                 predict_window=predict_window,
                                 rand_seed=seed, train_skip_first=hparams.train_skip_first,
                                 back_offset=predict_window if forward_split else 0)
                inp_scope.reuse_variables()
                if side_split:
                    side_eval_pipe = InputPipe(inp, features=split.test_set, n_pages=split.test_size,
                                               mode=ModelMode.EVAL, batch_size=eval_batch_size, n_epoch=None,
                                               verbose=verbose, predict_window=predict_window,
                                               train_completeness_threshold=0.01, predict_completeness_threshold=0,
                                               train_window=train_window, rand_seed=seed, runs_in_burst=eval_batches,
                                               back_offset=predict_window * (2 if forward_split else 1))
                else:
                    side_eval_pipe = None
                if forward_split:
                    forward_eval_pipe = InputPipe(inp, features=split.test_set, n_pages=split.test_size,
                                                  mode=ModelMode.EVAL, batch_size=eval_batch_size, n_epoch=None,
                                                  verbose=verbose, predict_window=predict_window,
                                                  train_completeness_threshold=0.01, predict_completeness_threshold=0,
                                                  train_window=train_window, rand_seed=seed, runs_in_burst=eval_batches,
                                                  back_offset=predict_window)
                else:
                    forward_eval_pipe = None
        avg_sgd = asgd_decay is not None
        #asgd_decay = 0.99 if avg_sgd else None
        train_model = Model(pipe, hparams, is_train=True, graph_prefix=prefix, asgd_decay=asgd_decay, seed=seed)
        scope.reuse_variables()

        eval_stages = []
        if side_split:
            side_eval_model = Model(side_eval_pipe, hparams, is_train=False,
                                    #loss_mask=np.concatenate([np.zeros(50, dtype=np.float32), np.ones(10, dtype=np.float32)]),
                                    seed=seed)
            eval_stages.append((Stage.EVAL_SIDE, side_eval_model))
            if avg_sgd:
                eval_stages.append((Stage.EVAL_SIDE_EMA, side_eval_model))
        if forward_split:
            forward_eval_model = Model(forward_eval_pipe, hparams, is_train=False, seed=seed)
            eval_stages.append((Stage.EVAL_FRWD, forward_eval_model))
            if avg_sgd:
                eval_stages.append((Stage.EVAL_FRWD_EMA, forward_eval_model))

        if write_summaries:
            summ_path = f"{logdir}/{name}_{index}"
            if os.path.exists(summ_path):
                shutil.rmtree(summ_path)
            summ_writer = tf.summary.FileWriter(summ_path)  # , graph=tf.get_default_graph()
        else:
            summ_writer = None
        if do_eval and forward_split:
            stop_metric = lambda metrics: metrics[Stage.EVAL_FRWD]['SMAPE'].avg_epoch
        else:
            stop_metric = None
        return ModelTrainerV2(train_model, eval_stages, index, patience=patience,
                              stop_metric=stop_metric,
                              summary_writer=summ_writer)


    if n_models == 1:
        with tf.device(f"/gpu:{gpu}"):
            scope = tf.get_variable_scope()
            all_models = [create_model(scope, 0, None, seed=seed)]
    else:
        for i in range(n_models):
            device = f"/gpu:{i}" if multi_gpu else f"/gpu:{gpu}"
            with tf.device(device):
                prefix = f"m_{i}"
                with tf.variable_scope(prefix) as scope:
                    all_models.append(create_model(scope, i, prefix=prefix, seed=seed + i))
    trainer = MultiModelTrainer(all_models, inc_step)
    if save_best_model or save_from_step:
        saver_path = f'data/cpt/{name}'
        if os.path.exists(saver_path):
            shutil.rmtree(saver_path)
        os.makedirs(saver_path)
        saver = tf.train.Saver(max_to_keep=10, name='train_saver')
    else:
        saver = None
    avg_sgd = asgd_decay is not None
    if avg_sgd:
        from itertools import chain
        def ema_vars(model):
            ema = model.train_model.ema
            return {ema.average_name(v):v for v in model.train_model.ema._averages}

        ema_names = dict(chain(*[ema_vars(model).items() for model in all_models]))
        #ema_names = all_models[0].train_model.ema.variables_to_restore()
        ema_loader = tf.train.Saver(var_list=ema_names,  max_to_keep=1, name='ema_loader')
        ema_saver = tf.train.Saver(max_to_keep=1, name='ema_saver')
    else:
        ema_loader = None

    init = tf.global_variables_initializer()

    if forward_split and do_eval:
        eval_smape = trainer.metric(Stage.EVAL_FRWD, 'SMAPE')
        eval_mae = trainer.metric(Stage.EVAL_FRWD, 'MAE')
    else:
        eval_smape = DummyMetric()
        eval_mae = DummyMetric()

    if side_split and do_eval:
        eval_mae_side = trainer.metric(Stage.EVAL_SIDE, 'MAE')
        eval_smape_side = trainer.metric(Stage.EVAL_SIDE, 'SMAPE')
    else:
        eval_mae_side = DummyMetric()
        eval_smape_side = DummyMetric()

    train_smape = trainer.metric(Stage.TRAIN, 'SMAPE')
    train_mae = trainer.metric(Stage.TRAIN, 'MAE')
    grad_norm = trainer.metric(Stage.TRAIN, 'GrNorm')
    eval_stages = []
    ema_eval_stages = []
    if forward_split and do_eval:
        eval_stages.append(Stage.EVAL_FRWD)
        ema_eval_stages.append(Stage.EVAL_FRWD_EMA)
    if side_split and do_eval:
        eval_stages.append(Stage.EVAL_SIDE)
        ema_eval_stages.append(Stage.EVAL_SIDE_EMA)

    # gpu_options=tf.GPUOptions(allow_growth=False),
    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                          gpu_options=tf.GPUOptions(allow_growth=gpu_allow_growth))) as sess:
        sess.run(init)
        # pipe.load_vars(sess)
        inp.restore(sess)
        for model in all_models:
            model.init(sess)
        # if beholder:
        #    visualizer = Beholder(session=sess, logdir=summ_path)
        step = 0
        prev_top = np.inf
        best_smape = np.inf
        # Contains best value (first item) and subsequent values
        best_epoch_smape = []

        for epoch in range(max_epoch):

            # n_steps = pusher.n_pages // batch_size
            if tqdm:
                #tqr = trange(steps_per_epoch, desc="%2d" % (epoch + 1), leave=False)
                tqr = trange(steps_per_epoch, desc="%2d" % (epoch + 1), leave=False,
                             file=logging.root.handlers[0].stream)

            else:
                tqr = range(steps_per_epoch)

            for _ in tqr:
                try:
                    step = trainer.train_step(sess, epoch)

                    pred, time_y, true_y, true_x, time_x, page_ix , norm_mean ,norm_std, lagged_ix =sess.run([trainer.trainers[0].train_model.predictions, trainer.trainers[0].train_model.inp.time_y,trainer.trainers[0].train_model.inp.true_y,
                                          trainer.trainers[0].train_model.inp.true_x,trainer.trainers[0].train_model.inp.time_x, trainer.trainers[0].train_model.inp.page_ix , trainer.trainers[0].train_model.inp.norm_mean, trainer.trainers[0].train_model.inp.norm_std
                                                                                                   ,trainer.trainers[0].train_model.inp.lagged_x])
                    #sess.run(trainer.trainers[0].train_model.inp.inp.hits)
                    #inp = all_models[0].train_model.inp.inp,

                    pred_exp = np.round(np.expm1(pred))

                    true_exp = np.expm1(true_y )

                    error_exp= np.mean(np.abs(true_exp-pred_exp) /(true_exp))
                    error= np.mean(np.abs(true_y -pred )/(true_y))
                    # page_ix = sess.run([trainer.trainers[0].train_model.inp.page_ix])[0][0]
                    # true_x = sess.run([trainer.trainers[0].train_model.inp.true_x])[0][0]
                    last_error =  error_exp
                    epsilon = 0.1  # Smoothing factor, helps SMAPE to be well-behaved near zero
                    true_o = np.expm1(true_y)
                    pred_o = np.expm1(pred)
                    summ = np.maximum(np.abs(true_o) + epsilon, 0.5 + epsilon)
                    smape = np.mean(np.abs(pred_o - true_o) / summ)



                except tf.errors.OutOfRangeError:
                    break
                    # if beholder:
                    #  if step % 5 == 0:
                    # noinspection PyUnboundLocalVariable
                    #  visualizer.update()
                if step % eval_every_step == 0:
                    if eval_stages:
                        trainer.eval_step(sess, epoch, step, eval_batches, stages=eval_stages)

                    if save_best_model and epoch > 0 and eval_smape.last < best_smape:
                        best_smape = eval_smape.last
                        saver.save(sess, f'data/cpt/{name}/cpt', global_step=step)
                    if save_from_step and step >= save_from_step:
                        saver.save(sess, f'data/cpt/{name}/cpt', global_step=step)

                    if avg_sgd and ema_eval_stages:
                        ema_saver.save(sess, 'data/cpt_tmp/ema',  write_meta_graph=False)
                        # restore ema-backed vars
                        ema_loader.restore(sess, 'data/cpt_tmp/ema')

                        trainer.eval_step(sess, epoch, step, eval_batches, stages=ema_eval_stages)
                        # restore normal vars
                        ema_saver.restore(sess, 'data/cpt_tmp/ema')

                MAE = "%.3f/%.3f/%.3f" % (eval_mae.last, eval_mae_side.last, train_mae.last)
                improvement = '↑' if eval_smape.improved else ' '
                SMAPE = "%s%.3f/%.3f/%.3f" % (improvement, eval_smape.last, eval_smape_side.last,  train_smape.last)
                if tqdm:
                    tqr.set_postfix(gr=grad_norm.last, MAE=MAE, SMAPE=SMAPE)
                if not trainer.has_active() or (max_steps and step > max_steps):
                    break

            if tqdm:
                tqr.close()
            trainer.end_epoch()
            if not best_epoch_smape or eval_smape.avg_epoch < best_epoch_smape[0]:
                best_epoch_smape = [eval_smape.avg_epoch]
            else:
                best_epoch_smape.append(eval_smape.avg_epoch)

            current_top = eval_smape.top
            if prev_top > current_top:
                prev_top = current_top
                has_best_indicator = '↑'
            else:
                has_best_indicator = ' '
            status = "%2d: Best top SMAPE=%.3f%s (%s)" % (
                epoch + 1, current_top, has_best_indicator,
                ",".join(["%.3f" % m.top for m in eval_smape.metrics]))


            if trainer.has_active():
                status += ", frwd/side best MAE=%.3f/%.3f, SMAPE=%.3f/%.3f; avg MAE=%.3f/%.3f, SMAPE=%.3f/%.3f, %d am ,Error=%3f " % \
                          (eval_mae.best_epoch, eval_mae_side.best_epoch, eval_smape.best_epoch, eval_smape_side.best_epoch,
                           eval_mae.avg_epoch,  eval_mae_side.avg_epoch,  eval_smape.avg_epoch,  eval_smape_side.avg_epoch,
                           trainer.has_active(), last_error)
                log.info(status)
            else:
                log.info(status)
                log.info("Early stopping!")
                break
            if max_steps and step > max_steps:
                log.info("Max steps calculated")
                break
            sys.stderr.flush()

        # noinspection PyUnboundLocalVariable
        return np.mean(best_epoch_smape, dtype=np.float64)
Exemple #5
0
def main(_):
    if len(sys.argv) < 3:
        print(
            'Usage: saved_model.py [--model_version=y] --data_dir=xxx --ckpt_dir=xxx --saved_dir=xxx'
        )
        sys.exit(-1)
    if FLAGS.training_iteration <= 0:
        print('Please specify a positive value for training iteration.')
        sys.exit(-1)
    if FLAGS.model_version <= 0:
        print('Please specify a positive value for version number.')
        sys.exit(-1)

    with open(FLAGS.config_file, 'r') as ymlfile:
        cfg = yaml.load(ymlfile)

    holiday_list = cfg['pipeline']['normalization']['holidays']
    if FLAGS.back_offset < FLAGS.predict_window:
        extend_inp(FLAGS.data_dir, FLAGS.predict_window, holiday_list)

    # create deploy model first
    back_offset_ = FLAGS.back_offset
    with tf.variable_scope('input') as inp_scope:
        with tf.device("/cpu:0"):
            if FLAGS.back_offset < FLAGS.predict_window:
                inp = VarFeeder.read_vars(
                    os.path.join(FLAGS.data_dir, 'predict_future'))
                back_offset_ += FLAGS.predict_window
            else:
                inp = VarFeeder.read_vars(FLAGS.data_dir)
            pipe = InputPipe(inp,
                             ucdoc_features(inp),
                             inp.hits.shape[0],
                             mode=ModelMode.PREDICT,
                             batch_size=FLAGS.batch_size,
                             n_epoch=1,
                             verbose=False,
                             train_completeness_threshold=0.01,
                             predict_window=FLAGS.predict_window,
                             predict_completeness_threshold=0.0,
                             train_window=FLAGS.train_window,
                             back_offset=back_offset_)

    asgd_decay = 0.99 if FLAGS.asgd else None

    if FLAGS.n_models == 1:
        model = Model(pipe,
                      build_from_set(FLAGS.hparam_set),
                      is_train=False,
                      seed=1,
                      asgd_decay=asgd_decay)
    else:
        models = []
        for i in range(FLAGS.n_models):
            prefix = f"m_{i}"
            with tf.variable_scope(prefix) as scope:
                models.append(
                    Model(pipe,
                          build_from_set(FLAGS.hparam_set),
                          is_train=False,
                          seed=1,
                          asgd_decay=asgd_decay,
                          graph_prefix=prefix))
        model = models[FLAGS.target_model]

    if FLAGS.asgd:
        var_list = model.ema.variables_to_restore()
        if FLAGS.n_models > 1:
            prefix = f"m_{target_model}"
            for var in list(var_list.keys()):
                if var.endswith('ExponentialMovingAverage'
                                ) and not var.startswith(prefix):
                    del var_list[var]
    else:
        var_list = None

    # load checkpoint model from training
    #ckpt_path = FLAGS.ckpt_dir
    print('loading checkpoint model...')
    ckpt_file = tf.train.latest_checkpoint(FLAGS.ckpt_dir)
    #graph = tf.Graph()
    graph = model.predictions.graph

    init = tf.global_variables_initializer()

    saver = tf.train.Saver(name='deploy_saver', var_list=var_list)
    with tf.Session(config=tf.ConfigProto(gpu_options=tf.GPUOptions(
            allow_growth=True))) as sess:
        sess.run(init)
        pipe.load_vars(sess)
        pipe.init_iterator(sess)
        saver.restore(sess, ckpt_file)
        print('Done loading checkpoint model')
        export_path_base = FLAGS.saved_dir
        export_path = os.path.join(
            tf.compat.as_bytes(export_path_base),
            tf.compat.as_bytes(str(FLAGS.model_version)))
        print('Exporting trained model to', export_path)
        if os.path.isdir(export_path):
            shutil.rmtree(export_path)
        builder = tf.saved_model.builder.SavedModelBuilder(export_path)

        true_x = tf.saved_model.utils.build_tensor_info(
            model.inp.true_x)  # pipe.true_x
        time_x = tf.saved_model.utils.build_tensor_info(
            model.inp.time_x)  # pipe.time_x
        norm_x = tf.saved_model.utils.build_tensor_info(
            model.inp.norm_x)  # pipe.norm_x
        lagged_x = tf.saved_model.utils.build_tensor_info(
            model.inp.lagged_x)  # pipe.lagged_x
        true_y = tf.saved_model.utils.build_tensor_info(
            model.inp.true_y)  # pipe.true_y
        time_y = tf.saved_model.utils.build_tensor_info(
            model.inp.time_y)  # pipe.time_y
        norm_y = tf.saved_model.utils.build_tensor_info(
            model.inp.norm_y)  # pipe.norm_y
        norm_mean = tf.saved_model.utils.build_tensor_info(
            model.inp.norm_mean)  # pipe.norm_mean
        norm_std = tf.saved_model.utils.build_tensor_info(
            model.inp.norm_std)  # pipe.norm_std
        pg_features = tf.saved_model.utils.build_tensor_info(
            model.inp.ucdoc_features)  # pipe.ucdoc_features
        page_ix = tf.saved_model.utils.build_tensor_info(
            model.inp.page_ix)  # pipe.page_ix

        #pred = tf.saved_model.utils.build_tensor_info(graph.get_operation_by_name('m_0/add').outputs[0])
        pred = tf.saved_model.utils.build_tensor_info(model.predictions)

        labeling_signature = (
            tf.saved_model.signature_def_utils.build_signature_def(
                inputs={
                    "truex": true_x,
                    "timex": time_x,
                    "normx": norm_x,
                    "laggedx": lagged_x,
                    "truey": true_y,
                    "timey": time_y,
                    "normy": norm_y,
                    "normmean": norm_mean,
                    "normstd": norm_std,
                    "page_features": pg_features,
                    "pageix": page_ix,
                },
                outputs={"predictions": pred},
                method_name="tensorflow/serving/predict"))

        legacy_init_op = tf.group(tf.tables_initializer(),
                                  name='legacy_init_op')

        builder.add_meta_graph_and_variables(
            sess, [tf.saved_model.tag_constants.SERVING],
            signature_def_map={
                tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
                labeling_signature
            },
            main_op=tf.tables_initializer(),
            strip_default_attrs=True)

        builder.save()
        print("Build Done")