Example #1
0
def val():
    net.eval()
    global threshold_rs, threshold_otp
    print('validating...')
    predict_total_hours = []
    valid_begin_time = []
    valid_signed_time = []
    for idx, (validdata, begin, target) in enumerate(tqdm(validloader)):
        if opt.USE_CUDA:
            validdata = validdata.cuda()
        validdata = validdata.float()
        outputs = net(validdata)
        if opt.USE_CUDA:
            outputs = outputs.cpu()
        outputs = outputs.numpy()
        for i in range(len(validdata)):
            predict_total_hours.append(outputs[i][0])
            payed_time = datetime.datetime.strptime(begin[i],
                                                    "%Y-%m-%d %H:%M:%S")
            signed_time = datetime.datetime.strptime(target[i],
                                                     "%Y-%m-%d %H:%M:%S")
            valid_begin_time.append(payed_time)
            valid_signed_time.append(signed_time)

    predict_date = time_predict(valid_begin_time, predict_total_hours)
    otp, rs = calculateAllMetrics(valid_signed_time, predict_date)
    print('on time percent: %lf\nrank score: %lf' % (otp, rs))

    if rs < threshold_rs and otp >= threshold_otp:
        print("==> saving model")
        if not os.path.exists('models'):
            os.mkdir('models')
        threshold_rs = rs
        threshold_otp = otp
        torch.save(net, './models/model_{}_{}.pt'.format(otp, rs))
Example #2
0
def val(epoch):
    global highest_metrics
    net.eval()
    pred_signed_time = []
    real_signed_time = []
    for batch_idx, (inputs, payed_time, shipped_time, dlved_time,
                    signed_time) in enumerate(tqdm(valloader)):
        if opt.USE_CUDA:
            inputs = inputs.cuda()

        inputs = torch.autograd.Variable(inputs)
        (output_FC_1_1, output_FC_1_2) = net(inputs.float())

        # calculate pred_signed_time via output
        for i in range(len(inputs)):
            #pred_ship_pay_day = torch.max(output_FC_2_1[i], 0)[1]
            #pred_ship_pay_day = output_FC_2_1[i]
            pred_time_day = output_FC_1_1[i]
            pred_time_hour = output_FC_1_2[i]

            temp_payed_time = payed_time[i]
            temp_payed_time = datetime.datetime.strptime(
                temp_payed_time, "%Y-%m-%d %H:%M:%S")
            temp_payed_time = temp_payed_time.replace(
                hour=int(pred_time_hour) % 24)

            temp_pred_signed_time = temp_payed_time + relativedelta(
                days=int(pred_time_day))
            temp_pred_signed_time = temp_pred_signed_time.replace(
                hour=int(pred_time_hour) % 24)
            temp_pred_signed_time = temp_pred_signed_time.replace(minute=0)
            temp_pred_signed_time = temp_pred_signed_time.replace(second=0)
            # temp_pred_signed_time.

            pred_signed_time.append(
                temp_pred_signed_time.strftime("%Y-%m-%d %H"))
            real_signed_time.append(signed_time[i])

    (rankScore_result, onTimePercent_result,
     accuracy_result) = calculateAllMetrics(real_signed_time, pred_signed_time)
    print("==> epoch {}: rankScore is {}, onTimePercent is {}, accuracy is {}".
          format(epoch, rankScore_result, onTimePercent_result,
                 accuracy_result))

    # save model
    if rankScore_result < highest_metrics and onTimePercent_result > 0.994:
        print("==> saving model")
        print("==> onTimePercent {} | rankScore {} ".format(
            onTimePercent_result, rankScore_result))
        highest_metrics = rankScore_result
        torch.save(net, opt.MODEL_SAVE_PATH)
Example #3
0
def train(train_set, train_target, valid_set, valid_target=None):
    print('training...')
    dtrain = xgb.DMatrix(train_set, label=train_target)
    del train_set, train_target
    param = {
        'booster': 'gbtree',
        'colsample_bytree': 0.8,
        'eta': 0.1,
        'max_depth': 20,  # 16
        # 'objective': 'reg:squarederror',
        'gamma': 0.2,
        # 'subsample': 1.0,
        'min_child_weight': 10  # 10
        # 'tree_method': 'gpu_hist'
    }
    num_round = 17  # 8
    if valid_target is not None:
        dvalid = xgb.DMatrix(valid_set, label=valid_target)
        bst = xgb.train(param,
                        dtrain,
                        num_round,
                        obj=my_loss_fun,
                        evals=[(dtrain, 'dtrain'), (dvalid, 'dvalid')])
        del dtrain

    else:
        bst = xgb.train(param,
                        dtrain,
                        num_round,
                        obj=my_loss_fun,
                        evals=[(dtrain, 'dtrain')])
        del dtrain
        dvalid = xgb.DMatrix(valid_set)

    print('train finished')
    # make prediction
    predict_total_hours = bst.predict(dvalid)
    predict_date = time_predict(valid_begin_time, predict_total_hours)

    if valid_target is not None:
        otp, rs = calculateAllMetrics(valid_signed_time, predict_date)
        print('on time percent: %lf\nrank score: %lf' % (otp, rs))

    return predict_date
Example #4
0
                   valid_set[1],
                   valid_target[:, 1],
                   status=1)
    #valid_set = np.c_[valid_set, hours2]
    hours3 = train(train_set[2],
                   train_target[:, 2],
                   valid_set[2],
                   valid_target[:, 2],
                   status=1)
    #valid_set = np.c_[valid_set, hours3]
    hours4 = train(train_set[3],
                   train_target[:, 3],
                   valid_set[3],
                   valid_target[:, 3],
                   status=1)
    # valid_set = np.c_[valid_set, hours4]
    # hours5 = train(train_set[4], train_target[:, 4], valid_set, valid_target[:, 4], status=1)
    predict_date = time_predict(valid_begin_time, hours1, hours2, hours3,
                                hours4)
    otp, rs = calculateAllMetrics(valid_signed_time, predict_date)
    print('on time percent: %lf\nrank score: %lf' % (otp, rs))
    """
    # test
    train_set, valid_set, train_target, train_begin_time, \
    train_signed_time, valid_begin_time = load_data(mode='test')
    hours1 = train(train_set, train_target, valid_set, status=1)
    hours2 = train(train_set, train_target, valid_set, status=2)
    predict_date = time_predict(valid_begin_time, hours1, hours2)
    submit(predict_date)
    """