Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<feature_engineering>
from logging import DEBUG
from logging import Formatter
from logging import getLogger
from logging import StreamHandler
from matplotlib import pyplot
from numpy import arange
from numpy import argmax
from numpy import asarray
from pandas import DataFrame
from pandas import read_csv
from tensorflow import random
from tensorflow.keras import initializers
from tensorflow.keras import layers
from tensorflow.keras import models
from tensorflow.keras import optimizers
from tensorflow.keras import preprocessing
from tensorflow.keras import utils
print("Text to seq process...")
 raw_text = np.hstack([train.item_description.str.lower() , train.name.str.lower() ])
 print(" Fitting tokenizer...")
 tok_raw = Tokenizer()
tok_raw.fit_on_texts(raw_text)
 print(" Transforming text to seq...")
 train["seq_item_description"] = tok_raw.texts_to_sequences(train.item_description.str.lower())
 test["seq_item_description"] = tok_raw.texts_to_sequences(test.item_description.str.lower())
 train["seq_name"] = tok_raw.texts_to_sequences(train.name.str.lower())
 test["seq_name"] = tok_raw.texts_to_sequences(test.name.str.lower())
 train.head(3 )<define_variables>
FOLDER_PATH = '.. /input/digit-recognizer/'
FILE_NAME_TRAIN = 'train.csv'
FILE_NAME_TEST = 'test.csv'
FILE_NAME_SUBMISSION = 'submission.csv'
MAX_NAME_SEQ = 10
MAX_ITEM_DESC_SEQ = 75
MAX_TEXT = np.max([np.max(train.seq_name.max())
 , np.max(test.seq_name.max())
 , np.max(train.seq_item_description.max())
 , np.max(test.seq_item_description.max())])+2
MAX_CATEGORY = np.max([train.category_name.max() , test.category_name.max() ])+1
MAX_BRAND = np.max([train.brand_name.max() , test.brand_name.max() ])+1
MAX_CONDITION = np.max([train.item_condition_id.max() , test.item_condition_id.max() ])+1<split>
def get_logger() :
fmt = '%(asctime)s'
fmt += ' - %(levelname)s'
fmt += ' - %(name)s'
fmt += ' - %(lineno)d'
fmt += ' - %(funcName)s'
fmt += ' - %(message)s'
handler = StreamHandler()
handler.setLevel(DEBUG)
 handler.setFormatter(Formatter(fmt))
logger = getLogger(__name__)
 logger.setLevel(DEBUG)
 logger.addHandler(handler)
 logger.propagate = False
return logger
def kill_logger(logger):
for h in logger.handlers:
logger.removeHandler(h)
dtrain, dvalid = train_test_split(train, random_state=123, train_size=0.99)
 print(dtrain.shape)
 print(dvalid.shape )<categorify>
def initialize() :
random.set_seed(0)
def get_keras_data(dataset):
X = {
'name': pad_sequences(dataset.seq_name, maxlen=MAX_NAME_SEQ)
 ,'item_desc': pad_sequences(dataset.seq_item_description, maxlen=MAX_ITEM_DESC_SEQ)
 ,'brand_name': np.array(dataset.brand_name)
 ,'category_name': np.array(dataset.category_name)
 ,'item_condition': np.array(dataset.item_condition_id)
 ,'num_vars': np.array(dataset[["shipping"]])
 }
return X
X_train = get_keras_data(dtrain)
 X_valid = get_keras_data(dvalid)
 X_test = get_keras_data(test )<compute_train_metric>
def get_model() :
layer_in, layer_out = get_layers()
model = models.Model(
 name='Model',
inputs=layer_in,
outputs=layer_out 
)
 model.compile(
 loss='categorical_crossentropy',
optimizer=optimizers.Nadam(learning_rate=0.01),
metrics=['accuracy'] 
)
 model.summary()
print()
return model
def get_layers() :
layer_in = get_input_layer('001', 28, 28, 1)
 layer_out = layer_in
