Kaggle competition: Digit Recognizer
tokenizer = Tokenizer(num_words = vocabulary_size+1,\
filters='!"
0123456789',\
lower=True, split=' ')
 X_train_raw = train_data["comment_text"]
X_test_raw = test_data["comment_text"]
bad_comment_cat = ['toxic', 'severe_toxic', 'obscene', 'threat',\
'insult', 'identity_hate']
Y_train = train_data[bad_comment_cat]<string_transform>
%matplotlib inline
np.random.seed(2)
 sns.set(style='white', context='notebook', palette='deep' )
X_train_raw = X_train_raw.apply(lambda x: preprocess(str(x)))
 X_test_raw = X_test_raw.apply(lambda x: preprocess(str(x)))
 tokenizer.fit_on_texts(X_train_raw)
 tokenizer.fit_on_texts(X_test_raw)
 X_train = pad_sequences(tokenizer.texts_to_sequences(X_train_raw),\
maxlen = max_len, truncating = "pre")
 X_test = pad_sequences(tokenizer.texts_to_sequences(X_test_raw),\
maxlen = max_len, truncating = "pre")
 x_train, x_val, y_train, y_val = train_test_split(X_train,Y_train,train_size=0.9, random_state=199)
 print(x_train.shape," ",y_train.shape," ",x_val.shape," ",y_val.shape )<categorify>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
def get_weights(embedding_vectors,embedding_dim):
global num_tokens,tokenizer
embedding_weights = np.zeros(( num_tokens,embedding_dim))
misses = 0
for word, i in tokenizer.word_index.items() :
vector = embedding_vectors.get(word)
 if i>=num_tokens :
break
elif vector is not None:
embedding_weights[i] = vector
else:
if len(word)<20:
word = Word(word)
 word = word.spellcheck() [0][0]
vector = embedding_vectors.get(str(word))
if vector is not None:
embedding_weights[i] = vector
else:
misses +=1
else:
misses +=1
print(f"The number of missed words is {misses}")
 return embedding_weights<string_transform>
X_train = X_train / 255.0
test = test / 255.0
embedding_vectors_fasttext = {}
with open("/kaggle/input/fasttext-crawl-300d-2m/crawl-300d-2M.vec","r")as file:
file.readline()
for line in file:
word , vector = line.split(maxsplit=1)
 vector = np.fromstring(vector,"float32",sep=" ")
 embedding_vectors_fasttext[word] = vector<load_pretrained>
Y_train = to_categorical(Y_train, num_classes = 10 )
embedding_weights_fasttext = get_weights(embedding_vectors_fasttext,embedding_dim=300 )<feature_engineering>
random_seed = 2
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=random_seed )
embedding_vectors_glove = {}
with open("/kaggle/input/glove6b/glove.6B.300d.txt","r")as file:
for line in file:
word , vector = line.split(maxsplit=1)
 vector = np.fromstring(vector,"float32",sep=" ")
 embedding_vectors_glove[word] = vector<load_pretrained>
model_test = Sequential()
model_test.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model_test.add(MaxPool2D(pool_size=(2,2)))
 model_test.add(Dropout(0.5))
model_test.add(Flatten())
 model_test.add(Dense(256, activation = "relu"))
model_test.add(Dropout(0.5))
model_test.add(Dense(10, activation = "softmax"))
embedding_weights_glove = get_weights(embedding_vectors_glove,embedding_dim=300 )<choose_model_class>
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model_test.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
 epochs = 2
batch_size = 86
def GRU_model_glove() :
global max_len,num_tokens,embedding_weights_glove
inputs = layers.Input(shape=(max_len,))
x = layers.Embedding(input_dim=num_tokens,\
output_dim=embedding_dim,\
embeddings_initializer=keras.initializers.Constant(embedding_weights_glove),\
trainable=True )(inputs)
 x = layers.SpatialDropout1D(0.3 )(x)
 forward_layer = layers.GRU(42,return_sequences=True)
 backward_layer = layers.GRU(42,activation="relu",dropout=0.1,return_sequences=True,go_backwards=True)
 x = layers.Bidirectional(forward_layer,backward_layer=backward_layer )(x)
 x = layers.GlobalMaxPooling1D()(x)
 outputs = layers.Dense(units=6,activation='sigmoid' )(x)
 model = keras.models.Model(inputs=inputs, outputs=outputs, name="GRU_model_glove")
 model.compile(optimizer=tf.optimizers.Adam() ,\
loss=tf.losses.BinaryCrossentropy() ,\
metrics=['AUC'])
 return model
