mnist 46% 달성
This commit is contained in:
jung-geun
2023-07-10 17:05:59 +09:00
parent f18932d6d2
commit 46aa52d81f
8 changed files with 650 additions and 80 deletions

View File

@@ -15,9 +15,9 @@ gpus = tf.config.experimental.list_physical_devices("GPU")
if gpus:
try:
# tf.config.experimental.set_visible_devices(gpus[0], "GPU")
print(tf.config.experimental.get_visible_devices("GPU"))
# print(tf.config.experimental.get_visible_devices("GPU"))
tf.config.experimental.set_memory_growth(gpus[0], True)
print("set memory growth")
# print("set memory growth")
except RuntimeError as e:
print(e)
@@ -39,8 +39,8 @@ class Optimizer:
w_max=1.5,
negative_swarm: float = 0,
mutation_swarm: float = 0,
np_seed: int = 777,
tf_seed: int = 777,
np_seed: int = None,
tf_seed: int = None,
):
"""
particle swarm optimization
@@ -55,9 +55,13 @@ class Optimizer:
w_max (float): 최대 관성 수치
negative_swarm (float): 최적해와 반대로 이동할 파티클 비율 - 0 ~ 1 사이의 값
mutation_swarm (float): 돌연변이가 일어날 확률
np_seed (int, optional): numpy seed. Defaults to None.
tf_seed (int, optional): tensorflow seed. Defaults to None.
"""
np.random.seed(np_seed)
tf.random.set_seed(tf_seed)
if np_seed is not None:
np.random.seed(np_seed)
if tf_seed is not None:
tf.random.set_seed(tf_seed)
self.model = model # 모델 구조
self.loss = loss # 손실함수
@@ -85,7 +89,7 @@ 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.uniform(-1, 2, len(w_))
w_ = np.random.uniform(-3, 3, len(w_))
m.set_weights(self._decode(w_, sh_, len_))
m.compile(loss=self.loss, optimizer="sgd", metrics=["accuracy"])
self.particles[i] = Particle(
@@ -210,7 +214,7 @@ class Optimizer:
epochs : int,
save : bool - True : save, False : not save
save_path : str ex) "./result",
renewal : str ex) "acc" or "loss",
renewal : str ex) "acc" or "loss" or "both",
empirical_balance : bool - True :
Dispersion : bool - True : g_best 의 값을 분산시켜 전역해를 찾음, False : g_best 의 값만 사용
check_point : int - 저장할 위치 - None : 저장 안함
@@ -247,6 +251,11 @@ class Optimizer:
self.g_best_score[1] = local_score[0]
self.g_best = p.get_best_weights()
self.g_best_ = p.get_best_weights()
elif renewal == "both":
if local_score[1] > self.g_best_score[0]:
self.g_best_score[0] = local_score[1]
self.g_best = p.get_best_weights()
self.g_best_ = p.get_best_weights()
if local_score[0] == None:
local_score[0] = np.inf
@@ -374,7 +383,19 @@ class Optimizer:
epochs_pbar.set_description(
f"best {self.g_best_score[0]:.4f} | {self.g_best_score[1]:.4f}"
)
elif renewal == "both":
if score[1] > self.g_best_score[0]:
self.g_best_score[0] = score[1]
self.g_best = self.particles[i].get_best_weights()
epochs_pbar.set_description(
f"best {self.g_best_score[0]:.4f} | {self.g_best_score[1]:.4f}"
)
if score[0] < self.g_best_score[1]:
self.g_best_score[1] = score[0]
self.g_best = self.particles[i].get_best_weights()
epochs_pbar.set_description(
f"best {self.g_best_score[0]:.4f} | {self.g_best_score[1]:.4f}"
)
if score[0] == None:
score[0] = np.inf
if score[1] == None: