Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<compute_test_metric>
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 X_train =(train.iloc[:,1:].values ).astype('float32')
 y_train = train.iloc[:,0].values.astype('int32')
 X_test = test.values.astype('float32')
 X_train_scaled = X_train.reshape(X_train.shape[0], 28, 28,1)/ 255
X_test_scaled = X_test.reshape(X_test.shape[0], 28, 28,1)/ 255
y_train = to_categorical(y_train)
 X_train_mean = train.iloc[:, 1:].sum().sum() /(train.shape[0] * train.shape[1])
 X_test_mean = test.iloc[:, 1:].sum().sum() /(test.shape[0] * test.shape[1])
 X_train_normalized =(( X_train - X_train_mean)/ X_train_mean ).reshape(train.shape[0], 28, 28,1)
 X_test_normalized =(( X_test - X_test_mean)/ X_test_mean ).reshape(test.shape[0], 28, 28,1 )
def metrics(y_true, y_pred):
print("
F1-score: ", round(f1_score(y_true, y_pred), 2))
print("Precision: ", round(precision_score(y_true, y_pred), 2))
print("Recall: ", round(recall_score(y_true, y_pred), 2))<train_model>
def format_predictions(model, test_data=X_test_scaled):

 preds = model.predict(test_data)
 preds_test = []
for i in preds:
preds_test.append(np.argmax(i))
return preds_test
early_stop = EarlyStopping(monitor='val_loss',
patience=20,
mode='min',
restore_best_weights=True)
 def scheduler(epoch, lr):
if epoch < 20:
return lr
return lr *(0.9 **(( epoch - 19)// 10))
rate_scheduler = LearningRateScheduler(scheduler )
start_time = time.time()
train_history = model.fit(X_train, y_train, epochs = 3, batch_size = 8)
 end_time = time.time()
print("
=>Training time :", round(end_time - start_time, 1), 's' )<predict_on_test>
def conv_block1(In, std):
out = SeparableConv2D(16,(3, 3), kernel_initializer=TruncatedNormal(0, std, 1))(In)
 out = BatchNormalization()(out)
 out = SeparableConv2D(16,(1, 1), kernel_initializer=TruncatedNormal(0, std-1e-5, 1))(out)
 return out
def conv_block2(In, std):
out = SeparableConv2D(16,(3, 1), kernel_initializer=TruncatedNormal(0, std, 1), padding='same' )(In)
 out = SeparableConv2D(16,(1, 3), kernel_initializer=TruncatedNormal(0, std+1e-4, 1), padding='same' )(out)
 out = MaxPooling2D(2 )(out)
 return out
def conv_block3(In, std):
out = SeparableConv2D(16,(5, 3), kernel_initializer=TruncatedNormal(0, std, 1), padding='same' )(In)
 out = BatchNormalization()(out)
 out = SeparableConv2D(16,(3, 5), kernel_initializer=TruncatedNormal(0, std+1e-4, 1))(out)
 out = MaxPooling2D(2 )(out)
 return out
start_time = time.time()
test_pred = model.predict(X_test, verbose=1 ).round().astype(int)
 end_time = time.time()
print('
=>Average Inference Time :', round(( end_time - start_time)/ len(test_pred)* 1000, 1), 'ms')
 metrics(y_test, test_pred )<save_to_csv>
scaled_input = tf.keras.Input(shape=(28,28,1))
norm_in = BatchNormalization(name='norm_in' )(scaled_input)
 out1 = []
for i in range(0, 8):
out1.append(conv_block1(norm_in, 1e3/10**(i)))
 out2 = []
for i in range(0, 8, 2):
out2.append(Add()([out1[i], out1[i+1]]))
for i in range(0, len(out2)) :
out2[i] = conv_block2(out2[i], 1e4/10**(i))
out2[i] = conv_block3(out2[i], 10/10**(i // 2))
flats = Concatenate()([Flatten()(out)for out in out2])
 d1 = Dense(1024,
kernel_initializer=TruncatedNormal(mean=10, stddev=2, seed=11),
bias_initializer=TruncatedNormal(mean=0, stddev=0.5, seed=11),
kernel_regularizer=l1(0.01)
 )(flats)
 d1 = BatchNormalization()(d1)
 d2 = Dense(1024,
kernel_initializer=TruncatedNormal(mean=1, stddev=0.02, seed=11),
bias_initializer=TruncatedNormal(mean=0, stddev=0.5, seed=11),
kernel_regularizer=l1(0.01)
 )(d1)
 d2 = BatchNormalization()(d2)
 final_prediction = Dense(10, activation='softmax' )(d2)
 model = tf.keras.Model(inputs=[scaled_input], outputs=[final_prediction] )
submission['target'] = model.predict(test_input, verbose=1 ).round().astype(int)
 submission.to_csv('submission.csv', index=False )<install_modules>
model.count_params() / 1e6
!pip install transformers<import_modules>
model.compile(optimizer=Adam(0.0075),
loss=CategoricalCrossentropy() ,
metrics=['accuracy'] )
import torch
from torch.utils.data import TensorDataset, random_split
from torch.utils.data import DataLoader, RandomSampler, SequentialSampler
from transformers import BertTokenizer, get_linear_schedule_with_warmup
from transformers import BertForSequenceClassification, AdamW, BertConfig
import torch.nn.functional as F
import pandas as pd
import numpy as np
import time
import datetime
import random, re
import seaborn as sns
import matplotlib.pyplot as plt<set_options>
def scheduler(epoch, lr):
if epoch < 20:
return lr
elif lr > 5e-5:
return lr *(0.95 **(epoch // 10 - 1))
return lr
hist = model.fit(X_train_scaled, y_train, epochs= 2000, batch_size=128,
callbacks=[
LearningRateScheduler(scheduler),
tfdocs.modeling.EpochDots() ,
ReduceLROnPlateau(
 monitor='val_accuracy', factor=0.6, patience=20,
verbose=1, mode='auto', min_delta=1e-4, cooldown=0, min_lr=1e-15 
)
 ],
verbose=True,
validation_split=0.33,
shuffle=True )
if torch.cuda.is_available() :
device = torch.device("cuda")
 print('There are %d GPU(s)available.{}'.format(torch.cuda.device_count()))
print('We will use the GPU: {}'.format(torch.cuda.get_device_name(0)))
 else:
print('No GPU available, using the CPU instead.')
 device = torch.device("cpu")
 seed_val = 42
random.seed(seed_val)
 np.random.seed(seed_val)
 torch.manual_seed(seed_val)
 torch.cuda.manual_seed_all(seed_val )<load_from_csv>
submission = pd.DataFrame({
"ImageId": [i+1 for i in range(0, 28000)],
"Label": format_predictions(model)
 })
 submission.to_csv('s.csv', index=False )
train = pd.read_csv('.. /input/nlp-getting-started/train.csv')
 test = pd.read_csv('.. /input/nlp-getting-started/test.csv')
 pd.set_option('display.max_colwidth', 150)
 train.head()<train_model>
try :
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 tpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)
 except:
print('No TPU being used!')
print('Number of training sentences: {:,}
'.format(train.shape[0]))
print('Number of test sentences: {:,}
'.format(test.shape[0]))<categorify>
def clean_text(text):
text = text.lower()
text = re.sub(r'[!]+','!',text)
 text = re.sub(r'[?]+','?',text)
 text = re.sub(r'[.]+','.',text)
 text = re.sub(r"'","",text)
 text = re.sub('\s+', '', text ).strip()
text = re.sub(r'&amp;?',r'and', text)
 text = re.sub(r"https?:\/\/t.co\/[A-Za-z0-9]+", "", text)
 text = re.sub(r'[:"$%&\*+,-/:;<=>@\\^_`{|}~]+','',text)
 return text
train.head()<load_pretrained>
sentences = train.text.values
labels = train.target.values
sentences_test = test.text.values
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased', do_lower_case=True )<categorify>
print(' Original: ', sentences[0])
 print('Tokenized: ', tokenizer.tokenize(sentences[0]))
print('Token IDs: ', tokenizer.convert_tokens_to_ids(tokenizer.tokenize(sentences[0])))
<define_variables>
max_len = 0
for sent in sentences:
input_ids = tokenizer.encode(sent, add_special_tokens=True)
 max_len = max(max_len, len(input_ids))
print('Max sentence length: ', max_len)
<categorify>