Kaggle competition: Leaf Classification
<SOS> metric: MulticlassLoss Kaggle data source: leaf-classification<predict_on_test>
train = pd.read_csv('.. /input/train.csv')
 x_train = train.drop(['id', 'species'], axis=1 ).values
le = LabelEncoder().fit(train['species'])
 y_train = le.transform(train['species'])
 scaler = StandardScaler().fit(x_train)
 x_train = scaler.transform(x_train)
 test = pd.read_csv('.. /input/test.csv')
 test_ids = test.pop('id')
 x_test = test.values
scaler = StandardScaler().fit(x_test)
 x_test = scaler.transform(x_test)
 xg_train = xgb.DMatrix(x_train, label=y_train)
 val = train.sample(100)
 x_val = val.drop(['id', 'species'], axis=1 ).values
y_val = le.transform(val['species'])
 xg_val = xgb.DMatrix(x_val, label=y_val)
 param = {'objective': 'multi:softmax', 'eta': 0.1, 'max_depth': 6, 'silent': 1, 'nthread': 4, 'num_class': 99}
watchlist = [(xg_train,'train'),(xg_val, 'test')]
num_round = 5
bst = xgb.train(param, xg_train, num_round, watchlist)
 pred = bst.predict(xg_val)
 print('predicting, classification error=%f' %(sum(int(pred[i])!= y_val[i] for i in range(len(y_val)))/ float(len(y_val))))
def get_scores(y_true, y_pred)-> Dict[str, float]:
assert y_true.shape == y_pred.shape
assert y_true.shape[1] == num_targets
scores = {}
for target_name, i in zip(target_names, range(y_true.shape[1])) :
scores[target_name] = scipy.stats.spearmanr(y_true[:, i], y_pred[:, i])[0]
return scores
def predict(model: nn.Module,
data_loader: DataLoader,
text_emb: torch.Tensor,
device: torch.device = torch.device('cuda')) -> np.ndarray:
model.eval()
preds_fold = np.zeros(( len(data_loader.dataset), num_targets))
with torch.no_grad() :
for index, x_batch in data_loader:
x_batch =(x.to(device)for x in x_batch)
 emb_batch = text_emb[list(index)].to(device)
 y_pred = model(*x_batch, emb_batch ).detach()
preds_fold[list(index)] = torch.sigmoid(y_pred.cpu() ).numpy()
return preds_fold<split>
submission = pd.DataFrame(y_test, index=test_ids, columns=le.classes_)
 submission.to_csv('submission2.csv' )
gkf = GroupKFold(n_splits=n_splits)
 cv_scores = []
ema_cv_scores = []
fold_scores = []
train_preds = np.zeros(( len(train_x), num_targets))
test_preds = np.zeros(( len(test_x), num_targets))
ema_train_preds = np.zeros(( len(train_x), num_targets))
ema_test_preds = np.zeros(( len(test_x), num_targets))
for i,(train_idx, valid_idx)in enumerate(list(gkf.split(train_x, train_y, train_group))):
print(f'fold {i + 1}')
 train_fold_x, train_fold_y = train_x[train_idx], train_y[train_idx]
valid_fold_x, valid_fold_y = train_x[valid_idx], train_y[valid_idx]
train_fold_emb = train_text_emb[train_idx]
valid_fold_emb = train_text_emb[valid_idx]
train_dataset = TextDataset(train_fold_x, train_fold_y)
 valid_dataset = TextDataset(valid_fold_x)
 train_sampler = BucketSampler(train_dataset, train_dataset.get_keys() ,
bucket_size=batch_size * 20, batch_size=batch_size)
 valid_sampler = BucketSampler(valid_dataset, valid_dataset.get_keys() ,
batch_size=batch_size, shuffle_data=False)
 train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=False,
sampler=train_sampler, num_workers=0, collate_fn=collate_fn)
 valid_loader = DataLoader(valid_dataset, batch_size=batch_size, shuffle=False,
sampler=valid_sampler, collate_fn=collate_fn)
 model = LstmModel(embedding_matrix ).to(device)
 model.zero_grad()
optimizer = torch.optim.Adam(model.parameters() , lr=0.001)
 ema_model = copy.deepcopy(model)
 ema_model.eval()
ema_n = int(len(train_loader.dataset)/(updates_per_epoch * batch_size))
ema = EMA(model, mu, n=ema_n)
 for epoch in range(n_epochs):
epoch_start_time = time.time()
model.train()
for index, x_batch, y_batch in train_loader:
x_batch =(x.to(device)for x in x_batch)
 y_batch = y_batch.to(device)
 emb_batch = train_fold_emb[list(index)].to(device)
 y_preds = model(*x_batch, emb_batch)
 loss = nn.BCEWithLogitsLoss()(y_preds, y_batch)
 loss.backward()
optimizer.step()
model.zero_grad()
ema.on_batch_end(model)
 valid_preds = predict(model, valid_loader, valid_fold_emb, device=device)
 scores = get_scores(valid_fold_y, valid_preds)
 score = np.mean(list(scores.values()))
fold_scores.append({
'fold': i + 1,
'epoch': epoch + 1,
'score': score
})
 elapsed_time = time.time() - epoch_start_time
print('Epoch {}/{} \t score: {:.4f} \t time: {:.2f}s'.format(
 epoch + 1, n_epochs, score, elapsed_time))
ema.on_epoch_end(model)
 ema.set_weights(ema_model)
 ema_model.flatten_parameters()
train_preds[valid_idx] = valid_preds
ema_valid_preds = predict(ema_model, valid_loader, valid_fold_emb, device=device)
 ema_train_preds[valid_idx] = ema_valid_preds
cv_scores.append(score)
 ema_scores = get_scores(valid_fold_y, ema_valid_preds)
 ema_score = np.mean(list(ema_scores.values()))
print(f'EMA score: {ema_score:.4f}')
 ema_cv_scores.append(ema_scores)
 test_preds += predict(model, test_loader, test_text_emb, device=device)/ n_splits
ema_test_preds += predict(ema_model, test_loader, test_text_emb, device=device)/ n_splits<save_to_csv>
clf.score(x_train, y_train )
submission = pd.read_csv(SAMPLE_SUBMISSION_PATH)
 submission[target_names] = ema_test_preds
submission.to_csv('submission.csv', index=False )<compute_test_metric>
clf.best_estimator_