-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
280 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,280 @@ | ||
{ | ||
"nbformat": 4, | ||
"nbformat_minor": 0, | ||
"metadata": { | ||
"colab": { | ||
"name": "Untitled0.ipynb", | ||
"provenance": [], | ||
"collapsed_sections": [] | ||
}, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3" | ||
}, | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "n6dTOOdqwfEX", | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"outputId": "40a6b482-3df8-4c21-a145-41319c5ad611" | ||
}, | ||
"source": [ | ||
"!pip install keras-tuner\n" | ||
], | ||
"execution_count": null, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"text": [ | ||
"Collecting keras-tuner\n", | ||
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/20/ec/1ef246787174b1e2bb591c95f29d3c1310070cad877824f907faba3dade9/keras-tuner-1.0.2.tar.gz (62kB)\n", | ||
"\r\u001b[K |█████▏ | 10kB 14.9MB/s eta 0:00:01\r\u001b[K |██████████▍ | 20kB 14.0MB/s eta 0:00:01\r\u001b[K |███████████████▋ | 30kB 9.8MB/s eta 0:00:01\r\u001b[K |████████████████████▉ | 40kB 8.4MB/s eta 0:00:01\r\u001b[K |██████████████████████████ | 51kB 5.4MB/s eta 0:00:01\r\u001b[K |███████████████████████████████▎| 61kB 5.9MB/s eta 0:00:01\r\u001b[K |████████████████████████████████| 71kB 4.2MB/s \n", | ||
"\u001b[?25hRequirement already satisfied: packaging in /usr/local/lib/python3.7/dist-packages (from keras-tuner) (20.9)\n", | ||
"Requirement already satisfied: future in /usr/local/lib/python3.7/dist-packages (from keras-tuner) (0.16.0)\n", | ||
"Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from keras-tuner) (1.19.5)\n", | ||
"Requirement already satisfied: tabulate in /usr/local/lib/python3.7/dist-packages (from keras-tuner) (0.8.9)\n", | ||
"Collecting terminaltables\n", | ||
" Downloading https://files.pythonhosted.org/packages/9b/c4/4a21174f32f8a7e1104798c445dacdc1d4df86f2f26722767034e4de4bff/terminaltables-3.1.0.tar.gz\n", | ||
"Collecting colorama\n", | ||
" Downloading https://files.pythonhosted.org/packages/44/98/5b86278fbbf250d239ae0ecb724f8572af1c91f4a11edf4d36a206189440/colorama-0.4.4-py2.py3-none-any.whl\n", | ||
"Requirement already satisfied: tqdm in /usr/local/lib/python3.7/dist-packages (from keras-tuner) (4.41.1)\n", | ||
"Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from keras-tuner) (2.23.0)\n", | ||
"Requirement already satisfied: scipy in /usr/local/lib/python3.7/dist-packages (from keras-tuner) (1.4.1)\n", | ||
"Requirement already satisfied: scikit-learn in /usr/local/lib/python3.7/dist-packages (from keras-tuner) (0.22.2.post1)\n", | ||
"Requirement already satisfied: pyparsing>=2.0.2 in /usr/local/lib/python3.7/dist-packages (from packaging->keras-tuner) (2.4.7)\n", | ||
"Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests->keras-tuner) (2.10)\n", | ||
"Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests->keras-tuner) (3.0.4)\n", | ||
"Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests->keras-tuner) (1.24.3)\n", | ||
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests->keras-tuner) (2020.12.5)\n", | ||
"Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.7/dist-packages (from scikit-learn->keras-tuner) (1.0.1)\n", | ||
"Building wheels for collected packages: keras-tuner, terminaltables\n", | ||
" Building wheel for keras-tuner (setup.py) ... \u001b[?25l\u001b[?25hdone\n", | ||
" Created wheel for keras-tuner: filename=keras_tuner-1.0.2-cp37-none-any.whl size=78938 sha256=95a44373e2186ae4493fa9994be226432f2ebcb8fb4285a2f1a44da1bf8de9f7\n", | ||
" Stored in directory: /root/.cache/pip/wheels/bb/a1/8a/7c3de0efb3707a1701b36ebbfdbc4e67aedf6d4943a1f463d6\n", | ||
" Building wheel for terminaltables (setup.py) ... \u001b[?25l\u001b[?25hdone\n", | ||
" Created wheel for terminaltables: filename=terminaltables-3.1.0-cp37-none-any.whl size=15356 sha256=467fd452ae8e548e4d91d768c5f1b93b7e69ef1f9ee57f3e7fc5be7ded1caafb\n", | ||
" Stored in directory: /root/.cache/pip/wheels/30/6b/50/6c75775b681fb36cdfac7f19799888ef9d8813aff9e379663e\n", | ||
"Successfully built keras-tuner terminaltables\n", | ||
"Installing collected packages: terminaltables, colorama, keras-tuner\n", | ||
"Successfully installed colorama-0.4.4 keras-tuner-1.0.2 terminaltables-3.1.0\n" | ||
], | ||
"name": "stdout" | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "d0Pfij4Kzc7y" | ||
}, | ||
"source": [ | ||
"import tensorflow as tf\n", | ||
"from tensorflow import keras\n", | ||
"import numpy as np" | ||
], | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "4KwmQJxxz4Z4" | ||
}, | ||
"source": [ | ||
"fashion_mnist=keras.datasets.fashion_mnist" | ||
], | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "2Yvoy-3E0OYM" | ||
}, | ||
"source": [ | ||
"(train_images,train_labels),(test_images,test_lables)=fashion_mnist.load_data()" | ||
], | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "QVkZaCzf0kYf" | ||
}, | ||
"source": [ | ||
"train_images=train_images/255.0\n", | ||
"test_images=test_images/255.0" | ||
], | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "V8XQqaSJ1dct", | ||
"outputId": "c51f8425-1094-4b7e-9173-181cbf596c49" | ||
}, | ||
"source": [ | ||
"train_images[0].shape" | ||
], | ||
"execution_count": null, | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"(28, 28)" | ||
] | ||
}, | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"execution_count": 41 | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "cxIViZ4-11ZK" | ||
}, | ||
"source": [ | ||
"train_images=train_images.reshape(len(train_images),28,28,1)\n", | ||
"test_images=test_images.reshape(len(test_images),28,28,1)" | ||
], | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "tK5cHhr82RNR" | ||
}, | ||
"source": [ | ||
"def build_model(hp):\n", | ||
" model=keras.Sequential([\n", | ||
" keras.layers.Conv2D(\n", | ||
" filters=hp.Int('conv_1_filter',min_value=32, max_value=128,step=16),\n", | ||
" kernel_size=hp.Choice('conv_1_kernel',values=[3,5]),\n", | ||
" activation='relu',\n", | ||
" input_shape=(28,28,1)\n", | ||
" \n", | ||
" ),\n", | ||
" keras.layers.Conv2D(\n", | ||
" filters=hp.Int('conv_2_filter',min_value=32, max_value=64,step=16),\n", | ||
" kernel_size=hp.Choice('conv_2_kernel',values=[3,5]),\n", | ||
" activation='relu'\n", | ||
" ),\n", | ||
" keras.layers.Flatten(),\n", | ||
" keras.layers.Dense(\n", | ||
" units=hp.Int('dense_1_units',min_value=32, max_value=128,step=16),\n", | ||
" activation='relu'\n", | ||
" ),\n", | ||
" keras.layers.Dense(10,activation='softmax')\n", | ||
" ])\n", | ||
" model.compile(optimizer=keras.optimizers.Adam(hp.Choice('learning_rate',values=[1e-2,1e-3])),\n", | ||
" loss='sparse_categorical_crossentropy',\n", | ||
" metrics=['accuracy'])\n", | ||
" return model" | ||
], | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "vUxboH8V8TKu" | ||
}, | ||
"source": [ | ||
"from kerastuner import RandomSearch\n", | ||
"from kerastuner.engine.hyperparameters import HyperParameters" | ||
], | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "dzhb_IkH9Kbl" | ||
}, | ||
"source": [ | ||
"tuner_search=RandomSearch(build_model,objective='val_accuracy',max_trials=5,directory='output',project_name=\"Mnist Fashion\")" | ||
], | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "qKH_cDV7-0Bl" | ||
}, | ||
"source": [ | ||
"tuner_search.search(train_images,train_labels,epochs=3,validation_split=0.1)" | ||
], | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "NPHGmFnNAhey" | ||
}, | ||
"source": [ | ||
"model=tuner_search.get_best_models(num_models=1)[0]" | ||
], | ||
"execution_count": 57, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "FO_tFSXQC9Ti", | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"outputId": "6fc24271-ed44-4b72-902a-aac438092ed7" | ||
}, | ||
"source": [ | ||
"model.summary()" | ||
], | ||
"execution_count": 58, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"text": [ | ||
"Model: \"sequential\"\n", | ||
"_________________________________________________________________\n", | ||
"Layer (type) Output Shape Param # \n", | ||
"=================================================================\n", | ||
"conv2d (Conv2D) (None, 24, 24, 96) 2496 \n", | ||
"_________________________________________________________________\n", | ||
"conv2d_1 (Conv2D) (None, 22, 22, 48) 41520 \n", | ||
"_________________________________________________________________\n", | ||
"flatten (Flatten) (None, 23232) 0 \n", | ||
"_________________________________________________________________\n", | ||
"dense (Dense) (None, 80) 1858640 \n", | ||
"_________________________________________________________________\n", | ||
"dense_1 (Dense) (None, 10) 810 \n", | ||
"=================================================================\n", | ||
"Total params: 1,903,466\n", | ||
"Trainable params: 1,903,466\n", | ||
"Non-trainable params: 0\n", | ||
"_________________________________________________________________\n" | ||
], | ||
"name": "stdout" | ||
} | ||
] | ||
} | ||
] | ||
} |