diff --git a/README.md b/README.md
index 16cf89f..5e3b213 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,4 @@
[](https://github.com/jung-geun/PSO/actions/workflows/pypi.yml)
-
# 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(...)
```
+
+
# 현재 진행 상황
## 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):