Created
October 28, 2020 16:42
-
-
Save ritog/3a0dd7b58fa87baf3fd89aa33e202f20 to your computer and use it in GitHub Desktop.
A barebone introduction to Keras
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"from random import randint\n", | |
"from sklearn.utils import shuffle\n", | |
"from sklearn.preprocessing import MinMaxScaler" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"train_labels = []\n", | |
"train_samples = []" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# initializing ages and side-effect for 5% of people\n", | |
"for i in range(50):\n", | |
" random_younger = randint(13,64)\n", | |
" train_samples.append(random_younger)\n", | |
" train_labels.append(1) # side-effect\n", | |
" \n", | |
" random_older = randint(65,100)\n", | |
" train_samples.append(random_older)\n", | |
" train_labels.append(0) # no side-effect" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# initializing ages and appearance of side-effects for 95% people\n", | |
"for i in range(1000):\n", | |
" random_younger = randint(13, 64)\n", | |
" train_samples.append(random_younger)\n", | |
" train_labels.append(0)\n", | |
" \n", | |
" random_older = randint(65, 100)\n", | |
" train_samples.append(random_older)\n", | |
" train_labels.append(1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[44, 85, 53, 82, 45]" | |
] | |
}, | |
"execution_count": 30, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"train_samples[:5]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 0, 1, 0, 1]" | |
] | |
}, | |
"execution_count": 31, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"train_labels[:5]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"train_labels = np.array(train_labels)\n", | |
"train_samples = np.array(train_samples)\n", | |
"train_labels, train_samples = shuffle(train_labels, train_samples)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"scaler = MinMaxScaler(feature_range=(0,1))\n", | |
"scaled_train_samples = scaler.fit_transform(train_samples.reshape(-1, 1))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[0.27586207],\n", | |
" [0.22988506],\n", | |
" [0.11494253],\n", | |
" [0.03448276],\n", | |
" [0.56321839]])" | |
] | |
}, | |
"execution_count": 36, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"scaled_train_samples[:5]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import tensorflow as tf\n", | |
"from tensorflow import keras\n", | |
"from tensorflow.keras.models import Sequential\n", | |
"from tensorflow.keras.layers import Activation, Dense\n", | |
"from tensorflow.keras.optimizers import Adam\n", | |
"from tensorflow.keras.metrics import categorical_crossentropy" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"model = Sequential([\n", | |
" Dense(units=16, input_shape=(1,), activation='relu'),\n", | |
" Dense(units=32, activation='relu'),\n", | |
" Dense(units=2, activation='softmax')\n", | |
"])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Model: \"sequential\"\n", | |
"_________________________________________________________________\n", | |
"Layer (type) Output Shape Param # \n", | |
"=================================================================\n", | |
"dense (Dense) (None, 16) 32 \n", | |
"_________________________________________________________________\n", | |
"dense_1 (Dense) (None, 32) 544 \n", | |
"_________________________________________________________________\n", | |
"dense_2 (Dense) (None, 2) 66 \n", | |
"=================================================================\n", | |
"Total params: 642\n", | |
"Trainable params: 642\n", | |
"Non-trainable params: 0\n", | |
"_________________________________________________________________\n" | |
] | |
} | |
], | |
"source": [ | |
"model.summary()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"model.compile(optimizer=Adam(learning_rate=0.0001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Epoch 1/30\n", | |
"189/189 - 1s - loss: 0.7034 - accuracy: 0.5079 - val_loss: 0.6826 - val_accuracy: 0.5429\n", | |
"Epoch 2/30\n", | |
"189/189 - 0s - loss: 0.6606 - accuracy: 0.6307 - val_loss: 0.6481 - val_accuracy: 0.6667\n", | |
"Epoch 3/30\n", | |
"189/189 - 0s - loss: 0.6235 - accuracy: 0.7148 - val_loss: 0.6166 - val_accuracy: 0.7000\n", | |
"Epoch 4/30\n", | |
"189/189 - 0s - loss: 0.5863 - accuracy: 0.7619 - val_loss: 0.5857 - val_accuracy: 0.7476\n", | |
"Epoch 5/30\n", | |
"189/189 - 0s - loss: 0.5496 - accuracy: 0.8143 - val_loss: 0.5546 - val_accuracy: 0.7571\n", | |
"Epoch 6/30\n", | |
"189/189 - 1s - loss: 0.5134 - accuracy: 0.8317 - val_loss: 0.5239 - val_accuracy: 0.8000\n", | |
"Epoch 7/30\n", | |
"189/189 - 1s - loss: 0.4783 - accuracy: 0.8598 - val_loss: 0.4952 - val_accuracy: 0.8333\n", | |
"Epoch 8/30\n", | |
"189/189 - 1s - loss: 0.4455 - accuracy: 0.8709 - val_loss: 0.4683 - val_accuracy: 0.8524\n", | |
"Epoch 9/30\n", | |
"189/189 - 1s - loss: 0.4155 - accuracy: 0.8862 - val_loss: 0.4439 - val_accuracy: 0.8714\n", | |
"Epoch 10/30\n", | |
"189/189 - 0s - loss: 0.3882 - accuracy: 0.8968 - val_loss: 0.4218 - val_accuracy: 0.8857\n", | |
"Epoch 11/30\n", | |
"189/189 - 0s - loss: 0.3646 - accuracy: 0.9106 - val_loss: 0.4039 - val_accuracy: 0.8905\n", | |
"Epoch 12/30\n", | |
"189/189 - 1s - loss: 0.3456 - accuracy: 0.9111 - val_loss: 0.3892 - val_accuracy: 0.9048\n", | |
"Epoch 13/30\n", | |
"189/189 - 1s - loss: 0.3302 - accuracy: 0.9206 - val_loss: 0.3773 - val_accuracy: 0.9048\n", | |
"Epoch 14/30\n", | |
"189/189 - 0s - loss: 0.3172 - accuracy: 0.9201 - val_loss: 0.3673 - val_accuracy: 0.9000\n", | |
"Epoch 15/30\n", | |
"189/189 - 0s - loss: 0.3070 - accuracy: 0.9280 - val_loss: 0.3591 - val_accuracy: 0.9000\n", | |
"Epoch 16/30\n", | |
"189/189 - 0s - loss: 0.2984 - accuracy: 0.9275 - val_loss: 0.3525 - val_accuracy: 0.9000\n", | |
"Epoch 17/30\n", | |
"189/189 - 0s - loss: 0.2917 - accuracy: 0.9312 - val_loss: 0.3472 - val_accuracy: 0.9000\n", | |
"Epoch 18/30\n", | |
"189/189 - 1s - loss: 0.2861 - accuracy: 0.9312 - val_loss: 0.3430 - val_accuracy: 0.9000\n", | |
"Epoch 19/30\n", | |
"189/189 - 1s - loss: 0.2815 - accuracy: 0.9344 - val_loss: 0.3396 - val_accuracy: 0.9000\n", | |
"Epoch 20/30\n", | |
"189/189 - 1s - loss: 0.2775 - accuracy: 0.9333 - val_loss: 0.3356 - val_accuracy: 0.9095\n", | |
"Epoch 21/30\n", | |
"189/189 - 1s - loss: 0.2743 - accuracy: 0.9344 - val_loss: 0.3331 - val_accuracy: 0.9095\n", | |
"Epoch 22/30\n", | |
"189/189 - 1s - loss: 0.2714 - accuracy: 0.9328 - val_loss: 0.3309 - val_accuracy: 0.9095\n", | |
"Epoch 23/30\n", | |
"189/189 - 0s - loss: 0.2692 - accuracy: 0.9365 - val_loss: 0.3291 - val_accuracy: 0.9095\n", | |
"Epoch 24/30\n", | |
"189/189 - 0s - loss: 0.2671 - accuracy: 0.9354 - val_loss: 0.3274 - val_accuracy: 0.9095\n", | |
"Epoch 25/30\n", | |
"189/189 - 1s - loss: 0.2653 - accuracy: 0.9360 - val_loss: 0.3253 - val_accuracy: 0.9095\n", | |
"Epoch 26/30\n", | |
"189/189 - 1s - loss: 0.2635 - accuracy: 0.9365 - val_loss: 0.3239 - val_accuracy: 0.9095\n", | |
"Epoch 27/30\n", | |
"189/189 - 0s - loss: 0.2620 - accuracy: 0.9365 - val_loss: 0.3227 - val_accuracy: 0.9095\n", | |
"Epoch 28/30\n", | |
"189/189 - 0s - loss: 0.2607 - accuracy: 0.9365 - val_loss: 0.3216 - val_accuracy: 0.9095\n", | |
"Epoch 29/30\n", | |
"189/189 - 0s - loss: 0.2595 - accuracy: 0.9365 - val_loss: 0.3201 - val_accuracy: 0.9095\n", | |
"Epoch 30/30\n", | |
"189/189 - 0s - loss: 0.2583 - accuracy: 0.9386 - val_loss: 0.3190 - val_accuracy: 0.9095\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<tensorflow.python.keras.callbacks.History at 0x7f2eefb01880>" | |
] | |
}, | |
"execution_count": 42, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"model.fit(x=scaled_train_samples, y=train_labels, validation_split=0.1, batch_size=10, epochs=30, shuffle=True, verbose=2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment