Kaggle competition: TalkingData AdTracking Fraud Detection Challenge
<SOS> metric: AUC Kaggle data source: talkingdata-adtracking-fraud-detection-challenge<choose_model_class>
import pandas as pd
import numpy as np
if TRAIN:
from_valid = False
only_valid = False
ckpts = [0]
valid_epochs = [
5, 10, 15, 20,
21, 22, 23, 24, 25,
26, 27, 28, 29, 30,
31, 32, 33, 34, 35,
36, 37, 38, 39, 40,
41, 42, 43, 44, 45,
46, 47, 48, 49, 50,
51, 52, 53, 54, 55,
56, 57, 58, 59, 60,
]
train_manager = Train_Manager(config, train_config)
 predictor, optimizer, loss_obj, loss_obj_answer, metrics = train_manager.get_train_objs(
 lr=LR, end_lr=END_LR, warmup_steps=WARMUP_STEPS 
)
 run_dummy_inputs(predictor)
 print('-' * 40)
 print('trainable variables:')
 for v in predictor.trainable_variables:
print(v.name)
 print('-' * 40)
 for ckpt_no in ckpts:
if RESUME_TRAINING or only_valid:
ckpt_manager, loaded_ckpt_no = load_ckpt(
 predictor, optimizer,
CKPT_TRAIN_PATH, ckpt_no=ckpt_no 
)
 else:
with strategy.scope() :
loaded_ckpt_no = 0
checkpoint = tf.train.Checkpoint(optimizer=optimizer, model=predictor)
 ckpt_path = CKPT_TRAIN_PATH
ckpt_manager = tf.train.CheckpointManager(checkpoint, directory=ckpt_path, max_to_keep=None)
 train_manager.train_valid(
 predictor, optimizer, ckpt_manager,
loss_obj, loss_obj_answer, metrics,
last_epoch=loaded_ckpt_no,
from_valid=from_valid,
valid_epochs=valid_epochs,
only_valid=only_valid
)<define_variables>
print("Reading the data...
 ")
 df1 = pd.read_csv('.. /input/r-lgbm-single-model-40m-rows-lb-0-9736/lgb_Usrnewness.csv')
 df2 = pd.read_csv('.. /input/simple-linear-stacking-with-ranks-lb-0-9760/sub_stacked.csv')
 df3 = pd.read_csv('.. /input/talkingdata-wordbatch-fm-ftrl-lb-0-9752/wordbatch_fm_ftrl.csv' )
CKPT_TRAIN_PATH<install_modules>
models = { 'df1' : {
'name':'r-lgbm',
'score':97.36,
'df':df1 },
'df2' : {
'name':'linstack',
'score':97.60,
'df':df2 },
'df3' : {
'name':'wordbatch',
'score':97.52,
'df':df3 }
}
if CKPT_TRAIN_PATH.startswith('gs://'):
!gsutil ls '{CKPT_TRAIN_PATH}'
else:
!ls -l '{CKPT_TRAIN_PATH}'<choose_model_class>
isa_lg = 0
isa_hm = 0
print("Blending...
 ")
 for df in models.keys() :
isa_lg += np.log(models[df]['df'].is_attributed)
 isa_hm += 1/(models[df]['df'].is_attributed)
 isa_lg = np.exp(isa_lg/3)
 isa_hm = 1/isa_hm
print("Isa log
")
 print(isa_lg[:5])
 print()
print("Isa harmo
")
 print(isa_hm[:5] )
predictor = TFEdFormerAnswerPredictionModel(config)
 if(( VALID or PRED)and not DUMMY)or SUBMISSION:
assert CKPT_PRED_PATH is not None and CKPT_PRED_PATH != './'<load_pretrained>
sub_log = pd.DataFrame()
sub_log['click_id'] = df1['click_id']
sub_log['is_attributed'] = isa_lg
sub_log.head()
if VALID:
valid_results = {'ckpt_no': [], 'valid_loss': [], 'valid_acc': [], 'valid_auc': []}
ckpts_to_run = [60]
print(f'ckpts_to_run: {ckpts_to_run}')
 for ckpt_no in ckpts_to_run:
if ckpt_no > 0:
ckpt_manager, loaded_ckpt_no = load_ckpt(
 predictor=predictor, optimizer=None,
ckpt_path=CKPT_PRED_PATH, ckpt_no=ckpt_no, 
)
 assert loaded_ckpt_no > 0
pred_iter_manager_class = Valid_Iter_Manager
pred_manager = Pred_Manager.create(
 config=config,
train_config=train_config,
pred_iter_manager_class=pred_iter_manager_class,
train_dt_path_or_obj=train_dt,
user_id_to_row_id_train_path=user_id_to_row_id_train_path,
unique_question_id_train_path=unique_question_id_splitted_train_path,
unique_lecture_id_train_path=unique_lecture_id_splitted_train_path,
question_history_at_training_end_path=question_history_at_training_end_for_valid_path,
single_question_history_at_training_end_optimized_path=single_question_history_at_training_end_optimized_for_valid_path,
valid_info_path=(valid_info_paths[VALID_FOLD] if VALID_FOLD in [0, 1, 2, 3] else None),
max_train_buffer_size=30000,
probe=False,
debug=False 
)
 run_pred(pred_manager, predictor, max_steps=MAX_VALID_ITER_STEPS, ckpt_no=ckpt_no)
 with open(f'valid_results_ckpt_{ckpt_no}.json', 'r', encoding='UTF-8')as fp:
valid_results_ckpt = json.load(fp)
 valid_results['ckpt_no'].append(ckpt_no)
 valid_results['valid_loss'].append(valid_results_ckpt['valid_loss'])
 valid_results['valid_acc'].append(valid_results_ckpt['valid_acc'])
 valid_results['valid_auc'].append(valid_results_ckpt['valid_auc'])
 print('=' * 80)
 with open(f'valid_results.json', 'w', encoding='UTF-8')as fp:
json.dump(valid_results, fp, ensure_ascii=False, indent=True)
 if not IS_KAGGLE:
!gsutil cp -r './valid_results.json' '{CKPT_PRED_PATH}'<load_from_csv>
sub_hm = pd.DataFrame()
sub_hm['click_id'] = df1['click_id']
sub_hm['is_attributed'] = isa_hm
sub_hm.head()
<load_from_disk><EOS> 
print("Writing...")
 sub_log.to_csv('sub_log.csv', index=False, float_format='%.9f')
 sub_hm.to_csv('sub_hm.csv', index=False, float_format='%.9f' )