{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport tensorflow as tf\n\ntf.test.gpu_device_name()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(\"../input/Kannada-MNIST/train.csv\")\ntest_df = pd.read_csv(\"../input/Kannada-MNIST/test.csv\")\n\n\nfeatures = train_df.iloc[:,1:].values\ntargets = train_df.iloc[:,0].values\nvalidation = test_df.iloc[:,1:].values\n\nprint(\"training images shape {}\".format(features.shape))\nprint(\"training labels shape {}\".format(targets.shape))\nprint(\"validation images shape {}\".format(validation.shape))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# reshape images\n\nflattened_features = features.reshape(features.shape[0], 28, 28, 1)\nflattened_validation = validation.reshape(validation.shape[0], 28, 28, 1)\n\nprint(\"x_flattened size {}\".format(flattened_features.shape))\nprint(\"val_flattened size {}\".format(flattened_validation.shape))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def normalize_data(df):\n  df = df / 255.\n  return df\n\nnormalized_features = normalize_data(flattened_features)\nnormalized_validation = normalize_data(flattened_validation)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\n\n# test train split\n\nX_train, X_test, y_train, y_test = train_test_split(normalized_features, targets, test_size=0.2, random_state=7)\n\n# viz\nplt.imshow(X_train[0][:,:,0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Build CNN with Keras\n# standard, with the addition of swish!\n\n# Config\nEPOCHS = 250\nBATCH_SIZE = 256\nLEARNING_RATE = 0.001 # 0.002\n\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom tensorflow.keras import backend as k\n\n# add custom swish activation function\nfrom tensorflow.keras.backend import sigmoid\ndef swish(x, beta = 1):\n    return (x * sigmoid(beta * x))\n\nfrom tensorflow.keras.layers import Activation\ntf.keras.utils.get_custom_objects().update({'swish': Activation(swish)})\n\n# Build model\n\nmodel = Sequential()\n\n# add layers\nmodel.add(Conv2D(filters=64, kernel_size=(5, 5), padding='Same', activation='swish', input_shape=(28, 28, 1)))\nmodel.add(Conv2D(filters=64, kernel_size=(5, 5), padding='Same', activation='swish', input_shape=(28, 28, 1)))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(filters=32, kernel_size=(2, 2), padding='Same', activation='swish'))\nmodel.add(Conv2D(filters=32, kernel_size=(2, 2), padding='Same', activation='swish'))\nmodel.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation='swish'))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(10, activation='softmax'))\n\n## optimizer\nkeras_optimizer = tf.keras.optimizers.Nadam(lr=LEARNING_RATE)\n\n# Compile model\nmodel.compile(optimizer=keras_optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trained_model = model.fit(x=X_train, y=y_train, epochs=EPOCHS, batch_size=BATCH_SIZE, validation_data=(X_test, y_test))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_submission = pd.read_csv(\"../input/Kannada-MNIST/sample_submission.csv\")\n\nclass_predictions = model.predict_classes(normalized_validation)\n\nsample_submission['label'] = pd.Series(class_predictions)\n\nsample_submission.to_csv(\"submission.csv\",index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}