mirror of
https://github.com/jung-geun/PSO.git
synced 2025-12-19 20:44:39 +09:00
23-07-06
dev container 실행 코드 추가
This commit is contained in:
@@ -1,16 +0,0 @@
|
|||||||
FROM mcr.microsoft.com/devcontainers/miniconda:0-3
|
|
||||||
|
|
||||||
# Copy environment.yml (if found) to a temp location so we update the environment. Also
|
|
||||||
# copy "noop.txt" so the COPY instruction does not fail if no environment.yml exists.
|
|
||||||
COPY environment.yml* .devcontainer/noop.txt /tmp/conda-tmp/
|
|
||||||
RUN if [ -f "/tmp/conda-tmp/environment.yml" ]; then umask 0002 && /opt/conda/bin/conda env update -n base -f /tmp/conda-tmp/environment.yml; fi \
|
|
||||||
&& rm -rf /tmp/conda-tmp
|
|
||||||
|
|
||||||
# [Optional] Uncomment to install a different version of Python than the default
|
|
||||||
# RUN conda install -y python=3.6 \
|
|
||||||
# && pip install --no-cache-dir pipx \
|
|
||||||
# && pipx reinstall-all
|
|
||||||
|
|
||||||
# [Optional] Uncomment this section to install additional OS packages.
|
|
||||||
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
|
|
||||||
# && apt-get -y install --no-install-recommends <your-package-list-here>
|
|
||||||
@@ -20,12 +20,9 @@
|
|||||||
"ghcr.io/devcontainers/features/python:1": {
|
"ghcr.io/devcontainers/features/python:1": {
|
||||||
"installTools": true,
|
"installTools": true,
|
||||||
"version": "3.9"
|
"version": "3.9"
|
||||||
},
|
|
||||||
"ghcr.io/rocker-org/devcontainer-features/miniforge:1": {
|
|
||||||
"version": "latest",
|
|
||||||
"variant": "Miniforge-pypy3"
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
"postCreateCommand": "conda env create --file environment.yaml --name pso"
|
||||||
// Features to add to the dev container. More info: https://containers.dev/features.
|
// Features to add to the dev container. More info: https://containers.dev/features.
|
||||||
// "features": {},
|
// "features": {},
|
||||||
// Use 'forwardPorts' to make a list of ports inside the container available locally.
|
// Use 'forwardPorts' to make a list of ports inside the container available locally.
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
This file is copied into the container along with environment.yml* from the
|
|
||||||
parent folder. This is done to prevent the Dockerfile COPY instruction from
|
|
||||||
failing if no environment.yml is found.
|
|
||||||
2
.github/workflows/python-package-conda.yml
vendored
2
.github/workflows/python-package-conda.yml
vendored
@@ -20,5 +20,5 @@ jobs:
|
|||||||
echo $CONDA/bin >> $GITHUB_PATH
|
echo $CONDA/bin >> $GITHUB_PATH
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
conda env update --file environment.yaml --name pso
|
conda env create --file environment.yaml --name pso
|
||||||
conda activate pso
|
conda activate pso
|
||||||
6
.vscode/settings.json
vendored
Normal file
6
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"[python]": {
|
||||||
|
"editor.defaultFormatter": "ms-python.black-formatter"
|
||||||
|
},
|
||||||
|
"python.formatting.provider": "none"
|
||||||
|
}
|
||||||
48
iris.py
48
iris.py
@@ -1,6 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
|
||||||
|
|
||||||
import gc
|
import gc
|
||||||
|
|
||||||
@@ -16,12 +16,13 @@ from tensorflow.keras.models import Sequential
|
|||||||
|
|
||||||
def make_model():
|
def make_model():
|
||||||
model = Sequential()
|
model = Sequential()
|
||||||
model.add(layers.Dense(10, activation='relu', input_shape=(4,)))
|
model.add(layers.Dense(10, activation="relu", input_shape=(4,)))
|
||||||
model.add(layers.Dense(10, activation='relu'))
|
model.add(layers.Dense(10, activation="relu"))
|
||||||
model.add(layers.Dense(3, activation='softmax'))
|
model.add(layers.Dense(3, activation="softmax"))
|
||||||
|
|
||||||
return model
|
return model
|
||||||
|
|
||||||
|
|
||||||
def load_data():
|
def load_data():
|
||||||
iris = load_iris()
|
iris = load_iris()
|
||||||
x = iris.data
|
x = iris.data
|
||||||
@@ -29,37 +30,40 @@ def load_data():
|
|||||||
|
|
||||||
y = keras.utils.to_categorical(y, 3)
|
y = keras.utils.to_categorical(y, 3)
|
||||||
|
|
||||||
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, shuffle=True, stratify=y)
|
x_train, x_test, y_train, y_test = train_test_split(
|
||||||
|
x, y, test_size=0.2, shuffle=True, stratify=y
|
||||||
|
)
|
||||||
|
|
||||||
return x_train, x_test, y_train, y_test
|
return x_train, x_test, y_train, y_test
|
||||||
|
|
||||||
|
|
||||||
model = make_model()
|
model = make_model()
|
||||||
x_train, x_test, y_train, y_test = load_data()
|
x_train, x_test, y_train, y_test = load_data()
|
||||||
|
|
||||||
loss = ['categorical_crossentropy']
|
loss = ["categorical_crossentropy"]
|
||||||
|
|
||||||
pso_iris = Optimizer(
|
pso_iris = Optimizer(
|
||||||
model,
|
model,
|
||||||
loss=loss[0],
|
loss=loss[0],
|
||||||
n_particles=100,
|
n_particles=100,
|
||||||
c0=0.4,
|
c0=0.4,
|
||||||
c1=0.8,
|
c1=0.8,
|
||||||
w_min=0.7,
|
w_min=0.7,
|
||||||
w_max=1.0,
|
w_max=1.0,
|
||||||
negative_swarm=0.1,
|
negative_swarm=0.1,
|
||||||
mutation_swarm=0.2,
|
mutation_swarm=0.2,
|
||||||
)
|
)
|
||||||
|
|
||||||
best_score = pso_iris.fit(
|
best_score = pso_iris.fit(
|
||||||
x_train,
|
x_train,
|
||||||
y_train,
|
y_train,
|
||||||
epochs=200,
|
epochs=200,
|
||||||
save=True,
|
save=True,
|
||||||
save_path="./result/iris",
|
save_path="./result/iris",
|
||||||
renewal="acc",
|
renewal="acc",
|
||||||
empirical_balance=False,
|
empirical_balance=False,
|
||||||
Dispersion=False,
|
Dispersion=False,
|
||||||
check_point=25
|
check_point=25,
|
||||||
)
|
)
|
||||||
|
|
||||||
gc.collect()
|
gc.collect()
|
||||||
|
|||||||
55
mnist.py
55
mnist.py
@@ -1,7 +1,7 @@
|
|||||||
# %%
|
# %%
|
||||||
import os
|
import os
|
||||||
|
|
||||||
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
|
||||||
|
|
||||||
import gc
|
import gc
|
||||||
|
|
||||||
@@ -26,58 +26,73 @@ def get_data():
|
|||||||
print(f"x_test : {x_test[0].shape} | y_test : {y_test[0].shape}")
|
print(f"x_test : {x_test[0].shape} | y_test : {y_test[0].shape}")
|
||||||
return x_train, y_train, x_test, y_test
|
return x_train, y_train, x_test, y_test
|
||||||
|
|
||||||
|
|
||||||
def get_data_test():
|
def get_data_test():
|
||||||
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
||||||
x_test = x_test.reshape((10000, 28, 28, 1))
|
x_test = x_test.reshape((10000, 28, 28, 1))
|
||||||
|
|
||||||
return x_test, y_test
|
return x_test, y_test
|
||||||
|
|
||||||
|
|
||||||
def make_model():
|
def make_model():
|
||||||
model = Sequential()
|
model = Sequential()
|
||||||
model.add(Conv2D(32, kernel_size=(5, 5),
|
model.add(
|
||||||
activation='relu', input_shape=(28, 28, 1)))
|
Conv2D(32, kernel_size=(5, 5), activation="relu", input_shape=(28, 28, 1))
|
||||||
|
)
|
||||||
model.add(MaxPooling2D(pool_size=(3, 3)))
|
model.add(MaxPooling2D(pool_size=(3, 3)))
|
||||||
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
|
model.add(Conv2D(64, kernel_size=(3, 3), activation="relu"))
|
||||||
model.add(MaxPooling2D(pool_size=(2, 2)))
|
model.add(MaxPooling2D(pool_size=(2, 2)))
|
||||||
model.add(Dropout(0.25))
|
model.add(Dropout(0.25))
|
||||||
model.add(Flatten())
|
model.add(Flatten())
|
||||||
model.add(Dense(128, activation='relu'))
|
model.add(Dense(128, activation="relu"))
|
||||||
model.add(Dense(10, activation='softmax'))
|
model.add(Dense(10, activation="softmax"))
|
||||||
|
|
||||||
return model
|
return model
|
||||||
|
|
||||||
|
|
||||||
# %%
|
# %%
|
||||||
model = make_model()
|
model = make_model()
|
||||||
x_test, y_test = get_data_test()
|
x_test, y_test = get_data_test()
|
||||||
|
|
||||||
loss = ['mse', 'categorical_crossentropy', 'binary_crossentropy', 'kullback_leibler_divergence', 'poisson', 'cosine_similarity', 'log_cosh', 'huber_loss', 'mean_absolute_error', 'mean_absolute_percentage_error']
|
loss = [
|
||||||
|
"mse",
|
||||||
|
"categorical_crossentropy",
|
||||||
|
"binary_crossentropy",
|
||||||
|
"kullback_leibler_divergence",
|
||||||
|
"poisson",
|
||||||
|
"cosine_similarity",
|
||||||
|
"log_cosh",
|
||||||
|
"huber_loss",
|
||||||
|
"mean_absolute_error",
|
||||||
|
"mean_absolute_percentage_error",
|
||||||
|
]
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
try:
|
try:
|
||||||
pso_mnist = Optimizer(
|
pso_mnist = Optimizer(
|
||||||
model,
|
model,
|
||||||
loss=loss[0],
|
loss=loss[0],
|
||||||
n_particles=100,
|
n_particles=100,
|
||||||
c0=0.35,
|
c0=0.35,
|
||||||
c1=0.8,
|
c1=0.8,
|
||||||
w_min=0.7,
|
w_min=0.7,
|
||||||
w_max=1.0,
|
w_max=1.1,
|
||||||
negative_swarm=0.2,
|
negative_swarm=0.2,
|
||||||
mutation_swarm=0.2,
|
mutation_swarm=0.1,
|
||||||
)
|
)
|
||||||
|
|
||||||
best_score = pso_mnist.fit(
|
best_score = pso_mnist.fit(
|
||||||
x_test,
|
x_test,
|
||||||
y_test,
|
y_test,
|
||||||
epochs=200,
|
epochs=200,
|
||||||
save=True,
|
save=True,
|
||||||
save_path="./result/mnist",
|
save_path="./result/mnist",
|
||||||
renewal="acc",
|
renewal="acc",
|
||||||
empirical_balance=False,
|
empirical_balance=False,
|
||||||
Dispersion=False,
|
Dispersion=False,
|
||||||
check_point=25
|
check_point=25,
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
finally:
|
finally:
|
||||||
gc.collect()
|
gc.collect()
|
||||||
|
|||||||
@@ -15,10 +15,13 @@ gpus = tf.config.experimental.list_physical_devices("GPU")
|
|||||||
if gpus:
|
if gpus:
|
||||||
try:
|
try:
|
||||||
# tf.config.experimental.set_visible_devices(gpus[0], "GPU")
|
# tf.config.experimental.set_visible_devices(gpus[0], "GPU")
|
||||||
|
print(tf.config.experimental.get_visible_devices("GPU"))
|
||||||
tf.config.experimental.set_memory_growth(gpus[0], True)
|
tf.config.experimental.set_memory_growth(gpus[0], True)
|
||||||
|
print("set memory growth")
|
||||||
except RuntimeError as e:
|
except RuntimeError as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
|
|
||||||
class Optimizer:
|
class Optimizer:
|
||||||
"""
|
"""
|
||||||
particle swarm optimization
|
particle swarm optimization
|
||||||
@@ -41,7 +44,7 @@ class Optimizer:
|
|||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
particle swarm optimization
|
particle swarm optimization
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
model (keras.models): 모델 구조
|
model (keras.models): 모델 구조
|
||||||
loss (str): 손실함수
|
loss (str): 손실함수
|
||||||
@@ -55,7 +58,7 @@ class Optimizer:
|
|||||||
"""
|
"""
|
||||||
np.random.seed(np_seed)
|
np.random.seed(np_seed)
|
||||||
tf.random.set_seed(tf_seed)
|
tf.random.set_seed(tf_seed)
|
||||||
|
|
||||||
self.model = model # 모델 구조
|
self.model = model # 모델 구조
|
||||||
self.loss = loss # 손실함수
|
self.loss = loss # 손실함수
|
||||||
self.n_particles = n_particles # 파티클 개수
|
self.n_particles = n_particles # 파티클 개수
|
||||||
@@ -66,11 +69,12 @@ class Optimizer:
|
|||||||
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.mutation_swarm = mutation_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 # 최고 점수를 받은 가중치 - 값의 분산을 위한 변수
|
||||||
self.avg_score = 0 # 평균 점수
|
self.avg_score = 0 # 평균 점수
|
||||||
|
self.save_path = None # 저장 위치
|
||||||
|
|
||||||
negative_count = 0
|
negative_count = 0
|
||||||
|
|
||||||
for i in tqdm(range(self.n_particles), desc="Initializing Particles"):
|
for i in tqdm(range(self.n_particles), desc="Initializing Particles"):
|
||||||
@@ -81,16 +85,18 @@ class Optimizer:
|
|||||||
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(
|
self.particles[i] = Particle(
|
||||||
m,
|
m,
|
||||||
loss,
|
loss,
|
||||||
negative=True if i < negative_swarm * self.n_particles else False,
|
negative=True if i < negative_swarm * self.n_particles else False,
|
||||||
mutation=mutation_swarm,
|
mutation=mutation_swarm,
|
||||||
)
|
)
|
||||||
if i < negative_swarm * self.n_particles:
|
if i < negative_swarm * self.n_particles:
|
||||||
negative_count += 1
|
negative_count += 1
|
||||||
|
|
||||||
print(f"negative swarm : {negative_count} / {self.n_particles}")
|
print(f"negative swarm : {negative_count} / {self.n_particles}")
|
||||||
print(f"mutation swarm : {mutation_swarm * self.n_particles} / {self.n_particles}")
|
print(
|
||||||
|
f"mutation swarm : {mutation_swarm * self.n_particles} / {self.n_particles}"
|
||||||
|
)
|
||||||
|
|
||||||
gc.collect()
|
gc.collect()
|
||||||
|
|
||||||
@@ -110,11 +116,10 @@ class Optimizer:
|
|||||||
del self.avg_score
|
del self.avg_score
|
||||||
gc.collect()
|
gc.collect()
|
||||||
|
|
||||||
|
|
||||||
def _encode(self, weights):
|
def _encode(self, weights):
|
||||||
"""
|
"""
|
||||||
가중치를 1차원으로 풀어서 반환
|
가중치를 1차원으로 풀어서 반환
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
weights (list) : keras model의 가중치
|
weights (list) : keras model의 가중치
|
||||||
Returns:
|
Returns:
|
||||||
@@ -132,15 +137,14 @@ class Optimizer:
|
|||||||
w_gpu = np.append(w_gpu, w_)
|
w_gpu = np.append(w_gpu, w_)
|
||||||
|
|
||||||
del weights
|
del weights
|
||||||
|
|
||||||
return w_gpu, shape, length
|
|
||||||
|
|
||||||
|
return w_gpu, shape, length
|
||||||
|
|
||||||
def _decode(self, weight, shape, length):
|
def _decode(self, weight, shape, length):
|
||||||
"""
|
"""
|
||||||
_encode 로 인코딩된 가중치를 원본 shape으로 복원
|
_encode 로 인코딩된 가중치를 원본 shape으로 복원
|
||||||
파라미터는 encode의 리턴값을 그대로 사용을 권장
|
파라미터는 encode의 리턴값을 그대로 사용을 권장
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
weight (numpy array): 가중치 - 1차원으로 풀어서 반환
|
weight (numpy array): 가중치 - 1차원으로 풀어서 반환
|
||||||
shape (list): 가중치의 원본 shape
|
shape (list): 가중치의 원본 shape
|
||||||
@@ -156,7 +160,7 @@ class Optimizer:
|
|||||||
w_ = np.reshape(w_, shape[i])
|
w_ = np.reshape(w_, shape[i])
|
||||||
weights.append(w_)
|
weights.append(w_)
|
||||||
start = end
|
start = end
|
||||||
|
|
||||||
del weight
|
del weight
|
||||||
del shape
|
del shape
|
||||||
del length
|
del length
|
||||||
@@ -183,7 +187,6 @@ class Optimizer:
|
|||||||
else:
|
else:
|
||||||
return 1 + np.abs(score)
|
return 1 + np.abs(score)
|
||||||
|
|
||||||
|
|
||||||
def fit(
|
def fit(
|
||||||
self,
|
self,
|
||||||
x,
|
x,
|
||||||
@@ -204,7 +207,7 @@ class Optimizer:
|
|||||||
save : bool - True : save, False : not save
|
save : bool - True : save, False : not save
|
||||||
save_path : str ex) "./result",
|
save_path : str ex) "./result",
|
||||||
renewal : str ex) "acc" or "loss",
|
renewal : str ex) "acc" or "loss",
|
||||||
empirical_balance : bool - True :
|
empirical_balance : bool - True :
|
||||||
Dispersion : bool - True : g_best 의 값을 분산시켜 전역해를 찾음, False : g_best 의 값만 사용
|
Dispersion : bool - True : g_best 의 값을 분산시켜 전역해를 찾음, False : g_best 의 값만 사용
|
||||||
check_point : int - 저장할 위치 - None : 저장 안함
|
check_point : int - 저장할 위치 - None : 저장 안함
|
||||||
"""
|
"""
|
||||||
@@ -226,7 +229,7 @@ class Optimizer:
|
|||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
print(e)
|
print(e)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
for i in tqdm(range(self.n_particles), desc="Initializing velocity"):
|
for i in tqdm(range(self.n_particles), desc="Initializing velocity"):
|
||||||
p = self.particles[i]
|
p = self.particles[i]
|
||||||
local_score = p.get_score(x, y, renewal=renewal)
|
local_score = p.get_score(x, y, renewal=renewal)
|
||||||
@@ -247,7 +250,7 @@ class Optimizer:
|
|||||||
|
|
||||||
if local_score[1] == None:
|
if local_score[1] == None:
|
||||||
local_score[1] = 0
|
local_score[1] = 0
|
||||||
|
|
||||||
if save:
|
if save:
|
||||||
with open(
|
with open(
|
||||||
f"./{save_path}/{self.day}_{self.n_particles}_{epochs}_{self.c0}_{self.c1}_{self.w_min}_{renewal}.csv",
|
f"./{save_path}/{self.day}_{self.n_particles}_{epochs}_{self.c0}_{self.c1}_{self.w_min}_{renewal}.csv",
|
||||||
@@ -258,15 +261,22 @@ class Optimizer:
|
|||||||
f.write(", ")
|
f.write(", ")
|
||||||
else:
|
else:
|
||||||
f.write("\n")
|
f.write("\n")
|
||||||
|
|
||||||
f.close()
|
f.close()
|
||||||
del local_score
|
del local_score
|
||||||
gc.collect()
|
gc.collect()
|
||||||
|
|
||||||
print(f"initial g_best_score : {self.g_best_score[0] if self.renewal == 'acc' else self.g_best_score[1]}")
|
print(
|
||||||
|
f"initial g_best_score : {self.g_best_score[0] if self.renewal == 'acc' else self.g_best_score[1]}"
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
epochs_pbar = tqdm(range(epochs), desc=f"best {self.g_best_score[0]:.4f}|{self.g_best_score[1]:.4f}", ascii=True, leave=True)
|
epochs_pbar = tqdm(
|
||||||
|
range(epochs),
|
||||||
|
desc=f"best {self.g_best_score[0]:.4f}|{self.g_best_score[1]:.4f}",
|
||||||
|
ascii=True,
|
||||||
|
leave=True,
|
||||||
|
)
|
||||||
for epoch in epochs_pbar:
|
for epoch in epochs_pbar:
|
||||||
acc = 0
|
acc = 0
|
||||||
loss = 0
|
loss = 0
|
||||||
@@ -277,9 +287,16 @@ class Optimizer:
|
|||||||
|
|
||||||
ts = self.c0 + np.random.rand() * (self.c1 - self.c0)
|
ts = self.c0 + np.random.rand() * (self.c1 - self.c0)
|
||||||
|
|
||||||
part_pbar = tqdm(range(len(self.particles)), desc=f"acc : {max_score:.4f} loss : {min_loss:.4f}", ascii=True, leave=False)
|
part_pbar = tqdm(
|
||||||
|
range(len(self.particles)),
|
||||||
|
desc=f"acc : {max_score:.4f} loss : {min_loss:.4f}",
|
||||||
|
ascii=True,
|
||||||
|
leave=False,
|
||||||
|
)
|
||||||
for i in part_pbar:
|
for i in part_pbar:
|
||||||
part_pbar.set_description(f"acc : {max_score:.4f} loss : {min_loss:.4f}")
|
part_pbar.set_description(
|
||||||
|
f"acc : {max_score:.4f} loss : {min_loss:.4f}"
|
||||||
|
)
|
||||||
w = self.w_max - (self.w_max - self.w_min) * epoch / epochs
|
w = self.w_max - (self.w_max - self.w_min) * epoch / epochs
|
||||||
|
|
||||||
g_, g_sh, g_len = self._encode(self.g_best)
|
g_, g_sh, g_len = self._encode(self.g_best)
|
||||||
@@ -298,7 +315,7 @@ class Optimizer:
|
|||||||
w_g_ = self.f(x, y, self.g_best)
|
w_g_ = self.f(x, y, self.g_best)
|
||||||
w_p = w_p_ / (w_p_ + w_g_)
|
w_p = w_p_ / (w_p_ + w_g_)
|
||||||
w_g = w_p_ / (w_p_ + w_g_)
|
w_g = w_p_ / (w_p_ + w_g_)
|
||||||
|
|
||||||
del w_p_
|
del w_p_
|
||||||
del w_g_
|
del w_g_
|
||||||
|
|
||||||
@@ -315,7 +332,7 @@ class Optimizer:
|
|||||||
p_ = np.exp(-1 * p_)
|
p_ = np.exp(-1 * p_)
|
||||||
w_p = p_
|
w_p = p_
|
||||||
w_g = 1 - p_
|
w_g = 1 - p_
|
||||||
|
|
||||||
del p_b
|
del p_b
|
||||||
del g_a
|
del g_a
|
||||||
del l_b
|
del l_b
|
||||||
@@ -339,7 +356,9 @@ class Optimizer:
|
|||||||
if score[0] < self.g_best_score[1]:
|
if score[0] < self.g_best_score[1]:
|
||||||
self.g_best_score[1] = score[0]
|
self.g_best_score[1] = score[0]
|
||||||
self.g_best = self.particles[i].get_best_weights()
|
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}")
|
epochs_pbar.set_description(
|
||||||
|
f"best {self.g_best_score[0]:.4f} | {self.g_best_score[1]:.4f}"
|
||||||
|
)
|
||||||
elif renewal == "loss":
|
elif renewal == "loss":
|
||||||
if score[0] <= self.g_best_score[1]:
|
if score[0] <= self.g_best_score[1]:
|
||||||
if score[0] < self.g_best_score[1]:
|
if score[0] < self.g_best_score[1]:
|
||||||
@@ -349,7 +368,9 @@ class Optimizer:
|
|||||||
if score[1] > self.g_best_score[0]:
|
if score[1] > self.g_best_score[0]:
|
||||||
self.g_best_score[0] = score[1]
|
self.g_best_score[0] = score[1]
|
||||||
self.g_best = self.particles[i].get_best_weights()
|
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}")
|
epochs_pbar.set_description(
|
||||||
|
f"best {self.g_best_score[0]:.4f} | {self.g_best_score[1]:.4f}"
|
||||||
|
)
|
||||||
|
|
||||||
if score[0] == None:
|
if score[0] == None:
|
||||||
score[0] = np.inf
|
score[0] = np.inf
|
||||||
@@ -386,7 +407,7 @@ class Optimizer:
|
|||||||
self.avg_score = acc / self.n_particles
|
self.avg_score = acc / self.n_particles
|
||||||
|
|
||||||
gc.collect()
|
gc.collect()
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("Ctrl + C : Stop Training")
|
print("Ctrl + C : Stop Training")
|
||||||
except MemoryError:
|
except MemoryError:
|
||||||
@@ -407,7 +428,7 @@ class Optimizer:
|
|||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
(keras.models): 모델
|
(keras.models): 모델
|
||||||
"""
|
"""
|
||||||
model = keras.models.model_from_json(self.model.to_json())
|
model = keras.models.model_from_json(self.model.to_json())
|
||||||
model.set_weights(self.g_best)
|
model.set_weights(self.g_best)
|
||||||
model.compile(loss=self.loss, optimizer="sgd", metrics=["accuracy"])
|
model.compile(loss=self.loss, optimizer="sgd", metrics=["accuracy"])
|
||||||
@@ -459,7 +480,7 @@ class Optimizer:
|
|||||||
"a",
|
"a",
|
||||||
) as f:
|
) as f:
|
||||||
json.dump(json_save, f, indent=4)
|
json.dump(json_save, f, indent=4)
|
||||||
|
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
def _check_point_save(self, save_path: str = f"./result/check_point"):
|
def _check_point_save(self, save_path: str = f"./result/check_point"):
|
||||||
|
|||||||
@@ -15,7 +15,10 @@ class Particle:
|
|||||||
4. 가중치 업데이트
|
4. 가중치 업데이트
|
||||||
5. 2번으로 돌아가서 반복
|
5. 2번으로 돌아가서 반복
|
||||||
"""
|
"""
|
||||||
def __init__(self, model: keras.models, loss, negative: bool = False, mutation: float = 0):
|
|
||||||
|
def __init__(
|
||||||
|
self, model: keras.models, loss, negative: bool = False, mutation: float = 0
|
||||||
|
):
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
model (keras.models): 학습 및 검증을 위한 모델
|
model (keras.models): 학습 및 검증을 위한 모델
|
||||||
@@ -36,7 +39,7 @@ class Particle:
|
|||||||
del i_w_, s_, l_
|
del i_w_, s_, l_
|
||||||
del init_weights
|
del init_weights
|
||||||
gc.collect()
|
gc.collect()
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
del self.model
|
del self.model
|
||||||
del self.loss
|
del self.loss
|
||||||
@@ -49,7 +52,7 @@ class Particle:
|
|||||||
def _encode(self, weights: list):
|
def _encode(self, weights: list):
|
||||||
"""
|
"""
|
||||||
가중치를 1차원으로 풀어서 반환
|
가중치를 1차원으로 풀어서 반환
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
weights (list) : keras model의 가중치
|
weights (list) : keras model의 가중치
|
||||||
Returns:
|
Returns:
|
||||||
@@ -68,12 +71,11 @@ class Particle:
|
|||||||
|
|
||||||
return w_gpu, shape, length
|
return w_gpu, shape, length
|
||||||
|
|
||||||
|
|
||||||
def _decode(self, weight: list, shape, length):
|
def _decode(self, weight: list, shape, length):
|
||||||
"""
|
"""
|
||||||
_encode 로 인코딩된 가중치를 원본 shape으로 복원
|
_encode 로 인코딩된 가중치를 원본 shape으로 복원
|
||||||
파라미터는 encode의 리턴값을 그대로 사용을 권장
|
파라미터는 encode의 리턴값을 그대로 사용을 권장
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
weight (numpy array): 가중치 - 1차원으로 풀어서 반환
|
weight (numpy array): 가중치 - 1차원으로 풀어서 반환
|
||||||
shape (list): 가중치의 원본 shape
|
shape (list): 가중치의 원본 shape
|
||||||
@@ -114,7 +116,7 @@ class Particle:
|
|||||||
self.best_score = score[1]
|
self.best_score = score[1]
|
||||||
self.best_weights = self.model.get_weights()
|
self.best_weights = self.model.get_weights()
|
||||||
elif renewal == "loss":
|
elif renewal == "loss":
|
||||||
if score[0] == 'nan':
|
if score[0] == "nan":
|
||||||
score[0] = np.inf
|
score[0] = np.inf
|
||||||
if score[0] < self.best_score:
|
if score[0] < self.best_score:
|
||||||
self.best_score = score[0]
|
self.best_score = score[0]
|
||||||
@@ -150,13 +152,13 @@ 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 np.random.rand() < self.mutation:
|
if np.random.rand() < self.mutation:
|
||||||
m_v = np.random.uniform(-0.1, 0.1, len(encode_v))
|
m_v = np.random.uniform(-0.1, 0.1, len(encode_v))
|
||||||
new_v = m_v
|
new_v = m_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
|
||||||
del encode_p, p_sh, p_len
|
del encode_p, p_sh, p_len
|
||||||
@@ -182,7 +184,7 @@ class Particle:
|
|||||||
encode_g, g_sh, g_len = self._encode(weights=g_best)
|
encode_g, g_sh, g_len = self._encode(weights=g_best)
|
||||||
r0 = np.random.rand()
|
r0 = np.random.rand()
|
||||||
r1 = np.random.rand()
|
r1 = np.random.rand()
|
||||||
|
|
||||||
if self.negative:
|
if self.negative:
|
||||||
new_v = (
|
new_v = (
|
||||||
w * encode_v
|
w * encode_v
|
||||||
@@ -195,13 +197,13 @@ 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 np.random.rand() < self.mutation:
|
if np.random.rand() < self.mutation:
|
||||||
m_v = np.random.uniform(-0.1, 0.1, len(encode_v))
|
m_v = np.random.uniform(-0.1, 0.1, len(encode_v))
|
||||||
new_v = m_v
|
new_v = m_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
|
||||||
del encode_p, p_sh, p_len
|
del encode_p, p_sh, p_len
|
||||||
@@ -216,7 +218,7 @@ class Particle:
|
|||||||
encode_v, v_sh, v_len = self._encode(weights=self.velocities)
|
encode_v, v_sh, v_len = self._encode(weights=self.velocities)
|
||||||
new_w = encode_w + encode_v
|
new_w = encode_w + encode_v
|
||||||
self.model.set_weights(self._decode(new_w, w_sh, w_len))
|
self.model.set_weights(self._decode(new_w, 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
|
||||||
|
|
||||||
@@ -234,7 +236,7 @@ class Particle:
|
|||||||
"""
|
"""
|
||||||
self.model.set_weights(weights)
|
self.model.set_weights(weights)
|
||||||
score = self.model.evaluate(x, y, verbose=0)[1]
|
score = self.model.evaluate(x, y, verbose=0)[1]
|
||||||
|
|
||||||
if score > 0:
|
if score > 0:
|
||||||
return 1 / (1 + score)
|
return 1 / (1 + score)
|
||||||
else:
|
else:
|
||||||
@@ -258,7 +260,7 @@ class Particle:
|
|||||||
"""
|
"""
|
||||||
self._update_velocity(local_rate, global_rate, w, g_best)
|
self._update_velocity(local_rate, global_rate, w, g_best)
|
||||||
self._update_weights()
|
self._update_weights()
|
||||||
|
|
||||||
return self.get_score(x, y, renewal)
|
return self.get_score(x, y, renewal)
|
||||||
|
|
||||||
def step_w(
|
def step_w(
|
||||||
@@ -284,7 +286,7 @@ class Particle:
|
|||||||
"""
|
"""
|
||||||
self._update_velocity_w(local_rate, global_rate, w, w_p, w_g, g_best)
|
self._update_velocity_w(local_rate, global_rate, w, w_p, w_g, g_best)
|
||||||
self._update_weights()
|
self._update_weights()
|
||||||
|
|
||||||
return self.get_score(x, y, renewal)
|
return self.get_score(x, y, renewal)
|
||||||
|
|
||||||
def get_best_score(self):
|
def get_best_score(self):
|
||||||
|
|||||||
52
xor.py
52
xor.py
@@ -1,10 +1,11 @@
|
|||||||
# %%
|
# %%
|
||||||
import os
|
import os
|
||||||
|
|
||||||
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import tensorflow as tf
|
import tensorflow as tf
|
||||||
|
|
||||||
# from pso_tf import PSO
|
# from pso_tf import PSO
|
||||||
from pso import Optimizer
|
from pso import Optimizer
|
||||||
from tensorflow import keras
|
from tensorflow import keras
|
||||||
@@ -14,31 +15,64 @@ from tensorflow.keras.models import Sequential
|
|||||||
print(tf.__version__)
|
print(tf.__version__)
|
||||||
print(tf.config.list_physical_devices())
|
print(tf.config.list_physical_devices())
|
||||||
|
|
||||||
|
|
||||||
def get_data():
|
def get_data():
|
||||||
x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
|
x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
|
||||||
y = np.array([[0], [1], [1], [0]])
|
y = np.array([[0], [1], [1], [0]])
|
||||||
return x, y
|
return x, y
|
||||||
|
|
||||||
|
|
||||||
def make_model():
|
def make_model():
|
||||||
leyer = []
|
leyer = []
|
||||||
leyer.append(layers.Dense(2, activation='sigmoid', input_shape=(2,)))
|
leyer.append(layers.Dense(2, activation="sigmoid", input_shape=(2,)))
|
||||||
# leyer.append(layers.Dense(2, activation='sigmoid'))
|
# leyer.append(layers.Dense(2, activation='sigmoid'))
|
||||||
leyer.append(layers.Dense(1, activation='sigmoid'))
|
leyer.append(layers.Dense(1, activation="sigmoid"))
|
||||||
|
|
||||||
model = Sequential(leyer)
|
model = Sequential(leyer)
|
||||||
|
|
||||||
return model
|
return model
|
||||||
|
|
||||||
|
|
||||||
# %%
|
# %%
|
||||||
model = make_model()
|
model = make_model()
|
||||||
x_test, y_test = get_data()
|
x_test, y_test = get_data()
|
||||||
|
|
||||||
loss = ['mean_squared_error', 'mean_squared_logarithmic_error', 'binary_crossentropy', 'categorical_crossentropy', 'sparse_categorical_crossentropy', 'kullback_leibler_divergence', 'poisson', 'cosine_similarity', 'log_cosh', 'huber_loss', 'mean_absolute_error', 'mean_absolute_percentage_error']
|
loss = [
|
||||||
|
"mean_squared_error",
|
||||||
|
"mean_squared_logarithmic_error",
|
||||||
|
"binary_crossentropy",
|
||||||
|
"categorical_crossentropy",
|
||||||
|
"sparse_categorical_crossentropy",
|
||||||
|
"kullback_leibler_divergence",
|
||||||
|
"poisson",
|
||||||
|
"cosine_similarity",
|
||||||
|
"log_cosh",
|
||||||
|
"huber_loss",
|
||||||
|
"mean_absolute_error",
|
||||||
|
"mean_absolute_percentage_error",
|
||||||
|
]
|
||||||
|
|
||||||
pso_xor = Optimizer(model,
|
pso_xor = Optimizer(
|
||||||
loss=loss[0], n_particles=75, c0=0.35, c1=0.8, w_min=0.6, w_max=1.2, negative_swarm=0.25, mutation_swarm=0.25)
|
model,
|
||||||
|
loss=loss[0],
|
||||||
|
n_particles=75,
|
||||||
|
c0=0.35,
|
||||||
|
c1=0.8,
|
||||||
|
w_min=0.6,
|
||||||
|
w_max=1.2,
|
||||||
|
negative_swarm=0.25,
|
||||||
|
mutation_swarm=0.25,
|
||||||
|
)
|
||||||
best_score = pso_xor.fit(
|
best_score = pso_xor.fit(
|
||||||
x_test, y_test, epochs=200, save=True, save_path="./result/xor", renewal="acc", empirical_balance=False, Dispersion=False, check_point=25)
|
x_test,
|
||||||
|
y_test,
|
||||||
|
epochs=200,
|
||||||
|
save=True,
|
||||||
|
save_path="./result/xor",
|
||||||
|
renewal="acc",
|
||||||
|
empirical_balance=False,
|
||||||
|
Dispersion=False,
|
||||||
|
check_point=25,
|
||||||
|
)
|
||||||
|
|
||||||
# %%
|
# %%
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user