코드 변경 내용: digits.py, iris.py, mnist.py, bean.py

Keras 모듈을 사용하여 코드를 업데이트했습니다.
This commit is contained in:
jung-geun
2024-02-25 08:09:20 +09:00
parent c45ee5873e
commit 0062b1850b
7 changed files with 165 additions and 183 deletions

View File

@@ -1,15 +1,16 @@
# %%
from pso import optimizer
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Conv2D, Dense, Dropout, Flatten, MaxPooling2D
from keras.datasets import mnist, fashion_mnist
import tensorflow as tf
import numpy as np
import json
import os
import sys
import numpy as np
import tensorflow as tf
from keras.datasets import fashion_mnist
from keras.layers import Conv2D, Dense, Dropout, Flatten, MaxPooling2D
from keras.models import Sequential
from pso import optimizer
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
@@ -22,10 +23,8 @@ def get_data():
y_train, y_test = tf.one_hot(y_train, 10), tf.one_hot(y_test, 10)
x_train, x_test = tf.convert_to_tensor(
x_train), tf.convert_to_tensor(x_test)
y_train, y_test = tf.convert_to_tensor(
y_train), tf.convert_to_tensor(y_test)
x_train, x_test = tf.convert_to_tensor(x_train), tf.convert_to_tensor(x_test)
y_train, y_test = tf.convert_to_tensor(y_train), tf.convert_to_tensor(y_test)
print(f"x_train : {x_train[0].shape} | y_train : {y_train[0].shape}")
print(f"x_test : {x_test[0].shape} | y_test : {y_test[0].shape}")
@@ -36,8 +35,7 @@ def get_data():
def make_model():
model = Sequential()
model.add(
Conv2D(32, kernel_size=(5, 5), 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=(2, 2)))
model.add(Conv2D(64, kernel_size=(3, 3), activation="relu"))
@@ -51,72 +49,39 @@ def make_model():
return model
def random_state():
with open(
"result/mnist/20230723-061626/mean_squared_error_[0.6384999752044678, 0.0723000094294548].json",
"r",
) as f:
json_ = json.load(f)
rs = (
json_["random_state_0"],
np.array(json_["random_state_1"]),
json_["random_state_2"],
json_["random_state_3"],
json_["random_state_4"],
)
return rs
# %%
model = make_model()
x_train, y_train, x_test, y_test = get_data()
loss = [
"mean_squared_error",
"categorical_crossentropy",
"sparse_categorical_crossentropy",
"binary_crossentropy",
"kullback_leibler_divergence",
"poisson",
"cosine_similarity",
"log_cosh",
"huber_loss",
"mean_absolute_error",
"mean_absolute_percentage_error",
]
# rs = random_state()
pso_mnist = optimizer(
model,
loss="categorical_crossentropy",
n_particles=500,
c0=0.5,
c1=1.0,
w_min=0.7,
w_max=1.2,
negative_swarm=0.05,
mutation_swarm=0.3,
n_particles=200,
c0=0.7,
c1=0.5,
w_min=0.1,
w_max=0.8,
negative_swarm=0.0,
mutation_swarm=0.05,
convergence_reset=True,
convergence_reset_patience=10,
convergence_reset_monitor="loss",
)
best_score = pso_mnist.fit(
x_train,
y_train,
epochs=200,
epochs=1000,
save_info=True,
log=2,
log_name="fashion_mnist",
renewal="acc",
renewal="loss",
check_point=25,
empirical_balance=False,
dispersion=False,
batch_size=5000,
back_propagation=True,
)
print("Done!")
sys.exit(0)