Kaggle competition: Kannada MNIST
print(tr.shape, chunk.shape, sub.shape, data.shape)
 print(tr.Patient.nunique() , chunk.Patient.nunique() , sub.Patient.nunique() ,
data.Patient.nunique() )<feature_engineering>
train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 test = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv' )
data['min_week'] = data['Weeks']
data.loc[data.WHERE=='test','min_week'] = np.nan
data['min_week'] = data.groupby('Patient')['min_week'].transform('min' )<categorify>
%matplotlib inline
np.random.seed(2)
 sns.set(style='white', context='notebook', palette='deep' )
base = data.loc[data.Weeks == data.min_week]
base = base[['Patient','FVC']].copy()
base.columns = ['Patient','min_FVC']
base['nb'] = 1
base['nb'] = base.groupby('Patient')['nb'].transform('cumsum')
 base = base[base.nb==1]
base.drop('nb', axis=1, inplace=True )<merge>
train_df = train
test_df = test
data = data.merge(base, on='Patient', how='left')
 data['base_week'] = data['Weeks'] - data['min_week']
del base<data_type_conversions>
x_train=train_df.drop(["label"], axis=1 ).values.astype('float32')
 y_train=train_df["label"].values.astype("int32")
 x_test=test_df.drop(["id"],axis=1 ).values.astype('float32' )
COLS = ['Sex','SmokingStatus']
FE = []
for col in COLS:
for mod in data[col].unique() :
FE.append(mod)
 data[mod] =(data[col] == mod ).astype(int )<feature_engineering>
x_train,x_val,y_train,y_val=train_test_split(x_train, y_train, test_size=0.2)
data['age'] =(data['Age'] - data['Age'].min())/(data['Age'].max() - data['Age'].min())
 data['BASE'] =(data['min_FVC'] - data['min_FVC'].min())/(data['min_FVC'].max() - data['min_FVC'].min())
 data['week'] =(data['base_week'] - data['base_week'].min())/(data['base_week'].max() - data['base_week'].min())
 data['percent'] =(data['Percent'] - data['Percent'].min())/(data['Percent'].max() - data['Percent'].min())
 FE += ['age','percent','week','BASE']<filter>
X=Input(shape=[28,28,1])
 x=Conv2D(16,(3,3), strides=1, padding="same", name="conv1" )(X)
 x=BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer="uniform", name="batch1" )(x)
 x=Activation('relu',name='relu1' )(x)
 x=Dropout(0.1 )(x)
 x=Conv2D(32,(3,3), strides=1, padding="same", name="conv2" )(x)
 x=BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch2" )(x)
 x=Activation('relu',name='relu2' )(x)
 x=Dropout(0.15 )(x)
 x=MaxPooling2D(pool_size=2, strides=2, padding="same", name="max2" )(x)
 x=Conv2D(64,(5,5), strides=1, padding ="same", name="conv3" )(x)
 x=BatchNormalization(momentum=0.17, epsilon=1e-5, gamma_initializer="uniform", name="batch3" )(x)
 x=Activation('relu', name="relu3" )(x)
 x=MaxPooling2D(pool_size=2, strides=2, padding="same", name="max3" )(x)
 x=Conv2D(128,(5,5), strides=1, padding="same", name="conv4" )(x)
 x=BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch4" )(x)
 x=Activation('relu', name="relu4" )(x)
 x=Dropout(0.17 )(x)
 x=Conv2D(64,(3,3), strides=1, padding="same", name="conv5" )(x)
 x=BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch5" )(x)
 x=Activation('relu', name='relu5' )(x)
 x=Dropout(0.2 )(x)
 x=Conv2D(32,(3,3), strides=1, padding="same", name="conv6" )(x)
 x=BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch6" )(x)
 x=Activation('relu', name="relu6" )(x)
 x=Dropout(0.05 )(x)
 x=Flatten()(x)
 x=Dense(50, name="Dense1" )(x)
 x=Activation('relu', name='relu7' )(x)
 x=Dropout(0.05 )(x)
 x=Dense(25, name="Dense2" )(x)
 x=Activation('relu', name='relu8' )(x)
 x=Dropout(0.03 )(x)
 x=Dense(10, name="Dense3" )(x)
 x=Activation('softmax' )(x)
 model=Model(inputs=X, outputs=x )
tr = data.loc[data.WHERE=='train']
chunk = data.loc[data.WHERE=='val']
sub = data.loc[data.WHERE=='test']
del data<import_modules>
checkpoint=ModelCheckpoint('bestweights.hdf5', monitor='val_accuracy', verbose=1, save_best_only=True, mode="max")
 epochs=80
lr=0.001
optimizer=Adam(lr=lr, decay=lr/(epochs*1.5))
model.compile(optimizer=optimizer, loss="sparse_categorical_crossentropy",metrics=["accuracy"])
 datagenerator=ImageDataGenerator(rotation_range=9, zoom_range=0.25, width_shift_range=0.25, height_shift_range=0.25)
 datagenerator.fit(x_train)
 batch_size=64
history=model.fit_generator(datagenerator.flow(x_train, y_train, batch_size=batch_size), epochs=epochs, validation_data=(x_val, y_val), verbose=2,steps_per_epoch=x_train.shape[0]//batch_size, callbacks=[checkpoint])
 model.load_weights("bestweights.hdf5" )
import tensorflow as tf
import tensorflow.keras.backend as K
import tensorflow.keras.layers as L
import tensorflow.keras.models as M<compute_test_metric>
results = model.predict(x_test )
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() :
z = L.Input(( 9,), 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="adam", metrics=[score])
 return model<prepare_x_and_y>
results = np.argmax(results, axis = 1 )
y = tr['FVC'].values
z = tr[FE].values
ze = sub[FE].values
pe = np.zeros(( ze.shape[0], 3))
pred = np.zeros(( z.shape[0], 3))<choose_model_class>
fin=pd.DataFrame()
fin['id']=list(test_df.values[0:,0])
 fin['label']=results
<predict_on_test><EOS> 
fin.to_csv('submission.csv' ,index = False )