Kaggle competition: Predicting Molecular Properties
<SOS> metric: GroupMeanLogMAE Kaggle data source: predicting-molecular-properties<choose_model_class>
%matplotlib inline
sns.set()
pd.options.display.precision = 15
warnings.filterwarnings("ignore")
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model.parameters() , lr=0.0005)
 try:
checkpoint = torch.load('.. /input/cpoint/checkpoint_5005.pth')
 model.load_state_dict(checkpoint['state_dict'])
 start = checkpoint['epoch']
print(start)
 optimizer.load_state_dict(checkpoint['optimizer'])
 except:
start = 0
scheduler = lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.1 )<find_best_params>
giba_columns = ['inv_dist0', 'inv_dist1', 'inv_distP', 'inv_dist0R', 'inv_dist1R', 'inv_distPR',
'inv_dist0E', 'inv_dist1E', 'inv_distPE', 'linkM0', 'linkM1',
'min_molecule_atom_0_dist_xyz',
'mean_molecule_atom_0_dist_xyz',
'max_molecule_atom_0_dist_xyz',
'sd_molecule_atom_0_dist_xyz',
'min_molecule_atom_1_dist_xyz',
'mean_molecule_atom_1_dist_xyz',
'max_molecule_atom_1_dist_xyz',
'sd_molecule_atom_1_dist_xyz',
'coulomb_C.x', 'coulomb_F.x', 'coulomb_H.x', 'coulomb_N.x', 'coulomb_O.x',
'yukawa_C.x', 'yukawa_F.x', 'yukawa_H.x', 'yukawa_N.x', 'yukawa_O.x',
'vander_C.x', 'vander_F.x', 'vander_H.x', 'vander_N.x', 'vander_O.x',
'coulomb_C.y', 'coulomb_F.y', 'coulomb_H.y', 'coulomb_N.y', 'coulomb_O.y',
'yukawa_C.y', 'yukawa_F.y', 'yukawa_H.y', 'yukawa_N.y', 'yukawa_O.y',
'vander_C.y', 'vander_F.y', 'vander_H.y', 'vander_N.y', 'vander_O.y',
'distC0', 'distH0', 'distN0', 'distC1', 'distH1', 'distN1',
'adH1', 'adH2', 'adH3', 'adH4', 'adC1', 'adC2', 'adC3', 'adC4',
'adN1', 'adN2', 'adN3', 'adN4',
'NC', 'NH', 'NN', 'NF', 'NO']
qm9_columns = [
'rc_A', 'rc_B', 'rc_C',
'mu', 'alpha',
'homo','lumo', 'gap',
'zpve', 'Cv',
'freqs_min', 'freqs_max', 'freqs_mean',
'mulliken_min', 'mulliken_max',
'mulliken_atom_0', 'mulliken_atom_1'
]
label_columns = ['molecule_name',
'atom_index_0', 'atom_index_1',
'structure_atom_0','structure_atom_1','type']
index_columns = ['type','molecule_name','id']
diff_columns = ['Cv',
'alpha', 'freqs_max', 'freqs_mean', 'freqs_min',
'gap', 'homo', 'linkM0',
'lumo', 'mu', 'mulliken_atom_0', 'mulliken_max', 'mulliken_min',
'rc_A', 'rc_B', 'rc_C', 'sd_molecule_atom_1_dist_xyz', 'zpve']
def cuda(x):
return x.cuda(non_blocking=True)if torch.cuda.is_available() else x
def eval_(output, target, maxk):
e_batch_size = target.size(0)
 _, pred = output.topk(maxk, 1, True, True)
 correct = pred.eq(target.topk(1, 1, True, True)[1].expand_as(pred))
res = []
k = maxk
correct_k = correct[:k].view(-1 ).float().sum(0)
 total = correct_k.item()
res.append(correct_k.mul_(100.0 / batch_size))
return res, total<init_hyperparams>
%%time
file_folder = '.. /input/champs-scalar-coupling'
print("Load Giba's features...")
 train = pd.read_csv('.. /input/giba-molecular-features/train_giba.csv/train_giba.csv',
usecols=index_columns+giba_columns+['scalar_coupling_constant'])
 y = train['scalar_coupling_constant']
train = reduce_mem_usage(train,verbose=True)
 test = pd.read_csv('.. /input/giba-molecular-features/test_giba.csv/test_giba.csv',
usecols=index_columns+giba_columns)
 test = reduce_mem_usage(test,verbose=True)
 print("Load QM9 features...")
 data_qm9 = pd.read_pickle('.. /input/quantum-machine-9-qm9/data.covs.pickle')
 data_qm9 = data_qm9.drop(columns = ['type', 'linear', 'atom_index_0', 'atom_index_1',
'scalar_coupling_constant', 'U', 'G', 'H',
'mulliken_mean', 'r2', 'U0'], axis=1)
 data_qm9 = reduce_mem_usage(data_qm9,verbose=False)
 train = pd.merge(train, data_qm9, how='left', on=['molecule_name','id'])
 test = pd.merge(test, data_qm9, how='left', on=['molecule_name','id'])
 del data_qm9
