Kaggle competition: Gendered Pronoun Resolution
<SOS> metric: MulticlassLoss Kaggle data source: gendered-pronoun-resolution<init_hyperparams>
nlp = spacy.load('en_core_web_sm')
 test = pd.read_csv('.. /input/test_stage_2.tsv', delimiter='\t' ).rename(columns={'A': 'A_Noun', 'B': 'B_Noun'})
 sub = pd.read_csv('.. /input/sample_submission_stage_2.csv')
 test.shape, sub.shape
LR_START = 0.00001
LR_MAX = 0.00005 * 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]))<compute_train_metric>
gh_test = pd.read_csv("https://raw.githubusercontent.com/google-research-datasets/gap-coreference/master/gap-test.tsv", delimiter='\t')
 gh_train = pd.read_csv("https://raw.githubusercontent.com/google-research-datasets/gap-coreference/master/gap-development.tsv", delimiter='\t')
 gh_valid = pd.read_csv("https://raw.githubusercontent.com/google-research-datasets/gap-coreference/master/gap-validation.tsv", delimiter='\t')
 train = pd.concat(( gh_test, gh_train, gh_valid)).rename(columns={'A': 'A_Noun', 'B': 'B_Noun'} ).reset_index(drop=True)
 train.shape
def categorical_smooth_loss(y_true, y_pred, label_smoothing=0.1):
loss = tf.keras.losses.categorical_crossentropy(y_true, y_pred, label_smoothing=label_smoothing)
 return loss<install_modules>
def name_replace(s, r1, r2):
s = str(s ).replace(r1,r2)
 for r3 in r1.split(' '):
s = str(s ).replace(r3,r2)
 return s
def get_features(df):
df['section_min'] = df[['Pronoun-offset', 'A-offset', 'B-offset']].min(axis=1)
 df['Pronoun-offset2'] = df['Pronoun-offset'] + df['Pronoun'].map(len)
 df['A-offset2'] = df['A-offset'] + df['A_Noun'].map(len)
 df['B-offset2'] = df['B-offset'] + df['B_Noun'].map(len)
 df['A-dist_abs'] =(df['Pronoun-offset'] - df['A-offset'] ).abs()
df['B-dist_abs'] =(df['Pronoun-offset'] - df['B-offset'] ).abs()
df['A-dist'] =(df['Pronoun-offset'] - df['A-offset'])
 df['B-dist'] =(df['Pronoun-offset'] - df['B-offset'])
 df['section_max'] = df[['Pronoun-offset2', 'A-offset2', 'B-offset2']].max(axis=1)
 df['A_max'] =(df['A-offset2'] == df['section_max'] ).astype(int)
 df['A_min'] =(df['A-offset2'] == df['section_min'] ).astype(int)
 df['B_max'] =(df['B-offset2'] == df['section_max'] ).astype(int)
 df['B_min'] =(df['B-offset2'] == df['section_min'] ).astype(int)
 df['wc'] = df.apply(lambda r: len(str(r['Text'][r['section_min']: r['section_max']] ).split(' ')) , axis=1)
 df['Text'] = df.apply(lambda r: name_replace(r['Text'], r['A_Noun'], 'subjectone'), axis=1)
 df['Text'] = df.apply(lambda r: name_replace(r['Text'], r['B_Noun'], 'subjecttwo'), axis=1)
 return(df)
 train = get_features(train)
 test = get_features(test )
!pip install -q efficientnet<import_modules>
def get_nlp_features(s, w):
doc = nlp(str(s))
tokens = pd.DataFrame([[token.text, token.dep_] for token in doc], columns=['text', 'dep'])
 return len(tokens[(( tokens['text']==w)&(tokens['dep']=='poss')) ])
 train['A-poss'] = train['Text'].map(lambda x: get_nlp_features(x, 'subjectone'))
train['B-poss'] = train['Text'].map(lambda x: get_nlp_features(x, 'subjecttwo'))
test['A-poss'] = test['Text'].map(lambda x: get_nlp_features(x, 'subjectone'))
test['B-poss'] = test['Text'].map(lambda x: get_nlp_features(x, 'subjecttwo'))
import efficientnet.tfkeras as efn<choose_model_class>
train = train.rename(columns={'A-coref':'A', 'B-coref':'B'})
 train['A'] = train['A'].astype(int)
 train['B'] = train['B'].astype(int)
 train['NEITHER'] = 1.0 -(train['A'] + train['B'] )
with strategy.scope() :
enet = efn.EfficientNetB7(input_shape=(512, 512, 3), weights='noisy-student', include_top=False)
 enet.trainable = False
model1 = tf.keras.Sequential([
enet,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation='softmax')
 ])
 model1.compile(
 optimizer=tf.keras.optimizers.Adam(lr=0.0001),
loss = categorical_smooth_loss,
metrics=['categorical_accuracy'] 
)
 model1.summary()<train_model>
col = ['Pronoun-offset', 'A-offset', 'B-offset', 'section_min', 'Pronoun-offset2', 'A-offset2', 'B-offset2', 'section_max',
'A-poss', 'B-poss', 'A-dist', 'B-dist', 'A-dist_abs', 'B-dist_abs', 'A_max', 'A_min', 'B_max', 'B_min', 'wc']
x1, x2, y1, y2 = model_selection.train_test_split(train[col].fillna(-1), train[['A', 'B', 'NEITHER']], test_size=0.2, random_state=1)
 etc = multiclass.OneVsRestClassifier(ensemble.ExtraTreesClassifier(n_jobs=-1, max_depth=9, n_estimators=1000, random_state=30))
etc.fit(x1, y1)
 print('log_loss', metrics.log_loss(y2, etc.predict_proba(x2)))
 etc.fit(train[col].fillna(-1), train[['A', 'B', 'NEITHER']])
 results = etc.predict_proba(test[col])
 test['A'] = results[:,0]
test['B'] = results[:,1]
test['NEITHER'] = results[:,2]
test[['ID', 'A', 'B', 'NEITHER']].to_csv('submission.csv', index=False )