{ "cells": [ { "metadata": {}, "cell_type": "markdown", "source": "# Laboratorium 4", "id": "a5fdda4037524d27" }, { "metadata": { "ExecuteTime": { "end_time": "2026-11-09T22:04:49.400753329Z", "start_time": "2026-11-09T22:04:49.357109712Z" } }, "cell_type": "code", "source": [ "import numpy as np\n", "import struct\n", "import scipy as sc" ], "id": "4ddc40d457336ec4", "outputs": [], "execution_count": 107 }, { "metadata": {}, "cell_type": "markdown", "source": "## Kod laboratorium 3:", "id": "69234b3906bf94a3" }, { "metadata": { "ExecuteTime": { "end_time": "2026-11-09T22:04:49.487814246Z", "start_time": "2026-11-09T22:04:49.426247352Z" } }, "cell_type": "code", "source": [ "def sigmoid(x):\n", " x = np.clip(x, -500, 500)\n", " return 1 / (1 + np.exp(-x))\n", "\n", "\n", "def sigmoid_deriv(x):\n", " s = sigmoid(x)\n", " return s * (1 - s)\n", "\n", "\n", "def tangens_hip(x):\n", " return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))\n", "\n", "\n", "def tangens_hip_deriv(x):\n", " return 1 - x ** 2\n", "\n", "\n", "def softmax(x):\n", " return sc.special.softmax(x)\n", "\n", "\n", "def softmax_deriv(x):\n", " return np.ones_like(x)\n", "\n", "\n", "def ReLu_deriv(x):\n", " return np.where(x > 0, 1, 0)\n", "\n", "\n", "def ReLu(x):\n", " return np.maximum(x, 0)\n", "\n", "\n", "def load_mnist_images(filename):\n", " with open(filename, 'rb') as f:\n", " magic, num_images, rows, cols = struct.unpack('>IIII', f.read(16))\n", " image_data = np.frombuffer(f.read(), dtype=np.uint8)\n", " images = image_data.reshape(num_images, rows * cols).astype(np.float32) / 255.0\n", " return images\n", "\n", "\n", "def load_mnist_labels(filename):\n", " with open(filename, 'rb') as f:\n", " magic, num_labels = struct.unpack('>II', f.read(8))\n", " labels = np.frombuffer(f.read(), dtype=np.uint8)\n", " return labels\n", "\n", "\n", "def to_one_hot(label, num_classes=10):\n", " one_hot = np.zeros((num_classes, 1))\n", " one_hot[label] = 1.0\n", " return one_hot\n", "\n", "\n", "class NeuralNewtork:\n", " def __init__(self, n, a=0.01):\n", " self.num_neurons = n\n", " self.alpha = a\n", " self.layers = {\n", " 'weights': [],\n", " 'activation_function': [],\n", " 'derivative_function': [],\n", " }\n", "\n", " def add_layer(self, n, activation_func, w_min=-1.0, w_max=1.0):\n", " weights = np.random.uniform(w_min, w_max, (n, self.num_neurons))\n", " self.layers['weights'].append(weights)\n", " self.num_neurons = n\n", " if activation_func is not None:\n", " self.layers['activation_function'].append(activation_func)\n", " if activation_func == sigmoid:\n", " self.layers['derivative_function'].append(sigmoid_deriv)\n", " elif activation_func == ReLu:\n", " self.layers['derivative_function'].append(ReLu_deriv)\n", " elif activation_func == tangens_hip:\n", " self.layers['derivative_function'].append(tangens_hip_deriv)\n", " elif activation_func == softmax:\n", " self.layers['derivative_function'].append(softmax_deriv)\n", " else:\n", " self.layers['activation_function'].append(None)\n", " self.layers['derivative_function'].append(None)\n", "\n", " def forward_propagation(self, input):\n", " activation = []\n", " raw_output = []\n", "\n", " current_input = input\n", " for i in range(len(self.layers['weights'])):\n", " z = np.dot(self.layers['weights'][i], current_input)\n", " raw_output.append(z)\n", "\n", " if self.layers['activation_function'][i] is not None:\n", " a = self.layers['activation_function'][i](z)\n", " else:\n", " a = z\n", " activation.append(a)\n", " current_input = a\n", "\n", " return activation, raw_output\n", "\n", " def back_propagation(self, activation, input, raw_output, expected_output):\n", " N = expected_output.shape[0]\n", " layer_output_delta = (2 * 1 / N) * (activation[-1] - expected_output)\n", "\n", " layer_hidden_delta = np.dot(self.layers['weights'][-1].T, layer_output_delta)\n", "\n", " layer_output_weight_delta = np.dot(layer_output_delta, activation[-2].T)\n", " self.layers['weights'][-1] = self.layers['weights'][-1] - self.alpha * layer_output_weight_delta\n", "\n", " for i in reversed(range(len(self.layers['weights']) - 1)):\n", " layer_hidden_delta = layer_hidden_delta * self.layers['derivative_function'][i](\n", " raw_output[i])\n", "\n", " wejscie_lub_aktywacja = activation[i - 1] if i > 0 else input\n", " layer_hidden_weight_delta = np.dot(layer_hidden_delta, wejscie_lub_aktywacja.T)\n", "\n", " self.layers['weights'][i] = self.layers['weights'][i] - self.alpha * layer_hidden_weight_delta\n", "\n", " def dropout(self, input, expected_output, proc=0.5):\n", " activation = []\n", " raw_output = []\n", " dropout_vectors = []\n", "\n", " current_input = input\n", " num_layers = len(self.layers['weights'])\n", " for i in range(num_layers):\n", " z = np.dot(self.layers['weights'][i], current_input)\n", " raw_output.append(z)\n", "\n", " if self.layers['activation_function'][i] is not None:\n", " a = self.layers['activation_function'][i](z)\n", " else:\n", " a = z\n", "\n", " if i < num_layers - 1:\n", " vector = np.random.default_rng().integers(0, 2, size=a.shape)\n", " dropout_vectors.append(vector)\n", " a = a * vector * (1 / proc)\n", " else:\n", " dropout_vectors.append(None)\n", "\n", " activation.append(a)\n", " current_input = a\n", "\n", " N = expected_output.shape[0]\n", " layer_output_delta = (2 * 1 / N) * (activation[-1] - expected_output)\n", "\n", " layer_hidden_delta = np.dot(self.layers['weights'][-1].T, layer_output_delta)\n", "\n", " layer_output_weight_delta = np.dot(layer_output_delta, activation[-2].T)\n", " self.layers['weights'][-1] = self.layers['weights'][-1] - self.alpha * layer_output_weight_delta\n", "\n", " for i in reversed(range(num_layers - 1)):\n", " layer_hidden_delta = dropout_vectors[i] * layer_hidden_delta * self.layers['derivative_function'][i](\n", " raw_output[i])\n", "\n", " wejscie_lub_aktywacja = activation[i - 1] if i > 0 else input\n", " layer_hidden_weight_delta = np.dot(layer_hidden_delta, wejscie_lub_aktywacja.T)\n", "\n", " self.layers['weights'][i] = self.layers['weights'][i] - self.alpha * layer_hidden_weight_delta\n", " if i > 0:\n", " layer_hidden_delta = np.dot(self.layers['weights'][i].T, layer_hidden_delta)\n", "\n", " def miniBatchGradientDescent(self, input, expected_output, n, proc=0.5):\n", " num_samples, h = input.shape\n", " for i in range(0, num_samples, n):\n", " batch_input = input[i: i + n, :].T\n", " batch_expected = expected_output[i: i + n, :].T\n", " dropout_mask = []\n", " activation = []\n", " raw_output = []\n", " Nr = batch_input.shape[1]\n", "\n", " current_input = batch_input\n", " for l in range(len(self.layers['weights'])):\n", " z = np.dot(self.layers['weights'][l], current_input)\n", "\n", " raw_output.append(z)\n", "\n", " if self.layers['activation_function'][l] is not None:\n", " a = self.layers['activation_function'][l](z)\n", " else:\n", " a = z\n", "\n", " if l < len(self.layers['weights']) - 1:\n", " mask = np.random.default_rng().integers(0, 2, size=a.shape)\n", " a = a * mask * (1 / proc)\n", " dropout_mask.append(mask)\n", " else:\n", " dropout_mask.append(None)\n", "\n", " activation.append(a)\n", " current_input = a\n", "\n", " if self.layers['activation_function'][-1] == softmax:\n", " layer_output_delta = ((activation[-1] - batch_expected)) / n\n", " else:\n", " layer_output_delta = (2 * 1 / Nr) * (activation[-1] - batch_expected)\n", "\n", " layer_hidden_delta = np.dot(self.layers['weights'][-1].T, layer_output_delta)\n", "\n", " layer_output_weight_delta = np.dot(layer_output_delta, activation[-2].T)\n", " self.layers['weights'][-1] = self.layers['weights'][-1] - self.alpha * layer_output_weight_delta\n", "\n", " for l in reversed(range(len(self.layers['weights']) - 1)):\n", " layer_hidden_delta = dropout_mask[l] * layer_hidden_delta * self.layers['derivative_function'][l](\n", " raw_output[l])\n", "\n", " wejscie_lub_aktywacja = activation[l - 1] if l > 0 else batch_input\n", " layer_hidden_weight_delta = np.dot(layer_hidden_delta, wejscie_lub_aktywacja.T)\n", "\n", " self.layers['weights'][l] = self.layers['weights'][l] - self.alpha * layer_hidden_weight_delta\n", " if l > 0:\n", " layer_hidden_delta = np.dot(self.layers['weights'][l].T, layer_hidden_delta)\n", "\n", " def fit(self, input, expected_output):\n", " input_data = (input.copy()).reshape(1, -1).T\n", " activation, raw_output = self.forward_propagation(input_data)\n", " self.back_propagation(activation, input_data, raw_output, expected_output)\n", "\n", " def predict(self, input):\n", " input_data = (input.copy()).reshape(-1, 1)\n", " activation, _ = self.forward_propagation(input_data)\n", " return activation[-1]\n" ], "id": "394f2d37d10b500e", "outputs": [], "execution_count": 108 }, { "metadata": {}, "cell_type": "markdown", "source": "## Zadanie 1\n", "id": "8338a244e10e008b" }, { "cell_type": "code", "id": "initial_id", "metadata": { "collapsed": true, "ExecuteTime": { "end_time": "2026-11-09T22:05:45.641840062Z", "start_time": "2026-11-09T22:04:49.513578355Z" } }, "source": [ "nn = NeuralNewtork(784, a=0.005)\n", "nn.add_layer(40, ReLu, -0.1, 0.1)\n", "nn.add_layer(10, None, -0.1, 0.1)\n", "\n", "train_image = load_mnist_images('train-images.idx3-ubyte')\n", "train_image_labels = load_mnist_labels('train-labels.idx1-ubyte')\n", "\n", "test_image = load_mnist_images('t10k-images.idx3-ubyte')\n", "test_image_labels = load_mnist_labels('t10k-labels.idx1-ubyte')\n", "\n", "epochs = 350\n", "n_train_samples = 1000\n", "\n", "print(\"Nauka z Dropout:\")\n", "for epoch in range(epochs):\n", " correct_train = 0\n", "\n", " for i in range(n_train_samples):\n", " x = train_image[i].reshape(-1, 1)\n", " label = train_image_labels[i]\n", " expected = to_one_hot(label)\n", "\n", " nn.dropout(x, expected)\n", "\n", " prediction = nn.predict(x)\n", " if np.argmax(prediction) == label:\n", " correct_train += 1\n", "\n", " train_acc = (correct_train / n_train_samples) * 100\n", " if (epoch + 1) % 10 == 0 or epoch == 0:\n", " print(f\"Epoka {epoch + 1}/{epochs} | Skutecznosc: {train_acc:.2f}%\")" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Nauka z Dropout:\n", "Epoka 1/350 | Skutecznosc: 20.80%\n", "Epoka 10/350 | Skutecznosc: 72.10%\n", "Epoka 20/350 | Skutecznosc: 77.30%\n", "Epoka 30/350 | Skutecznosc: 83.00%\n", "Epoka 40/350 | Skutecznosc: 85.10%\n", "Epoka 50/350 | Skutecznosc: 87.60%\n", "Epoka 60/350 | Skutecznosc: 89.70%\n", "Epoka 70/350 | Skutecznosc: 90.60%\n", "Epoka 80/350 | Skutecznosc: 91.70%\n", "Epoka 90/350 | Skutecznosc: 92.40%\n", "Epoka 100/350 | Skutecznosc: 93.20%\n", "Epoka 110/350 | Skutecznosc: 93.80%\n", "Epoka 120/350 | Skutecznosc: 93.70%\n", "Epoka 130/350 | Skutecznosc: 95.00%\n", "Epoka 140/350 | Skutecznosc: 95.40%\n", "Epoka 150/350 | Skutecznosc: 95.60%\n", "Epoka 160/350 | Skutecznosc: 95.80%\n", "Epoka 170/350 | Skutecznosc: 95.80%\n", "Epoka 180/350 | Skutecznosc: 96.20%\n", "Epoka 190/350 | Skutecznosc: 96.40%\n", "Epoka 200/350 | Skutecznosc: 96.60%\n", "Epoka 210/350 | Skutecznosc: 96.90%\n", "Epoka 220/350 | Skutecznosc: 96.90%\n", "Epoka 230/350 | Skutecznosc: 97.30%\n", "Epoka 240/350 | Skutecznosc: 97.20%\n", "Epoka 250/350 | Skutecznosc: 97.40%\n", "Epoka 260/350 | Skutecznosc: 97.60%\n", "Epoka 270/350 | Skutecznosc: 97.80%\n", "Epoka 280/350 | Skutecznosc: 97.70%\n", "Epoka 290/350 | Skutecznosc: 98.00%\n", "Epoka 300/350 | Skutecznosc: 97.90%\n", "Epoka 310/350 | Skutecznosc: 98.10%\n", "Epoka 320/350 | Skutecznosc: 97.90%\n", "Epoka 330/350 | Skutecznosc: 98.10%\n", "Epoka 340/350 | Skutecznosc: 97.90%\n", "Epoka 350/350 | Skutecznosc: 98.20%\n" ] } ], "execution_count": 109 }, { "metadata": { "ExecuteTime": { "end_time": "2026-11-09T22:05:45.801683169Z", "start_time": "2026-11-09T22:05:45.644344961Z" } }, "cell_type": "code", "source": [ "print(\"Sprawdzamy skutecznosc\")\n", "correct_train = 0\n", "\n", "for i in range(n_train_samples):\n", " x = test_image[i].reshape(-1, 1)\n", " label = test_image_labels[i]\n", " expected = to_one_hot(label)\n", "\n", " prediction = nn.predict(x)\n", "\n", " if np.argmax(prediction) == label:\n", " correct_train += 1\n", "\n", "train_acc = (correct_train / n_train_samples) * 100\n", "print(f\"Skutecznosc: {train_acc:.2f}%\")" ], "id": "97ae5a0126c2ef3f", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sprawdzamy skutecznosc\n", "Skutecznosc: 84.00%\n" ] } ], "execution_count": 110 }, { "metadata": {}, "cell_type": "markdown", "source": "## Zadanie 2\n", "id": "5dedb9abf3e6fb73" }, { "metadata": { "ExecuteTime": { "end_time": "2026-11-09T22:05:53.643833057Z", "start_time": "2026-11-09T22:05:45.804478234Z" } }, "cell_type": "code", "source": [ "nn = NeuralNewtork(784, a=0.1)\n", "nn.add_layer(40, ReLu, -0.1, 0.1)\n", "nn.add_layer(10, None, -0.1, 0.1)\n", "\n", "epochs = 350\n", "n_train_samples = 1000\n", "\n", "X_train = train_image[:n_train_samples]\n", "Y_train = np.zeros((n_train_samples, 10))\n", "for i in range(n_train_samples):\n", " Y_train[i, train_image_labels[i]] = 1.0\n", "\n", "print(\"Nauka z miniBatchGradientDescent:\")\n", "for epoch in range(epochs):\n", " nn.miniBatchGradientDescent(X_train, Y_train, n=100)\n", "\n", " correct_train = 0\n", " for i in range(n_train_samples):\n", " prediction = nn.predict(train_image[i])\n", " if np.argmax(prediction) == train_image_labels[i]:\n", " correct_train += 1\n", "\n", " train_acc = (correct_train / n_train_samples) * 100\n", " if (epoch + 1) % 10 == 0 or epoch == 0:\n", " print(f\"Epoka {epoch + 1}/{epochs} | Skutecznosc: {train_acc:.2f}%\")" ], "id": "6a52cd7422f24dea", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Nauka z miniBatchGradientDescent:\n", "Epoka 1/350 | Skutecznosc: 43.00%\n", "Epoka 10/350 | Skutecznosc: 78.30%\n", "Epoka 20/350 | Skutecznosc: 85.60%\n", "Epoka 30/350 | Skutecznosc: 89.00%\n", "Epoka 40/350 | Skutecznosc: 91.60%\n", "Epoka 50/350 | Skutecznosc: 93.10%\n", "Epoka 60/350 | Skutecznosc: 93.80%\n", "Epoka 70/350 | Skutecznosc: 94.40%\n", "Epoka 80/350 | Skutecznosc: 95.10%\n", "Epoka 90/350 | Skutecznosc: 95.40%\n", "Epoka 100/350 | Skutecznosc: 95.90%\n", "Epoka 110/350 | Skutecznosc: 96.60%\n", "Epoka 120/350 | Skutecznosc: 96.60%\n", "Epoka 130/350 | Skutecznosc: 96.70%\n", "Epoka 140/350 | Skutecznosc: 97.10%\n", "Epoka 150/350 | Skutecznosc: 97.10%\n", "Epoka 160/350 | Skutecznosc: 97.20%\n", "Epoka 170/350 | Skutecznosc: 97.60%\n", "Epoka 180/350 | Skutecznosc: 97.80%\n", "Epoka 190/350 | Skutecznosc: 98.10%\n", "Epoka 200/350 | Skutecznosc: 98.00%\n", "Epoka 210/350 | Skutecznosc: 98.50%\n", "Epoka 220/350 | Skutecznosc: 98.70%\n", "Epoka 230/350 | Skutecznosc: 98.60%\n", "Epoka 240/350 | Skutecznosc: 98.80%\n", "Epoka 250/350 | Skutecznosc: 98.50%\n", "Epoka 260/350 | Skutecznosc: 99.20%\n", "Epoka 270/350 | Skutecznosc: 99.10%\n", "Epoka 280/350 | Skutecznosc: 99.20%\n", "Epoka 290/350 | Skutecznosc: 99.20%\n", "Epoka 300/350 | Skutecznosc: 99.30%\n", "Epoka 310/350 | Skutecznosc: 99.30%\n", "Epoka 320/350 | Skutecznosc: 99.30%\n", "Epoka 330/350 | Skutecznosc: 99.30%\n", "Epoka 340/350 | Skutecznosc: 99.40%\n", "Epoka 350/350 | Skutecznosc: 99.40%\n" ] } ], "execution_count": 111 }, { "metadata": { "ExecuteTime": { "end_time": "2026-11-09T22:08:43.069226299Z", "start_time": "2026-11-09T22:08:43.001351653Z" } }, "cell_type": "code", "source": [ "print(\"Sprawdzamy skutecznosc\")\n", "correct_train = 0\n", "\n", "for i in range(n_train_samples):\n", " x = test_image[i].reshape(-1, 1)\n", " label = test_image_labels[i]\n", " expected = to_one_hot(label)\n", "\n", " prediction = nn.predict(x)\n", "\n", " if np.argmax(prediction) == label:\n", " correct_train += 1\n", "\n", "train_acc = (correct_train / n_train_samples) * 100\n", "print(f\"Skutecznosc: {train_acc:.2f}%\")" ], "id": "c895ea161a612627", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sprawdzamy skutecznosc\n", "Skutecznosc: 9.90%\n" ] } ], "execution_count": 128 }, { "metadata": {}, "cell_type": "markdown", "source": "## Zadanie 3\n", "id": "406348aaf794ae" }, { "metadata": {}, "cell_type": "markdown", "source": "### a) Tangens Hipreboliczny:", "id": "dc984f165eaa6ed" }, { "metadata": { "ExecuteTime": { "end_time": "2026-11-09T22:06:07.914731874Z", "start_time": "2026-11-09T22:05:53.769525789Z" } }, "cell_type": "code", "source": [ "nn = NeuralNewtork(784, a=0.02)\n", "nn.add_layer(100, tangens_hip, -0.01, 0.01)\n", "nn.add_layer(10, None, -0.1, 0.1)\n", "\n", "epochs = 350\n", "n_train_samples = 1000\n", "\n", "X_train = train_image[:n_train_samples]\n", "Y_train = np.zeros((n_train_samples, 10))\n", "for i in range(n_train_samples):\n", " Y_train[i, train_image_labels[i]] = 1.0\n", "\n", "print(\"Nauka z miniBatchGradientDescent: (Tangens hiperboliczny)\")\n", "for epoch in range(epochs):\n", " nn.miniBatchGradientDescent(X_train, Y_train, n=100)\n", "\n", " correct_train = 0\n", " for i in range(n_train_samples):\n", " prediction = nn.predict(train_image[i])\n", " if np.argmax(prediction) == train_image_labels[i]:\n", " correct_train += 1\n", "\n", " train_acc = (correct_train / n_train_samples) * 100\n", " if (epoch + 1) % 10 == 0 or epoch == 0:\n", " print(f\"Epoka {epoch + 1}/{epochs} | Skutecznosc: {train_acc:.2f}%\")" ], "id": "b326f81a8fa25dad", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Nauka z miniBatchGradientDescent: (Tangens hiperboliczny)\n", "Epoka 1/350 | Skutecznosc: 59.80%\n", "Epoka 10/350 | Skutecznosc: 81.20%\n", "Epoka 20/350 | Skutecznosc: 84.50%\n", "Epoka 30/350 | Skutecznosc: 86.20%\n", "Epoka 40/350 | Skutecznosc: 87.30%\n", "Epoka 50/350 | Skutecznosc: 88.00%\n", "Epoka 60/350 | Skutecznosc: 89.10%\n", "Epoka 70/350 | Skutecznosc: 89.50%\n", "Epoka 80/350 | Skutecznosc: 90.10%\n", "Epoka 90/350 | Skutecznosc: 90.60%\n", "Epoka 100/350 | Skutecznosc: 90.90%\n", "Epoka 110/350 | Skutecznosc: 90.50%\n", "Epoka 120/350 | Skutecznosc: 91.00%\n", "Epoka 130/350 | Skutecznosc: 91.80%\n", "Epoka 140/350 | Skutecznosc: 92.10%\n", "Epoka 150/350 | Skutecznosc: 92.10%\n", "Epoka 160/350 | Skutecznosc: 92.50%\n", "Epoka 170/350 | Skutecznosc: 92.70%\n", "Epoka 180/350 | Skutecznosc: 92.90%\n", "Epoka 190/350 | Skutecznosc: 93.20%\n", "Epoka 200/350 | Skutecznosc: 93.50%\n", "Epoka 210/350 | Skutecznosc: 93.30%\n", "Epoka 220/350 | Skutecznosc: 93.70%\n", "Epoka 230/350 | Skutecznosc: 93.60%\n", "Epoka 240/350 | Skutecznosc: 93.70%\n", "Epoka 250/350 | Skutecznosc: 93.80%\n", "Epoka 260/350 | Skutecznosc: 94.00%\n", "Epoka 270/350 | Skutecznosc: 94.40%\n", "Epoka 280/350 | Skutecznosc: 94.50%\n", "Epoka 290/350 | Skutecznosc: 94.50%\n", "Epoka 300/350 | Skutecznosc: 94.40%\n", "Epoka 310/350 | Skutecznosc: 94.90%\n", "Epoka 320/350 | Skutecznosc: 94.90%\n", "Epoka 330/350 | Skutecznosc: 95.30%\n", "Epoka 340/350 | Skutecznosc: 95.30%\n", "Epoka 350/350 | Skutecznosc: 95.20%\n" ] } ], "execution_count": 113 }, { "metadata": { "ExecuteTime": { "end_time": "2026-11-09T22:06:08.030264881Z", "start_time": "2026-11-09T22:06:07.917035085Z" } }, "cell_type": "code", "source": [ "print(\"Sprawdzamy skutecznosc (Hipreboliczny)\")\n", "correct_train = 0\n", "\n", "for i in range(n_train_samples):\n", " x = test_image[i].reshape(-1, 1)\n", " label = test_image_labels[i]\n", " expected = to_one_hot(label)\n", "\n", " prediction = nn.predict(x)\n", "\n", " if np.argmax(prediction) == label:\n", " correct_train += 1\n", "\n", "train_acc = (correct_train / n_train_samples) * 100\n", "print(f\"Skutecznosc: {train_acc:.2f}%\")" ], "id": "802956bbd42c04f5", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sprawdzamy skutecznosc (Hipreboliczny)\n", "Skutecznosc: 76.00%\n" ] } ], "execution_count": 114 }, { "metadata": {}, "cell_type": "markdown", "source": "### b) Sigmoidalna:", "id": "a9e73283f38189" }, { "metadata": { "ExecuteTime": { "end_time": "2026-11-09T22:06:23.079766733Z", "start_time": "2026-11-09T22:06:08.056998719Z" } }, "cell_type": "code", "source": [ "nn = NeuralNewtork(784, a=0.01)\n", "nn.add_layer(100, sigmoid, -0.1, 0.1)\n", "nn.add_layer(10, None, -0.1, 0.1)\n", "\n", "epochs = 350\n", "n_train_samples = 1000\n", "\n", "X_train = train_image[:n_train_samples]\n", "Y_train = np.zeros((n_train_samples, 10))\n", "for i in range(n_train_samples):\n", " Y_train[i, train_image_labels[i]] = 1.0\n", "\n", "print(\"Nauka z miniBatchGradientDescent: (Tangens Sigmoidalna)\")\n", "for epoch in range(epochs):\n", " nn.miniBatchGradientDescent(X_train, Y_train, n=100)\n", "\n", " correct_train = 0\n", " for i in range(n_train_samples):\n", " prediction = nn.predict(train_image[i])\n", " if np.argmax(prediction) == train_image_labels[i]:\n", " correct_train += 1\n", "\n", " train_acc = (correct_train / n_train_samples) * 100\n", " if (epoch + 1) % 10 == 0 or epoch == 0:\n", " print(f\"Epoka {epoch + 1}/{epochs} | Skutecznosc: {train_acc:.2f}%\")" ], "id": "d02b5688f2c296cb", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Nauka z miniBatchGradientDescent: (Tangens Sigmoidalna)\n", "Epoka 1/350 | Skutecznosc: 12.30%\n", "Epoka 10/350 | Skutecznosc: 38.10%\n", "Epoka 20/350 | Skutecznosc: 52.40%\n", "Epoka 30/350 | Skutecznosc: 56.50%\n", "Epoka 40/350 | Skutecznosc: 61.20%\n", "Epoka 50/350 | Skutecznosc: 65.50%\n", "Epoka 60/350 | Skutecznosc: 62.00%\n", "Epoka 70/350 | Skutecznosc: 64.50%\n", "Epoka 80/350 | Skutecznosc: 63.60%\n", "Epoka 90/350 | Skutecznosc: 65.60%\n", "Epoka 100/350 | Skutecznosc: 63.00%\n", "Epoka 110/350 | Skutecznosc: 66.90%\n", "Epoka 120/350 | Skutecznosc: 67.20%\n", "Epoka 130/350 | Skutecznosc: 68.50%\n", "Epoka 140/350 | Skutecznosc: 70.40%\n", "Epoka 150/350 | Skutecznosc: 66.30%\n", "Epoka 160/350 | Skutecznosc: 68.60%\n", "Epoka 170/350 | Skutecznosc: 69.50%\n", "Epoka 180/350 | Skutecznosc: 70.30%\n", "Epoka 190/350 | Skutecznosc: 67.90%\n", "Epoka 200/350 | Skutecznosc: 69.40%\n", "Epoka 210/350 | Skutecznosc: 69.50%\n", "Epoka 220/350 | Skutecznosc: 68.30%\n", "Epoka 230/350 | Skutecznosc: 68.50%\n", "Epoka 240/350 | Skutecznosc: 69.60%\n", "Epoka 250/350 | Skutecznosc: 70.00%\n", "Epoka 260/350 | Skutecznosc: 69.00%\n", "Epoka 270/350 | Skutecznosc: 69.20%\n", "Epoka 280/350 | Skutecznosc: 70.90%\n", "Epoka 290/350 | Skutecznosc: 71.20%\n", "Epoka 300/350 | Skutecznosc: 70.50%\n", "Epoka 310/350 | Skutecznosc: 69.90%\n", "Epoka 320/350 | Skutecznosc: 69.90%\n", "Epoka 330/350 | Skutecznosc: 71.40%\n", "Epoka 340/350 | Skutecznosc: 72.00%\n", "Epoka 350/350 | Skutecznosc: 71.90%\n" ] } ], "execution_count": 115 }, { "metadata": { "ExecuteTime": { "end_time": "2026-11-09T22:06:23.176585148Z", "start_time": "2026-11-09T22:06:23.082613887Z" } }, "cell_type": "code", "source": [ "print(\"Sprawdzamy skutecznosc (Sigmoidalna)\")\n", "correct_train = 0\n", "\n", "for i in range(n_train_samples):\n", " x = test_image[i].reshape(-1, 1)\n", " label = test_image_labels[i]\n", " expected = to_one_hot(label)\n", "\n", " prediction = nn.predict(x)\n", "\n", " if np.argmax(prediction) == label:\n", " correct_train += 1\n", "\n", "train_acc = (correct_train / n_train_samples) * 100\n", "print(f\"Skutecznosc: {train_acc:.2f}%\")" ], "id": "97bdfb311beef8b", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sprawdzamy skutecznosc (Sigmoidalna)\n", "Skutecznosc: 65.30%\n" ] } ], "execution_count": 116 }, { "metadata": {}, "cell_type": "markdown", "source": "### c) Softmax:", "id": "3a651d88e1268c45" }, { "metadata": { "ExecuteTime": { "end_time": "2026-11-09T22:41:09.173072614Z", "start_time": "2026-11-09T22:41:04.716200045Z" } }, "cell_type": "code", "source": [ "nn = NeuralNewtork(784, a=0.02)\n", "nn.add_layer(100, ReLu, -0.1, 0.1)\n", "nn.add_layer(10, softmax, -0.1, 0.1)\n", "\n", "epochs = 350\n", "n_train_samples = 1000\n", "\n", "X_train = train_image[:n_train_samples]\n", "Y_train = np.zeros((n_train_samples, 10))\n", "for i in range(n_train_samples):\n", " Y_train[i, train_image_labels[i]] = 1.0\n", "\n", "print(\"Nauka z miniBatchGradientDescent: (Softmax:)\")\n", "for epoch in range(epochs):\n", " nn.miniBatchGradientDescent(X_train, Y_train, n=100)\n", "\n", " correct_train = 0\n", " for i in range(n_train_samples):\n", " prediction = nn.predict(train_image[i])\n", " if np.argmax(prediction) == train_image_labels[i]:\n", " correct_train += 1\n", "\n", " train_acc = (correct_train / n_train_samples) * 100\n", " if (epoch + 1) % 10 == 0 or epoch == 0:\n", " print(f\"Epoka {epoch + 1}/{epochs} | Skutecznosc: {train_acc:.2f}%\")" ], "id": "f286afb719a046b0", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Nauka z miniBatchGradientDescent: (Softmax:)\n", "Epoka 1/350 | Skutecznosc: 13.90%\n", "Epoka 10/350 | Skutecznosc: 14.40%\n", "Epoka 20/350 | Skutecznosc: 11.70%\n", "Epoka 30/350 | Skutecznosc: 11.70%\n", "Epoka 40/350 | Skutecznosc: 11.70%\n", "Epoka 50/350 | Skutecznosc: 11.70%\n", "Epoka 60/350 | Skutecznosc: 11.70%\n", "Epoka 70/350 | Skutecznosc: 11.70%\n", "Epoka 80/350 | Skutecznosc: 11.70%\n" ] }, { "ename": "KeyboardInterrupt", "evalue": "", "output_type": "error", "traceback": [ "\u001B[31m---------------------------------------------------------------------------\u001B[39m", "\u001B[31mKeyboardInterrupt\u001B[39m Traceback (most recent call last)", "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[129]\u001B[39m\u001B[32m, line 19\u001B[39m\n\u001B[32m 15\u001B[39m nn.miniBatchGradientDescent(X_train, Y_train, n=\u001B[32m100\u001B[39m)\n\u001B[32m 16\u001B[39m \n\u001B[32m 17\u001B[39m correct_train = \u001B[32m0\u001B[39m\n\u001B[32m 18\u001B[39m \u001B[38;5;28;01mfor\u001B[39;00m i \u001B[38;5;28;01min\u001B[39;00m range(n_train_samples):\n\u001B[32m---> \u001B[39m\u001B[32m19\u001B[39m prediction = nn.predict(train_image[i])\n\u001B[32m 20\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m np.argmax(prediction) == train_image_labels[i]:\n\u001B[32m 21\u001B[39m correct_train += \u001B[32m1\u001B[39m\n\u001B[32m 22\u001B[39m \n", "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[108]\u001B[39m\u001B[32m, line 224\u001B[39m, in \u001B[36mNeuralNewtork.predict\u001B[39m\u001B[34m(self, input)\u001B[39m\n\u001B[32m 222\u001B[39m \u001B[38;5;28;01mdef\u001B[39;00m predict(self, input):\n\u001B[32m 223\u001B[39m input_data = (input.copy()).reshape(-\u001B[32m1\u001B[39m, \u001B[32m1\u001B[39m)\n\u001B[32m--> \u001B[39m\u001B[32m224\u001B[39m activation, _ = self.forward_propagation(input_data)\n\u001B[32m 225\u001B[39m \u001B[38;5;28;01mreturn\u001B[39;00m activation[-\u001B[32m1\u001B[39m]\n", "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[108]\u001B[39m\u001B[32m, line 90\u001B[39m, in \u001B[36mNeuralNewtork.forward_propagation\u001B[39m\u001B[34m(self, input)\u001B[39m\n\u001B[32m 86\u001B[39m raw_output = []\n\u001B[32m 87\u001B[39m \n\u001B[32m 88\u001B[39m current_input = input\n\u001B[32m 89\u001B[39m \u001B[38;5;28;01mfor\u001B[39;00m i \u001B[38;5;28;01min\u001B[39;00m range(len(self.layers[\u001B[33m'weights'\u001B[39m])):\n\u001B[32m---> \u001B[39m\u001B[32m90\u001B[39m z = np.dot(self.layers[\u001B[33m'weights'\u001B[39m][i], current_input)\n\u001B[32m 91\u001B[39m raw_output.append(z)\n\u001B[32m 92\u001B[39m \n\u001B[32m 93\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m self.layers[\u001B[33m'activation_function'\u001B[39m][i] \u001B[38;5;28;01mis\u001B[39;00m \u001B[38;5;28;01mnot\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m:\n", "\u001B[31mKeyboardInterrupt\u001B[39m: " ] } ], "execution_count": 129 }, { "metadata": { "ExecuteTime": { "end_time": "2026-11-09T22:06:40.771702833Z", "start_time": "2026-11-09T22:06:40.613076766Z" } }, "cell_type": "code", "source": [ "correct_train = 0\n", "\n", "for i in range(n_train_samples):\n", " x = test_image[i].reshape(-1, 1)\n", " label = test_image_labels[i]\n", " expected = to_one_hot(label)\n", "\n", " prediction = nn.predict(x)\n", "\n", " if np.argmax(prediction) == label:\n", " correct_train += 1\n", "\n", "train_acc = (correct_train / n_train_samples) * 100\n", "print(f\"Skutecznosc: {train_acc:.2f}%\")" ], "id": "dbe2b39d2226ed3a", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Skutecznosc: 9.90%\n" ] } ], "execution_count": 118 } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }