Kaggle competition: Jigsaw Multilingual Toxic Comment Classification
<SOS> metric: AUC Kaggle data source: jigsaw-multilingual-toxic-comment-classification<load_from_csv>
import numpy as np
import pandas as pd
from scipy.special import softmax
import os
train = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/train.csv')
 test = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/test.csv')
 sub = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/sample_submission.csv')
 train_paths = train.image_id.apply(format_path ).values
test_paths = test.image_id.apply(format_path ).values
train_labels = train.loc[:, 'healthy':].values
train_paths, valid_paths, train_labels, valid_labels = train_test_split(
 train_paths, train_labels, test_size=0.06, random_state=2020)
 train_paths = train.image_id.apply(format_path ).values
train_labels = train.loc[:, 'healthy':].values<normalization>
in_path = '/kaggle/input/'
path = '/kaggle/input/jigsaw-multilingual-toxic-comment-classification/'
files = ['tpu-inference-super-fast-xlmroberta/submission.csv',
'tpu-training-super-fast-xlmroberta/submission.csv',
'train-from-mlm-finetuned-xlm-roberta-large/submission.csv',
'xlm-roberta-augmentation-ssl-0-9417-pub-lb/submission.csv',
'jigsaw-tpu-bert-with-huggingface-and-keras/submission.csv']
sumbs = np.array([pd.read_csv(in_path+f ).toxic for f in files])
 pb = np.array([0.9459,0.9416,0.9422,0.9399,0.9158] )
img_size = 768
def decode_image(filename, label=None, image_size=(img_size, img_size)) :
bits = tf.io.read_file(filename)
 image = tf.image.decode_jpeg(bits, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.image.resize(image, image_size)
 if label is None:
return image
else:
return image, label
def data_augment(image, label=None, seed=2020):
image = tf.image.random_flip_left_right(image, seed=seed)
 image = tf.image.random_flip_up_down(image, seed=seed)
 if label is None:
return image
else:
return image, label<categorify>

 weight = lambda x: softmax(1/(1-x))
train_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( train_paths, train_labels)) 
.map(decode_image, num_parallel_calls=AUTO)
.map(data_augment, num_parallel_calls=AUTO)
.repeat() 
.shuffle(512)
.batch(BATCH_SIZE)
.prefetch(AUTO)
)
 valid_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( valid_paths, valid_labels)) 
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
.cache() 
.prefetch(AUTO)
)
 test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(test_paths)
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
 )<init_hyperparams>
w = weight(pb)
 print(w)
 sub = pd.read_csv(path+'sample_submission.csv')
 sub['toxic'] = sumbs.T@w
sub.head()
LR_START = 0.00001
LR_MAX = 0.0001 * strategy.num_replicas_in_sync
LR_MIN = 0.00001
LR_RAMPUP_EPOCHS = 5
LR_SUSTAIN_EPOCHS = 0
LR_EXP_DECAY =.8
def lrfn(epoch):
if epoch < LR_RAMPUP_EPOCHS:
lr =(LR_MAX - LR_START)/ LR_RAMPUP_EPOCHS * epoch + LR_START
elif epoch < LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS:
lr = LR_MAX
else:
lr =(LR_MAX - LR_MIN)* LR_EXP_DECAY**(epoch - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS)+ LR_MIN
return lr
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=True)
 rng = [i for i in range(EPOCHS)]
y = [lrfn(x)for x in rng]
plt.plot(rng, y)
 print("Learning rate schedule: {:.3g} to {:.3g} to {:.3g}".format(y[0], max(y), y[-1]))<choose_model_class>
test = pd.read_csv(path+'test.csv')
 dic_ids = {k:v.id for k,v in test.groupby(["lang"])}
adj = {
"fr":1.04,
"es":1.06,
"pt":.96,
"it":.97,
"tr":.98,
}
for l,v in adj.items() :
ids = dic_ids[l]
sub.loc[ids,"toxic"] *= v
sub.head()
def get_model(use_model):
base_model = use_model(weights='noisy-student',
include_top=False, pooling='avg',
input_shape=(img_size, img_size, 3))
x = base_model.output
predictions = Dense(train_labels.shape[1], activation="softmax" )(x)
 return Model(inputs=base_model.input, outputs=predictions)
 with strategy.scope() :
model = get_model(EfficientNetB7)
 model.compile(optimizer='nadam', loss='categorical_crossentropy',metrics=['categorical_accuracy'])
<train_model>
sub.to_csv('submission.csv', index=False )
<save_to_csv><EOS> 
sub.to_csv('submission.csv', index=False )