Kaggle competition: Digit Recognizer
features = ['unigram_disas','unigram_nondisas','unigram_disas_hash','unigram_nondisas_hash','bigram_disas','bigram_nondisas']
encode_features_tr = all_df[features].iloc[0:31962,:]<train_on_grid>
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import LearningRateScheduler
y_enc = finetune_train["target"]
loss = tf.keras.losses.BinaryCrossentropy(from_logits=False)
 optimizer = keras.optimizers.Adam(lr=1e-3,decay=1e-3/64)
 model.compile(optimizer=optimizer, loss=[loss, loss],metrics=["accuracy"])
 checkpoint = tf.keras.callbacks.ModelCheckpoint('model.h5', monitor='val_accuracy', save_best_only=True)
 earlystopping = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy')
 fine_history = model.fit([encode_ds_tr,encode_features_tr], y_enc, validation_split=0.34,shuffle=True,epochs=2,batch_size=64,verbose=1 )<categorify>
train_file = ".. /input/digit-recognizer/train.csv"
test_file = ".. /input/digit-recognizer/test.csv"
sample_submission = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv' )
all_df = pd.concat([train,test])
 max_len = get_max_len(train["clean"])+ 1
encode_ds_all = encode(all_df["clean"] )<categorify>
raw_data = np.loadtxt(train_file, skiprows=1, dtype='int', delimiter=',')
 x_train, x_val, y_train, y_val = train_test_split(
 raw_data[:,1:], raw_data[:,0], test_size=0.1 )
encode_ds_tr = {'input_ids':encode_ds_all["input_ids"][0:7613,:],
'input_mask':encode_ds_all["input_mask"][0:7613,:],
'input_type_ids':encode_ds_all["input_type_ids"][0:7613,:]}
encode_ds_tr<categorify>
x_train = x_train.astype("float32")/255.
 x_val = x_val.astype("float32")/255 .
encode_features_tr = all_df[features].iloc[0:7613,:]<train_on_grid>
y_train = to_categorical(y_train)
 y_val = to_categorical(y_val)
 print(y_train[0] )
y_enc = train["target"]
loss = tf.keras.losses.BinaryCrossentropy(from_logits=False)
 optimizer = keras.optimizers.Adam(lr=1e-5,decay=1e-5/64)
 model.compile(optimizer=optimizer, loss=[loss, loss],metrics=["accuracy"])
 checkpoint = tf.keras.callbacks.ModelCheckpoint('model.h5', monitor='val_accuracy', save_best_only=True)
 earlystopping = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy')
 fine_history = model.fit([encode_ds_tr,encode_features_tr], y_enc, validation_split=0.34,shuffle=True,epochs=3,batch_size=64,verbose=1 )<predict_on_test>
