Files
PSO/xor.ipynb
jung-geun 91c6ec965b 23-05-29
EBPSO 알고리즘 구현 - 선택지로 추가
random 으로 분산시키는 방법 구현 - 선택지로 추가
iris 기준 98퍼센트로 나오나 정확한 결과를 지켜봐야 할것으로 보임
2023-05-29 04:01:48 +09:00

1651 lines
42 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2023-05-26 10:26:11.173286: E tensorflow/stream_executor/cuda/cuda_blas.cc:2981] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n",
"/home/pieroot/miniconda3/envs/pso/lib/python3.8/site-packages/cupy/_environment.py:445: UserWarning: \n",
"--------------------------------------------------------------------------------\n",
"\n",
" CuPy may not function correctly because multiple CuPy packages are installed\n",
" in your environment:\n",
"\n",
" cupy, cupy-cuda11x\n",
"\n",
" Follow these steps to resolve this issue:\n",
"\n",
" 1. For all packages listed above, run the following command to remove all\n",
" existing CuPy installations:\n",
"\n",
" $ pip uninstall <package_name>\n",
"\n",
" If you previously installed CuPy via conda, also run the following:\n",
"\n",
" $ conda uninstall cupy\n",
"\n",
" 2. Install the appropriate CuPy package.\n",
" Refer to the Installation Guide for detailed instructions.\n",
"\n",
" https://docs.cupy.dev/en/stable/install.html\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\n",
" warnings.warn(f'''\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.10.0\n",
"[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'), PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]\n"
]
}
],
"source": [
"import os\n",
"os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'\n",
"\n",
"import tensorflow as tf\n",
"tf.random.set_seed(777) # for reproducibility\n",
"\n",
"# from pso_tf import PSO\n",
"from pso import Optimizer\n",
"from tensorflow import keras\n",
"\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from tqdm import tqdm\n",
"\n",
"from tensorflow import keras\n",
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras import layers\n",
"\n",
"from datetime import datetime\n",
"\n",
"import json\n",
"\n",
"print(tf.__version__)\n",
"print(tf.config.list_physical_devices())\n",
"\n",
"def get_data():\n",
" x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])\n",
" y = np.array([[0], [1], [1], [0]])\n",
" return x, y\n",
"\n",
"def make_model():\n",
" leyer = []\n",
" leyer.append(layers.Dense(2, activation='sigmoid', input_shape=(2,)))\n",
" leyer.append(layers.Dense(1, activation='sigmoid'))\n",
"\n",
" model = Sequential(leyer)\n",
"\n",
" return model"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 0/99: 4%|4 | 4/100 [00:00<00:18, 5.11it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING:tensorflow:5 out of the last 5 calls to <function Model.make_test_function.<locals>.test_function at 0x7f8a2a586e50> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has reduce_retracing=True option that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 0/99: 5%|5 | 5/100 [00:01<00:15, 6.06it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING:tensorflow:6 out of the last 6 calls to <function Model.make_test_function.<locals>.test_function at 0x7f8a2a509280> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has reduce_retracing=True option that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 0/99: 100%|##########| 100/100 [00:12<00:00, 8.33it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.707333293557167 | acc avg : 0.5 | Best score : 0.6307240724563599\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 1/99: 100%|##########| 100/100 [00:03<00:00, 30.91it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.7328412693738937 | acc avg : 0.505 | Best score : 0.6244710087776184\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 2/99: 100%|##########| 100/100 [00:03<00:00, 30.94it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.7291719603538513 | acc avg : 0.5075 | Best score : 0.621765673160553\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 3/99: 100%|##########| 100/100 [00:03<00:00, 30.73it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5099389508366585 | acc avg : 0.7175 | Best score : 0.3762193024158478\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 4/99: 100%|##########| 100/100 [00:03<00:00, 31.01it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.4717184057831764 | acc avg : 0.7525 | Best score : 0.37326303124427795\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 5/99: 100%|##########| 100/100 [00:03<00:00, 31.35it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5643444469571114 | acc avg : 0.675 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 6/99: 100%|##########| 100/100 [00:03<00:00, 31.47it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.6967811548709869 | acc avg : 0.605 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 7/99: 100%|##########| 100/100 [00:03<00:00, 31.09it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.7016823583841324 | acc avg : 0.6225 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 8/99: 100%|##########| 100/100 [00:03<00:00, 31.32it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5891013583540916 | acc avg : 0.7 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 9/99: 100%|##########| 100/100 [00:03<00:00, 26.91it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5186755350232124 | acc avg : 0.7375 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 10/99: 100%|##########| 100/100 [00:03<00:00, 30.86it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5601680752635002 | acc avg : 0.7075 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 11/99: 100%|##########| 100/100 [00:03<00:00, 31.87it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.6089285349845887 | acc avg : 0.69 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 12/99: 100%|##########| 100/100 [00:03<00:00, 31.42it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.621009130179882 | acc avg : 0.685 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 13/99: 100%|##########| 100/100 [00:03<00:00, 31.64it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.601193311214447 | acc avg : 0.7 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 14/99: 100%|##########| 100/100 [00:03<00:00, 31.60it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.582365850508213 | acc avg : 0.7125 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 15/99: 100%|##########| 100/100 [00:03<00:00, 31.50it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5699197337031364 | acc avg : 0.72 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 16/99: 100%|##########| 100/100 [00:03<00:00, 31.53it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5882085087895393 | acc avg : 0.695 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 17/99: 100%|##########| 100/100 [00:03<00:00, 30.72it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.588711973130703 | acc avg : 0.71 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 18/99: 100%|##########| 100/100 [00:03<00:00, 31.07it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.6058803015947342 | acc avg : 0.715 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 19/99: 100%|##########| 100/100 [00:03<00:00, 31.21it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5897892582416534 | acc avg : 0.715 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 20/99: 100%|##########| 100/100 [00:03<00:00, 25.47it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5821597665548325 | acc avg : 0.73 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 21/99: 100%|##########| 100/100 [00:03<00:00, 29.90it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5819245061278343 | acc avg : 0.7425 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 22/99: 100%|##########| 100/100 [00:03<00:00, 31.59it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5784307581186294 | acc avg : 0.7425 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 23/99: 100%|##########| 100/100 [00:03<00:00, 32.03it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5862669295072556 | acc avg : 0.7275 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 24/99: 100%|##########| 100/100 [00:03<00:00, 31.06it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5841098502278328 | acc avg : 0.73 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 25/99: 100%|##########| 100/100 [00:03<00:00, 31.17it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.589279696047306 | acc avg : 0.7325 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 26/99: 100%|##########| 100/100 [00:03<00:00, 31.84it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5820297047495842 | acc avg : 0.7325 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 27/99: 100%|##########| 100/100 [00:03<00:00, 31.29it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5813658729195594 | acc avg : 0.7325 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 28/99: 100%|##########| 100/100 [00:03<00:00, 31.12it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5836457046866417 | acc avg : 0.7425 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 29/99: 100%|##########| 100/100 [00:03<00:00, 31.67it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5902055448293686 | acc avg : 0.7375 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 30/99: 100%|##########| 100/100 [00:03<00:00, 29.97it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.6035681679844856 | acc avg : 0.715 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 31/99: 100%|##########| 100/100 [00:04<00:00, 24.32it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.6004572728276253 | acc avg : 0.715 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 32/99: 100%|##########| 100/100 [00:03<00:00, 30.03it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5800464290380478 | acc avg : 0.74 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 33/99: 100%|##########| 100/100 [00:03<00:00, 31.34it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5788086211681366 | acc avg : 0.74 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 34/99: 100%|##########| 100/100 [00:03<00:00, 30.63it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5828433361649513 | acc avg : 0.735 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 35/99: 100%|##########| 100/100 [00:03<00:00, 31.56it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5914300563931465 | acc avg : 0.72 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 36/99: 100%|##########| 100/100 [00:03<00:00, 31.02it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5877807715535164 | acc avg : 0.7375 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 37/99: 100%|##########| 100/100 [00:03<00:00, 31.83it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5808902844786644 | acc avg : 0.7275 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 38/99: 100%|##########| 100/100 [00:03<00:00, 31.48it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5850541380047798 | acc avg : 0.725 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 39/99: 100%|##########| 100/100 [00:03<00:00, 31.54it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5824474242329597 | acc avg : 0.7375 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 40/99: 100%|##########| 100/100 [00:03<00:00, 31.48it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5857477381825447 | acc avg : 0.7325 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 41/99: 100%|##########| 100/100 [00:03<00:00, 31.43it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5872031468153 | acc avg : 0.73 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 42/99: 100%|##########| 100/100 [00:03<00:00, 25.95it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5911645150184631 | acc avg : 0.7275 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 43/99: 100%|##########| 100/100 [00:03<00:00, 30.14it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5885934352874755 | acc avg : 0.735 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 44/99: 100%|##########| 100/100 [00:03<00:00, 31.60it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5766231667995453 | acc avg : 0.7375 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 45/99: 100%|##########| 100/100 [00:03<00:00, 31.48it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5796105325222015 | acc avg : 0.7275 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 46/99: 100%|##########| 100/100 [00:03<00:00, 31.67it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5890544068813324 | acc avg : 0.7325 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 47/99: 100%|##########| 100/100 [00:03<00:00, 31.54it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5860040760040284 | acc avg : 0.7425 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 48/99: 100%|##########| 100/100 [00:03<00:00, 31.59it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5852086874842644 | acc avg : 0.7325 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 49/99: 100%|##########| 100/100 [00:03<00:00, 31.71it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5891327604651451 | acc avg : 0.73 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 50/99: 100%|##########| 100/100 [00:03<00:00, 31.47it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5878473871946335 | acc avg : 0.735 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 51/99: 100%|##########| 100/100 [00:03<00:00, 31.59it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5877139037847519 | acc avg : 0.7425 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 52/99: 100%|##########| 100/100 [00:03<00:00, 31.53it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5883922311663627 | acc avg : 0.7375 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 53/99: 100%|##########| 100/100 [00:03<00:00, 25.49it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5879773423075676 | acc avg : 0.7375 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 54/99: 100%|##########| 100/100 [00:03<00:00, 30.00it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5927090826630592 | acc avg : 0.7325 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 55/99: 100%|##########| 100/100 [00:03<00:00, 31.61it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.590822865664959 | acc avg : 0.74 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 56/99: 100%|##########| 100/100 [00:03<00:00, 31.29it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5883511942625046 | acc avg : 0.7325 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 57/99: 100%|##########| 100/100 [00:03<00:00, 31.66it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.584270852804184 | acc avg : 0.7375 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 58/99: 100%|##########| 100/100 [00:03<00:00, 31.81it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.584944163262844 | acc avg : 0.73 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 59/99: 100%|##########| 100/100 [00:03<00:00, 31.22it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5890933072566986 | acc avg : 0.72 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 60/99: 100%|##########| 100/100 [00:03<00:00, 31.70it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.59233806848526 | acc avg : 0.73 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 61/99: 100%|##########| 100/100 [00:03<00:00, 31.13it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5855201661586762 | acc avg : 0.735 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 62/99: 100%|##########| 100/100 [00:03<00:00, 31.50it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5888289919495583 | acc avg : 0.7375 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 63/99: 100%|##########| 100/100 [00:03<00:00, 31.55it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5848286512494087 | acc avg : 0.735 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 64/99: 100%|##########| 100/100 [00:03<00:00, 25.72it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5835971942543984 | acc avg : 0.74 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 65/99: 100%|##########| 100/100 [00:03<00:00, 30.24it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.58549885481596 | acc avg : 0.7275 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 66/99: 100%|##########| 100/100 [00:03<00:00, 31.78it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5821312037110329 | acc avg : 0.7375 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 67/99: 100%|##########| 100/100 [00:03<00:00, 31.46it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5888780787587166 | acc avg : 0.735 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 68/99: 100%|##########| 100/100 [00:03<00:00, 31.63it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5965503272414208 | acc avg : 0.7275 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 69/99: 100%|##########| 100/100 [00:03<00:00, 31.32it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5966133666038513 | acc avg : 0.725 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 70/99: 100%|##########| 100/100 [00:03<00:00, 31.51it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5885627806186676 | acc avg : 0.735 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 71/99: 100%|##########| 100/100 [00:03<00:00, 31.42it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5855536741018296 | acc avg : 0.7325 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 72/99: 100%|##########| 100/100 [00:03<00:00, 31.97it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5875397002696991 | acc avg : 0.7375 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 73/99: 100%|##########| 100/100 [00:03<00:00, 31.62it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5828473618626595 | acc avg : 0.7425 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 74/99: 100%|##########| 100/100 [00:03<00:00, 31.45it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5906080171465874 | acc avg : 0.74 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 75/99: 100%|##########| 100/100 [00:03<00:00, 25.97it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.584847458600998 | acc avg : 0.7325 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 76/99: 100%|##########| 100/100 [00:03<00:00, 30.39it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.591842500269413 | acc avg : 0.7375 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 77/99: 100%|##########| 100/100 [00:03<00:00, 31.56it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5940096437931061 | acc avg : 0.7325 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 78/99: 100%|##########| 100/100 [00:03<00:00, 31.84it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5915093126893044 | acc avg : 0.735 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 79/99: 100%|##########| 100/100 [00:03<00:00, 31.88it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5805989000201225 | acc avg : 0.7425 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 80/99: 100%|##########| 100/100 [00:03<00:00, 31.87it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5855838099122047 | acc avg : 0.7275 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 81/99: 100%|##########| 100/100 [00:03<00:00, 31.77it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5896048584580421 | acc avg : 0.7325 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 82/99: 100%|##########| 100/100 [00:03<00:00, 31.78it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5899882999062538 | acc avg : 0.735 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 83/99: 100%|##########| 100/100 [00:03<00:00, 31.88it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.589127992093563 | acc avg : 0.7375 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 84/99: 100%|##########| 100/100 [00:03<00:00, 31.76it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5816178262233734 | acc avg : 0.7325 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 85/99: 100%|##########| 100/100 [00:03<00:00, 31.89it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5880844354629516 | acc avg : 0.74 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 86/99: 100%|##########| 100/100 [00:03<00:00, 25.91it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5908357509970665 | acc avg : 0.73 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 87/99: 100%|##########| 100/100 [00:03<00:00, 30.36it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5950687676668167 | acc avg : 0.72 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 88/99: 100%|##########| 100/100 [00:03<00:00, 31.64it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5923378536105156 | acc avg : 0.735 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 89/99: 100%|##########| 100/100 [00:03<00:00, 31.75it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.591719012260437 | acc avg : 0.73 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 90/99: 100%|##########| 100/100 [00:03<00:00, 31.62it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5900497680902481 | acc avg : 0.7375 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 91/99: 100%|##########| 100/100 [00:03<00:00, 31.81it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5890539279580116 | acc avg : 0.7375 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 92/99: 100%|##########| 100/100 [00:03<00:00, 31.47it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5918287739157677 | acc avg : 0.73 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 93/99: 100%|##########| 100/100 [00:03<00:00, 31.68it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5798978772759438 | acc avg : 0.7375 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 94/99: 100%|##########| 100/100 [00:03<00:00, 31.78it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5842345660924911 | acc avg : 0.735 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 95/99: 100%|##########| 100/100 [00:03<00:00, 31.81it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5843563464283943 | acc avg : 0.7425 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 96/99: 100%|##########| 100/100 [00:03<00:00, 31.63it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5905592763423919 | acc avg : 0.7425 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 97/99: 100%|##########| 100/100 [00:03<00:00, 25.60it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5898371124267578 | acc avg : 0.7375 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 98/99: 100%|##########| 100/100 [00:03<00:00, 29.91it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.5929944407939911 | acc avg : 0.73 | Best score : 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"epoch 99/99: 100%|##########| 100/100 [00:03<00:00, 31.68it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss avg : 0.593219024837017 | acc avg : 0.7325 | Best score : 0.37197786569595337\n",
"1/1 [==============================] - 0s 42ms/step\n",
"추론 > [[0.42852464]\n",
" [0.5473223 ]\n",
" [0.580507 ]\n",
" [0.5538927 ]]\n",
"실 데이터 > [[0]\n",
" [1]\n",
" [1]\n",
" [0]]\n",
"score > 0.37197786569595337\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
},
{
"data": {
"text/plain": [
"<pso.optimizer.Optimizer at 0x7f8a2b933ee0>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def fit(x, y, model:keras.models = make_model(), loss_method=\"binary_crossentropy\", n_particles=100, maxiter=50, c0=0.5, c1=1.5, w=0.75,renewal=\"acc\"):\n",
" x, y = get_data()\n",
" x_test = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])\n",
" y_test = np.array([[0], [1], [1], [0]])\n",
"\n",
" model = make_model()\n",
"\n",
" # loss = loss_method\n",
" day = datetime.now().strftime(\"%m-%d-%H-%M\")\n",
" pso_xor = Optimizer(model=model, loss=loss_method, n_particles=n_particles, c0=c0, c1=c1, w=w)\n",
"\n",
" weight, score = pso_xor.fit(x, y, epochs=maxiter, save=True, save_path=\"./result/xor\", renewal=renewal)\n",
" # pso_xor = PSO(model=model, loss_method=loss, n_particles=n_particles)\n",
"\n",
" # best_weights, score = pso_xor.optimize(x, y, maxiter=maxiter, c0=c0, c1=c1, w=w)\n",
"\n",
" model.set_weights(weight)\n",
"\n",
" y_pred = model.predict(x_test)\n",
" print(f\"추론 > {y_pred}\")\n",
" print(f\"실 데이터 > {y_test}\")\n",
"\n",
" # score_ = model.evaluate(x_test, y_test, verbose=2)\n",
" print(f\"score > {score}\")\n",
" \n",
" # pso_xor.plot_history()\n",
" \n",
" # history = pso_xor.global_history()\n",
" json_data = {\n",
" \"best score\": score,\n",
" \"epoch\": maxiter,\n",
" \"n_particles\": n_particles,\n",
" \"c0\": c0,\n",
" \"c1\": c1,\n",
" \"w\": w,\n",
" \"loss_method\": loss_method\n",
" }\n",
" \n",
" with open(f\"./result/xor/{day}_{loss_method}_{n_particles}_{maxiter}.json\", \"w\") as f:\n",
" json.dump(json_data, f, indent=4)\n",
" \n",
" return pso_xor\n",
"\n",
"fit(*get_data(), make_model(), n_particles=100, maxiter=100, c0=0.5, c1=1.5, w=0.65, renewal=\"loss\")\n",
"\n",
"# pso_=fit(*get_data(), make_model(), n_particles=10, maxiter=10, c0=0.5, c1=1.5, w=0.75)\n",
"# print(f\"history > {history}\")\n",
"# print(f\"score > {score}\")\n",
"# plt.plot(history)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "pso",
"language": "python",
"name": "pso"
},
"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.16"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}