23-05-24 | 2

pyplot 을 추가하여 loss 와 acc 가 학습할때 어떻게 변화하는지 적용
This commit is contained in:
jung-geun
2023-05-24 15:39:17 +09:00
parent 27d40ab56c
commit 7a612e4ca7
4 changed files with 433 additions and 131 deletions

View File

@@ -9,7 +9,7 @@ class PSO(object):
Class implementing PSO algorithm
"""
def __init__(self, model: keras.models, x_train, y_train, loss_method=keras.losses.MeanSquaredError(), n_particles=5):
def __init__(self, model: keras.models, loss_method=keras.losses.MeanSquaredError(), n_particles=5):
"""
Initialize the key variables.
@@ -31,10 +31,8 @@ class PSO(object):
m = keras.models.model_from_json(self.model_structure)
m.compile(loss=self.loss_method,
optimizer="adam", metrics=["accuracy"])
# m.fit(x_train, y_train, epochs=1, batch_size=32, verbose=0) # 결과가 너무 좋지 않아서 처음 초기화 할때 어느정도 위치를 수정
self.particles_weights[_] = m.get_weights()
# print(f"shape > {self.particles_weights[_][0]}")
# self.particles_weights.append(particle_node)
@@ -78,7 +76,8 @@ class PSO(object):
n_particles)] # 각 파티클의 최적값의 점수
self.g_best_score = 0 # 전역 최적값의 점수(초기화 - 무한대)
self.g_history = []
self.all_cost_history = [[] for i in range(n_particles)]
self.loss_history = [[] for i in range(n_particles)]
self.acc_history = [[] for i in range(n_particles)]
self.g_best_score_history = []
self.history = []
@@ -224,12 +223,13 @@ class PSO(object):
if score[1] > self.g_best_score:
self.g_best_score = score[1]
self.g_best = self.particles_weights[i].copy()
self.g_history.append(self.g_best)
self.g_history.append(self.g_best.copy())
self.g_best_score_history.append(
self.g_best_score)
self.score = score
self.all_cost_history[i].append(score)
self.loss_history[i].append(score[0])
self.acc_history[i].append(score[1])
# if self.func(self.particles_weights[i]) < self.func(p_best):
# self.p_best[i] = self.particles_weights[i]
# if self.
@@ -240,7 +240,7 @@ class PSO(object):
# self.g_history.append(self.g_best)
# print(f"{i} particle score : {score[0]}")
print(
f"loss avg : {self.score[0]/self.n_particles} | acc avg : {self.score[1]/self.n_particles} | best loss : {self.g_best_score}")
f"loss avg : {self.score[0]/self.n_particles} | acc avg : {self.score[1]/self.n_particles} | best score : {self.g_best_score}")
# self.history.append(self.particles_weights.copy())
@@ -278,6 +278,6 @@ class PSO(object):
def global_score_history(self):
return self.g_best_score_history.copy()
def all_cost(self):
return self.all_cost_history.copy()
def all_history(self):
return self.loss_history, self.acc_history.copy()