mirror of
https://github.com/jung-geun/PSO.git
synced 2025-12-19 20:44:39 +09:00
23-06-29
momentum (관성) -> momentun (돌연변이)로 수정
This commit is contained in:
10
.gitignore
vendored
10
.gitignore
vendored
@@ -1,5 +1,13 @@
|
||||
*.pyc
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
.ipynb_checkpoints/
|
||||
|
||||
# 결과 저장용 디렉토리
|
||||
result/
|
||||
|
||||
# 논문 관련 파일
|
||||
*.pdf
|
||||
result/
|
||||
*.pptx
|
||||
@@ -32,7 +32,7 @@ class Optimizer:
|
||||
w_min=0.5,
|
||||
w_max=1.5,
|
||||
negative_swarm: float = 0,
|
||||
momentun_swarm: float = 0,
|
||||
mutation_swarm: float = 0,
|
||||
):
|
||||
"""
|
||||
particle swarm optimization
|
||||
@@ -57,7 +57,7 @@ class Optimizer:
|
||||
self.w_min = w_min # 최소 관성 수치
|
||||
self.w_max = w_max # 최대 관성 수치
|
||||
self.negative_swarm = negative_swarm # 최적해와 반대로 이동할 파티클 비율 - 0 ~ 1 사이의 값
|
||||
self.momentun_swarm = momentun_swarm # 관성을 추가로 사용할 파티클 비율 - 0 ~ 1 사이의 값
|
||||
self.mutation_swarm = mutation_swarm # 관성을 추가로 사용할 파티클 비율 - 0 ~ 1 사이의 값
|
||||
self.g_best_score = [0 , np.inf] # 최고 점수 - 시작은 0으로 초기화
|
||||
self.g_best = None # 최고 점수를 받은 가중치
|
||||
self.g_best_ = None # 최고 점수를 받은 가중치 - 값의 분산을 위한 변수
|
||||
@@ -67,11 +67,11 @@ class Optimizer:
|
||||
m = keras.models.model_from_json(model.to_json())
|
||||
init_weights = m.get_weights()
|
||||
w_, sh_, len_ = self._encode(init_weights)
|
||||
w_ = np.random.rand(len(w_)) * 4 - 2
|
||||
w_ = np.random.rand(len(w_)) * 5 - 2.5
|
||||
# w_ = np.random.uniform(-1.5, 1.5, len(w_))
|
||||
m.set_weights(self._decode(w_, sh_, len_))
|
||||
m.compile(loss=self.loss, optimizer="sgd", metrics=["accuracy"])
|
||||
self.particles[i] = Particle(m, loss, negative=True if i < negative_swarm * self.n_particles else False, momentun=True if i > self.n_particles * (1 - self.momentun_swarm) else False)
|
||||
self.particles[i] = Particle(m, loss, negative=True if i < negative_swarm * self.n_particles else False, mutation=True if i > self.n_particles * (1 - self.mutation_swarm) else False)
|
||||
|
||||
gc.collect()
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ class Particle:
|
||||
"""
|
||||
Particle Swarm Optimization의 Particle을 구현한 클래스
|
||||
"""
|
||||
def __init__(self, model: keras.models, loss, negative: bool = False, momentun: bool = False):
|
||||
def __init__(self, model: keras.models, loss, negative: bool = False, mutation: bool = False):
|
||||
"""
|
||||
Args:
|
||||
model (keras.models): 학습 및 검증을 위한 모델
|
||||
@@ -24,7 +24,7 @@ class Particle:
|
||||
i_w_ = np.random.rand(len(i_w_)) / 2 - 0.25
|
||||
self.velocities = self._decode(i_w_, s_, l_)
|
||||
self.negative = negative
|
||||
self.momentun = momentun
|
||||
self.mutation = mutation
|
||||
self.best_score = 0
|
||||
self.best_weights = init_weights
|
||||
|
||||
@@ -150,7 +150,7 @@ class Particle:
|
||||
+ local_rate * r0 * (encode_p - encode_w)
|
||||
+ global_rate * r1 * (encode_g - encode_w)
|
||||
)
|
||||
if self.momentun:
|
||||
if self.mutation:
|
||||
new_v += 0.5 * encode_v
|
||||
self.velocities = self._decode(new_v, w_sh, w_len)
|
||||
del encode_w, w_sh, w_len
|
||||
@@ -190,7 +190,7 @@ class Particle:
|
||||
+ local_rate * r0 * (w_p * encode_p - encode_w)
|
||||
+ global_rate * r1 * (w_g * encode_g - encode_w)
|
||||
)
|
||||
if self.momentun:
|
||||
if self.mutation:
|
||||
new_v += 0.5 * encode_v
|
||||
self.velocities = self._decode(new_v, w_sh, w_len)
|
||||
del encode_w, w_sh, w_len
|
||||
|
||||
Reference in New Issue
Block a user