mirror of
https://github.com/jung-geun/PSO.git
synced 2025-12-19 20:44:39 +09:00
23-07-26
파티클의 전역 최적값이 이전 회차와 동일할 때 점진적으로 가중치의 감소, 다를 때 순간적으로 두배의 관성치를 주는 방식을 추가
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user