momentum (관성) -> momentun (돌연변이)로 수정
This commit is contained in:
jung-geun
2023-06-29 22:22:03 +09:00
parent 544a818940
commit 97abf75149
3 changed files with 17 additions and 9 deletions

10
.gitignore vendored
View File

@@ -1,5 +1,13 @@
*.pyc *.pyc
__pycache__/ __pycache__/
*.py[cod]
*$py.class
.ipynb_checkpoints/ .ipynb_checkpoints/
*.pdf
# 결과 저장용 디렉토리
result/ result/
# 논문 관련 파일
*.pdf
*.pptx

View File

@@ -32,7 +32,7 @@ class Optimizer:
w_min=0.5, w_min=0.5,
w_max=1.5, w_max=1.5,
negative_swarm: float = 0, negative_swarm: float = 0,
momentun_swarm: float = 0, mutation_swarm: float = 0,
): ):
""" """
particle swarm optimization particle swarm optimization
@@ -57,7 +57,7 @@ class Optimizer:
self.w_min = w_min # 최소 관성 수치 self.w_min = w_min # 최소 관성 수치
self.w_max = w_max # 최대 관성 수치 self.w_max = w_max # 최대 관성 수치
self.negative_swarm = negative_swarm # 최적해와 반대로 이동할 파티클 비율 - 0 ~ 1 사이의 값 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_score = [0 , np.inf] # 최고 점수 - 시작은 0으로 초기화
self.g_best = None # 최고 점수를 받은 가중치 self.g_best = None # 최고 점수를 받은 가중치
self.g_best_ = None # 최고 점수를 받은 가중치 - 값의 분산을 위한 변수 self.g_best_ = None # 최고 점수를 받은 가중치 - 값의 분산을 위한 변수
@@ -67,11 +67,11 @@ class Optimizer:
m = keras.models.model_from_json(model.to_json()) m = keras.models.model_from_json(model.to_json())
init_weights = m.get_weights() init_weights = m.get_weights()
w_, sh_, len_ = self._encode(init_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_)) # w_ = np.random.uniform(-1.5, 1.5, len(w_))
m.set_weights(self._decode(w_, sh_, len_)) m.set_weights(self._decode(w_, sh_, len_))
m.compile(loss=self.loss, optimizer="sgd", metrics=["accuracy"]) 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() gc.collect()

View File

@@ -10,7 +10,7 @@ class Particle:
""" """
Particle Swarm Optimization의 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: Args:
model (keras.models): 학습 및 검증을 위한 모델 model (keras.models): 학습 및 검증을 위한 모델
@@ -24,7 +24,7 @@ class Particle:
i_w_ = np.random.rand(len(i_w_)) / 2 - 0.25 i_w_ = np.random.rand(len(i_w_)) / 2 - 0.25
self.velocities = self._decode(i_w_, s_, l_) self.velocities = self._decode(i_w_, s_, l_)
self.negative = negative self.negative = negative
self.momentun = momentun self.mutation = mutation
self.best_score = 0 self.best_score = 0
self.best_weights = init_weights self.best_weights = init_weights
@@ -150,7 +150,7 @@ class Particle:
+ local_rate * r0 * (encode_p - encode_w) + local_rate * r0 * (encode_p - encode_w)
+ global_rate * r1 * (encode_g - encode_w) + global_rate * r1 * (encode_g - encode_w)
) )
if self.momentun: if self.mutation:
new_v += 0.5 * encode_v new_v += 0.5 * encode_v
self.velocities = self._decode(new_v, w_sh, w_len) self.velocities = self._decode(new_v, w_sh, w_len)
del encode_w, 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) + local_rate * r0 * (w_p * encode_p - encode_w)
+ global_rate * r1 * (w_g * encode_g - encode_w) + global_rate * r1 * (w_g * encode_g - encode_w)
) )
if self.momentun: if self.mutation:
new_v += 0.5 * encode_v new_v += 0.5 * encode_v
self.velocities = self._decode(new_v, w_sh, w_len) self.velocities = self._decode(new_v, w_sh, w_len)
del encode_w, w_sh, w_len del encode_w, w_sh, w_len