layer_out = get_conv_layer(layer_out, '002', 3, 3, 32)
 layer_out = get_conv_layer(layer_out, '003', 3, 3, 32)
 layer_out = get_pooling_layer(layer_out, '004', 2, 2)
 layer_out = get_dropout_layer(layer_out, '005', 0.2)
 layer_out = get_batchnormalization_layer(layer_out, '006')
 layer_out = get_conv_layer(layer_out, '007', 3, 3, 32)
 layer_out = get_conv_layer(layer_out, '008', 3, 3, 32)
 layer_out = get_pooling_layer(layer_out, '009', 2, 2)
 layer_out = get_dropout_layer(layer_out, '010', 0.2)
 layer_out = get_batchnormalization_layer(layer_out, '011')
 layer_out = get_flatten_layer(layer_out, '012')
 layer_out = get_full_connect_layer(layer_out, '013', 400)
 layer_out = get_dropout_layer(layer_out, '014', 0.5)
 layer_out = get_batchnormalization_layer(layer_out, '015')
 layer_out = get_output_layer(layer_out, '016', 10)
 return layer_in, layer_out
def get_callbacks(filepath, patience=2):
es = EarlyStopping('val_loss', patience=patience, mode="min")
 msave = ModelCheckpoint(filepath, save_best_only=True)
 return [es, msave]
def rmsle_cust(y_true, y_pred):
first_log = K.log(K.clip(y_pred, K.epsilon() , None)+ 1.)
second_log = K.log(K.clip(y_true, K.epsilon() , None)+ 1.)
return K.sqrt(K.mean(K.square(first_log - second_log), axis=-1))
def get_model() :
dr_r = 0.1
name = Input(shape=[X_train["name"].shape[1]], name="name")
 item_desc = Input(shape=[X_train["item_desc"].shape[1]], name="item_desc")
 brand_name = Input(shape=[1], name="brand_name")
 category_name = Input(shape=[1], name="category_name")
 item_condition = Input(shape=[1], name="item_condition")
 num_vars = Input(shape=[X_train["num_vars"].shape[1]], name="num_vars")
 emb_name = Embedding(MAX_TEXT, 50 )(name)
 emb_item_desc = Embedding(MAX_TEXT, 50 )(item_desc)
 emb_brand_name = Embedding(MAX_BRAND, 10 )(brand_name)
 emb_category_name = Embedding(MAX_CATEGORY, 10 )(category_name)
 emb_item_condition = Embedding(MAX_CONDITION, 5 )(item_condition)
 rnn_layer1 = GRU(16 )(emb_item_desc)
 rnn_layer2 = GRU(8 )(emb_name)
 main_l = concatenate([
Flatten()(emb_brand_name)
 , Flatten()(emb_category_name)
 , Flatten()(emb_item_condition)
 , rnn_layer1
, rnn_layer2
, num_vars
])
 main_l = Dropout(dr_r )(Dense(128 )(main_l))
main_l = Dropout(dr_r )(Dense(64 )(main_l))
output = Dense(1, activation="linear" )(main_l)
 model = Model([name, item_desc, brand_name
, category_name, item_condition, num_vars], output)
 model.compile(loss="mse", optimizer="adam", metrics=["mae", rmsle_cust])
 return model
model = get_model()
model.summary()<train_model>
def get_input_layer(name, width, height, channel):
return layers.Input(
 name=name,
shape=(width, height, channel)
)
BATCH_SIZE = 20000
epochs = 5
model = get_model()
model.fit(X_train, dtrain.target, epochs=epochs, batch_size=BATCH_SIZE
, validation_data=(X_valid, dvalid.target)
 , verbose=1 )<compute_test_metric>
def get_conv_layer(layer, name, width, height, filters):
stddev =(1 / width / height / layer.shape[-1])** 0.5
initializer = initializers.TruncatedNormal(stddev=stddev)
 return layers.Conv2D(
 name=name,
filters=filters,
kernel_size=(width, height),
kernel_initializer=initializer,
activation='relu'
)(layer)
val_preds = model.predict(X_valid)
 val_preds = np.exp(val_preds)+1
y_true = np.array(dvalid.price.values)
 y_pred = val_preds[:,0]
v_rmsle = rmsle(y_true, y_pred)
 print(" RMSLE error on dev test: "+str(v_rmsle))<predict_on_test>
def get_pooling_layer(layer, name, width, height):
return layers.MaxPooling2D(
 name=name,
pool_size=(width, height)
 )(layer)
