Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<choose_model_class>
WORKERS = 2
CHANNEL = 3
warnings.filterwarnings("ignore")
 SIZE = 128
NUM_CLASSES = 10
%config InlineBackend.figure_format = 'retina'
%matplotlib inline
def create_model_candidate() -> keras.Model:
pretrained_bert_model = get_pretrained_bert_model()
keyword_ids = keras.Input(( 8,), name="keywords")
 keyword_features = Embedding(input_dim=pretrained_bert_model.config.vocab_size, output_dim=16, input_length=8, mask_zero=True )(keyword_ids)
 keyword_features = Flatten()(keyword_features)
 keyword_features = Dense(best_arch_hp.get("keyword_units"))(keyword_features)
 input_ids = Input(shape=(max_length_tweet,), dtype="int32", name="input_ids")
 attention_mask = Input(shape=(max_length_tweet,), dtype="int32", name="attention_mask")
 bert_outputs = pretrained_bert_model(input_ids, attention_mask)
 bert_token_embeddings = bert_outputs.last_hidden_state[:, 1:, :]
tweet_features = GRU(best_arch_hp.get("GRU_units"), return_sequences=True )(bert_token_embeddings)
 tweet_features = Dropout(best_arch_hp.get("GRU_dropout"))(tweet_features)
 tweet_features = GlobalMaxPooling1D()(tweet_features)
 for i in range(best_arch_hp.get("num_layers")) :
tweet_features = Dense(best_arch_hp.get("layer_" + str(i)+ "_units"), activation="relu" )(tweet_features)
 tweet_features = Dropout(best_arch_hp.get("layer_" + str(i)+ "_dropout"))(tweet_features)
 combined_features = concatenate([keyword_features, tweet_features])
 combined_prediction = Dense(1, activation="sigmoid" )(combined_features)
 model = keras.Model(inputs = [keyword_ids, input_ids, attention_mask], outputs=combined_prediction)
 model.compile(
 optimizer=keras.optimizers.Adam(learning_rate=5e-5),
loss="binary_crossentropy",
metrics=keras.metrics.BinaryAccuracy(name="accuracy"), 
)
 return model 
<train_model>
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv' )
model = create_model_candidate()
history = model.fit(
 train_multi_input_dataset.batch(32),
validation_data=val_multi_input_dataset.batch(32),
epochs=6,
class_weight=class_weights,
callbacks=[
keras.callbacks.EarlyStopping(
 monitor="val_accuracy", restore_best_weights=True 
)
 ], 
)
 best_epoch = len(history.history["val_accuracy"])- 1
print_metrics(
 model, train_inputs_encoded, y_train, validation_inputs_encoded, y_val
)<categorify>
x = x / 255.0
test = test / 255.0
test_tweets_encoded = tokenize_encode(test_df["text"].to_list() , max_length_tweet)
 test_inputs_encoded = dict(test_tweets_encoded)
 test_dataset = tf.data.Dataset.from_tensor_slices(test_inputs_encoded)
 test_keywords_encoded = tokenize_encode(test_df["keyword"].to_list() , max_length_keyword)
 test_inputs_encoded["keywords"] = test_keywords_encoded["input_ids"]
test_multi_input_dataset = tf.data.Dataset.from_tensor_slices(test_inputs_encoded )<train_model>
y = to_categorical(y, num_classes = 10 )
full_train_dataset = train_multi_input_dataset.concatenate(val_multi_input_dataset)
 model = create_model_candidate()
model.fit(
 full_train_dataset.batch(32),
epochs=best_epoch,
class_weight=class_weights,
)<save_to_csv>
x_train, x_valid, y_train, y_valid = train_test_split(x, y, test_size = 0.1, random_state=2, stratify = y, shuffle = True )
preds = np.squeeze(model.predict(test_multi_input_dataset.batch(32)))
 preds =(preds >= 0.5 ).astype(int)
 pd.DataFrame({"id": test_df.id, "target": preds} ).to_csv("submission.csv", index=False )<import_modules>
BatchNormalization, Input, Conv2D, GlobalAveragePooling2D)
import numpy as np
import pandas as pd
import os 
<import_modules>
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
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(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
import re
import seaborn as sns
import matplotlib.pyplot as plt
from collections import defaultdict, Counter
from sklearn.feature_extraction.text import CountVectorizer
import nltk
from nltk.corpus import stopwords
from wordcloud import WordCloud
from nltk.tokenize import word_tokenize<set_options>
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0 )
nltk.download('stopwords', quiet=True)
 stopwords = stopwords.words('english')
 sns.set(style="white", font_scale=1.2)
 plt.rcParams["figure.figsize"] = [10,8]
pd.set_option.display_max_columns = 0
pd.set_option.display_max_rows = 0<load_from_csv>
EarlyStopping, ReduceLROnPlateau)
 epochs = 80; batch_size = 1024
checkpoint = ModelCheckpoint('.. /working/Resnet50-visible.h5', monitor='val_loss', verbose=1,
save_best_only=True, mode='min', save_weights_only = True)
 reduceLROnPlat = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=4,
verbose=1, mode='min', epsilon=0.0001)
 early = EarlyStopping(monitor="val_loss",
mode="min",
patience=9)
 callbacks_list = [checkpoint, reduceLROnPlat, early]
train = pd.read_csv(".. /input/nlp-getting-started/train.csv")
 test = pd.read_csv(".. /input/nlp-getting-started/test.csv" )<feature_engineering>
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )
null_counts = pd.DataFrame({"Num_Null": train.isnull().sum() })
 null_counts["Pct_Null"] = null_counts["Num_Null"] / train.count() * 100
null_counts<count_values>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(x_train )
len(train["keyword"].value_counts() )<count_values>
batch_size = 1024
epochs = 80
history = model.fit_generator(datagen.flow(x_train,y_train, batch_size=batch_size),
epochs = epochs, validation_data =(x_valid,y_valid),
verbose = 1, steps_per_epoch=x_train.shape[0] // batch_size
, callbacks=callbacks_list )
disaster_keywords = train.loc[train["target"] == 1]["keyword"].value_counts()
nondisaster_keywords = train.loc[train["target"] == 0]["keyword"].value_counts() 
<feature_engineering>
model.load_weights('.. /working/Resnet50-visible.h5')
 results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
<sort_values><EOS> 
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("submission.csv",index=False )