패키지 호출 단순 수정
This commit is contained in:
jung-geun
2023-06-24 03:31:40 +00:00
parent 2a28b7fa04
commit 983913f2d2
5 changed files with 70 additions and 94 deletions

View File

@@ -1,19 +1,18 @@
import gc
import json
import os
import sys
import tensorflow as tf
from tensorflow import keras
from datetime import datetime
import numpy as np
import tensorflow as tf
from pso.particle import Particle
from tensorflow import keras
from tqdm import tqdm
# import cupy as cp
from tqdm import tqdm
from datetime import datetime
import json
import gc
from copy import copy, deepcopy
from pso.particle import Particle
gpus = tf.config.experimental.list_physical_devices("GPU")
if gpus:
@@ -35,6 +34,7 @@ class Optimizer:
w_min=0.5,
w_max=1.5,
negative_swarm: float = 0,
momentun_swarm: float = 0,
):
"""
Args:
@@ -46,6 +46,7 @@ class Optimizer:
w_min (float): 최소 관성 수치
w_max (float): 최대 관성 수치
nefative_swarm (float): 최적해와 반대로 이동할 파티클 비율 - 0 ~ 1 사이의 값
momentun_swarm (float): 관성을 추가로 사용할 파티클 비율 - 0 ~ 1 사이의 값
"""
self.model = model # 모델 구조
self.loss = loss # 손실함수
@@ -56,6 +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.g_best_score = [0 , np.inf] # 최고 점수 - 시작은 0으로 초기화
self.g_best = None # 최고 점수를 받은 가중치
self.g_best_ = None # 최고 점수를 받은 가중치 - 값의 분산을 위한 변수
@@ -68,10 +70,8 @@ class Optimizer:
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"])
if i < negative_swarm * self.n_particles:
self.particles[i] = Particle(m, loss, negative=True)
else:
self.particles[i] = Particle(m, loss, negative=False)
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)
gc.collect()
def __del__(self):