Kaggle competition: Digit Recognizer
train_tensor = tokenizer(list(train_df["text"]), padding="max_length",
truncation=True, max_length=30,
return_tensors="pt")["input_ids"]<categorify>
Train = pd.read_csv(".. /input/train.csv")
 Test = pd.read_csv(".. /input/test.csv" )
class TweetDataset:
def __init__(self, tensors, targ, ids):
self.text = tensors[ids, :]
self.targ = targ[ids].reset_index(drop=True)
 def __len__(self):
return len(self.text)
 def __getitem__(self, idx):
t = self.text[idx]
y = self.targ[idx]
return t, tensor(y )<split>
y_train = Train['label']
X_train = Train.drop(labels='label', axis=1)
 y_train.value_counts()
train_ids, valid_ids = RandomSplitter()(train_df)
 target = train_df["target"]
train_ds = TweetDataset(train_tensor, target, train_ids)
 valid_ds = TweetDataset(train_tensor, target, valid_ids)
 train_dl = DataLoader(train_ds, bs=64)
 valid_dl = DataLoader(valid_ds, bs=512)
 dls = DataLoaders(train_dl, valid_dl ).to("cuda" )<choose_model_class>
X_train = X_train/255.0
Test = Test/255.0
bert = AutoModelForSequenceClassification.from_pretrained("roberta-large", num_labels=2 ).train().to("cuda")
 class BertClassifier(Module):
def __init__(self, bert):
self.bert = bert
def forward(self, x):
return self.bert(x ).logits
model = BertClassifier(bert )<choose_model_class>
y_train = to_categorical(y_train, num_classes = 10 )
learn = Learner(dls, model, metrics=[accuracy, F1Score() ] ).to_fp16()
learn.lr_find()<train_model>
X_train,X_val,Y_train,Y_val =train_test_split(X_train, y_train, test_size=0.1, random_state=0 )
learn.fit_one_cycle(3, lr_max=1e-5 )<find_best_params>
datagen = ImageDataGenerator(rotation_range = 15, width_shift_range = 0.1, height_shift_range = 0.1)
 datagen.fit(X_train)
 np.concatenate(( X_train,X_train),axis=0)
 random.seed(12345)
 for X_batch, Y_batch in datagen.flow(np.concatenate(( X_train,X_train),axis=0), np.concatenate(( Y_train,Y_train),axis=0), batch_size=35700):
break
X_train_aug = X_batch
Y_train_aug = Y_batch
for i in range(0, 9):
pyplot.subplot(330 + 1 + i)
 pyplot.imshow(X_train_aug[i].reshape(28, 28), cmap=pyplot.get_cmap('gray'))
pyplot.show()
preds, targs = learn.get_preds()
min_threshold = None
max_f1 = -float("inf")
 thresholds = np.linspace(0.3, 0.7, 50)
 for threshold in thresholds:
f1 = f1_score(targs, F.softmax(preds, dim=1)[:, 1]>threshold)
 if f1 > max_f1:
min_threshold = threshold
min_f1 = f1
print(f"threshold:{threshold:.4f} - f1:{f1:.4f}" )<string_transform>
X_train_aug = np.concatenate(( X_train,X_train_aug),axis=0)
 Y_train_aug = np.concatenate(( Y_train,Y_train_aug),axis=0)
 print(X_train_aug.shape,Y_train_aug.shape )
test_tensor = tokenizer(list(test_df["text"]),
padding="max_length",
truncation=True,
max_length=30,
return_tensors="pt")["input_ids"]<load_pretrained>
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"))
class TestDS:
def __init__(self, tensors):
self.tensors = tensors
def __len__(self):
return len(self.tensors)
 def __getitem__(self, idx):
t = self.tensors[idx]
return t, tensor(0)
 test_dl = DataLoader(TestDS(test_tensor), bs=128 )<predict_on_test>
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0 )
test_preds = learn.get_preds(dl=test_dl )<save_to_csv>
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
sub = pd.read_csv(dir_path + "sample_submission.csv")
 prediction =(F.softmax(test_preds[0], dim=1)[:, 1]>min_threshold ).int()
sub = pd.read_csv(dir_path + "sample_submission.csv")
 sub["target"] = prediction
sub.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<load_from_csv>
epochs = 10
batch_size = 86
train=pd.read_csv('.. /input/nlp-getting-started/train.csv')
 test=pd.read_csv('.. /input/nlp-getting-started/test.csv' )<load_pretrained>
history = model.fit(X_train_aug,
Y_train_aug,
batch_size = batch_size,
epochs = epochs,
validation_data =(X_val, Y_val),
verbose = 2,
callbacks=[learning_rate_reduction] )
nltk.download('punkt')
 nltk.download('stopwords')
 !pip install contractions
nltk.download('wordnet')
 !pip install pyspellchecker 
<string_transform>
result = model.predict(Test)
 result = np.argmax(result,axis = 1)
 result.shape
stop_words=nltk.corpus.stopwords.words('english')
 i=0
wnl=WordNetLemmatizer()
stemmer=SnowballStemmer('english')
 for doc in train.text:
doc=re.sub(r'https?://\S+|www\.\S+','',doc)
 doc=re.sub(r'<.*?>','',doc)
 doc=re.sub(r'[^a-zA-Z\s]','',doc,re.I|re.A)
 doc=' '.join([wnl.lemmatize(i)for i in doc.lower().split() ])
 doc=contractions.fix(doc)
 tokens=nltk.word_tokenize(doc)
 filtered=[token for token in tokens if token not in stop_words]
doc=' '.join(filtered)
 train.text[i]=doc
i+=1
i=0
for doc in test.text:
doc=re.sub(r'https?://\S+|www\.\S+','',doc)
 doc=re.sub(r'<.*?>','',doc)
 doc=re.sub(r'[^a-zA-Z\s]','',doc,re.I|re.A)
 doc=' '.join([wnl.lemmatize(i)for i in doc.lower().split() ])
 doc=contractions.fix(doc)
 tokens=nltk.word_tokenize(doc)
 filtered=[token for token in tokens if token not in stop_words]
doc=' '.join(filtered)
 test.text[i]=doc
i+=1<install_modules>
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )