Kaggle competition: Jigsaw Multilingual Toxic Comment Classification
C1, C2 = tf.constant(70, dtype='float32'), tf.constant(1000, dtype="float32")
 def score(y_true, y_pred):
tf.dtypes.cast(y_true, tf.float32)
 tf.dtypes.cast(y_pred, tf.float32)
 sigma = y_pred[:, 2] - y_pred[:, 0]
fvc_pred = y_pred[:, 1]
sigma_clip = tf.maximum(sigma, C1)
 delta = tf.abs(y_true[:, 0] - fvc_pred)
 delta = tf.minimum(delta, C2)
 sq2 = tf.sqrt(tf.dtypes.cast(2, dtype=tf.float32))
metric =(delta / sigma_clip)*sq2 + tf.math.log(sigma_clip* sq2)
 return K.mean(metric)
 def qloss(y_true, y_pred):
qs = [0.2, 0.50, 0.8]
q = tf.constant(np.array([qs]), dtype=tf.float32)
 e = y_true - y_pred
v = tf.maximum(q*e,(q-1)*e)
 return K.mean(v)
 def mloss(_lambda):
def loss(y_true, y_pred):
return _lambda * qloss(y_true, y_pred)+(1 - _lambda)*score(y_true, y_pred)
 return loss
def make_model(nh):
z = L.Input(( nh,), name="Patient")
 x = L.Dense(100, activation="relu", name="d1" )(z)
 x = L.Dense(100, activation="relu", name="d2" )(x)
 p1 = L.Dense(3, activation="linear", name="p1" )(x)
 p2 = L.Dense(3, activation="relu", name="p2" )(x)
 preds = L.Lambda(lambda x: x[0] + tf.cumsum(x[1], axis=1),
name="preds" )([p1, p2])
 model = M.Model(z, preds, name="CNN")
 model.compile(loss=mloss(0.8), optimizer=tf.keras.optimizers.Adam(lr=0.1, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.01, amsgrad=False), metrics=[score])
 return model<prepare_x_and_y>
test0 = pd.read_csv('/kaggle/input/fastsub-test/submission.csv')
 test0.head()
y = tr['FVC'].values
z = tr[FE].values
ze = sub[FE].values
nh = z.shape[1]
pe = np.zeros(( ze.shape[0], 3))
pred = np.zeros(( z.shape[0], 3))<choose_model_class>
test1 = pd.read_csv('/kaggle/input/kernel0531v4/submission.csv')
 test1.head()
NFOLD = 5
kf = KFold(n_splits=NFOLD )<compute_test_metric>
test2 = pd.read_csv('/kaggle/input/jmtfstsub0407/submission.csv')
 test2.head()
def score2(y_true, y_pred):
sigma = y_pred[:, 2] - y_pred[:, 0]
fvc_pred = y_pred[:, 1]
sigma_clip = tf.maximum(sigma, C1)
 delta = np.abs(y_true - fvc_pred)
 delta[delta>1000]=1000
sq2 = np.sqrt(2)
 metric =(delta / sigma_clip)*sq2 + tf.math.log(sigma_clip* sq2)
 return np.mean(metric)
<predict_on_test>
test3 = test1.copy()
test3['toxic'] = test1['toxic']*0.55 + 0.45*(test0['toxic']*0.5+test2['toxic']*0.5 )
%%time
cnt = 0
EPOCHS = 800
for tr_idx, val_idx in kf.split(z):
cnt += 1
print(f"FOLD {cnt}")
 net = make_model(nh)
 net.fit(z[tr_idx], y[tr_idx], batch_size=BATCH_SIZE, epochs=EPOCHS,
validation_data=(z[val_idx], y[val_idx]), verbose=0)
 print("train", net.evaluate(z[tr_idx], y[tr_idx], verbose=0, batch_size=BATCH_SIZE))
print("val", net.evaluate(z[val_idx], y[val_idx], verbose=0, batch_size=BATCH_SIZE))
print("predict val...")
 pred[val_idx] = net.predict(z[val_idx], batch_size=BATCH_SIZE, verbose=0)
 print("predict test...")
 pe += net.predict(ze, batch_size=BATCH_SIZE, verbose=0)/ NFOLD 
<compute_test_metric>
test=pd.read_csv('.. /input/jigsaw-multilingual-toxic-comment-classification/test.csv')
 test3=pd.merge(test3,test,on='id')
 test3.head()
print(score2(y,pred))<compute_test_metric>
test3.loc[test3["lang"] == "es", "toxic"] *= 1.06
test3.loc[test3["lang"] == "fr", "toxic"] *= 1.04
test3.loc[test3["lang"] == "it", "toxic"] *= 0.97
test3.loc[test3["lang"] == "pt", "toxic"] *= 0.96
test3.loc[test3["lang"] == "tr", "toxic"] *= 0.98
sigma_opt = mean_absolute_error(y, pred[:, 1])
 unc = pred[:,2] - pred[:, 0]
sigma_mean = np.mean(unc)
 print(sigma_opt, sigma_mean )<prepare_output>
test3[['id','toxic']].to_csv('submission.csv', index=False )
<prepare_output><EOS> 
test3['l']=test1['toxic']
test3.corr()