{"cells":[{"metadata":{},"cell_type":"markdown","source":"In this tutorial we will try to work with kerastuner package.\nhttps://keras-team.github.io/keras-tuner/\nThis Package let us tuning hyperparameters.\nSo, at first, we need to install it:"},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install -U keras-tuner","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nfrom __future__ import print_function\nimport keras\nimport tensorflow as tf\nfrom keras.datasets import mnist\nfrom keras import backend as K\nimport pandas as pd\nimport numpy as np\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom kerastuner.applications import HyperResNet\nfrom kerastuner.tuners import Hyperband\n\n#our training parameters\nbatch_size = 500\nnum_classes = 10\nepochs = 15\n\n# input image dimensions\nimg_rows, img_cols = 28, 28\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Reading our **datasets**"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"test_df = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\ntrain_df = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now we separate **labels** and **images**:"},{"metadata":{"trusted":true},"cell_type":"code","source":"Y_train_df = train_df['label']\n#Y_train_df = pd.get_dummies(Y_train_df).values\n#Y_train_df = Y_train_df.values.reshape(-1, 1)\n#Y_train_df = standard_scaler.fit_transform(Y_train_df)\n#Y_train_df = keras.utils.np_utils.to_categorical(Y_train_df)\n#Y_train_df = Y_train_df.reshape(-1, 10, 1)\nprint(Y_train_df.shape)\nX_train_df = train_df.drop(columns=\"label\")\n#X_train_df = standard_scaler.fit_transform(X_train_df)\n#X_train_df = keras.utils.np_utils.to_categorical(X_train_df)\nprint(X_train_df.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_test_df = test_df\n#X_train_df = standard_scaler.fit_transform(X_train_df)\n#X_train_df = keras.utils.np_utils.to_categorical(X_train_df)\nprint(X_test_df.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"np.random.seed(7)\n# split into 67% for train and 15% for test\nX_train, X_valid, y_train, y_valid = train_test_split(X_train_df, Y_train_df, test_size=0.15, random_state=7)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Artificially increase training set\ntrain_datagen = ImageDataGenerator(rescale=1./255.,\n                                   rotation_range=10,\n                                   width_shift_range=0.25,\n                                   height_shift_range=0.25,\n                                   shear_range=0.1,\n                                   zoom_range=0.25,\n                                   horizontal_flip=False)\n\nvalid_datagen = ImageDataGenerator(rescale=1./255.)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_test = test_df.drop('id', axis = 1)\n# X_test = standard_scaler.fit_transform(X_test)\ny_test = test_df['id']\n# y_test = keras.utils.np_utils.to_categorical(y_test)\ny_test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train = X_train.values\nx_valid = X_valid.values\ny_train = y_train.values\ny_valid = y_valid.values\nx_test = X_test.values","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Input data shape settings:"},{"metadata":{"trusted":true},"cell_type":"code","source":"if K.image_data_format() == 'channels_first':\n    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)\n    x_valid = x_valid.reshape(x_valid.shape[0], 1, img_rows, img_cols)\n    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)\n    input_shape = (1, img_rows, img_cols)\nelse:\n    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)\n    x_valid = x_valid.reshape(x_valid.shape[0], img_rows, img_cols, 1)\n    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)\n    input_shape = (img_rows, img_cols, 1)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train = x_train.astype('float32')\nx_valid = x_valid.astype('float32')\nx_test = x_test.astype('float32')\n# x_train /= 255\n# x_valid /= 255\n# x_test /= 255\nprint('x_train shape:', x_train.shape)\nprint(x_train.shape[0], 'train samples')\nprint(x_valid.shape[0], 'test samples')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# convert class vectors to binary class matrices\ny_train = keras.utils.to_categorical(y_train, num_classes)\ny_valid = keras.utils.to_categorical(y_valid, num_classes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_tuning_model(num_classes, input_shape):\n    model = HyperResNet(input_shape=input_shape, classes=num_classes)\n    return model","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Create standard HyperResNet model and find results:"},{"metadata":{"trusted":true},"cell_type":"code","source":"model = get_tuning_model(num_classes, input_shape)\ntuner = Hyperband(\n    model,\n    objective='val_accuracy',\n    max_epochs=2,\n    directory='my_dir',\n    project_name='helloworld')\n\ntuner.search(x_train, y_train, epochs=2, validation_data=(x_valid, y_valid))","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}