model = Sequential()
model.add(Conv2D(filters = 16, kernel_size =(3, 3), activation='relu',
input_shape =(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 16, kernel_size =(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 32, kernel_size =(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(512, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
y_pred=model.predict([encode_ds_tr,encode_features_tr])
 y_pred = y_pred.round()
print(classification_report(y_enc,y_pred))<categorify>
datagen = ImageDataGenerator(zoom_range = 0.1,
height_shift_range = 0.1,
width_shift_range = 0.1,
rotation_range = 10 )
encode_ds_ts = {'input_ids':encode_ds_all["input_ids"][7613:,:],
'input_mask':encode_ds_all["input_mask"][7613:,:],
'input_type_ids':encode_ds_all["input_type_ids"][7613:,:]}
encode_ds_ts<define_variables>
model.compile(loss='categorical_crossentropy', optimizer = Adam(lr=1e-4), metrics=["accuracy"] )
encode_features_ts = all_df[features].iloc[7613:,:]<load_from_csv>
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.9 ** x )
y_pred=model.predict([encode_ds_ts,encode_features_ts])
 y_pred= y_pred.round()
submission=pd.read_csv('/kaggle/input/nlp-getting-started/sample_submission.csv')
 submission['id']=test['id']
submission['target']=y_pred
submission['target']=submission['target'].astype(int)
 submission.head(10)
<save_to_csv>
hist = model.fit_generator(datagen.flow(x_train, y_train, batch_size=16),
steps_per_epoch=500,
epochs=20,
verbose=2,
validation_data=(x_val[:400,:], y_val[:400,:]),
callbacks=[annealer] )
submission.to_csv('sample_submission.csv',index=False )<set_options>
final_loss, final_acc = model.evaluate(x_val, y_val, verbose=0)
 print("Final loss: {0:.4f}, final accuracy: {1:.4f}".format(final_loss, final_acc))
pd.set_option('display.max_columns', None)
 pd.set_option('display.max_rows', 20)
 pd.set_option('display.max_colwidth', -1 )<load_from_csv>
y_hat = model.predict(x_val)
 y_pred = np.argmax(y_hat, axis=1)
 y_true = np.argmax(y_val, axis=1)
 cm = confusion_matrix(y_true, y_pred)
 print(cm )
df = pd.read_csv('.. /input/nlp-getting-started/train.csv')
 print(len(df))
print(df.columns)
 df<categorify>
mnist_testset = np.loadtxt(test_file, skiprows=1, dtype='int', delimiter=',')
 x_test = mnist_testset.astype("float32")
 x_test = x_test.reshape(-1, 28, 28, 1)/255 .
def clean_text(text):
text = re.sub(r'http\S+', '', text)
 text = re.sub(r"(?:\@)\w+", '', text)
 text = re.sub(r'[^a-zA-Z0-9'.,?$&\s]', '', text)
 text = text.lower()
return text
for i in range(10):
index = np.random.randint(low=0, high=len(df))
print('Raw text:', df['text'][index])
 print('Cleaned text:', clean_text(df['text'][index]))
print('Label: ', df['target'][index], '
' )<categorify>
y_hat = model.predict(x_test, batch_size=64 )
def convert_to_features(data, tokenizer, max_len=None):
data = data.replace('
', '')
 if max_len is not None:
tokenized = tokenizer.encode_plus(
 data,
padding ='max_length',
max_length=max_len,
truncation=True,
return_tensors='np',
return_attention_mask=True,
return_token_type_ids=True, 
)
 else:
tokenized = tokenizer.encode_plus(
 data,
return_tensors='np',
return_attention_mask=True,
return_token_type_ids=True, 
)
 return tokenized
def create_inputs_with_targets(x, y, tokenizer, max_len=128):
dataset_dict = {
"input_ids": [],
"attention_mask": [],
'labels': []
}
for sentence, label in tqdm(zip(x,y)) :
cleaned_sentence = clean_text(sentence)
 temp = convert_to_features(cleaned_sentence, tokenizer, max_len=max_len)
 dataset_dict["input_ids"].append(temp["input_ids"][0])
 dataset_dict["attention_mask"].append(temp["attention_mask"][0])
 dataset_dict["labels"].append(label)
 x = [
np.array(dataset_dict["input_ids"]),
np.array(dataset_dict["attention_mask"]),
]
y = np.array(dataset_dict['labels'])
 return x, y
def create_inputs_without_targets(x, tokenizer, max_len=128):
dataset_dict = {
"input_ids": [],
"attention_mask": [],
}
for sentence in tqdm(x):
cleaned_sentence = clean_text(sentence)
 temp = convert_to_features(cleaned_sentence, tokenizer, max_len=max_len)
 dataset_dict["input_ids"].append(temp["input_ids"][0])
 dataset_dict["attention_mask"].append(temp["attention_mask"][0])
 x = [
np.array(dataset_dict["input_ids"]),
np.array(dataset_dict["attention_mask"]),
]
return x<load_pretrained>
y_pred = np.argmax(y_hat,axis=1 )
base_model = 'bert-base-uncased'
bert_tokenizer = transformers.BertTokenizer.from_pretrained(base_model)
 max_len = 80<prepare_x_and_y>
solution = pd.DataFrame({'ImageId': sample_submission['ImageId'], 'Label': y_pred})
 solution[["ImageId","Label"]].to_csv("CNNPrediction.csv", index=False)
 solution.head()