367 lines
10 KiB
Plaintext
367 lines
10 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "e240313e75a92965",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Laboratorium 2"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "bd24186044d05b5c",
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2026-10-14T09:25:08.681806831Z",
|
|
"start_time": "2026-10-14T09:25:08.610679950Z"
|
|
}
|
|
},
|
|
"source": [
|
|
"## Zadanie 1"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"id": "24c6b783334b0556",
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2026-11-04T13:37:32.519912534Z",
|
|
"start_time": "2026-11-04T13:37:32.466261885Z"
|
|
}
|
|
},
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"weight_value = np.array(0.5)\n",
|
|
"expected_value = np.array(0.8)\n",
|
|
"input = np.array(2)\n",
|
|
"alpha = 0.1\n",
|
|
"n = 1\n",
|
|
"\n",
|
|
"def error_prediction_n_epoch(weight_value, expected_value, input, alpha, n, x):\n",
|
|
" for i in range(x):\n",
|
|
" prediction = weight_value * input\n",
|
|
" delta = (np.outer(((2 / n) * (prediction - expected_value)),\n",
|
|
" input))\n",
|
|
" weight_value = weight_value - (alpha * delta)\n",
|
|
" return prediction, delta"
|
|
],
|
|
"outputs": [],
|
|
"execution_count": 1
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"id": "fbbac2e949ca045d",
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2026-11-04T13:37:32.584393763Z",
|
|
"start_time": "2026-11-04T13:37:32.523292517Z"
|
|
}
|
|
},
|
|
"source": [
|
|
"pred_5, err_5 = error_prediction_n_epoch(weight_value, expected_value, input, alpha, n, 5)\n",
|
|
"print(f\"Wynik dla 5 epok: {pred_5.item():.5f}, Błąd: {err_5.item():.10f}\\n\", )"
|
|
],
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Wynik dla 5 epok: 0.80032, Błąd: 0.0012800000\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"execution_count": 2
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"id": "9d6d0aec86b777ae",
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2026-11-04T13:37:32.634140435Z",
|
|
"start_time": "2026-11-04T13:37:32.591494653Z"
|
|
}
|
|
},
|
|
"source": [
|
|
"pred_20, err_20 = error_prediction_n_epoch(weight_value, expected_value, input, alpha, n, 20)\n",
|
|
"print(f\"Wynik dla 20 epok: {pred_20.item():.5f}, Błąd: {err_20.item():.10f}\\n\", )"
|
|
],
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Wynik dla 20 epok: 0.80000, Błąd: 0.0000000000\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"execution_count": 3
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"id": "62a29a8fa0d41975",
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2026-11-04T13:37:32.687186787Z",
|
|
"start_time": "2026-11-04T13:37:32.636238862Z"
|
|
}
|
|
},
|
|
"source": [
|
|
"new_alpha = 1\n",
|
|
"pred_alp_1, err_alp_1 = error_prediction_n_epoch(weight_value, expected_value, input, alpha, n, 5)\n",
|
|
"print(f\"Wynik alpha = 1: {pred_alp_1.item():.5f}, Błąd: {err_alp_1.item():.10f}\\n\", )\n",
|
|
"# Nigdy nei osiagnie celu przez zbyt wysoka alphe przez co przeskakujemy wartosc najblizsza."
|
|
],
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Wynik alpha = 1: 0.80032, Błąd: 0.0012800000\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"execution_count": 4
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"id": "5f47e48f0964e2d3",
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2026-11-04T13:37:32.781481476Z",
|
|
"start_time": "2026-11-04T13:37:32.689353042Z"
|
|
}
|
|
},
|
|
"source": [
|
|
"new_input = np.array(0.1)\n",
|
|
"pred_alp_1, err_alp_1 = error_prediction_n_epoch(weight_value, expected_value, new_input, alpha, n, 20000)\n",
|
|
"print(f\"Wynik input = 0.1: {pred_alp_1.item():.5f}, Błąd: {err_alp_1.item():.10f}\\n\", )\n",
|
|
"# Osiagnie cel dopiero po 20k epok."
|
|
],
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Wynik input = 0.1: 0.80000, Błąd: -0.0000000000\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"execution_count": 5
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "73b83e4e6fc78d91",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Zadanie 2"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"id": "ed6809cca4d96211",
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2026-11-04T13:37:32.839400273Z",
|
|
"start_time": "2026-11-04T13:37:32.786766917Z"
|
|
}
|
|
},
|
|
"source": [
|
|
"input_data = np.array([\n",
|
|
" [0.5, 0.1, 0.2, 0.8],\n",
|
|
" [0.75, 0.3, 0.1, 0.9],\n",
|
|
" [0.1, 0.7, 0.6, 0.2]\n",
|
|
"])\n",
|
|
"expected_results = np.array([\n",
|
|
" [0.1, 0.5, 0.1, 0.7],\n",
|
|
" [1.0, 0.2, 0.3, 0.6],\n",
|
|
" [0.1, -0.5, 0.2, 0.2],\n",
|
|
" [0.0, 0.3, 0.9, -0.1],\n",
|
|
" [-0.1, 0.7, 0.1, 0.8]\n",
|
|
"])\n",
|
|
"weights = np.array([\n",
|
|
" [0.1, 0.1, -0.3],\n",
|
|
" [0.1, 0.2, 0.0],\n",
|
|
" [0.0, 0.7, 0.1],\n",
|
|
" [0.2, 0.4, 0.0],\n",
|
|
" [-0.3, 0.5, 0.1]\n",
|
|
"])\n",
|
|
"epochs = 1000\n",
|
|
"alpha = 0.01\n",
|
|
"\n",
|
|
"\n",
|
|
"def neuron_network_learning_per_series(weights, input_data, expected_results, alpha, epochs):\n",
|
|
" w = weights.copy()\n",
|
|
" num_series = input_data.shape[1]\n",
|
|
" num_neurons = expected_results.shape[0]\n",
|
|
"\n",
|
|
" for epoch in range(epochs):\n",
|
|
" epoch_error = 0\n",
|
|
" for i in range(num_series):\n",
|
|
" x_i = input_data[:, i:i + 1]\n",
|
|
" y_i = expected_results[:, i:i + 1]\n",
|
|
" pred_i = np.dot(w, x_i)\n",
|
|
" error_i = (1 / num_neurons) * np.sum((pred_i - y_i) ** 2)\n",
|
|
" epoch_error += error_i\n",
|
|
" delta_i = (2 / num_neurons) * np.dot((pred_i - y_i), x_i.T)\n",
|
|
" w = w - alpha * delta_i\n",
|
|
" # print(f\"\\nEpoch {epoch + 1}:\")\n",
|
|
" # print(f\"Serie: {i + 1}:\\n\")\n",
|
|
" # print(\"\\nOutput: \\n\", pred_i)\n",
|
|
" # print(\"\\nWeights: \\n\", w)\n",
|
|
" # print(f\"\\nError: {error_i:.6f}\\n\")\n",
|
|
" return epoch_error"
|
|
],
|
|
"outputs": [],
|
|
"execution_count": 6
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"id": "298d88832156e7e3",
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2026-11-04T13:37:32.930222326Z",
|
|
"start_time": "2026-11-04T13:37:32.841342726Z"
|
|
}
|
|
},
|
|
"source": "epoch_err = neuron_network_learning_per_series(weights, input_data, expected_results, alpha, epochs)",
|
|
"outputs": [],
|
|
"execution_count": 7
|
|
},
|
|
{
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2026-11-04T13:37:32.982286508Z",
|
|
"start_time": "2026-11-04T13:37:32.932885017Z"
|
|
}
|
|
},
|
|
"cell_type": "code",
|
|
"source": "print(f\"Blad ostatniej epoki: {epoch_err:.6f}\\n\")",
|
|
"id": "b031dd3370566a75",
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Blad ostatniej epoki: 0.258218\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"execution_count": 8
|
|
},
|
|
{
|
|
"metadata": {},
|
|
"cell_type": "markdown",
|
|
"source": "## Zadanie 3",
|
|
"id": "b010bf3ee2e61ff0"
|
|
},
|
|
{
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2026-11-04T13:37:33.122170592Z",
|
|
"start_time": "2026-11-04T13:37:32.985840238Z"
|
|
}
|
|
},
|
|
"cell_type": "code",
|
|
"source": [
|
|
"w = np.random.uniform(-1, 1, (4, 3))\n",
|
|
"data = np.loadtxt('training_colors.txt')\n",
|
|
"\n",
|
|
"input = data[:, :3]\n",
|
|
"input_info = data[:, 3]\n",
|
|
"\n",
|
|
"goal = np.zeros((1, 4))\n",
|
|
"\n",
|
|
"color_map = {\n",
|
|
" 1: [1.0, 0.0, 0.0, 0.0], # Czerwony\n",
|
|
" 2: [0.0, 1.0, 0.0, 0.0], # Zielony\n",
|
|
" 3: [0.0, 0.0, 1.0, 0.0], # Niebieski\n",
|
|
" 4: [0.0, 0.0, 0.0, 1.0], # Żółty\n",
|
|
"}\n",
|
|
"\n",
|
|
"goal_list = [color_map[int(label)] for label in input_info]\n",
|
|
"goal = np.array(goal_list)\n",
|
|
"\n",
|
|
"goal = goal.T\n",
|
|
"input = input.T\n",
|
|
"alpha = 0.01\n",
|
|
"epochs = 100\n",
|
|
"\n",
|
|
"\n",
|
|
"def neuron_network_learning_per_series(weights, input_data, expected_results, alpha, epochs):\n",
|
|
" w = weights.copy()\n",
|
|
" num_series = input_data.shape[1]\n",
|
|
" num_neurons = expected_results.shape[0]\n",
|
|
"\n",
|
|
" for epoch in range(epochs):\n",
|
|
" epoch_error = 0\n",
|
|
" for i in range(num_series):\n",
|
|
" x_i = input_data[:, i:i + 1]\n",
|
|
" y_i = expected_results[:, i:i + 1]\n",
|
|
" pred_i = np.dot(w, x_i)\n",
|
|
" error_i = (1 / num_neurons) * np.sum((pred_i - y_i) ** 2)\n",
|
|
" epoch_error += error_i\n",
|
|
" delta_i = (2 / num_neurons) * np.dot((pred_i - y_i), x_i.T)\n",
|
|
" w = w - alpha * delta_i\n",
|
|
" return w\n",
|
|
"\n",
|
|
"w = neuron_network_learning_per_series(w, input, goal, alpha, epochs)\n",
|
|
"\n",
|
|
"test_data = np.loadtxt('test_colors.txt')\n",
|
|
"X_test = test_data[:, :3].T\n",
|
|
"y_test_info = test_data[:, 3]\n",
|
|
"\n",
|
|
"predictions_test = np.dot(w, X_test)\n",
|
|
"\n",
|
|
"y_pred_test = np.argmax(predictions_test, axis=0) + 1\n",
|
|
"\n",
|
|
"correct_predictions = np.sum(y_pred_test == y_test_info)\n",
|
|
"total_samples = len(y_test_info)\n",
|
|
"accuracy = (correct_predictions / total_samples) * 100.0\n",
|
|
"\n",
|
|
"print(f\"Poprawnie rozpoznane na zbiorze testowym: {correct_predictions} / {total_samples}\")\n",
|
|
"print(f\"Skuteczność sieci: {accuracy:.2f}%\")\n"
|
|
],
|
|
"id": "123e2ae3e3e2ebed",
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Poprawnie rozpoznane na zbiorze testowym: 130 / 130\n",
|
|
"Skuteczność sieci: 100.00%\n"
|
|
]
|
|
}
|
|
],
|
|
"execution_count": 9
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"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.13.13"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|