Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<create_dataframe>
%matplotlib inline
np.random.seed(2)
 sns.set(style='white', context='notebook', palette='deep' )
history_df = pd.DataFrame(history)
 history_df<load_from_csv>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
X_test = pd.read_csv(".. /input/nlp-getting-started/test.csv")["text"]
X_test_tokens = []
for text in X_test:
encoded_dict = tokenizer.encode_plus(text,
add_special_tokens=True,
max_length=sequence_length,
padding="max_length",
return_tensors='pt',
truncation=True)
 X_test_tokens.append(encoded_dict['input_ids'])
 X_test_tokens = torch.cat(X_test_tokens, dim=0)
 test_set = TensorDataset(X_test_tokens)
 test_dataloader = DataLoader(test_set,
sampler=SequentialSampler(test_set),
batch_size=batch_size )<load_from_csv>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
X_test = pd.read_csv(".. /input/nlp-getting-started/test.csv")["text"]
X_test_tokens = []
for text in X_test:
encoded_dict = tokenizer.encode_plus(text,
add_special_tokens=True,
max_length=sequence_length,
padding="max_length",
return_tensors='pt',
truncation=True)
 X_test_tokens.append(encoded_dict['input_ids'])
 X_test_tokens = torch.cat(X_test_tokens, dim=0)
 test_set = TensorDataset(X_test_tokens)
 test_dataloader = DataLoader(test_set,
sampler=SequentialSampler(test_set),
batch_size=batch_size )<categorify>
X_train = X_train / 255.0
test = test / 255.0
all_preds = []
for batch in test_dataloader:
x_batch = batch[0].to(device)
 with torch.no_grad() :
probas = baseline_bert_clf(tokens=x_batch)
 preds = np.round(probas.cpu().detach().numpy() ).astype(int ).flatten()
all_preds.extend(preds )<save_to_csv>
X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1 )
challenge_pred = pd.concat([pd.read_csv(".. /input/nlp-getting-started/sample_submission.csv")["id"], pd.Series(all_preds)], axis=1)
 challenge_pred.columns = ['id', 'target']
challenge_pred.to_csv("submission.csv", index=False )<import_modules>
Y_train = to_categorical(Y_train, num_classes = 10 )
import numpy as np
import pandas as pd
from fastai.text.all import *
import re<load_from_csv>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=2 )
dir_path = "/kaggle/input/nlp-getting-started/"
train_df = pd.read_csv(dir_path + "train.csv")
 test_df = pd.read_csv(dir_path + "test.csv" )<drop_column>
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same', kernel_initializer='he_normal',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same', kernel_initializer='he_normal',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.2))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', kernel_initializer='he_normal',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', kernel_initializer='he_normal',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.2))
model.add(Conv2D(128,(3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))
model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Dense(10, activation = "softmax"))
train_df = train_df.drop(columns=["id", "keyword", "location"] )<count_values>
optimizer = Adam(lr=0.003 )
train_df["target"].value_counts()<feature_engineering>
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )
def remove_URL(text):
url = re.compile(r'https?://\S+|www\.\S+')
 return url.sub(r'',text)
 train_df["text"] = train_df["text"].apply(remove_URL)
 test_df["text"] = test_df["text"].apply(remove_URL )<feature_engineering>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.000001 )
def remove_html(text):
html=re.compile(r'<.*?>')
 return html.sub(r'',text)
 train_df["text"] = train_df["text"].apply(remove_html)
 test_df["text"] = test_df["text"].apply(remove_html )<drop_column>
epochs = 35
batch_size = 64
def remove_emoji(text):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F"
u"\U0001F300-\U0001F5FF"
u"\U0001F680-\U0001F6FF"
u"\U0001F1E0-\U0001F1FF"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE)
 return emoji_pattern.sub(r'', text)
 train_df["text"] = train_df["text"].apply(remove_emoji)
 test_df["text"] = test_df["text"].apply(remove_emoji )<string_transform>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=15,
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_df["text"].apply(lambda x:len(x.split())).plot(kind="hist");<import_modules>
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] )
from transformers import AutoTokenizer, AutoModelForSequenceClassification<load_pretrained>
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
tokenizer = AutoTokenizer.from_pretrained("roberta-large" )<string_transform>
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )