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": {
|
||||
"installTools": true,
|
||||
"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": {},
|
||||
// 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
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
conda env update --file environment.yaml --name pso
|
||||
conda env create --file environment.yaml --name 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
|
||||
|
||||
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
||||
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
|
||||
|
||||
import gc
|
||||
|
||||
@@ -16,12 +16,13 @@ from tensorflow.keras.models import Sequential
|
||||
|
||||
def make_model():
|
||||
model = Sequential()
|
||||
model.add(layers.Dense(10, activation='relu', input_shape=(4,)))
|
||||
model.add(layers.Dense(10, activation='relu'))
|
||||
model.add(layers.Dense(3, activation='softmax'))
|
||||
model.add(layers.Dense(10, activation="relu", input_shape=(4,)))
|
||||
model.add(layers.Dense(10, activation="relu"))
|
||||
model.add(layers.Dense(3, activation="softmax"))
|
||||
|
||||
return model
|
||||
|
||||
|
||||
def load_data():
|
||||
iris = load_iris()
|
||||
x = iris.data
|
||||
@@ -29,37 +30,40 @@ def load_data():
|
||||
|
||||
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
|
||||
|
||||
|
||||
model = make_model()
|
||||
x_train, x_test, y_train, y_test = load_data()
|
||||
|
||||
loss = ['categorical_crossentropy']
|
||||
loss = ["categorical_crossentropy"]
|
||||
|
||||
pso_iris = Optimizer(
|
||||
model,
|
||||
loss=loss[0],
|
||||
n_particles=100,
|
||||
c0=0.4,
|
||||
c1=0.8,
|
||||
loss=loss[0],
|
||||
n_particles=100,
|
||||
c0=0.4,
|
||||
c1=0.8,
|
||||
w_min=0.7,
|
||||
w_max=1.0,
|
||||
w_max=1.0,
|
||||
negative_swarm=0.1,
|
||||
mutation_swarm=0.2,
|
||||
)
|
||||
)
|
||||
|
||||
best_score = pso_iris.fit(
|
||||
x_train,
|
||||
y_train,
|
||||
epochs=200,
|
||||
save=True,
|
||||
save_path="./result/iris",
|
||||
renewal="acc",
|
||||
empirical_balance=False,
|
||||
Dispersion=False,
|
||||
check_point=25
|
||||
)
|
||||
x_train,
|
||||
y_train,
|
||||
epochs=200,
|
||||
save=True,
|
||||
save_path="./result/iris",
|
||||
renewal="acc",
|
||||
empirical_balance=False,
|
||||
Dispersion=False,
|
||||
check_point=25,
|
||||
)
|
||||
|
||||
gc.collect()
|
||||
|
||||
55
mnist.py
55
mnist.py
@@ -1,7 +1,7 @@
|
||||
# %%
|
||||
import os
|
||||
|
||||
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
||||
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
|
||||
|
||||
import gc
|
||||
|
||||
@@ -26,58 +26,73 @@ def get_data():
|
||||
print(f"x_test : {x_test[0].shape} | y_test : {y_test[0].shape}")
|
||||
return x_train, y_train, x_test, y_test
|
||||
|
||||
|
||||
def get_data_test():
|
||||
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
||||
x_test = x_test.reshape((10000, 28, 28, 1))
|
||||
|
||||
|
||||
return x_test, y_test
|
||||
|
||||
|
||||
def make_model():
|
||||
model = Sequential()
|
||||
model.add(Conv2D(32, kernel_size=(5, 5),
|
||||
activation='relu', input_shape=(28, 28, 1)))
|
||||
model.add(
|
||||
Conv2D(32, kernel_size=(5, 5), activation="relu", input_shape=(28, 28, 1))
|
||||
)
|
||||
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(Dropout(0.25))
|
||||
model.add(Flatten())
|
||||
model.add(Dense(128, activation='relu'))
|
||||
model.add(Dense(10, activation='softmax'))
|
||||
model.add(Dense(128, activation="relu"))
|
||||
model.add(Dense(10, activation="softmax"))
|
||||
|
||||
return model
|
||||
|
||||
|
||||
# %%
|
||||
model = make_model()
|
||||
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__":
|
||||
try:
|
||||
pso_mnist = Optimizer(
|
||||
model,
|
||||
loss=loss[0],
|
||||
loss=loss[0],
|
||||
n_particles=100,
|
||||
c0=0.35,
|
||||
c1=0.8,
|
||||
c0=0.35,
|
||||
c1=0.8,
|
||||
w_min=0.7,
|
||||
w_max=1.0,
|
||||
w_max=1.1,
|
||||
negative_swarm=0.2,
|
||||
mutation_swarm=0.2,
|
||||
)
|
||||
mutation_swarm=0.1,
|
||||
)
|
||||
|
||||
best_score = pso_mnist.fit(
|
||||
x_test,
|
||||
y_test,
|
||||
epochs=200,
|
||||
save=True,
|
||||
save_path="./result/mnist",
|
||||
renewal="acc",
|
||||
save_path="./result/mnist",
|
||||
renewal="acc",
|
||||
empirical_balance=False,
|
||||
Dispersion=False,
|
||||
check_point=25
|
||||
)
|
||||
Dispersion=False,
|
||||
check_point=25,
|
||||
)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
finally:
|
||||
gc.collect()
|
||||
gc.collect()
|
||||
|
||||
@@ -15,10 +15,13 @@ 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"))
|
||||
tf.config.experimental.set_memory_growth(gpus[0], True)
|
||||
print("set memory growth")
|
||||
except RuntimeError as e:
|
||||
print(e)
|
||||
|
||||
|
||||
class Optimizer:
|
||||
"""
|
||||
particle swarm optimization
|
||||
@@ -41,7 +44,7 @@ class Optimizer:
|
||||
):
|
||||
"""
|
||||
particle swarm optimization
|
||||
|
||||
|
||||
Args:
|
||||
model (keras.models): 모델 구조
|
||||
loss (str): 손실함수
|
||||
@@ -55,7 +58,7 @@ class Optimizer:
|
||||
"""
|
||||
np.random.seed(np_seed)
|
||||
tf.random.set_seed(tf_seed)
|
||||
|
||||
|
||||
self.model = model # 모델 구조
|
||||
self.loss = loss # 손실함수
|
||||
self.n_particles = n_particles # 파티클 개수
|
||||
@@ -66,11 +69,12 @@ class Optimizer:
|
||||
self.w_max = w_max # 최대 관성 수치
|
||||
self.negative_swarm = negative_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.avg_score = 0 # 평균 점수
|
||||
|
||||
self.save_path = None # 저장 위치
|
||||
|
||||
negative_count = 0
|
||||
|
||||
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.compile(loss=self.loss, optimizer="sgd", metrics=["accuracy"])
|
||||
self.particles[i] = Particle(
|
||||
m,
|
||||
loss,
|
||||
negative=True if i < negative_swarm * self.n_particles else False,
|
||||
m,
|
||||
loss,
|
||||
negative=True if i < negative_swarm * self.n_particles else False,
|
||||
mutation=mutation_swarm,
|
||||
)
|
||||
)
|
||||
if i < negative_swarm * self.n_particles:
|
||||
negative_count += 1
|
||||
|
||||
|
||||
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()
|
||||
|
||||
@@ -110,11 +116,10 @@ class Optimizer:
|
||||
del self.avg_score
|
||||
gc.collect()
|
||||
|
||||
|
||||
def _encode(self, weights):
|
||||
"""
|
||||
가중치를 1차원으로 풀어서 반환
|
||||
|
||||
|
||||
Args:
|
||||
weights (list) : keras model의 가중치
|
||||
Returns:
|
||||
@@ -132,15 +137,14 @@ class Optimizer:
|
||||
w_gpu = np.append(w_gpu, w_)
|
||||
|
||||
del weights
|
||||
|
||||
return w_gpu, shape, length
|
||||
|
||||
return w_gpu, shape, length
|
||||
|
||||
def _decode(self, weight, shape, length):
|
||||
"""
|
||||
_encode 로 인코딩된 가중치를 원본 shape으로 복원
|
||||
파라미터는 encode의 리턴값을 그대로 사용을 권장
|
||||
|
||||
|
||||
Args:
|
||||
weight (numpy array): 가중치 - 1차원으로 풀어서 반환
|
||||
shape (list): 가중치의 원본 shape
|
||||
@@ -156,7 +160,7 @@ class Optimizer:
|
||||
w_ = np.reshape(w_, shape[i])
|
||||
weights.append(w_)
|
||||
start = end
|
||||
|
||||
|
||||
del weight
|
||||
del shape
|
||||
del length
|
||||
@@ -183,7 +187,6 @@ class Optimizer:
|
||||
else:
|
||||
return 1 + np.abs(score)
|
||||
|
||||
|
||||
def fit(
|
||||
self,
|
||||
x,
|
||||
@@ -204,7 +207,7 @@ class Optimizer:
|
||||
save : bool - True : save, False : not save
|
||||
save_path : str ex) "./result",
|
||||
renewal : str ex) "acc" or "loss",
|
||||
empirical_balance : bool - True :
|
||||
empirical_balance : bool - True :
|
||||
Dispersion : bool - True : g_best 의 값을 분산시켜 전역해를 찾음, False : g_best 의 값만 사용
|
||||
check_point : int - 저장할 위치 - None : 저장 안함
|
||||
"""
|
||||
@@ -226,7 +229,7 @@ class Optimizer:
|
||||
except ValueError as e:
|
||||
print(e)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
for i in tqdm(range(self.n_particles), desc="Initializing velocity"):
|
||||
p = self.particles[i]
|
||||
local_score = p.get_score(x, y, renewal=renewal)
|
||||
@@ -247,7 +250,7 @@ class Optimizer:
|
||||
|
||||
if local_score[1] == None:
|
||||
local_score[1] = 0
|
||||
|
||||
|
||||
if save:
|
||||
with open(
|
||||
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(", ")
|
||||
else:
|
||||
f.write("\n")
|
||||
|
||||
|
||||
f.close()
|
||||
del local_score
|
||||
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:
|
||||
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:
|
||||
acc = 0
|
||||
loss = 0
|
||||
@@ -277,9 +287,16 @@ class Optimizer:
|
||||
|
||||
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:
|
||||
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
|
||||
|
||||
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_p = w_p_ / (w_p_ + w_g_)
|
||||
w_g = w_p_ / (w_p_ + w_g_)
|
||||
|
||||
|
||||
del w_p_
|
||||
del w_g_
|
||||
|
||||
@@ -315,7 +332,7 @@ class Optimizer:
|
||||
p_ = np.exp(-1 * p_)
|
||||
w_p = p_
|
||||
w_g = 1 - p_
|
||||
|
||||
|
||||
del p_b
|
||||
del g_a
|
||||
del l_b
|
||||
@@ -339,7 +356,9 @@ class Optimizer:
|
||||
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}")
|
||||
epochs_pbar.set_description(
|
||||
f"best {self.g_best_score[0]:.4f} | {self.g_best_score[1]:.4f}"
|
||||
)
|
||||
elif renewal == "loss":
|
||||
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]:
|
||||
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}")
|
||||
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
|
||||
@@ -386,7 +407,7 @@ class Optimizer:
|
||||
self.avg_score = acc / self.n_particles
|
||||
|
||||
gc.collect()
|
||||
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("Ctrl + C : Stop Training")
|
||||
except MemoryError:
|
||||
@@ -407,7 +428,7 @@ class Optimizer:
|
||||
|
||||
Returns:
|
||||
(keras.models): 모델
|
||||
"""
|
||||
"""
|
||||
model = keras.models.model_from_json(self.model.to_json())
|
||||
model.set_weights(self.g_best)
|
||||
model.compile(loss=self.loss, optimizer="sgd", metrics=["accuracy"])
|
||||
@@ -459,7 +480,7 @@ class Optimizer:
|
||||
"a",
|
||||
) as f:
|
||||
json.dump(json_save, f, indent=4)
|
||||
|
||||
|
||||
f.close()
|
||||
|
||||
def _check_point_save(self, save_path: str = f"./result/check_point"):
|
||||
|
||||
@@ -15,7 +15,10 @@ class Particle:
|
||||
4. 가중치 업데이트
|
||||
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:
|
||||
model (keras.models): 학습 및 검증을 위한 모델
|
||||
@@ -36,7 +39,7 @@ class Particle:
|
||||
del i_w_, s_, l_
|
||||
del init_weights
|
||||
gc.collect()
|
||||
|
||||
|
||||
def __del__(self):
|
||||
del self.model
|
||||
del self.loss
|
||||
@@ -49,7 +52,7 @@ class Particle:
|
||||
def _encode(self, weights: list):
|
||||
"""
|
||||
가중치를 1차원으로 풀어서 반환
|
||||
|
||||
|
||||
Args:
|
||||
weights (list) : keras model의 가중치
|
||||
Returns:
|
||||
@@ -68,12 +71,11 @@ class Particle:
|
||||
|
||||
return w_gpu, shape, length
|
||||
|
||||
|
||||
def _decode(self, weight: list, shape, length):
|
||||
"""
|
||||
_encode 로 인코딩된 가중치를 원본 shape으로 복원
|
||||
파라미터는 encode의 리턴값을 그대로 사용을 권장
|
||||
|
||||
|
||||
Args:
|
||||
weight (numpy array): 가중치 - 1차원으로 풀어서 반환
|
||||
shape (list): 가중치의 원본 shape
|
||||
@@ -114,7 +116,7 @@ class Particle:
|
||||
self.best_score = score[1]
|
||||
self.best_weights = self.model.get_weights()
|
||||
elif renewal == "loss":
|
||||
if score[0] == 'nan':
|
||||
if score[0] == "nan":
|
||||
score[0] = np.inf
|
||||
if score[0] < self.best_score:
|
||||
self.best_score = score[0]
|
||||
@@ -150,13 +152,13 @@ class Particle:
|
||||
+ local_rate * r0 * (encode_p - encode_w)
|
||||
+ global_rate * r1 * (encode_g - encode_w)
|
||||
)
|
||||
|
||||
|
||||
if np.random.rand() < self.mutation:
|
||||
m_v = np.random.uniform(-0.1, 0.1, len(encode_v))
|
||||
new_v = m_v
|
||||
|
||||
|
||||
self.velocities = self._decode(new_v, w_sh, w_len)
|
||||
|
||||
|
||||
del encode_w, w_sh, w_len
|
||||
del encode_v, v_sh, v_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)
|
||||
r0 = np.random.rand()
|
||||
r1 = np.random.rand()
|
||||
|
||||
|
||||
if self.negative:
|
||||
new_v = (
|
||||
w * encode_v
|
||||
@@ -195,13 +197,13 @@ class Particle:
|
||||
+ local_rate * r0 * (w_p * encode_p - encode_w)
|
||||
+ global_rate * r1 * (w_g * encode_g - encode_w)
|
||||
)
|
||||
|
||||
|
||||
if np.random.rand() < self.mutation:
|
||||
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)
|
||||
|
||||
|
||||
del encode_w, w_sh, w_len
|
||||
del encode_v, v_sh, v_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)
|
||||
new_w = encode_w + encode_v
|
||||
self.model.set_weights(self._decode(new_w, w_sh, w_len))
|
||||
|
||||
|
||||
del encode_w, w_sh, w_len
|
||||
del encode_v, v_sh, v_len
|
||||
|
||||
@@ -234,7 +236,7 @@ class Particle:
|
||||
"""
|
||||
self.model.set_weights(weights)
|
||||
score = self.model.evaluate(x, y, verbose=0)[1]
|
||||
|
||||
|
||||
if score > 0:
|
||||
return 1 / (1 + score)
|
||||
else:
|
||||
@@ -258,7 +260,7 @@ class Particle:
|
||||
"""
|
||||
self._update_velocity(local_rate, global_rate, w, g_best)
|
||||
self._update_weights()
|
||||
|
||||
|
||||
return self.get_score(x, y, renewal)
|
||||
|
||||
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_weights()
|
||||
|
||||
|
||||
return self.get_score(x, y, renewal)
|
||||
|
||||
def get_best_score(self):
|
||||
|
||||
52
xor.py
52
xor.py
@@ -1,10 +1,11 @@
|
||||
# %%
|
||||
import os
|
||||
|
||||
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
||||
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
|
||||
|
||||
import numpy as np
|
||||
import tensorflow as tf
|
||||
|
||||
# from pso_tf import PSO
|
||||
from pso import Optimizer
|
||||
from tensorflow import keras
|
||||
@@ -14,31 +15,64 @@ from tensorflow.keras.models import Sequential
|
||||
print(tf.__version__)
|
||||
print(tf.config.list_physical_devices())
|
||||
|
||||
|
||||
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]])
|
||||
return x, y
|
||||
|
||||
|
||||
def make_model():
|
||||
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(1, activation='sigmoid'))
|
||||
leyer.append(layers.Dense(1, activation="sigmoid"))
|
||||
|
||||
model = Sequential(leyer)
|
||||
|
||||
return model
|
||||
|
||||
|
||||
# %%
|
||||
model = make_model()
|
||||
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,
|
||||
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)
|
||||
pso_xor = Optimizer(
|
||||
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(
|
||||
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