Kaggle competition: Mechanisms of Action (MoA) Prediction
layer_outputs = [layer.output for layer in model.layers[0:8]]
activation_model = models.Model(inputs = model.input, outputs = layer_outputs )<predict_on_test>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tqdm import tqdm_notebook
activations = activation_model.predict(img_tensor )<predict_on_test>
train=pd.read_csv("/kaggle/input/lish-moa/train_features.csv")
 train
results = np.argmax(model.predict(X_test), axis=1)
 results = pd.Series(results, name = "Label")
 results.head(2 )<save_to_csv>
train_target = pd.read_csv(".. /input/lish-moa/train_targets_scored.csv")
 train_target
submission.to_csv("CNN_MNIST_results.csv",index=False )<import_modules>
ctlVehicle_idx = train["cp_type"] != "ctl_vehicle"
train = train.loc[ctlVehicle_idx].reset_index(drop=True)
 train = train.drop("cp_type", axis=1)
 train_target = train_target.loc[ctlVehicle_idx].reset_index(drop=True )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import random
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.utils import np_utils<train_model>
gcols = [g for g in train.columns if "g-" in g]
ccols = [c for c in train.columns if "c-" in c]
cpcols = [cp for cp in train.columns if "cp_" in cp]
( X_train, y_train),(X_test, y_test)= mnist.load_data()
print("X_train shape", X_train.shape)
 print("y_train shape", y_train.shape)
 print("X_test shape", X_test.shape)
 print("y_test shape", y_test.shape )<load_from_csv>
test = pd.read_csv("/kaggle/input/lish-moa/test_features.csv")
 ctlVehicle_test = test["cp_type"] == "ctl_vehicle"
test = test.drop("cp_type", axis=1)
 train["cp_time"]=train["cp_time"].map({24: 1, 48: 2, 72: 3})
 train["cp_dose"] = train["cp_dose"].map({"D1": 1, "D2": 2})
 test["cp_time"]= test["cp_time"].map({24: 1, 48: 2, 72: 3})
 test["cp_dose"]=test["cp_dose"].map({"D1": 1, "D2": 2})
 train
test_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv', delimiter = ',', header = 0, usecols = [x for x in range(0, 784)] )<data_type_conversions>
train_target = train_target.drop("sig_id", axis=1)
 train_target
X_train = X_train.reshape(60000, 784)
 X_test = X_test.reshape(10000, 784)
 X_train = X_train.astype('float32')
 X_test = X_test.astype('float32')
 X_train /= 255
X_test /= 255
print("Training matrix shape", X_train.shape)
 print("Testing matrix shape", X_test.shape )<categorify>
train = train.drop("sig_id", axis=1)
 test = test.drop("sig_id", axis=1 )
no_classes = 10
Y_train = np_utils.to_categorical(y_train, no_classes)
 Y_test = np_utils.to_categorical(y_test, no_classes )<choose_model_class>
def create_shallow_model(out_shape=206):
inp = tf.keras.Input(shape=(train.shape[1],))
x = tfa.layers.WeightNormalization(L.Dense(train.shape[1]))(inp)
 x = L.BatchNormalization()(x)
 x = L.LeakyReLU(alpha=0.005 )(x)
 x = L.Dropout(0.2 )(x)
 x = tfa.layers.WeightNormalization(L.Dense(128))(x)
 x = L.BatchNormalization()(x)
 x = L.LeakyReLU(alpha=0.005 )(x)
 x = L.Dropout(0.2 )(x)
 output = tfa.layers.WeightNormalization(L.Dense(out_shape, activation="sigmoid"))(x)
 model = tf.keras.Model(inputs=inp, outputs=output)
 sgd = tf.keras.optimizers.SGD()
adamw = tfa.optimizers.AdamW(weight_decay = 0.0001)
 adam = tf.keras.optimizers.Adam()
radam = tfa.optimizers.RectifiedAdam()
lookahead_radam = tfa.optimizers.Lookahead(radam)
 lookahead_adamw = tfa.optimizers.Lookahead(adamw)
 model.compile(loss=tf.keras.losses.BinaryCrossentropy(label_smoothing=1e-6), optimizer=adamw)
 return model
