Kaggle competition: Toxic Comment Classification Challenge
<SOS> metric: MCAUC Kaggle data source: toxic-comment-classification-challenge<choose_model_class>
warnings.filterwarnings("ignore")
def build_model(transformer, max_len=512):

 input_word_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
 sequence_output = transformer(input_word_ids)[0]
cls_token = sequence_output[:, 0, :]
out = tf.keras.layers.Dense(192, activation='relu' )(cls_token)
 out = tf.keras.layers.Dense(64, activation='relu' )(out)
 out = tf.keras.layers.Dense(64, activation='relu' )(out)
 out = Dense(1, activation='sigmoid' )(out)
 model = Model(inputs=input_word_ids, outputs=out)
 model.compile(Adam(lr=1e-5), loss='binary_crossentropy', metrics=['accuracy'])
 return model<train_model>
train = pd.read_csv('.. /input/jigsaw-toxic-comment-classification-challenge/train.csv.zip' ).fillna(' ')
 test = pd.read_csv('.. /input/jigsaw-toxic-comment-classification-challenge/test.csv.zip' ).fillna(' ' )
n_steps = x_train.shape[0] // BATCH_SIZE
train_history = model.fit(
 train_dataset,
steps_per_epoch=n_steps,
validation_data=valid_dataset,
epochs=EPOCHS
)<train_model>
train_text = train['comment_text']
test_text = test['comment_text']
all_text = pd.concat([train_text, test_text])
 class_names = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
n_steps = x_valid.shape[0] // BATCH_SIZE
train_history_2 = model.fit(
 valid_dataset.repeat() ,
steps_per_epoch=n_steps,
epochs=EPOCHS*2
)<save_to_csv>
word_vec = TfidfVectorizer(
 sublinear_tf=True,
strip_accents='unicode',
analyzer='word',
token_pattern=r'\w{1,}',
ngram_range=(1, 2),
max_features=30000)
 char_vec = TfidfVectorizer(
 sublinear_tf=True,
strip_accents='unicode',
analyzer='char',
ngram_range=(1, 3),
max_features=30000)
 vec1 = make_union(word_vec, char_vec)
 vec1.fit(all_text)
 train_features = vec1.transform(train_text)
 test_features = vec1.transform(test_text )
sub['toxic'] = model.predict(test_dataset, verbose=1)
 sub1 = sub[['id', 'toxic']]<save_to_csv>
re_tok = re.compile(f'([{string.punctuation}“”¨«»®´·º½¾¿¡§£₤‘’])')
 def tokenize(s):
return re_tok.sub(r' \1 ', s ).split()
vec2 = TfidfVectorizer(ngram_range=(1,2),
tokenizer=tokenize,
min_df=3,
max_df=0.9,
strip_accents='unicode',
use_idf=1,
smooth_idf=1,
sublinear_tf=1)
 train_term_doc = vec2.fit_transform(train_text)
 test_term_doc = vec2.transform(test_text )
sub1.rename(columns={'toxic':'toxic1'}, inplace=True)
 sub2.rename(columns={'toxic':'toxic2'}, inplace=True)
 sub3 = pd.merge(sub1, sub2, how='left', on='id')
 sub3['toxic'] =(sub3['toxic1'] * 0.1)+(sub3['toxic2'] * 0.9)
 sub3['toxic'] =(sub3['toxic2'] * 0.39)+(sub3['toxic'] * 0.61)
 sub3[['id', 'toxic']].to_csv('submission.csv', index=False )<import_modules>
scores1 = []
sub1 = pd.DataFrame.from_dict({'id': test['id']})
 for class_name in class_names:
train_target = train[class_name]
classifier = LogisticRegression(C = 0.1, solver='saga', class_weight='balanced')
 cv_score = np.mean(cross_val_score(classifier, train_features, train_target, cv=3, scoring='roc_auc'))
scores1.append(cv_score)
 print('CV score for class {} is {:.4%}'.format(class_name, cv_score))
classifier.fit(train_features, train_target)
 sub1[class_name] = classifier.predict_proba(test_features)[:, 1]
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import re
import tensorflow as tf
from tensorflow.keras.layers import Dense, Dropout, Input
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint
from kaggle_datasets import KaggleDatasets
import transformers
from tqdm.notebook import tqdm
from tokenizers import BertWordPieceTokenizer<categorify>
train_x = train_term_doc
test_x = test_term_doc
def pr(y_i, y):
p = train_x[y==y_i].sum(0)
 return(p+1)/(( y==y_i ).sum() +1)
 def get_mdl(y):
y = y.values
r = np.log(pr(1,y)/ pr(0,y))
m = LogisticRegression(C=4, solver='liblinear', dual=True)
 x_nb = train_x.multiply(r)
 return m.fit(x_nb, y), r
scores2 = []
sub2 = pd.DataFrame.from_dict({'id': test['id']})
 for i, j in enumerate(class_names):
m,r = get_mdl(train[j])
 sub2[j] = m.predict_proba(test_x.multiply(r)) [:,1]
y_pred = m.predict_proba(train_x.multiply(r)) [:,1]
print('fit score for class {} is {:.4%}'.format(j, roc_auc_score(train_target, y_pred)))
 scores2.append(roc_auc_score(train_target, y_pred))
def clean_text(text):
text = str(text)
 text = re.sub(r'[0-9"]', '', text)
 text = re.sub(r'
text = re.sub(r'@[\S]+\b', '', text)
 text = re.sub(r'https?\S+', '', text)
 text = re.sub(r'\s+', ' ', text)
 return text.strip()<string_transform>
sub = sub1.copy()
for i, j in enumerate(class_names):
pb = np.array([scores1[i], scores2[i]])
 weights = lambda x: x/sum(x)
 w = weights(pb)
 print(w)
 sub[j] = sub1[j]*w[0] + sub2[j]*w[1]
def text_process(text):
ws = text.split(' ')
 if(len(ws)>160):
text = ' '.join(ws[:160])+ ' ' + ' '.join(ws[-32:])
 return text<categorify>
sub.to_csv('submission.csv', index=False )
<load_pretrained><EOS> 
sub.to_csv('submission.csv', index=False )