패키지 호출 단순 수정
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,30 +1,28 @@
# %% # %%
import os import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf import tensorflow as tf
tf.random.set_seed(777) # for reproducibility tf.random.set_seed(777) # for reproducibility
import numpy as np import numpy as np
np.random.seed(777) np.random.seed(777)
from tensorflow import keras import gc
from keras.datasets import mnist from datetime import date
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
from keras import backend as K
from keras.datasets import mnist
from keras.layers import Conv2D, Dense, Dropout, Flatten, MaxPooling2D
from keras.models import Sequential
# from pso_tf import PSO # from pso_tf import PSO
from pso import Optimizer from pso import Optimizer
# from optimizer import Optimizer from tensorflow import keras
from datetime import date
from tqdm import tqdm from tqdm import tqdm
import gc
# print(tf.__version__) # print(tf.__version__)
# print(tf.config.list_physical_devices()) # print(tf.config.list_physical_devices())
# print(f"Num GPUs Available: {len(tf.config.list_physical_devices('GPU'))}") # print(f"Num GPUs Available: {len(tf.config.list_physical_devices('GPU'))}")
@@ -74,12 +72,13 @@ if __name__ == "__main__":
pso_mnist = Optimizer( pso_mnist = Optimizer(
model, model,
loss=loss[0], loss=loss[0],
n_particles=50, n_particles=75,
c0=0.35, c0=0.35,
c1=0.8, c1=0.8,
w_min=0.7, w_min=0.7,
w_max=1.15, w_max=1.15,
negative_swarm=0.25 negative_swarm=0.25,
momentun_swarm=0.25,
) )
best_score = pso_mnist.fit( best_score = pso_mnist.fit(
@@ -88,7 +87,7 @@ if __name__ == "__main__":
epochs=200, epochs=200,
save=True, save=True,
save_path="./result/mnist", save_path="./result/mnist",
renewal="acc", renewal="loss",
empirical_balance=False, empirical_balance=False,
Dispersion=False, Dispersion=False,
check_point=25 check_point=25

File diff suppressed because one or more lines are too long

View File

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

View File

@@ -1,15 +1,16 @@
import tensorflow as tf import gc
from tensorflow import keras
# import cupy as cp # import cupy as cp
import numpy as np import numpy as np
import gc import tensorflow as tf
from tensorflow import keras
class Particle: class Particle:
""" """
Particle Swarm Optimization의 Particle을 구현한 클래스 Particle Swarm Optimization의 Particle을 구현한 클래스
""" """
def __init__(self, model: keras.models, loss, negative: bool = False|True): def __init__(self, model: keras.models, loss, negative: bool = False, momentun: bool = False):
""" """
Args: Args:
model (keras.models): 학습 및 검증을 위한 모델 model (keras.models): 학습 및 검증을 위한 모델
@@ -23,6 +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.best_score = 0 self.best_score = 0
self.best_weights = init_weights self.best_weights = init_weights
@@ -146,6 +148,8 @@ 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:
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
del encode_v, v_sh, v_len del encode_v, v_sh, v_len
@@ -184,6 +188,8 @@ 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:
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
del encode_v, v_sh, v_len del encode_v, v_sh, v_len

File diff suppressed because one or more lines are too long