preds = model.predict(X_test, batch_size=BATCH_SIZE)
 preds = np.exp(preds)-1
submission = test[["test_id"]]
submission["price"] = preds<save_to_csv>
def get_dropout_layer(layer, name, rate):
return layers.Dropout(
 name=name,
rate=rate
)(layer)
submission.to_csv("./submission.csv", index=False)
 submission.price.hist()<load_from_csv>
def get_batchnormalization_layer(layer, name):
return layers.BatchNormalization(
 name=name
)(layer)
!apt-get install p7zip
!p7zip -d -f -k /kaggle/input/mercari-price-suggestion-challenge/train.tsv.7z
!p7zip -d -f -k /kaggle/input/mercari-price-suggestion-challenge/test.tsv.7z
!p7zip -d -f -k /kaggle/input/mercari-price-suggestion-challenge/sample_submission.csv.7z
!unzip /kaggle/input/mercari-price-suggestion-challenge/sample_submission_stg2.csv.zip
!unzip /kaggle/input/mercari-price-suggestion-challenge/test_stg2.tsv.zip 
<load_from_csv>
def get_flatten_layer(layer, name):
return layers.Flatten(
 name=name
)(layer)
train_dataset = pd.read_table("train.tsv")
 test_dataset = pd.read_table("test_stg2.tsv" )<split>
def get_full_connect_layer(layer, name, units):
stddev =(1 / layer.shape[-1])** 0.5
initializer = initializers.TruncatedNormal(stddev=stddev)
 return layers.Dense(
 name=name,
units=units,
kernel_initializer=initializer,
activation='relu'
)(layer)
train_dataset = train_dataset[train_dataset['price'] > 0].reset_index(drop = True)
 train, validation = train_test_split(train_dataset, test_size = 0.1, random_state = 30)
 test = test_dataset.copy()
print(train.shape)
 print(validation.shape)
 print(test.shape )<count_missing_values>
def get_output_layer(layer, name, units):
stddev =(1 / layer.shape[-1])** 0.5
initializer = initializers.TruncatedNormal(stddev=stddev)
 return layers.Dense(
 name=name,
units=units,
kernel_initializer=initializer,
activation='softmax'
)(layer)
train.isnull().sum()<count_missing_values>
def read_csv_x(file_path):
return read_csv(
 file_path,
usecols=lambda col: col != 'label',
dtype=float 
)
 def read_csv_y(file_path):
return read_csv(
 file_path,
usecols=['label'],
dtype=int 
)
train.isnull().sum()<count_missing_values>
def transform_x_train(x_read):
x_base = x_read.values.reshape(-1, 28, 28, 1)/ 255.0
result = list(x_base.copy())
 for t in [10, -10]:
result.extend(
 [
preprocessing.image.apply_affine_transform(
 x,
channel_axis=2,
theta=t,
fill_mode='nearest' 
)for x in x_base
] 
)
 return asarray(result)
 def transform_x_test(x_read):
return x_read.values.reshape(-1, 28, 28, 1)/ 255.0
def transform_y(y_read):
y_base = y_read.values.reshape(-1, 1)
 result = list(y_base.copy())
 for i in range(2):
result.extend(list(y_base))
return utils.to_categorical(asarray(result))
validation.isnull().sum()<count_missing_values>
def predict(model, x):
return argmax(model.predict(x, batch_size=1000), axis=1)
test.isnull().sum()<count_missing_values>
def write_csv(result):
df = DataFrame()
df['ImageId'] = arange(len(result)) + 1
df['Label'] = result
df.to_csv(FILE_NAME_SUBMISSION, index=False)
<categorify><EOS> 
logger = get_logger()
logger.debug('Start')
 initialize()
model = get_model()
x_read_train = read_csv_x(FOLDER_PATH + FILE_NAME_TRAIN)
 y_read_train = read_csv_y(FOLDER_PATH + FILE_NAME_TRAIN)
 x_read_test = read_csv_x(FOLDER_PATH + FILE_NAME_TEST)
 x_train = transform_x_train(x_read_train)
 y_train = transform_y(y_read_train)
 x_test = transform_x_test(x_read_test)
 train(model, x_train, y_train)
 result = predict(model, x_test)
 write_csv(result)
 terminate()
logger.debug('End')
 kill_logger(logger)