From ab937ac71c28d49090aca7578f3a00f4a4fdf9d0 Mon Sep 17 00:00:00 2001 From: jung-geun Date: Wed, 26 Jul 2023 23:26:39 +0900 Subject: [PATCH] =?UTF-8?q?23-07-26=20=ED=8C=8C=ED=8B=B0=ED=81=B4=EC=9D=98?= =?UTF-8?q?=20=EC=A0=84=EC=97=AD=20=EC=B5=9C=EC=A0=81=EA=B0=92=EC=9D=B4=20?= =?UTF-8?q?=EC=9D=B4=EC=A0=84=20=ED=9A=8C=EC=B0=A8=EC=99=80=20=EB=8F=99?= =?UTF-8?q?=EC=9D=BC=ED=95=A0=20=EB=95=8C=20=EC=A0=90=EC=A7=84=EC=A0=81?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EA=B0=80=EC=A4=91=EC=B9=98=EC=9D=98=20?= =?UTF-8?q?=EA=B0=90=EC=86=8C,=20=EB=8B=A4=EB=A5=BC=20=EB=95=8C=20?= =?UTF-8?q?=EC=88=9C=EA=B0=84=EC=A0=81=EC=9C=BC=EB=A1=9C=20=EB=91=90?= =?UTF-8?q?=EB=B0=B0=EC=9D=98=20=EA=B4=80=EC=84=B1=EC=B9=98=EB=A5=BC=20?= =?UTF-8?q?=EC=A3=BC=EB=8A=94=20=EB=B0=A9=EC=8B=9D=EC=9D=84=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +++-- mnist.py | 16 ++++++++-------- pso/optimizer.py | 10 +++++----- pso/particle.py | 15 +++++++++++++-- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 16cf89f..5e3b213 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ [![Python Package Index publish](https://github.com/jung-geun/PSO/actions/workflows/pypi.yml/badge.svg?event=push)](https://github.com/jung-geun/PSO/actions/workflows/pypi.yml) -Open In Colab # PSO 알고리즘 구현 및 새로운 시도 @@ -29,7 +28,7 @@ pso 알고리즘을 사용하여 새로운 학습 방법을 찾는중 입니다 자동으로 conda 환경을 설정하기 위해서는 다음 명령어를 사용합니다 ```shell -conda env create -f ./conda_env/environment.yaml +conda env create -f conda_env/environment.yaml ``` 현재 python 3.9 버전, tensorflow 2.11 버전에서 테스트 되었습니다 @@ -51,6 +50,8 @@ pso_model = Optimizer(...) pso_model.fit(...) ``` +Open In Colab + # 현재 진행 상황 ## 1. PSO 알고리즘 구현 diff --git a/mnist.py b/mnist.py index d0ce923..9d1d39c 100644 --- a/mnist.py +++ b/mnist.py @@ -106,15 +106,15 @@ loss = [ pso_mnist = Optimizer( model, loss=loss[0], - n_particles=1000, - c0=0.4, - c1=0.6, - w_min=0.5, - w_max=0.8, + n_particles=500, + c0=0.3, + c1=0.5, + w_min=0.4, + w_max=0.9, negative_swarm=0.1, - mutation_swarm=0.2, - particle_min=-5, - particle_max=5, + mutation_swarm=0.3, + particle_min=-4, + particle_max=4, ) best_score = pso_mnist.fit( diff --git a/pso/optimizer.py b/pso/optimizer.py index 65bac56..e64bceb 100644 --- a/pso/optimizer.py +++ b/pso/optimizer.py @@ -70,6 +70,7 @@ class Optimizer: if random_state is not None: np.random.set_state(random_state) + model.compile(loss=loss, optimizer="sgd", metrics=["accuracy"]) self.model = model # 모델 구조 self.loss = loss # 손실함수 self.n_particles = n_particles # 파티클 개수 @@ -203,7 +204,6 @@ class Optimizer: (float): 목적 함수 값 """ self.model.set_weights(weights) - # self.model.compile(loss=self.loss, optimizer="sgd", metrics=["accuracy"]) score = self.model.evaluate(x, y, verbose=0)[1] if score > 0: return 1 / (1 + score) @@ -221,7 +221,7 @@ class Optimizer: save_path: str = "./result", renewal: str = "acc", empirical_balance: bool = False, - Dispersion: bool = False, + dispersion: bool = False, check_point: int = None, ): """ @@ -234,12 +234,12 @@ class Optimizer: save_path : str - ex) "./result", renewal : str ex) "acc" or "loss" or "both", empirical_balance : bool - True : EBPSO, False : PSO, - Dispersion : bool - True : g_best 의 값을 분산시켜 전역해를 찾음, False : g_best 의 값만 사용 + dispersion : bool - True : g_best 의 값을 분산시켜 전역해를 찾음, False : g_best 의 값만 사용 check_point : int - 저장할 위치 - None : 저장 안함 """ self.save_path = save_path self.empirical_balance = empirical_balance - self.Dispersion = Dispersion + self.Dispersion = dispersion self.renewal = renewal try: @@ -336,7 +336,7 @@ class Optimizer: f"acc : {max_score:.4f} loss : {min_loss:.4f}" ) - if Dispersion: + if dispersion: ts = self.c0 + np.random.rand() * (self.c1 - self.c0) g_, g_sh, g_len = self._encode(self.g_best) decrement = (epochs - (epoch) + 1) / epochs diff --git a/pso/particle.py b/pso/particle.py index 435f7d4..8f8df15 100644 --- a/pso/particle.py +++ b/pso/particle.py @@ -35,7 +35,9 @@ class Particle: self.mutation = mutation self.best_score = 0 self.best_weights = init_weights - + self.before_best = init_weights + self.before_w = 0 + del i_w_, s_, l_ del init_weights @@ -107,7 +109,6 @@ class Particle: Returns: (float): 점수 """ - # self.model.compile(loss=self.loss, optimizer="sgd", metrics=["accuracy"]) score = self.model.evaluate(x, y, verbose=0, use_multiprocessing=True) if renewal == "acc": if score[1] > self.best_score: @@ -138,6 +139,15 @@ class Particle: encode_g, g_sh, g_len = self._encode(weights=g_best) r0 = np.random.rand() r1 = np.random.rand() + encode_before, before_sh, before_len = self._encode(weights=self.before_best) + + if (encode_before != encode_g).all(): + self.before_w = w + w = w + (self.before_w) + else: + self.before_w *= 0.6 + w = w + self.before_w + if self.negative: new_v = ( w * encode_v @@ -161,6 +171,7 @@ class Particle: del encode_v, v_sh, v_len del encode_p, p_sh, p_len del encode_g, g_sh, g_len + del encode_before, before_sh, before_len del r0, r1 def _update_velocity_w(self, local_rate, global_rate, w, w_p, w_g, g_best):