Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<save_to_csv>
MaxPool2D, ReLU)
 %matplotlib inline
sub = df_sample.copy()
sub["target"] = y_pred
sub.to_csv("submission_optuna_lgbm_ohe_or_v1.csv", index=False)
 sub.head()<import_modules>
print("Loading...")
 data_train = pd.read_csv(".. /input/train.csv")
 data_test = pd.read_csv(".. /input/test.csv")
 print("Done!" )
import os
import numpy as np
import pandas as pd<load_from_csv>
print("Training data: {} rows, {} columns.".format(data_train.shape[0], data_train.shape[1]))
print("Test data: {} rows, {} columns.".format(data_test.shape[0], data_test.shape[1]))
pred1 = pd.read_csv(".. /input/tps-feb-submission-ensemble/submission_pseudo_lgb.csv")
 pred2 = pd.read_csv(".. /input/tps-feb-submission-ensemble/submission_pseudo_lgb_4.csv")
 pred3 = pd.read_csv(".. /input/tps-feb-submission-ensemble/submission_pseudo_lgb_5.csv")
 pred4 = pd.read_csv(".. /input/tps-feb-submission-ensemble/pseudo_lgb_1.csv" )<compute_test_metric>
x_train = data_train.values[:, 1:]
y_train = data_train.values[:, 0]
pred =(pred1.target + pred2.target + pred3.target + pred4.target)/ 4
pred<load_from_csv>
def convert_2d(x):

 if len(x.shape)== 1:
m = 1
height = width = int(np.sqrt(x.shape[0]))
else:
m = x.shape[0]
height = width = int(np.sqrt(x.shape[1]))
x_2d = np.reshape(x,(m, height, width, 1))
return x_2d
submission = pd.read_csv(".. /input/tabular-playground-series-feb-2021/sample_submission.csv")
 submission.target = pred
submission<save_to_csv>
def translate(x, y, dist):

 images = convert_2d(x)
 m, height, width, channel = images.shape
anchors = []
anchors.append(( 0, height, int(dist * width), width, 0, height, 0, width - int(dist * width)))
 anchors.append(( 0, height, 0, width - int(dist * width), 0, height, int(dist * width), width))
anchors.append(( int(dist * height), height, 0, width, 0, height - int(dist * height), 0, width))
anchors.append(( 0, height - int(dist * height), 0, width, int(dist * height), height, 0, width))
new_images = np.zeros(( 4, m, height, width, channel))
for i in range(4):
top, bottom, left, right, new_top, new_bottom, new_left, new_right = anchors[i]
new_images[i, :, new_top:new_bottom, new_left:new_right, :] = images[:, top:bottom, left:right, :]
new_images = np.reshape(new_images,(4 * m, -1))
y = np.tile(y,(4, 1)).reshape(( -1, 1))
new_images = np.concatenate(( y, new_images), axis=1 ).astype(int)
 return new_images
submission.to_csv("ensemble.csv", index=False )<install_modules>
def add_noise(x, y, noise_lvl):

 m, n = x.shape
noise_num = int(noise_lvl * n)
 for i in range(m):
noise_idx = np.random.randint(0, n, n ).argsort() [:noise_num]
x[i, noise_idx] = np.random.randint(0, 255, noise_num)
 noisy_data = np.concatenate(( y.reshape(( -1, 1)) , x), axis=1 ).astype("int")
 return noisy_data
!pip install -q transformers ekphrasis keras-tuner<import_modules>
start = time.clock()
print("Augment the data...")
 cropped_imgs = crop_image(x_train, y_train, 0.9)
 translated_imgs = translate(x_train, y_train, 0.1)
 noisy_imgs = add_noise(x_train, y_train, 0.1)
 rotated_imgs = rotate_image(x_train, y_train, 10)
 data_train = np.vstack(( data_train, cropped_imgs, translated_imgs, noisy_imgs, rotated_imgs))
np.random.shuffle(data_train)
 print("Done!")
 time_used = int(time.clock() - start)
 print("Time used: {}s.".format(time_used))
Input,
Dense,
Embedding,
Flatten,
Dropout,
GlobalMaxPooling1D,
GRU,
concatenate, 
)
 DistilBertTokenizerFast,
TFDistilBertModel,
DistilBertConfig, 
)
<compute_train_metric>
x_train = data_train[:, 1:]
y_train = data_train[:, 0]
x_test = data_test.values
print("Augmented training data: {} rows, {} columns.".format(data_train.shape[0], data_train.shape[1]))
def print_metrics(model, x_train, y_train, x_val, y_val):
train_acc = dict(model.evaluate(x_train, y_train, verbose=0, return_dict=True)) [
"accuracy"
]
val_acc = dict(model.evaluate(x_val, y_val, verbose=0, return_dict=True)) [
"accuracy"
]
val_preds = model.predict(x_val)
 val_preds_bool = val_preds >= 0.5
print("")
 print(f"Training Accuracy: {train_acc:.2%}")
 print(f"Validation Accuracy: {val_acc:.2%}")
 print("")
 print(f"Validation f1 score: {sklearn.metrics.f1_score(val_preds_bool, y_val):.2%}" )<load_pretrained>
x_train = convert_2d(x_train)
 x_test = convert_2d(x_test )
model_class, tokenizer_class, pretrained_weights =(TFDistilBertModel, DistilBertTokenizerFast, 'distilbert-base-uncased')
 pretrained_bert_tokenizer = tokenizer_class.from_pretrained(pretrained_weights)
 def get_pretrained_bert_model(config=pretrained_weights):
