diff --git a/mnist.py b/mnist.py index e8d8b0b..24a884a 100644 --- a/mnist.py +++ b/mnist.py @@ -73,7 +73,7 @@ if __name__ == "__main__": n_particles=100, c0=0.35, c1=0.8, - w_min=0.7, + w_min=0.4, w_max=1.1, negative_swarm=0.2, mutation_swarm=0.1, diff --git a/psokeras/__init__.py b/psokeras/__init__.py deleted file mode 100755 index b3a9721..0000000 --- a/psokeras/__init__.py +++ /dev/null @@ -1,14 +0,0 @@ -# -*- coding: utf-8 -*- -"""PSOkeras - Particle Swarm Optimizer for Keras models - -This module implements a particle swarm optimizer for training the weights of Keras models. The - -""" - - -from .version import __version__ -from .optimizer import Optimizer - -__all__ = [ - 'Optimizer', -] diff --git a/psokeras/optimizer.py b/psokeras/optimizer.py deleted file mode 100755 index b4fbaab..0000000 --- a/psokeras/optimizer.py +++ /dev/null @@ -1,67 +0,0 @@ -BIG_SCORE = 1.e6 # type: float - -import keras -from psokeras.particle import Particle -from .util import ProgressBar - - -class Optimizer: - def __init__(self, model, loss, - n=10, - acceleration=0.1, - local_rate=1.0, - global_rate=1.0): - - self.n_particles = n - self.structure = model.to_json() - self.particles = [None] * n - self.loss = loss - self.length = len(model.get_weights()) - - params = {'acc': acceleration, 'local_acc': local_rate, 'global_acc': global_rate} - - for i in range(n-1): - m = keras.models.model_from_json(self.structure) - m.compile(loss=loss,optimizer='sgd') - self.particles[i] = Particle(m, params) - - self.particles[n-1] = Particle(model, params) - - self.global_best_weights = None - self.global_best_score = BIG_SCORE - - def fit(self, x, y, steps=0, batch_size=32): - num_batches = x.shape[0] // batch_size - - for i, p in enumerate(self.particles): - local_score = p.get_score(x, y) - - if local_score < self.global_best_score: - self.global_best_score = local_score - self.global_best_weights = p.get_best_weights() - - print("PSO -- Initial best score {:0.4f}".format(self.global_best_score)) - - bar = ProgressBar(steps, updates=20) - - for i in range(steps): - for j in range(num_batches): - x_ = x[j*batch_size:(j+1)*batch_size,:] - y_ = y[j*batch_size:(j+1)*batch_size] - - for p in self.particles: - local_score = p.step(x_, y_, self.global_best_weights) - - if local_score < self.global_best_score: - self.global_best_score = local_score - self.global_best_weights = p.get_best_weights() - - bar.update(i) - - bar.done() - - def get_best_model(self): - best_model = keras.models.model_from_json(self.structure) - best_model.set_weights(self.global_best_weights) - best_model.compile(loss=self.loss,optimizer='sgd') - return best_model diff --git a/psokeras/particle.py b/psokeras/particle.py deleted file mode 100755 index 15a9091..0000000 --- a/psokeras/particle.py +++ /dev/null @@ -1,66 +0,0 @@ -import random - -import numpy as np - -from psokeras.optimizer import BIG_SCORE - - -class Particle: - def __init__(self, model, params): - self.model = model - self.params = params - self.init_weights = model.get_weights() - self.velocities = [None] * len(self.init_weights) - self.length = len(self.init_weights) - for i, layer in enumerate(self.init_weights): - self.velocities[i] = np.random.rand(*layer.shape) / 5 - 0.10 - # self.velocities[i] = np.zeros(layer.shape) - - self.best_weights = None - self.best_score = BIG_SCORE - - def get_score(self, x, y, update=True): - local_score = self.model.evaluate(x, y, verbose=0) - if local_score < self.best_score and update: - self.best_score = local_score - self.best_weights = self.model.get_weights() - - return local_score - - def _update_velocities(self, global_best_weights, depth): - new_velocities = [None] * len(self.init_weights) - weights = self.model.get_weights() - local_rand, global_rand = random.random(), random.random() - - for i, layer in enumerate(weights): - if i >= depth: - new_velocities[i] = self.velocities[i] - continue - new_v = self.params['acc'] * self.velocities[i] - new_v = new_v + self.params['local_acc'] * local_rand * (self.best_weights[i] - layer) - new_v = new_v + self.params['global_acc'] * global_rand * (global_best_weights[i] - layer) - new_velocities[i] = new_v - - self.velocities = new_velocities - - def _update_weights(self, depth): - old_weights = self.model.get_weights() - new_weights = [None] * len(old_weights) - for i, layer in enumerate(old_weights): - if i>= depth: - new_weights[i] = layer - continue - new_w = layer + self.velocities[i] - new_weights[i] = new_w - - self.model.set_weights(new_weights) - - def step(self, x, y, global_best_weights,depth=None): - if depth is None: - depth = self.length - self._update_velocities(global_best_weights, depth) - self._update_weights(depth) - return self.get_score(x, y) - - def get_best_weights(self): - return self.best_weights diff --git a/psokeras/util.py b/psokeras/util.py deleted file mode 100755 index 08ce5ed..0000000 --- a/psokeras/util.py +++ /dev/null @@ -1,31 +0,0 @@ -class ProgressBar: - def __init__(self, steps, updates=10): - self.step = 0 - self.step_size = (steps // updates) - self.total_steps = steps - self.updates = updates - - bar = self._make_bar(0) - print(bar, end=' ') - - def update(self, i): - if i % self.step_size > 0: - return - - self.step = i // self.step_size - bar = self._make_bar(i) - - print(bar, end=' ') - - def done(self): - self.step = self.total_steps - bar = self._make_bar(self.updates) - print(bar) - - def _make_bar(self, x): - bar = "[" - for x in range(self.updates): - print("\r", end=' ') - bar += "=" if x < self.step else " " - bar += "]" - return bar diff --git a/psokeras/version.py b/psokeras/version.py deleted file mode 100755 index 7fd229a..0000000 --- a/psokeras/version.py +++ /dev/null @@ -1 +0,0 @@ -__version__ = '0.2.0' diff --git a/pyswarms/example.ipynb b/pyswarms/example.ipynb deleted file mode 100644 index 95d53ed..0000000 --- a/pyswarms/example.ipynb +++ /dev/null @@ -1,484 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "2023-05-28 17:28:58.354284: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n", - "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", - "2023-05-28 17:28:58.477863: 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", - "2023-05-28 17:28:58.851418: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvrtc.so.11.1: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-11.2/lib64:/usr/local/TensorRT/lib:/usr/local/cuda-11.2/lib64:/usr/local/TensorRT/lib:\n", - "2023-05-28 17:28:58.851559: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvrtc.so.11.1: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-11.2/lib64:/usr/local/TensorRT/lib:/usr/local/cuda-11.2/lib64:/usr/local/TensorRT/lib:\n", - "2023-05-28 17:28:58.851564: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.\n" - ] - } - ], - "source": [ - "import time\n", - "\n", - "import numpy as np\n", - "from IPython.display import Image\n", - "from keras.callbacks import TensorBoard\n", - "from keras.layers import Dense\n", - "from keras.models import Sequential\n", - "from pyswarms.single.global_best import GlobalBestPSO\n", - "from pyswarms.utils.functions import single_obj as fx\n", - "from pyswarms.utils.plotters import plot_surface\n", - "from pyswarms.utils.plotters.formatters import Animator, Designer, Mesher\n", - "from sklearn.datasets import load_iris\n", - "from sklearn.model_selection import train_test_split\n", - "from sklearn.preprocessing import OneHotEncoder, StandardScaler\n", - "import warnings\n", - "warnings.filterwarnings(\"ignore\")" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "iris = load_iris()\n", - "X = iris['data']\n", - "y = iris['target']\n", - "names = iris['target_names']\n", - "feature_names = iris['feature_names']\n", - "enc = OneHotEncoder()\n", - "Y = enc.fit_transform(y[:, np.newaxis]).toarray()\n", - "scaler = StandardScaler()\n", - "X_scaled = scaler.fit_transform(X)\n", - "X_train, X_test, Y_train, Y_test = train_test_split(\n", - " X_scaled, Y, test_size=0.5, random_state=2)\n", - "n_features = X.shape[1]\n", - "n_classes = Y.shape[1]" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "def create_custom_model(input_dim, output_dim, nodes, n=1, name='model'):\n", - " model = Sequential(name=name)\n", - " for i in range(n):\n", - " model.add(Dense(nodes, input_dim=input_dim, activation='relu'))\n", - " model.add(Dense(output_dim, activation='softmax'))\n", - " model.compile(loss='categorical_crossentropy',\n", - " optimizer='adam',\n", - " metrics=['accuracy'])\n", - " return model\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Model: \"model\"\n", - "_________________________________________________________________\n", - " Layer (type) Output Shape Param # \n", - "=================================================================\n", - " dense (Dense) (None, 4) 20 \n", - " \n", - " dense_1 (Dense) (None, 3) 15 \n", - " \n", - "=================================================================\n", - "Total params: 35\n", - "Trainable params: 35\n", - "Non-trainable params: 0\n", - "_________________________________________________________________\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "2023-05-28 17:29:19.512279: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:980] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", - "2023-05-28 17:29:19.516705: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:980] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", - "2023-05-28 17:29:19.516924: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:980] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", - "2023-05-28 17:29:19.517342: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n", - "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", - "2023-05-28 17:29:19.517934: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:980] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", - "2023-05-28 17:29:19.518103: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:980] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", - "2023-05-28 17:29:19.518250: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:980] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", - "2023-05-28 17:29:19.891585: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:980] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", - "2023-05-28 17:29:19.891755: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:980] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", - "2023-05-28 17:29:19.891875: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:980] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n", - "2023-05-28 17:29:19.891968: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1616] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 10109 MB memory: -> device: 0, name: NVIDIA GeForce RTX 3060, pci bus id: 0000:09:00.0, compute capability: 8.6\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Model name: model\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "2023-05-28 17:29:20.713676: I tensorflow/stream_executor/cuda/cuda_blas.cc:1614] TensorFloat-32 will be used for the matrix multiplication. This will only be logged once.\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3/3 [==============================] - 0s 1ms/step - loss: 0.0960 - accuracy: 0.9600\n", - "Test loss: 0.09600644558668137\n", - "Test accuracy: 0.9599999785423279\n", - "--- 13.446455717086792 seconds ---\n" - ] - } - ], - "source": [ - "n_layers = 1\n", - "model = create_custom_model(n_features, n_classes,\n", - " 4, n_layers)\n", - "model.summary()\n", - "\n", - "start_time = time.time()\n", - "print('Model name:', model.name)\n", - "history_callback = model.fit(X_train, Y_train,\n", - " batch_size=5,\n", - " epochs=400,\n", - " verbose=0,\n", - " validation_data=(X_test, Y_test)\n", - " )\n", - "score = model.evaluate(X_test, Y_test)\n", - "print('Test loss:', score[0])\n", - "print('Test accuracy:', score[1])\n", - "print(\"--- %s seconds ---\" % (time.time() - start_time))" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "def get_shape(model):\n", - " weights_layer = model.get_weights()\n", - " shapes = []\n", - " for weights in weights_layer:\n", - " shapes.append(weights.shape)\n", - " return shapes\n", - "def set_shape(weights,shapes):\n", - " new_weights = []\n", - " index=0\n", - " for shape in shapes:\n", - " if(len(shape)>1):\n", - " n_nodes = np.prod(shape)+index\n", - " else:\n", - " n_nodes=shape[0]+index\n", - " tmp = np.array(weights[index:n_nodes]).reshape(shape)\n", - " new_weights.append(tmp)\n", - " index=n_nodes\n", - " return new_weights" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "2023-05-28 17:29:35,386 - pyswarms.single.global_best - INFO - Optimize for 20 iters with {'c1': 0.4, 'c2': 0.6, 'w': 0.4}\n", - "pyswarms.single.global_best: 100%|██████████|20/20, best_cost=0.0133\n", - "2023-05-28 17:30:07,637 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 0.013333320617675781, best pos: [ 0.17027965 0.17696722 -0.07395054 0.31544984 0.17052408 -0.37810479\n", - " 0.24267479 0.16931148 0.65606942 -0.24207116 -0.66562722 0.02191478\n", - " 0.5870387 0.78966943 -0.4457816 0.0907434 -0.1808341 0.29282655\n", - " 0.61472003 0.90660508 0.16469465 -0.55057763 0.54702005 -0.22636745\n", - " 0.01125538 0.62431828 0.02128613 -0.26723577 -0.43527016 0.51223244\n", - " 0.76388399 -0.02073011 0.15949622 0.45878514 0.01787211]\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3/3 [==============================] - 0s 1ms/step - loss: 0.7467 - accuracy: 0.9600\n", - "Test loss: 0.7467350363731384\n", - "Test accuracy: 0.9599999785423279\n", - "--- 32.29235529899597 seconds ---\n" - ] - } - ], - "source": [ - "start_time = time.time()\n", - "def evaluate_nn(W, shape,X_train=X_train, Y_train=Y_train):\n", - " results = []\n", - " for weights in W:\n", - " model.set_weights(set_shape(weights,shape))\n", - " score = model.evaluate(X_train, Y_train, verbose=0)\n", - " results.append(1-score[1])\n", - " return results\n", - "\n", - "shape = get_shape(model)\n", - "x_max = 1.0 * np.ones(35)\n", - "x_min = -1.0 * x_max\n", - "bounds = (x_min, x_max)\n", - "options = {'c1': 0.4, 'c2': 0.6, 'w': 0.4}\n", - "optimizer = GlobalBestPSO(n_particles=50, dimensions=35,\n", - " options=options, bounds=bounds)\n", - "cost, pos = optimizer.optimize(evaluate_nn, 20, X_train=X_train, Y_train=Y_train,shape=shape)\n", - "model.set_weights(set_shape(pos,shape))\n", - "score = model.evaluate(X_test, Y_test)\n", - "print('Test loss:', score[0])\n", - "print('Test accuracy:', score[1])\n", - "print(\"--- %s seconds ---\" % (time.time() - start_time))" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "2023-05-28 17:30:08,140 - matplotlib.animation - WARNING - MovieWriter pillowwritter unavailable; using Pillow instead.\n", - "2023-05-28 17:30:08,141 - matplotlib.animation - INFO - Animation.save using \n" - ] - }, - { - "data": { - "text/html": [ - "" - ], - "text/plain": [ - "" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - }, - { - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAg8AAAH/CAYAAADQXz4mAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/bCgiHAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAdcUlEQVR4nO3dbWyd5X348Z+T4GNQsUmXxXmYaQYdpS0loQlxDY0Qk1dLoHR5Md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QDAJAiHgCAFPEAAKSIBwAgRTwAACniAQBIEQ8AQIp4AABSxAMAkCIeAIAU8QAApIgHACBFPAAAKeIBAEgRDwBAingAAFLEAwCQIh4AgBTxAACkiAcAIEU8AAAp4gEASBEPAECKeAAAUsQDAJAiHgCAFPEAAKSIBwAgRTwAACniAQBIEQ8AQIp4AABSxAMAkCIeAIAU8QAApIgHACBFPAAAKeIBAEgRDwBAingAAFLEAwCQIh4AgBTxAACkiAcAIEU8AAAp4gEASBEPAECKeAAAUsQDAJAiHgCAFPEAAKSIBwAgRTwAACniAQBIGVE8rF27NmbMmBFVVVVRX18f27ZtO+78NWvWxIc+9KE4++yzo66uLpYtWxa//e1vR7RgAGBspeNh48aN0dLSEq2trbFjx46YOXNmNDU1xRtvvHHU+Y8//ngsX748WltbY9euXfHII4/Exo0b4/bbb3/XiwcAyi8dDw888EDceOONsWTJkvjIRz4S69ati3POOSceffTRo85//vnn48orr4zrr78+ZsyYEZ/61Kfiuuuu+4NXKwCAU1MqHvr7+2P79u3R2Nj4zh2MGxeNjY2xdevWox5zxRVXxPbt24diYffu3bF58+a45ppr3sWyAYCxMiEzef/+/TEwMBC1tbXDxmtra+Oll1466jHXX3997N+/Pz75yU9GURRx+PDhuPnmm4/7skVfX1/09fUN/dzb25tZJgAwikb9ry22bNkSq1atioceeih27NgRTz75ZGzatCnuueeeYx7T1tYWNTU1Q7e6urrRXiYAcIIqiqIoTnRyf39/nHPOOfHEE0/EggULhsYXL14cBw4ciH/7t3874ph58+bFJz7xifj6178+NPbP//zPcdNNN8Wvf/3rGDfuyH452pWHurq66Onpierq6hNdLgC85/X29kZNTc1JfQ5NXXmorKyM2bNnR0dHx9DY4OBgdHR0RENDw1GPefPNN48IhPHjx0dExLG6pVQqRXV19bAbAHBqSL3nISKipaUlFi9eHHPmzIm5c+fGmjVr4tChQ7FkyZKIiFi0aFFMnz492traIiJi/vz58cADD8Rll10W9fX18eqrr8Zdd90V8+fPH4oIAOD0kY6H5ubm2LdvX6xc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U6NUKsVFF13k35ek7J6vWbMmPvShD8XZZ58ddXV1sWzZsvjtb39bptWe3n70ox/F/PnzY9q0aVFRUXHc741625YtW+LjH/94lEql+OAHPxiPPfZY/sTFKWDDhg1FZWVl8eijjxb/9V//Vdx4443FeeedV3R3dx91/o9//ONi/Pjxxb333lu8+OKLxZ133lmcddZZxQsvvFDmlZ++snt+/fXXF2vXri127txZ7Nq1q/ibv/mboqampvjv//7vMq/89JXd87e99tprxfTp04t58+YVf/mXf1mexZ4hsnve19dXzJkzp7jmmmuK5557rnjttdeKLVu2FJ2dnWVe+ekru+ff+c53ilKpVHznO98pXnvtteKZZ54ppk6dWixbtqzMKz89bd68ubjjjjuKJ598soiI4qmnnjru/N27dxfnnHNO0dLSUrz44ovFN77xjWL8+PFFe3t76rynRDzMnTu3WLp06dDPAwMDxbRp04q2trajzv/MZz5TXHvttcPG6uvri7/9278d1XWeSbJ7/vsOHz5cnHvuucW3v/3t0VriGWcke3748OHiiiuuKL71rW8VixcvFg9J2T3/5je/WVxwwQVFf39/uZZ4xsnu+dKlS4s///M/HzbW0tJSXHnllaO6zjPRicTDl770peKjH/3osLHm5uaiqakpda4xf9ni7a/5bmxsHBo7ka/5/r/zI373Nd/Hms9wI9nz3/fmm2/GW2+9dVK/aOVMNtI9/8pXvhKTJ0+OG264oRzLPKOMZM+/973vRUNDQyxdujRqa2vjkksuiVWrVsXAwEC5ln1aG8meX3HFFbF9+/ahlzZ2794dmzdvjmuuuaYsa36vOVnPn2P+rZrl+ppv3jGSPf99t912W0ybNu2IX0KObiR7/txzz8UjjzwSnZ2dZVjhmWcke7579+74j//4j/jsZz8bmzdvjldffTW+8IUvxFtvvRWtra3lWPZpbSR7fv3118f+/fvjk5/8ZBRFEYcPH46bb745br/99nIs+T3nWM+fvb298Zvf/CbOPvvsE7qfMb/ywOln9erVsWHDhnjqqaeiqqpqrJdzRjp48GAsXLgw1q9fH5MmTRrr5bxnDA4OxuTJk+Phhx+O2bNnR3Nzc9xxxx2xbt26sV7aGWvLli2xatWqeOihh2LHjh3x5JNPxqZNm+Kee+4Z66VxHGN+5aFcX/PNO0ay52+77777YvXq1fGDH/wgLr300tFc5hklu+c/+9nP4vXXX4/58+cPjQ0ODkZExIQJE+Lll1+OCy+8cHQXfZobye/51KlT46yzzorx48cPjX34wx+Orq6u6O/vj8rKylFd8+luJHt+1113xcKFC+Nzn/tcRER87GMfi0OHDsVNN90Ud9xxx0n9GmmO/fxZXV19wlcdIk6BKw++5rv8RrLnERH33ntv3HPPPdHe3h5z5swpx1LPGNk9v/jii+OFF16Izs7OodunP/3puPrqq6OzszPq6urKufzT0kh+z6+88sp49dVXh0ItIuKVV16JqVOnCocTMJI9f/PNN48IhLfjrfDVSyfdSXv+zL2Xc3Rs2LChKJVKxWOPPVa8+OKLxU033VScd955RVdXV1EURbFw4cJi+fLlQ/N//OMfFxMmTCjuu+++YteuXUVra6s/1UzK7vnq1auLysrK4oknnih++ctfDt0OHjw4Vg/htJPd89/nry3ysnu+Z8+e4txzzy3+7u/+rnj55ZeL73//+8XkyZOLr371q2P1EE472T1vbW0tzj333OJf/uVfit27dxf//u//Xlx44YXFZz7zmbF6CKeVgwcPFjt37ix27txZRETxwAMPFDt37ix+/vOfF0VRFMuXLy8WLlw4NP/tP9X8h3/4h2LXrl3F2rVrT98/1SyKovjGN75RnH/++UVlZWUxd+7c4j//8z+H/ttVV11VLF68eNj87373u8VFF11UVFZWFh/96EeLTZs2lXnFp7/Mnn/gAx8oIuKIW2tra/kXfhrL/p7/X+JhZLJ7/vzzzxf19fVFqVQqLrjgguJrX/tacfjw4TKv+vSW2fO33nqr+PKXv1xceOGFRVVVVVFXV1d84QtfKP73f/+3/As/Df3whz886r/Nb+/x4sWLi6uuuuqIY2bNmlVUVlYWF1xwQfFP//RP6fP6Sm4AIGXM3/MAAJxexAMAkCIeAIAU8QAApIgHACBFPAAAKeIBAEgRDwBAingAAFLEAwCQIh4AgBTxAACk/D9IPdB/NJXrLwAAAABJRU5ErkJggg==", - "text/plain": [ - "
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "m = Mesher(func=fx.sphere)\n", - "pos_history = [pos[:, :2] for pos in optimizer.pos_history]\n", - "pos3d = m.compute_history_3d(pos_history)\n", - "# Assuming we already had an optimizer ready\n", - "my_animator = Animator(repeat=False)\n", - "my_designer = Designer(figsize=(6, 6))\n", - "animation = plot_surface(pos3d, animator=my_animator, designer=my_designer)\n", - "# %%\n", - "animation.save('pso.gif', writer='pillowwritter', fps=6, )\n", - "Image(url='pso.gif')" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "\n", - "from sklearn.model_selection import KFold" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1/1 [==============================] - 0s 71ms/step - loss: 0.0138 - accuracy: 1.0000\n", - "1/1 [==============================] - 0s 67ms/step - loss: 0.1226 - accuracy: 0.9000\n", - "1/1 [==============================] - 0s 67ms/step - loss: 0.1448 - accuracy: 0.9333\n", - "WARNING:tensorflow:5 out of the last 3010 calls to .test_function at 0x7fee6622df70> 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": [ - "2023-05-28 17:31:01,885 - tensorflow - WARNING - 5 out of the last 3010 calls to .test_function at 0x7fee6622df70> 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": "stdout", - "output_type": "stream", - "text": [ - "1/1 [==============================] - 0s 69ms/step - loss: 0.0451 - accuracy: 1.0000\n", - "WARNING:tensorflow:6 out of the last 3011 calls to .test_function at 0x7fee66257b80> 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": [ - "2023-05-28 17:31:08,794 - tensorflow - WARNING - 6 out of the last 3011 calls to .test_function at 0x7fee66257b80> 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": "stdout", - "output_type": "stream", - "text": [ - "1/1 [==============================] - 0s 68ms/step - loss: 0.0970 - accuracy: 0.9667\n" - ] - } - ], - "source": [ - "X = iris[\"data\"]\n", - "y = iris[\"target\"]\n", - "\n", - "enc = OneHotEncoder()\n", - "Y = enc.fit_transform(y[:, np.newaxis]).toarray()\n", - "scaler = StandardScaler()\n", - "X_scaled = scaler.fit_transform(X)\n", - "\n", - "inputs = X_scaled\n", - "targets = Y\n", - "\n", - "num_folds = 5\n", - "\n", - "kfold = KFold(n_splits=num_folds, shuffle=True)\n", - "\n", - "fold_no = 1\n", - "accs_bp = []\n", - "\n", - "x_max = 1.0 * np.ones(35)\n", - "x_min = -1.0 * x_max\n", - "bounds = (x_min, x_max)\n", - "\n", - "for train, test in kfold.split(inputs, targets):\n", - " model = create_custom_model(n_features, n_classes,\n", - " 4, 1)\n", - " shape = get_shape(model)\n", - " history_callback = model.fit(inputs[train], targets[train],\n", - " batch_size=5,\n", - " epochs=400,\n", - " verbose=0,\n", - " )\n", - " score = model.evaluate(inputs[test], targets[test])\n", - " fold_no += 1\n", - " accs_bp.append(score[1])" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1/1 [==============================] - 0s 14ms/step - loss: 0.5820 - accuracy: 1.0000\n", - "1/1 [==============================] - 0s 13ms/step - loss: 0.6069 - accuracy: 0.7667\n", - "1/1 [==============================] - 0s 13ms/step - loss: 0.7855 - accuracy: 0.9000\n", - "1/1 [==============================] - 0s 14ms/step - loss: 0.7858 - accuracy: 0.9000\n", - "1/1 [==============================] - 0s 13ms/step - loss: 0.6576 - accuracy: 0.9333\n" - ] - } - ], - "source": [ - "accs_pso=[]\n", - "for train, test in kfold.split(inputs, targets):\n", - " options = {'c1': 0.4, 'c2': 0.4, 'w': 0.6}\n", - " optimizer = GlobalBestPSO(n_particles=25, dimensions=35,\n", - " options=options, bounds=bounds)\n", - " cost, pos = optimizer.optimize(evaluate_nn, 20, X_train=inputs[train], Y_train=targets[train],shape=shape, verbose=0)\n", - " model.set_weights(set_shape(pos,shape))\n", - " score = model.evaluate(inputs[test], targets[test])\n", - " accs_pso.append(score[1])" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Accuracy 0.96 +/- 0.04\n" - ] - } - ], - "source": [ - "accs_bp\n", - "print(\"Accuracy {:.2f} +/- {:.2f}\".format(np.average(accs_bp),np.std(accs_bp)))" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Accuracy 0.90 +/- 0.08\n" - ] - } - ], - "source": [ - "accs_pso\n", - "print(\"Accuracy {:.2f} +/- {:.2f}\".format(np.average(accs_pso),np.std(accs_pso)))" - ] - }, - { - "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" - }, - "orig_nbformat": 4 - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/pyswarms/pso.gif b/pyswarms/pso.gif deleted file mode 100644 index bf9a271..0000000 Binary files a/pyswarms/pso.gif and /dev/null differ diff --git a/pyswarms/pyswarm.py b/pyswarms/pyswarm.py deleted file mode 100644 index 551e453..0000000 --- a/pyswarms/pyswarm.py +++ /dev/null @@ -1,93 +0,0 @@ -from sklearn.datasets import load_iris -from sklearn.model_selection import train_test_split -from sklearn.preprocessing import OneHotEncoder, StandardScaler - -import tensorflow as tf -from tensorflow import keras -from tensorflow.keras.models import Sequential -from tensorflow.keras.layers import Dense, Dropout, Flatten - -import time as time -import numpy as np - - -iris = load_iris() -iris = load_iris() -X = iris['data'] -y = iris['target'] -names = iris['target_names'] -feature_names = iris['feature_names'] -enc = OneHotEncoder() -Y = enc.fit_transform(y[:, np.newaxis]).toarray() -scaler = StandardScaler() -X_scaled = scaler.fit_transform(X) -X_train, X_test, Y_train, Y_test = train_test_split( - X_scaled, Y, test_size=0.5, random_state=2) -n_features = X.shape[1] -n_classes = Y.shape[1] - -def create_custom_model(input_dim, output_dim, nodes, n=1, name='model'): - model = Sequential(name=name) - for i in range(n): - model.add(Dense(nodes, input_dim=input_dim, activation='relu')) - model.add(Dense(output_dim, activation='softmax')) - model.compile(loss='categorical_crossentropy', - optimizer='adam', - metrics=['accuracy']) - return model - - -# n_layers = 1 -# model = create_custom_model(n_features, n_classes, -# 4, n_layers) -# model.summary() - -# start_time = time.time() -# print('Model name:', model.name) -# history_callback = model.fit(X_train, Y_train, -# batch_size=5, -# epochs=400, -# verbose=0, -# validation_data=(X_test, Y_test) -# ) -# score = model.evaluate(X_test, Y_test) -# print('Test loss:', score[0]) -# print('Test accuracy:', score[1]) -# print("--- %s seconds ---" % (time.time() - start_time)) - - -def get_shape(model): - weights_layer = model.get_weights() - shapes = [] - for weights in weights_layer: - shapes.append(weights.shape) - return shapes - -def set_shape(weights,shapes): - new_weights = [] - index=0 - for shape in shapes: - if(len(shape)>1): - n_nodes = np.prod(shape)+index - else: - n_nodes=shape[0]+index - tmp = np.array(weights[index:n_nodes]).reshape(shape) - new_weights.append(tmp) - index=n_nodes - return new_weights - -def evaluate_nn(W, shape,X_train=X_train, Y_train=Y_train): - results = [] - for weights in W: - model.set_weights(set_shape(weights,shape)) - score = model.evaluate(X_train, Y_train, verbose=0) - results.append(1-score[1]) - return results - -shape = get_shape(model) -x_max = 1.0 * np.ones(83) -x_min = -1.0 * x_max -bounds = (x_min, x_max) -options = {'c1': 0.4, 'c2': 0.8, 'w': 0.4} -optimizer = GlobalBestPSO(n_particles=25, dimensions=83, - options=options, bounds=bounds) \ No newline at end of file diff --git a/pyswarms/report.log b/pyswarms/report.log deleted file mode 100644 index c782ce2..0000000 --- a/pyswarms/report.log +++ /dev/null @@ -1,11 +0,0 @@ -2023-05-28 17:29:35,386 - pyswarms.single.global_best - INFO - Optimize for 20 iters with {'c1': 0.4, 'c2': 0.6, 'w': 0.4} -2023-05-28 17:30:07,637 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 0.013333320617675781, best pos: [ 0.17027965 0.17696722 -0.07395054 0.31544984 0.17052408 -0.37810479 - 0.24267479 0.16931148 0.65606942 -0.24207116 -0.66562722 0.02191478 - 0.5870387 0.78966943 -0.4457816 0.0907434 -0.1808341 0.29282655 - 0.61472003 0.90660508 0.16469465 -0.55057763 0.54702005 -0.22636745 - 0.01125538 0.62431828 0.02128613 -0.26723577 -0.43527016 0.51223244 - 0.76388399 -0.02073011 0.15949622 0.45878514 0.01787211] -2023-05-28 17:30:08,140 - matplotlib.animation - WARNING - MovieWriter pillowwritter unavailable; using Pillow instead. -2023-05-28 17:30:08,141 - matplotlib.animation - INFO - Animation.save using -2023-05-28 17:31:01,885 - tensorflow - WARNING - 5 out of the last 3010 calls to .test_function at 0x7fee6622df70> 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. -2023-05-28 17:31:08,794 - tensorflow - WARNING - 6 out of the last 3011 calls to .test_function at 0x7fee66257b80> 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. diff --git a/readme.md b/readme.md index 653ee75..7254486 100644 --- a/readme.md +++ b/readme.md @@ -46,10 +46,6 @@ $$ | |-- __init__.py # pso 모듈을 사용하기 위한 초기화 파일 | |-- optimizer.py # pso 알고리즘 이용을 위한 기본 코드 | |-- particle.py # 각 파티클의 정보 및 위치를 저장하는 코드 -|-- /psokeras # keras 모델을 이용가능한 PSO 알고리즘 - 다른 사람의 코드 -| |-- *** -|-- /pyswarms # pyswarms 라이브러리를 이용가능한 PSO 알고리즘 - 다른 사람의 코드 -| |-- *** |-- examples.py # psokeras 코드를 이용한 예제 |-- xor.ipynb # pso 를 이용한 xor 문제 풀이 |-- iris.py # pso 를 이용한 iris 문제 풀이