GRU_model_glove = GRU_model_glove()
GRU_model_glove.summary()<train_model>
history = model_test.fit(X_train, Y_train, batch_size = batch_size, epochs = epochs,
validation_data =(X_val, Y_val), verbose = 2 )
history = GRU_model_glove.fit(x_train, y_train, epochs=2,\
batch_size=128, validation_data=(x_val,y_val))<choose_model_class>
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(5,5),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(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
def GRU_model_fasttext() :
global max_len,num_tokens,embedding_weights_fasttext
inputs = layers.Input(shape=(max_len,))
x = layers.Embedding(input_dim=num_tokens,\
output_dim=embedding_dim,\
embeddings_initializer=keras.initializers.Constant(embedding_weights_fasttext),\
trainable=False )(inputs)
 x = layers.SpatialDropout1D(0.3 )(x)
 forward_layer = layers.GRU(64,return_sequences=True)
 backward_layer = layers.GRU(64,activation="relu",dropout=0.3,return_sequences=True,go_backwards=True)
 x = layers.Bidirectional(forward_layer,backward_layer=backward_layer )(x)
 avg_pool = layers.GlobalAveragePooling1D()(x)
 max_pool = layers.GlobalMaxPooling1D()(x)
 x = layers.concatenate([avg_pool,max_pool])
 outputs = layers.Dense(units=6,activation='sigmoid' )(x)
 model = keras.models.Model(inputs=inputs, outputs=outputs, name="GRU_model")
 model.compile(optimizer=tf.optimizers.Adam() ,\
loss=tf.losses.BinaryCrossentropy() ,\
metrics=['AUC'])
 return model
GRU_model_fasttext = GRU_model_fasttext()
GRU_model_fasttext.summary()
history = GRU_model_fasttext.fit(x_train, y_train, \
epochs=2, batch_size=32,\
validation_data=(x_val,y_val))<predict_on_test>
plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True)
 Image("model.png" )
model_nums = 2
size1 = x_train.shape[0]
y_train_pred = np.zeros(( model_nums,size1,6),dtype="float32")
 y_train_pred[0] = GRU_model_fasttext.predict(x_train)
 y_train_pred[1] = GRU_model_glove.predict(x_train)
 size2 = X_test.shape[0]
y_test_pred = np.zeros(( model_nums,size2,6),dtype="float32")
 y_test_pred[0] = GRU_model_fasttext.predict(X_test)
 y_test_pred[1] = GRU_model_glove.predict(X_test)
 y_pred = np.zeros(( size2,6),dtype="float32")
 for i in range(6):
lg = LogisticRegression()
temp = np.zeros(( size1,model_nums),dtype="float32")
 for j in range(model_nums):
temp[:,j] = y_train_pred[j,:,i]
lg.fit(temp,y_train[bad_comment_cat[i]])
 temp = np.zeros(( size2,model_nums),dtype="float32")
 for j in range(model_nums):
temp[:,j] = y_test_pred[j,:,i]
y_pred[:,i] = lg.predict_proba(temp)[:,1]<save_to_csv>
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
 epochs = 30
batch_size = 80
submission_result[bad_comment_cat] = y_pred
submission_result.to_csv("submission.csv",index=False )<import_modules>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )
import numpy as np
import pandas as pd
import datetime
from xgboost import XGBRegressor
from sklearn.model_selection import GridSearchCV, KFold<load_from_csv>
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 )
train = pd.read_csv(r'.. /input/rossmann-store-sales/train.csv', parse_dates=['Date'], low_memory=False)
 train.head()<load_from_csv>
history = model.fit_generator(datagen.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=[learning_rate_reduction] )
test = pd.read_csv(r'.. /input/rossmann-store-sales/test.csv', parse_dates=['Date'], low_memory=False, index_col='Id')
 test.head()<load_from_csv>
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_pre.csv",index=False )