umz21/lab/Sieci_neuronowe_Keras.ipynb
2021-06-02 10:40:42 +02:00

261 lines
9.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"source": [
"### Uczenie maszynowe — zastosowania\n",
"# Sieci neuronowe (Keras)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Keras](https://keras.io) to napisany w języku Python interfejs do platformy [TensorFlow](https://www.tensorflow.org), służącej do uczenia maszynowego.\n",
"\n",
"Aby z niego korzystać, trzeba zainstalować bibliotekę TensorFlow.\n",
"\n",
"Instrukcja dla skryptów Pythona (pliki *.py*):\n",
" * `pip`: https://www.tensorflow.org/install\n",
" * `conda`: https://docs.anaconda.com/anaconda/user-guide/tasks/tensorflow"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Aby uruchomić TensorFlow w środowisku Jupyter, należy wykonać następujące czynności:\n",
"\n",
"#### Przed pierwszym uruchomieniem (wystarczy wykonać tylko raz)\n",
"\n",
"Instalacja biblioteki TensorFlow w środowisku Anaconda:\n",
"\n",
"1. Uruchom *Anaconda Navigator*\n",
"1. Wybierz kafelek *CMD.exe Prompt*\n",
"1. Kliknij przycisk *Launch*\n",
"1. Pojawi się konsola. Wpisz następujące polecenia, każde zatwierdzając wciśnięciem klawisza Enter:\n",
"```\n",
"conda create -n tf tensorflow\n",
"conda activate tf\n",
"conda install pandas matplotlib\n",
"jupyter notebook\n",
"```\n",
"\n",
"#### Przed każdym uruchomieniem\n",
"\n",
"Jeżeli chcemy korzystać z biblioteki TensorFlow, to środowisko Jupyter Notebook należy uruchomić w następujący sposób:\n",
"\n",
"1. Uruchom *Anaconda Navigator*\n",
"1. Wybierz kafelek *CMD.exe Prompt*\n",
"1. Kliknij przycisk *Launch*\n",
"1. Pojawi się konsola. Wpisz następujące polecenia, każde zatwierdzając wciśnięciem klawisza Enter:\n",
"```\n",
"conda activate tf\n",
"jupyter notebook\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Przykład implementacji sieci neuronowej do rozpoznawania cyfr ze zbioru [MNIST](https://en.wikipedia.org/wiki/MNIST_database), według https://keras.io/examples/vision/mnist_convnet"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Konieczne importy\n",
"\n",
"import numpy as np\n",
"from tensorflow import keras\n",
"from tensorflow.keras import layers"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'keras' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-1-d9ae37c68de4>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;31m# podział danych na zbiory uczący i testowy\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[1;33m(\u001b[0m\u001b[0mx_train\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my_train\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mx_test\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my_test\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mkeras\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdatasets\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmnist\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mload_data\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 8\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;31m# skalowanie wartości pikseli do przedziału [0, 1]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mNameError\u001b[0m: name 'keras' is not defined"
]
}
],
"source": [
"# Przygotowanie danych\n",
"\n",
"num_classes = 10\n",
"input_shape = (28, 28, 1)\n",
"\n",
"# podział danych na zbiory uczący i testowy\n",
"(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()\n",
"\n",
"# skalowanie wartości pikseli do przedziału [0, 1]\n",
"x_train = x_train.astype(\"float32\") / 255\n",
"x_test = x_test.astype(\"float32\") / 255\n",
"# upewnienie się, że obrazy mają wymiary (28, 28, 1)\n",
"x_train = np.expand_dims(x_train, -1)\n",
"x_test = np.expand_dims(x_test, -1)\n",
"print(\"x_train shape:\", x_train.shape)\n",
"print(x_train.shape[0], \"train samples\")\n",
"print(x_test.shape[0], \"test samples\")\n",
"\n",
"# konwersja danych kategorycznych na binarne\n",
"y_train = keras.utils.to_categorical(y_train, num_classes)\n",
"y_test = keras.utils.to_categorical(y_test, num_classes)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model: \"sequential\"\n",
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"conv2d (Conv2D) (None, 26, 26, 32) 320 \n",
"_________________________________________________________________\n",
"max_pooling2d (MaxPooling2D) (None, 13, 13, 32) 0 \n",
"_________________________________________________________________\n",
"conv2d_1 (Conv2D) (None, 11, 11, 64) 18496 \n",
"_________________________________________________________________\n",
"max_pooling2d_1 (MaxPooling2 (None, 5, 5, 64) 0 \n",
"_________________________________________________________________\n",
"flatten (Flatten) (None, 1600) 0 \n",
"_________________________________________________________________\n",
"dropout (Dropout) (None, 1600) 0 \n",
"_________________________________________________________________\n",
"dense (Dense) (None, 10) 16010 \n",
"=================================================================\n",
"Total params: 34,826\n",
"Trainable params: 34,826\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"# Stworzenie modelu\n",
"\n",
"model = keras.Sequential(\n",
" [\n",
" keras.Input(shape=input_shape),\n",
" layers.Conv2D(32, kernel_size=(3, 3), activation=\"relu\"),\n",
" layers.MaxPooling2D(pool_size=(2, 2)),\n",
" layers.Conv2D(64, kernel_size=(3, 3), activation=\"relu\"),\n",
" layers.MaxPooling2D(pool_size=(2, 2)),\n",
" layers.Flatten(),\n",
" layers.Dropout(0.5),\n",
" layers.Dense(num_classes, activation=\"softmax\"),\n",
" ]\n",
")\n",
"\n",
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"422/422 [==============================] - 38s 91ms/step - loss: 0.0556 - accuracy: 0.9826 - val_loss: 0.0412 - val_accuracy: 0.9893\n"
]
},
{
"data": {
"text/plain": [
"<tensorflow.python.keras.callbacks.History at 0x1a50b35a070>"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Uczenie modelu\n",
"\n",
"batch_size = 128\n",
"epochs = 15\n",
"\n",
"model.compile(loss=\"categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"])\n",
"\n",
"model.fit(x_train, y_train, epochs=1, batch_size=batch_size, epochs=epochs, validation_split=0.1)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test loss: 0.03675819933414459\n",
"Test accuracy: 0.988099992275238\n"
]
}
],
"source": [
"# Ewaluacja modelu\n",
"\n",
"score = model.evaluate(x_test, y_test, verbose=0)\n",
"print(\"Test loss:\", score[0])\n",
"print(\"Test accuracy:\", score[1])"
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"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.3"
},
"livereveal": {
"start_slideshow_at": "selected",
"theme": "amu"
}
},
"nbformat": 4,
"nbformat_minor": 4
}