mirror of
https://github.com/jung-geun/PSO.git
synced 2025-12-19 20:44:39 +09:00
tensorboard 선택 시 자동으로 프로세스 실행 비어있는 포트를 자동으로 탐색하여 오픈 이전 최적해와 비교하여 관성치를 높게 주는 방법을 일시 폐기 digits 테스트 추가 tensorboard 자동 설치 추가
72 lines
1.4 KiB
Python
72 lines
1.4 KiB
Python
from pso import optimizer
|
|
from tensorflow.keras.models import Sequential
|
|
from tensorflow.keras import layers
|
|
from tensorflow import keras
|
|
from sklearn.model_selection import train_test_split
|
|
from sklearn.datasets import load_iris
|
|
import gc
|
|
import os
|
|
import sys
|
|
|
|
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
|
|
|
|
|
|
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"))
|
|
|
|
return model
|
|
|
|
|
|
def load_data():
|
|
iris = load_iris()
|
|
x = iris.data
|
|
y = iris.target
|
|
|
|
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
|
|
)
|
|
|
|
return x_train, x_test, y_train, y_test
|
|
|
|
|
|
model = make_model()
|
|
x_train, x_test, y_train, y_test = load_data()
|
|
|
|
|
|
pso_iris = optimizer(
|
|
model,
|
|
loss="categorical_crossentropy",
|
|
n_particles=100,
|
|
c0=0.5,
|
|
c1=0.3,
|
|
w_min=0.2,
|
|
w_max=0.9,
|
|
negative_swarm=0,
|
|
mutation_swarm=0.1,
|
|
convergence_reset=True,
|
|
convergence_reset_patience=10,
|
|
convergence_reset_monitor="mse",
|
|
convergence_reset_min_delta=0.001,
|
|
)
|
|
|
|
best_score = pso_iris.fit(
|
|
x_train,
|
|
y_train,
|
|
epochs=500,
|
|
save_info=True,
|
|
log=2,
|
|
log_name="iris",
|
|
renewal="mse",
|
|
check_point=25,
|
|
validate_data=(x_test, y_test),
|
|
)
|
|
|
|
gc.collect()
|
|
print("Done!")
|
|
sys.exit(0)
|