def create_mid_model(out_shape=206):
inp = tf.keras.Input(shape=(train.shape[1],))
x = tfa.layers.WeightNormalization(L.Dense(train.shape[1]))(inp)
 x = L.BatchNormalization()(x)
 x = L.LeakyReLU(alpha=0.001 )(x)
 x = L.Dropout(0.3 )(x)
 x = tfa.layers.WeightNormalization(L.Dense(512))(x)
 x = L.BatchNormalization()(x)
 x = L.LeakyReLU(alpha=0.001 )(x)
 x = L.Dropout(0.3 )(x)
 x = tfa.layers.WeightNormalization(L.Dense(512))(x)
 x = L.BatchNormalization()(x)
 x = L.LeakyReLU(alpha=0.001 )(x)
 x = L.Dropout(0.2 )(x)
 output = tfa.layers.WeightNormalization(L.Dense(out_shape, activation="sigmoid"))(x)
 model = tf.keras.Model(inputs=inp, outputs=output)
 sgd = tf.keras.optimizers.SGD(momentum=0.9)
 adamw = tfa.optimizers.AdamW(weight_decay = 0.001)
 adam = tf.keras.optimizers.Adam()
radam = tfa.optimizers.RectifiedAdam()
lookahead_radam = tfa.optimizers.Lookahead(radam)
 lookahead_adamw = tfa.optimizers.Lookahead(adamw)
 model.compile(loss=tf.keras.losses.BinaryCrossentropy(label_smoothing=1e-6), optimizer=radam)
 return model
def create_deep_model(out_shape=206):
inp = tf.keras.Input(shape=(train.shape[1],))
x = tfa.layers.WeightNormalization(L.Dense(train.shape[1]))(inp)
 x = L.BatchNormalization()(x)
 x = L.Activation("relu" )(x)
 x = L.Dropout(0.3 )(x)
 x = tfa.layers.WeightNormalization(L.Dense(512))(x)
 x = L.BatchNormalization()(x)
 x = L.Activation("relu" )(x)
 x = L.Dropout(0.42 )(x)
 x = tfa.layers.WeightNormalization(L.Dense(512))(x)
 x = L.BatchNormalization()(x)
 x = L.Activation("relu" )(x)
 x = L.Dropout(0.3 )(x)
 x = tfa.layers.WeightNormalization(L.Dense(256))(x)
 x = L.BatchNormalization()(x)
 x = L.Activation("relu" )(x)
 x = L.Dropout(0.3 )(x)
 x = tfa.layers.WeightNormalization(L.Dense(256))(x)
 x = L.BatchNormalization()(x)
 x = L.Activation("relu" )(x)
 x = L.Dropout(0.2 )(x)
 output = tfa.layers.WeightNormalization(L.Dense(out_shape, activation="sigmoid"))(x)
 model = tf.keras.Model(inputs=inp, outputs=output)
 sgd = tf.keras.optimizers.SGD()
adamw = tfa.optimizers.AdamW(weight_decay = 0.0001)
 adam = tf.keras.optimizers.Adam()
radam = tfa.optimizers.RectifiedAdam()
lookahead_radam = tfa.optimizers.Lookahead(radam)
 lookahead_adamw = tfa.optimizers.Lookahead(adamw)
 model.compile(loss=tf.keras.losses.BinaryCrossentropy(label_smoothing=1e-6), optimizer=adamw)
 return model
model = Sequential()<choose_model_class>
predictions = []
kf = MultilabelStratifiedKFold(n_splits=5)
 tf.random.set_seed(42)
 for fold_id,(train_idx, valid_idx)in enumerate(kf.split(train, train_target)) :
model2 = create_mid_model()
history2 = model2.fit(train.iloc[train_idx], train_target.iloc[train_idx], batch_size=32,
validation_data=(train.iloc[valid_idx], train_target.iloc[valid_idx]),
epochs=200,
verbose=2,
callbacks=[
tf.keras.callbacks.ReduceLROnPlateau() ,
tf.keras.callbacks.EarlyStopping(patience=10, restore_best_weights=True),
tf.keras.callbacks.ModelCheckpoint("model2_fold" + str(fold_id)+ ".h5", save_best_only=True, save_weights_only=True)
 ])
 print("Model 2, Fold ID: {}, train loss: {}, valid loss: {}".format(fold_id, min(history2.history["loss"]), min(history2.history["val_loss"])))
 model2 = create_mid_model()
model2.load_weights("model2_fold" + str(fold_id)+ ".h5")
 pred2 = model2.predict(test)
 predictions.append(pred2 )
model.add(Dense(512, input_shape=(784,)) )<choose_model_class>
pred = np.mean(predictions, axis=0)
 pred.shape
model.add(Activation('relu'))<choose_model_class>
for i in range(len(pred)) :
for j in range(len(pred[0])) :
if pred[i][j] < 0.5:
pred[i][j] = 0.001
else:
pred[i][j] = 0.999
pred
model.add(Dropout(0.2))<choose_model_class>
sub = pd.read_csv(".. /input/lish-moa/sample_submission.csv")
 sub.loc[:, 1:] = pred
sub.loc[ctlVehicle_test, sub.columns != "sig_id"] = 0
sub
<choose_model_class><EOS> 
sub.to_csv("submission.csv", index=False )