<jupyter_start><jupyter_text>Import libraries<jupyter_code>import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = "3"

import logging
logging.getLogger("tensorflow").setLevel(logging.ERROR)
import time
import random
import datetime
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline

from sklearn.model_selection import train_test_split
print("CUDA avilable:",
      True if tf.config.list_physical_devices('GPU') else False)
# 재현성 및 동일 조건을 만들기 위해 seed 설정
SEED = 1234
np.random.seed(SEED)
tf.random.set_seed(SEED)<jupyter_output><empty_output><jupyter_text>Load data<jupyter_code>train = pd.read_csv("./data/train.csv")
test = pd.read_csv("./data/test.csv")
submission = pd.read_csv("./data/submission.csv")<jupyter_output><empty_output><jupyter_text>EDA<jupyter_code>train.set_index("id").head()
train.shape<jupyter_output><empty_output><jupyter_text>- train: digit, letter, 0~783 pixels<jupyter_code>test.set_index("id").head()
test.shape<jupyter_output><empty_output><jupyter_text>- test: letter, 0~783 pixels<jupyter_code>idx = 100
img = train.loc[idx, "0":].values.reshape(28, 28).astype(int)
digit = train.loc[idx, "digit"]
letter = train.loc[idx, "letter"]

plt.title("Index: %i, Digit: %s, Letter: %s" % (idx, digit, letter))
plt.imshow(img)
plt.show()<jupyter_output><empty_output><jupyter_text>Data preprocessing<jupyter_code>x_train = train.drop(["id", "digit", "letter"], axis=1).values
x_train = x_train.reshape(-1, 28, 28, 1)
x_train = x_train/255
y = train["digit"]
y_train = np.zeros((len(y), len(y.unique())))

for idx, digit in enumerate(y):
    y_train[idx, digit] = 1
x_train, x_valid, y_train, y_valid = train_test_split(x_train, y_train, test_size=0.2, random_state=SEED, shuffle=True)<jupyter_output><empty_output><jupyter_text>Train Model<jupyter_code>@tf.autograph.experimental.do_not_convert
def create_cnn_model(x_train):
    inputs = tf.keras.layers.Input(x_train.shape[1:])

    bn = tf.keras.layers.BatchNormalization()(inputs)
    conv = tf.keras.layers.Conv2D(128, kernel_size=5, strides=1, padding='same', activation='relu')(bn)
    bn = tf.keras.layers.BatchNormalization()(conv)
    conv = tf.keras.layers.Conv2D(128, kernel_size=2, strides=1, padding='same', activation='relu')(bn)
    pool = tf.keras.layers.MaxPooling2D((2, 2))(conv)

    bn = tf.keras.layers.BatchNormalization()(pool)
    conv = tf.keras.layers.Conv2D(256, kernel_size=2, strides=1, padding='same', activation='relu')(bn)
    bn = tf.keras.layers.BatchNormalization()(conv)
    conv = tf.keras.layers.Conv2D(256, kernel_size=2, strides=1, padding='same', activation='relu')(bn)
    pool = tf.keras.layers.MaxPooling2D((2, 2))(conv)

    flatten = tf.keras.layers.Flatten()(pool)

    bn = tf.keras.layers.BatchNormalization()(flatten)
    dense = tf.keras.layers.Dense(1000, activation='relu')(bn)

    bn = tf.keras.layers.BatchNormalization()(dense)
    outputs = tf.keras.layers.Dense(10, activation='softmax')(bn)

    model = tf.keras.models.Model(inputs=inputs, outputs=outputs)

    return model
model = create_cnn_model(x_train)
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
model.summary()
history = model.fit(x_train, y_train, epochs=20, validation_data=(x_valid, y_valid))<jupyter_output>Epoch 1/20
WARNING:tensorflow:AutoGraph could not transform <function Model.make_train_function.<locals>.train_function at 0x000001DE1A9571E0> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: 'arguments' object has no attribute 'posonlyargs'
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
WARNING: AutoGraph could not transform <function Model.make_train_function.<locals>.train_function at 0x000001DE1A9571E0> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: 'arguments' object has no attribute 'posonlyargs'
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
 1/52 [..............................] - ETA: 0s - loss: 3.2391 - a[...]<jupyter_text>Visualize Model Metrics<jupyter_code>img_save_path = "./img/v1/"
ig, ax = plt.subplots(1, 2, figsize=(20, 3))
ax = ax.ravel()

for i, met in enumerate(['accuracy', 'loss']):
    ax[i].plot(history.history[met])
    ax[i].plot(history.history['val_' + met])
    ax[i].set_title('Model {}'.format(met))
    ax[i].set_xlabel('epochs')
    ax[i].set_ylabel(met)
    ax[i].legend(['train', 'val'])

try:
    if not(os.path.isdir(img_save_path)):
        os.makedirs(os.path.join(img_save_path))

    plt.savefig(img_save_path + "ANBWv1_acc_loss.png", facecolor="#eeeeee", bbox_inches="tight")
    plt.show()
except:
    print("[Failed to create directory]")<jupyter_output><empty_output><jupyter_text>Predict<jupyter_code>x_test = test.drop(['id', 'letter'], axis=1).values
x_test = x_test.reshape(-1, 28, 28, 1)
x_test = x_test/255
submission = pd.read_csv('data/submission.csv')
submission['digit'] = np.argmax(model.predict(x_test), axis=1)
submission.head()
test_imgs = []
for test_img in list(test.loc[:8, "0":].values):
    test_imgs.append(test_img.reshape(28, 28).astype(int))

digits = list(submission.loc[:8, "digit"].values)
plt.figure(figsize=(10, 10))
for i in range(9):
    ax = plt.subplot(3, 3, i+1)
    plt.imshow(test_imgs[i])
    plt.title("Predict: %i" % digits[i])
    plt.axis("off")
    
try:
    if not(os.path.isdir(img_save_path)):
        os.makedirs(os.path.join(img_save_path))

    plt.savefig(img_save_path + "test_visualize.png", facecolor="#eeeeee", bbox_inches="tight")
    plt.show()
except:
    print("[Failed to create directory]")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1574230639161312, 'fraction_numerical': 0.025701724721001015, 'mean_word_length': 5.661036036036036, 'pattern_counts': {'":': 2, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '351415', 'license': 'Apache-2.0', 'n_tokens_mistral': 2331, 'n_tokens_neox': 2206, 'n_words': 412, 'path': 'ANBW_v1.ipynb', 'repo_name': 'Mayner0220/ANBW', 'size': '5914'}