gc.collect()
print("Encoding label features...
 ")
 for f in label_columns:
if f in train.columns:
lbl = LabelEncoder()
lbl.fit(list(train[f].values)+ list(test[f].values))
train[f] = lbl.transform(list(train[f].values))
test[f] = lbl.transform(list(test[f].values))
train = train[index_columns+giba_columns+qm9_columns]
test = test[index_columns+giba_columns+qm9_columns]
n_epochs = start + 6
cur_epoch = 0
sum_true_pred = 0
for epoch in range(start, n_epochs):
train_loss = []
cur_epoch = epoch
save_checkpoint({'epoch': epoch,
'state_dict': model.state_dict() ,
'optimizer' : optimizer.state_dict() ,
}, False)
 for batch_i,(data, target)in tqdm_notebook(enumerate(train_loader), total = len(train_loader)) :
data, target = cuda(data), cuda(target)
 optimizer.zero_grad()
output = model(data)
 loss = criterion(output, target.float())
 with open('results.txt', 'a')as file:
file.write(str(loss.item())+'
')
 sum_true_pred+=(eval_(output, target, 5)) [1]
train_loss.append(loss.item())
 loss.backward()
optimizer.step()
scheduler.step()
print(f'Epoch {epoch}, train loss: {np.mean(train_loss):.4f}')
 save_checkpoint({'epoch': cur_epoch,
'state_dict': model.state_dict() ,
'optimizer' : optimizer.state_dict() ,
}, False )<save_to_csv>
coef = [0.25, 0.5, 0.2, 0.05]
def custom_objective(y_true, y_pred):
c = 0.5
residual = y_pred - y_true
grad = c * residual /(np.abs(residual)+ c)
 hess = c ** 2 /(np.abs(residual)+ c)** 2
h = 1.2
scale = 1 +(residual / h)** 2
scale_sqrt = np.sqrt(scale)
 grad_huber = residual / scale_sqrt
hess_huber = 1 / scale / scale_sqrt
grad_rmse = residual
hess_rmse = 1.0
grad_mae = np.array(residual)
 grad_mae[grad_mae > 0] = 1.
 grad_mae[grad_mae <= 0] = -1.
 hess_mae = 1.0
return coef[0] * grad + coef[1] * grad_huber + coef[2] * grad_rmse + coef[3] * grad_mae, \
coef[0] * hess + coef[1] * hess_huber + coef[2] * hess_rmse + coef[3] * hess_mae
params = {
'num_leaves': 400,
'objective': custom_objective,
'max_depth': 9,
'learning_rate': 0.1,
'boosting_type': 'gbdt',
'metric': 'mae',
'verbosity': -1,
'lambda_l1': 2,
'lambda_l2': 0.2,
'feature_fraction': 0.6,
}
sub = pd.read_csv('.. /input/humpback-whale-identification/sample_submission.csv')
 model.eval()
for(data, target, name)in tqdm_notebook(test_loader):
data = cuda(data)
 output = model(data)
 output = output.cpu().detach().numpy()
for i,(e, n)in enumerate(list(zip(output, name))):
sub.loc[sub['Image'] == n, 'Id'] = ' '.join(lab_encoder.inverse_transform(e.argsort() [-5:][::-1]))
sub.to_csv('submission.csv', index=False )<set_options>
X_short = pd.DataFrame({'ind': list(train.index),
'type': train['type'].values,
'oof': [0] * len(train),
'target': y.values})
 X_short_test = pd.DataFrame({'ind': list(test.index),
'type': test['type'].values,
'prediction': [0] * len(test)} )
%matplotlib inline
warnings.filterwarnings("ignore",category=DeprecationWarning )<load_from_csv>
%%time
CV_score = 0
folds = GroupKFold(n_splits=3)
 n_estimators = [6000, 2500, 3500, 3000, 3000, 5000, 3000, 3000]
for t in train['type'].unique() :
type_ = lbl.inverse_transform([t])[0]
print(f'Training of type {t}: {type_}.
 ')
 X_t = train.loc[train['type'] == t]
X_test_t = test.loc[test['type'] == t]
y_t = X_short.loc[X_short['type'] == t, 'target']
scaler = StandardScaler()
X_t[diff_columns] = scaler.fit_transform(X_t[diff_columns].fillna(-999))
X_test_t[diff_columns] = scaler.transform(X_test_t[diff_columns].fillna(-999))
molecules_id = X_t.molecule_name
result_dict_lgb = train_lgb_regression_group(X=X_t.drop(columns=['molecule_name','id']),
X_test=X_test_t.drop(columns=['molecule_name','id']),
y=y_t, params=params,
folds=folds, groups=molecules_id,
eval_metric='group_mae',
plot_feature_importance=False,
verbose=3000, early_stopping_rounds=200,
n_estimators=n_estimators[t])
 X_short.loc[X_short['type'] == t, 'oof'] = result_dict_lgb['oof']
X_short_test.loc[X_short_test['type'] == t, 'prediction'] = result_dict_lgb['prediction']
CV_score += np.array(result_dict_lgb['scores'] ).mean() /8
train_df = pd.read_csv('.. /input/humpback-whale-identification/train.csv')
 sub = pd.read_csv('.. /input/humpback-whale-identification/sample_submission.csv')
 best_model_path = '.. /input/bbox-seresnext101-pytorch-hung96ad/best_model.pth'
TRN_IMGS_DIR = '.. /input/humpback-whale-identification/train/'
TST_IMGS_DIR = '.. /input/humpback-whale-identification/test/'
BBOX_TRAIN_CSV = '.. /input/box-whale/bounding/bounding_boxes_train.csv'
BBOX_TEST_CSV = '.. /input/box-whale/bounding/bounding_boxes_test.csv'
bbox_train = pd.read_csv(BBOX_TRAIN_CSV)
 bbox_test = pd.read_csv(BBOX_TEST_CSV)
 bbox_train = train_df.join(bbox_train.set_index('Image'), on='Image' )<categorify>
sub = pd.read_csv(f'{file_folder}/sample_submission.csv')
 sub['scalar_coupling_constant'] = X_short_test['prediction']
today = str(datetime.date.today())
 sub.to_csv(f'LGB_{today}_{CV_score:.4f}.csv', index=False )