if not config:
config = DistilBertConfig(num_labels=2)
 return model_class.from_pretrained(pretrained_weights, config=config)
<load_from_csv>
num_classes = 10
y_train = keras.utils.to_categorical(y_train, num_classes )
train_df = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 test_df = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv" )<count_values>
x_train = x_train / 255
x_test = x_test / 255
print("label counts:")
 train_df.target.value_counts()<count_missing_values>
seed = np.random.randint(1, 100)
 x_train, x_dev, y_train, y_dev = train_test_split(x_train, y_train, test_size=0.1, random_state=seed )
print("train precentage of nulls:")
 print(round(train_df.isnull().sum() / train_df.count() * 100, 2))<count_missing_values>
del data_train
del data_test
gc.collect()
print("test precentage of nulls:")
 print(round(test_df.isnull().sum() / test_df.count() * 100, 2))<categorify>
filters =(32, 32, 64, 64)
 kernel =(5, 5)
 drop_prob = 0.2
model = keras.models.Sequential()
model.add(Conv2D(filters[0], kernel, padding="same", input_shape=(28, 28, 1),
kernel_initializer=keras.initializers.he_normal()))
model.add(BatchNormalization())
 model.add(ReLU())
 model.add(Conv2D(filters[0], kernel, padding="same",
kernel_initializer=keras.initializers.he_normal()))
model.add(BatchNormalization())
 model.add(ReLU())
 model.add(MaxPool2D())
 model.add(Dropout(drop_prob))
model.add(Conv2D(filters[1], kernel, padding="same",
kernel_initializer=keras.initializers.he_normal()))
model.add(BatchNormalization())
 model.add(ReLU())
 model.add(MaxPool2D())
 model.add(Dropout(drop_prob))
model.add(Conv2D(filters[2], kernel, padding="same",
kernel_initializer=keras.initializers.he_normal()))
model.add(BatchNormalization())
 model.add(ReLU())
 model.add(MaxPool2D())
 model.add(Dropout(drop_prob))
model.add(Conv2D(filters[3], kernel, padding="same",
kernel_initializer=keras.initializers.he_normal()))
model.add(BatchNormalization())
 model.add(ReLU())
 model.add(MaxPool2D())
 model.add(Dropout(drop_prob))
model.add(Flatten())
 model.add(Dropout(drop_prob))
model.add(Dense(128, activation="relu"))
model.add(Dropout(drop_prob))
model.add(Dense(num_classes, activation="softmax"))
model.compile(keras.optimizers.Adam() , "categorical_crossentropy", metrics=["accuracy"] )
classes = np.unique(train_df["target"])
 class_weights = sklearn.utils.class_weight.compute_class_weight(
 "balanced", classes=classes, y=train_df["target"] 
)
 class_weights = {clazz : weight for clazz, weight in zip(classes, class_weights)}<count_duplicates>
iters = 100
batch_size = 1024
train_df.drop_duplicates(subset="text", inplace=True, keep=False)
 print("train rows:", len(train_df.index))
print("test rows:", len(test_df.index))<categorify>
lr_decay = ReduceLROnPlateau(monitor="val_acc", factor=0.5, patience=3, verbose=1, min_lr=1e-5 )
class TweetPreProcessor:

 def __init__(self):
self.text_processor = TextPreProcessor(
 normalize=[
"url",
"email",
"phone",
"user",
"time",
"date",
],
annotate={"repeated", "elongated"},
segmenter="twitter",
spell_correction=True,
corrector="twitter",
unpack_hashtags=False,
unpack_contractions=False,
spell_correct_elong=True,
fix_bad_unicode=True,
tokenizer=Tokenizer(lowercase=True ).tokenize,
dicts=[emoticons, slangdict], 
)
 def preprocess_tweet(self, tweet):
return " ".join(self.text_processor.pre_process_doc(tweet))
def __call__(self, tweet):
return self.text_processor.pre_process_doc(tweet)
 tweet_preprocessor = TweetPreProcessor()<categorify>
early_stopping = EarlyStopping(monitor="val_acc", patience=7, verbose=1 )
for tweet in train_df[100:120]["text"]:
print("original: ", tweet)
 print("processed: ", tweet_preprocessor.preprocess_tweet(tweet))
print("" )<categorify>
print("Training model...")
 fit_params = {
"batch_size": batch_size,
"epochs": iters,
"verbose": 1,
"callbacks": [lr_decay, early_stopping],
"validation_data":(x_dev, y_dev)
 }
history = model.fit(x_train, y_train, **fit_params)
 print("Done!" )
train_df["text"] = train_df["text"].apply(tweet_preprocessor.preprocess_tweet)
 test_df["text"] = test_df["text"].apply(tweet_preprocessor.preprocess_tweet )<feature_engineering>
loss, acc = model.evaluate(x_dev, y_dev)
 print("Validation loss: {:.4f}".format(loss))
print("Validation accuracy: {:.4f}".format(acc))
<split><EOS> 
y_pred = model.predict(x_test, batch_size=batch_size)
 y_pred = np.argmax(y_pred, axis=1 ).reshape(( -1, 1))
idx = np.reshape(np.arange(1, len(y_pred)+ 1),(len(y_pred), -1))
y_pred = np.hstack(( idx, y_pred))
y_pred = pd.DataFrame(y_pred, columns=['ImageId', 'Label'])
 y_pred.to_csv('y_pred.csv', index=False )