Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<categorify>
import gc
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras import layers, optimizers
from tensorflow.keras.callbacks import ReduceLROnPlateau
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
def fast_encode(texts, tokenizer, chunk_size=256, maxlen=512):
tokenizer.enable_truncation(max_length=maxlen)
 tokenizer.enable_padding(max_length=maxlen)
 all_ids = []
for i in tqdm(range(0, len(texts), chunk_size)) :
text_chunk = texts[i:i+chunk_size].tolist()
encs = tokenizer.encode_batch(text_chunk)
 all_ids.extend([enc.ids for enc in encs])
 return np.array(all_ids )<categorify>
X = pd.read_csv('.. /input/train.csv')
 X_test = pd.read_csv('.. /input/test.csv')
 Y = X[['label']]
X = X.drop(["label"], axis=1)
 X_train = X.values.reshape(X.shape[0], 28, 28, 1)
 Y_train = tf.keras.utils.to_categorical(Y.values, 10)
 X_test = X_test.values.reshape(X_test.shape[0], 28, 28, 1 )
def regular_encode(texts, tokenizer, maxlen = 512):
enc_di = tokenizer.batch_encode_plus(texts,return_attention_mask = False, return_token_type_ids = False,
pad_to_max_length = True, max_length = maxlen, truncation = True)
 return np.array(enc_di["input_ids"])
<import_modules>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=42)
 X_train = X_train.astype('float32')
 X_test = X_test.astype('float32')
 X_val = X_val.astype('float32')
 X_train /= 255
X_test /= 255
X_val /= 255
import transformers<choose_model_class>
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1)
 datagen.fit(X_train )
def build_model(transformer,maxlen = 512):
input_word_ids = Input(shape=(maxlen,), dtype=tf.int32, name="input_word_ids")
 sequence_output = transformer(input_word_ids)[0]
cls_token = sequence_output[:, 0, :]
out = Dense(1, activation='sigmoid' )(cls_token)
 model = Model(inputs=input_word_ids, outputs=out)
 model.compile(Adam(lr=1e-5), loss='binary_crossentropy', metrics=['accuracy'])
 return model<set_options>
model = tf.keras.Sequential()
model.add(layers.Conv2D(32, kernel_size=(5, 5),
activation='relu',
padding='same',
input_shape=(28, 28, 1)))
 model.add(layers.BatchNormalization())
 model.add(layers.Conv2D(32,(5, 5), activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.Dropout(0.2))
model.add(layers.Conv2D(64,(3, 3), activation='relu', padding='same'))
model.add(layers.BatchNormalization())
 model.add(layers.Conv2D(64,(3, 3), activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.MaxPooling2D(pool_size=(2, 2)))
 model.add(layers.Dropout(0.2))
model.add(layers.Conv2D(128,(3, 3), activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.MaxPooling2D(pool_size=(2, 2), padding='same'))
model.add(layers.Dropout(0.2))
model.add(layers.Flatten())
 model.add(layers.Dense(1024, activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.Dropout(0.3))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.Dropout(0.3))
model.add(layers.Dense(256, activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.Dropout(0.3))
model.add(layers.Dense(10, activation='softmax'))
model.summary()
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync)
<define_variables>
model.compile(loss="categorical_crossentropy",
optimizer=optimizers.Adadelta(lr=1.0, rho=0.95, epsilon=1e-08, decay=0.0),
metrics=['accuracy'])
 reduce_lr = ReduceLROnPlateau(monitor='val_acc',
factor=0.5,
patience=3,
min_lr=0.00001,
verbose=1 )
AUTO = tf.data.experimental.AUTOTUNE
GCS_DS_PATH = KaggleDatasets().get_gcs_path()
EPOCHS = 2
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
MAX_LEN = 152
MODEL = 'jplu/tf-xlm-roberta-large'<load_pretrained>
model.fit(datagen.flow(X_train, Y_train, batch_size=128),
epochs=30,
validation_data=(X_val, Y_val),
verbose=1,
steps_per_epoch=X_train.shape[0] // 128,
callbacks=[reduce_lr] )
<define_variables><EOS> 
Y_test = model.predict(X_test, batch_size=128, verbose=1)
 Y_test = np.argmax(Y_test, axis=1)
 frame = pd.DataFrame({'Label': Y_test})
 frame.insert(0, 'ImageId', range(1, 1 + len(frame)))
 frame.to_csv('output.csv', index=False )