Kaggle competition: Kannada MNIST
words = tokenizer.vocab
w_rank = {}
for i,word in enumerate(words):
w_rank[word] = i
WORDS = w_rank
def words(text): return re.findall(r'\w+', text.lower())
 def P(word):
"Probability of `word`."
return - WORDS.get(word, 0)
 def correction(word):
"Most probable spelling correction for word."
return max(candidates(word), key=P)
 def candidates(word):
"Generate possible spelling corrections for word."
return(known([word])or known(edits1(word)) or [word])
 def known(words):
"The subset of `words` that appear in the dictionary of WORDS."
return set(w for w in words if w in WORDS)
 def edits1(word):
"All edits that are one edit away from `word`."
letters = 'abcdefghijklmnopqrstuvwxyz'
splits = [(word[:i], word[i:])for i in range(len(word)+ 1)]
deletes = [L + R[1:] for L, R in splits if R]
transposes = [L + R[1] + R[0] + R[2:] for L, R in splits if len(R)>1]
replaces = [L + c + R[1:] for L, R in splits if R for c in letters]
inserts = [L + c + R for L, R in splits for c in letters]
return set(deletes + transposes + replaces + inserts )<string_transform>
df_train = pd.read_csv(DATAPATH/'train.csv')
 df_train['fn'] = df_train.index
def search_word(key):
word = key
if word in tokenizer.vocab:
return word
word = ps.stem(key)
 if word in tokenizer.vocab:
return word
word = lc.stem(key)
 if word in tokenizer.vocab:
return word
word = sb.stem(key)
 if word in tokenizer.vocab:
return word
if len(key)> 1:
word = correction(key)
 if word in tokenizer.vocab:
return word
return word<categorify>
class PixelImageItemList(ImageList):
def open(self,fn):
img_pixel = self.inner_df.loc[self.inner_df['fn'] == int(fn[2:])].values[0,1:785]
img_pixel = img_pixel.reshape(28,28)
 return vision.Image(pil2tensor(img_pixel,np.float32 ).div_(255))
def encode_sentence(s, tokenizer):
tokens = tokenizer.tokenize(s)
 tokens = list(map(search_word, tokens))
tokens.append('[SEP]')
 return tokenizer.convert_tokens_to_ids(tokens)
 def bert_encode(glue_dict, tokenizer):
sentence1 = tf.ragged.constant([encode_sentence(s, tokenizer)for s in np.array(glue_dict['keyword'])])
 sentence2 = tf.ragged.constant([encode_sentence(s, tokenizer)for s in np.array(glue_dict['text'])])
 cls = [tokenizer.convert_tokens_to_ids(['[CLS]'])] * sentence1.shape[0]
input_word_ids = tf.concat([cls, sentence1, sentence2], axis=-1)
 input_mask = tf.ones_like(input_word_ids ).to_tensor()
type_cls = tf.ones_like(cls)
 type_s1 = tf.ones_like(sentence1)
 type_s2 = tf.ones_like(sentence2)
 input_type_ids = tf.concat([type_cls, type_s1, type_s2], axis=-1 ).to_tensor()
inputs = {
"input_word_ids": input_word_ids.to_tensor() ,
"input_mask": input_mask,
"input_type_ids": input_type_ids
}
return inputs<categorify>
tfms=get_transforms(do_flip=False)
 with_tta =([],tfms[0] )
glue_train = bert_encode(train, tokenizer)
 glue_train_labels = train['target']
glue_val = bert_encode(val, tokenizer)
 glue_val_labels = val['target']
glue_test = bert_encode(test, tokenizer )<load_from_disk>
bs = 1024
data =(src.transform(tfms=with_tta)
.databunch(num_workers=2,bs=bs)
.normalize() )
bert_config_file = os.path.join(gs_folder_bert, 'bert_config.json')
 config_dict = json.loads(tf.io.gfile.GFile(bert_config_file ).read())
 bert_config = bert.configs.BertConfig.from_dict(config_dict)
 print(config_dict )<categorify>
best_architecture = nn.Sequential(
 conv_layer(1,32,stride=1,ks=3),
conv_layer(32,32,stride=1,ks=3),
conv_layer(32,32,stride=2,ks=5),
nn.Dropout(0.4),
conv_layer(32,64,stride=1,ks=3),
conv_layer(64,64,stride=1,ks=3),
conv_layer(64,64,stride=2,ks=5),
nn.Dropout(0.4),
Flatten() ,
nn.Linear(3136, 128),
relu(inplace=True),
nn.BatchNorm1d(128),
nn.Dropout(0.4),
nn.Linear(128,10),
nn.Softmax(-1)
 )
bert_classifier, bert_encoder = bert.bert_models.classifier_model(bert_config, num_labels=2 )<categorify>
learn = Learner(data, best_architecture, loss_func = nn.CrossEntropyLoss() , metrics=[accuracy] ).mixup()
checkpoint = tf.train.Checkpoint(model=bert_encoder)
 checkpoint.restore(os.path.join(gs_folder_bert, 'bert_model.ckpt')).assert_consumed()<define_variables>
learn.fit_one_cycle(50,1e-2 )
epochs = 10
batch_size = 32
eval_batch_size = 32
train_data_size = len(glue_train_labels)
 steps_per_epoch = int(train_data_size / batch_size)
 num_train_steps = steps_per_epoch * epochs
warmup_steps = int(epochs * train_data_size * 0.1 / batch_size )<choose_model_class>
df_test = pd.read_csv(DATAPATH/'test.csv')
 df_test.rename(columns={'id':'label'}, inplace=True)
 df_test['fn'] = df_test.index
test_set = PixelImageItemList.from_df(df_test,path='.',cols='fn' )
optimizer = nlp.optimization.create_optimizer(2e-5, num_train_steps=num_train_steps, num_warmup_steps=warmup_steps )<define_variables>
learn.data.add_test(test_set )
checkpoint_filepath = '/kaggle/working/checkpoint/best_val'<choose_model_class>
preds, _ = learn.get_preds(DatasetType.Test)
 preds.unsqueeze_(1);preds.shape
metrics = [tf.keras.metrics.SparseCategoricalAccuracy('accuracy', dtype=tf.float32)]
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
 bert_classifier.compile(optimizer=optimizer, loss=loss, metrics=metrics )<train_model>
num_preds = 14
for x in range(num_preds):
new_preds, _ = learn.get_preds(DatasetType.Test)
 new_preds.unsqueeze_(1);preds.shape
preds = torch.cat(( preds,new_preds),1 )
bert_classifier.fit(glue_train, glue_train_labels, validation_data=(glue_val, glue_val_labels), batch_size=32, epochs=epochs, callbacks=[checkpoint_callback] )<load_pretrained>
indv_preds = torch.argmax(preds, dim=2);indv_preds.shape
bert_classifier.load_weights(checkpoint_filepath)
<load_from_csv>
submission = pd.DataFrame({ 'id': np.arange(0,len(winner)) ,'label': winner } )
<compute_test_metric><EOS> 
submission.to_csv(path_or_buf ="submission.csv", index=False )