Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<choose_model_class>
K.set_image_data_format('channels_last')
 print(os.listdir(".. /input"))
def get_model_origin(matrix):
inp = Input(shape=(max_len,))
x = Embedding(vocab_size,300,weights=[matrix],input_length=max_len,trainable=False )(inp)
 x = Bidirectional(LSTM(128, return_sequences=True))(x)
 x = Conv1D(64,3,activation="relu" )(x)
 x = GlobalMaxPool1D()(x)
 x = Dense(128, activation="relu" )(x)
 x = Dropout(0.2 )(x)
 x = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=inp, outputs=x)
 return model<choose_model_class>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 df = train.copy()
df_test = test.copy()
opt=Adam(learning_rate=0.001)
 BATCH_SIZE = 1024
bin_loss=tf.keras.losses.BinaryCrossentropy(
 from_logits=False,
label_smoothing=0,
name='binary_crossentropy' 
)
 early_stopping=tf.keras.callbacks.EarlyStopping(
 monitor="val_loss",
patience=3,
mode="min",
restore_best_weights=True 
)
 reduce_lr=tf.keras.callbacks.ReduceLROnPlateau(
 monitor="val_loss",
factor=0.2,
patience=2,
verbose=1,
mode="auto" 
)
 my_callbacks=[early_stopping,reduce_lr]<set_options>
print("Train: ", df.shape)
 print("Test: ", df_test.shape )
strategy = None
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()
strategy = tf.distribute.TPUStrategy(tpu)
 print('Use TPU')
 except ValueError:
if len(tf.config.list_physical_devices('GPU')) > 0:
strategy = tf.distribute.MirroredStrategy()
print('Use GPU')
 else:
strategy = tf.distribute.get_strategy()
print('Use CPU' )<train_on_grid>
df.isnull().any().sum()
with strategy.scope() :
google_model = get_model_origin(embedding_mat)
 google_model.compile(loss=bin_loss, optimizer=opt, metrics=['accuracy'])
 history=google_model.fit(train_padded, train.target, batch_size=BATCH_SIZE, epochs=30, validation_data=(val_padded, val.target),callbacks=my_callbacks )<find_best_params>
df_test.isnull().any().sum()
google_y_pre=google_model.predict(val_padded, verbose=1)
 best_score = 0
best_thresh = 0
for thresh in np.arange(0.1,0.5,0.01):
if(best_score < metrics.f1_score(val.target,(google_y_pre>thresh ).astype(int))):
best_score = metrics.f1_score(val.target,(google_y_pre>thresh))
best_thresh = round(thresh, 2)
 print("threshold {0:2.2f} f1 score:{1:2.3f}".format(thresh,metrics.f1_score(val.target,(google_y_pre>thresh ).astype(int))))
print("\033[1mBest result {0:2.3f} in thresh_hold {1:2.2f}\033[0m".format(best_score, best_thresh))<drop_column>
x = train.drop(labels = ["label"],axis = 1)
 y = train["label"]
del model_embed, history, best_score, best_thresh, google_model, google_y_pre
gc.collect()<choose_model_class>
xtrain, xtest, ytrain, ytest = train_test_split(x,y, test_size = 0.2, random_state = 123)
 xtrain.shape, xtest.shape, ytrain.shape, ytest.shape
def get_model(matrix):
inp = Input(shape=(max_len,))
x = Embedding(vocab_size, 300, weights=[matrix], trainable=False )(inp)
 x = SpatialDropout1D(0.3 )(x)
 x1 = Bidirectional(LSTM(256, return_sequences=True))(x)
 x2 = Bidirectional(GRU(128, return_sequences=True))(x1)
 max_pool1 = GlobalMaxPooling1D()(x1)
 max_pool2 = GlobalMaxPooling1D()(x2)
 conc = Concatenate()([max_pool1, max_pool2])
 predictions = Dense(1, activation='sigmoid' )(conc)
 model = Model(inputs=inp, outputs=predictions)
 adam = optimizers.Adam(lr=0.001)
 model.compile(optimizer=adam, loss='binary_crossentropy', metrics=['accuracy'])
 return model<set_options>
xtrain = xtrain.astype("float32")/255
xtest = xtest.astype("float32")/255
test = test.astype("float32")/255
ytrain = to_categorical(ytrain, num_classes=10)
 ytest = to_categorical(ytest, num_classes=10 )
strategy = None
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()
strategy = tf.distribute.TPUStrategy(tpu)
 print('Use TPU')
 except ValueError:
if len(tf.config.list_physical_devices('GPU')) > 0:
strategy = tf.distribute.MirroredStrategy()
print('Use GPU')
 else:
strategy = tf.distribute.get_strategy()
print('Use CPU' )<train_model>
model = Sequential()
model.add(Conv2D(filters = 32,
kernel_size =(3,3),padding = 'Same', activation ='relu',
input_shape =(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Dense(10, activation = "softmax"))
with strategy.scope() :
google_model = get_model(embedding_mat)
 google_model.compile(loss=bin_loss, optimizer=Adam(lr=0.001), metrics=['accuracy'])
 history=google_model.fit(train_padded, train.target, batch_size=BATCH_SIZE, epochs=30, validation_data=(val_padded, val.target),callbacks=my_callbacks )<find_best_params>
optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999)
 model.compile(optimizer=optimizer, loss="categorical_crossentropy", metrics=["accuracy"])
 model.summary()
google_y_pre=google_model.predict(val_padded, verbose=1)
 best_score = 0
best_thresh = 0
for thresh in np.arange(0.1,0.5,0.01):
if(best_score < metrics.f1_score(val.target,(google_y_pre>thresh ).astype(int))):
best_score = metrics.f1_score(val.target,(google_y_pre>thresh))
best_thresh = round(thresh, 2)
 print("threshold {0:2.2f} f1 score:{1:2.3f}".format(thresh,metrics.f1_score(val.target,(google_y_pre>thresh ).astype(int))))
print("\033[1mBest result {0:2.3f} in thresh_hold {1:2.2f}\033[0m".format(best_score, best_thresh))<predict_on_test>
batch_size = 128
epochs = 50
reduce_lr = LearningRateScheduler(lambda x: 1e-3 * 0.9 ** x)
 history = model.fit_generator(datagen.flow(xtrain, ytrain, batch_size = batch_size), epochs = epochs,
validation_data =(xtest, ytest), verbose=2,
steps_per_epoch=xtrain.shape[0] // batch_size,
callbacks = [reduce_lr] )
threshold=best_thresh
google_y_test_pre=google_model.predict(test_padded, batch_size=BATCH_SIZE, verbose=1)
 google_y_test_pre=(google_y_test_pre>thresh ).astype(int )<drop_column>
model.evaluate(xtest, ytest )
del embedding_mat, history, best_score, best_thresh
gc.collect()<split>
model.evaluate(xtrain, ytrain )
def load_embed(file):
def get_coefs(word,*arr):
return word, np.asarray(arr, dtype='float32')
 if file == '.. /input/embeddings/wiki-news-300d-1M/wiki-news-300d-1M.vec':
embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(file)if len(o)>100)
 else:
embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(file, encoding='latin'))
return embeddings_index<define_variables>
testprediction=np.argmax(model.predict(test),axis=1)
 testimage=[]
for i in range(len(testprediction)) :
testimage.append(i+1)
 final={'ImageId':testimage,'Label':testprediction}
submission=pd.DataFrame(final)
 submission.to_csv('submssion.csv',index=False )