mirror of
https://github.com/jung-geun/PSO.git
synced 2025-12-19 20:44:39 +09:00
23-07-07
dev container 설정 - tqdm + tensorflow 자동 설치 env name = pso 로 자동 생성
This commit is contained in:
@@ -2,14 +2,20 @@
|
||||
// README at: https://github.com/devcontainers/templates/tree/main/src/miniconda
|
||||
{
|
||||
"name": "Miniconda (Python 3)",
|
||||
// Configure tool-specific properties.
|
||||
"customizations": {
|
||||
"vscode": {
|
||||
"extensions": [
|
||||
"ms-python.python",
|
||||
"ms-toolsai.jupyter"
|
||||
"ms-toolsai.jupyter",
|
||||
"donjayamanne.python-extension-pack",
|
||||
"tht13.python",
|
||||
"esbenp.prettier-vscode",
|
||||
"ms-python.black-formatter"
|
||||
]
|
||||
}
|
||||
},
|
||||
// Features to add to the dev container. More info: https://containers.dev/features.
|
||||
"features": {
|
||||
"ghcr.io/devcontainers/features/nvidia-cuda:1": {
|
||||
"installCudnn": true
|
||||
@@ -22,15 +28,13 @@
|
||||
"version": "3.9"
|
||||
}
|
||||
},
|
||||
"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.
|
||||
// "forwardPorts": [],
|
||||
// Use 'postCreateCommand' to run commands after the container is created.
|
||||
// "postCreateCommand": "python --version",
|
||||
// Configure tool-specific properties.
|
||||
// "customizations": {},
|
||||
"postCreateCommand": [
|
||||
"conda env create --file environment.yaml --name pso",
|
||||
"conda activate pso"
|
||||
]
|
||||
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
|
||||
// "remoteUser": "root"
|
||||
}
|
||||
24
.github/workflows/python-package-conda.yml
vendored
24
.github/workflows/python-package-conda.yml
vendored
@@ -1,24 +0,0 @@
|
||||
name: Python Package using Conda
|
||||
|
||||
on: [push]
|
||||
|
||||
jobs:
|
||||
build-linux:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
max-parallel: 5
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Set up Python 3.9
|
||||
uses: actions/setup-python@v3
|
||||
with:
|
||||
python-version: "3.9"
|
||||
- name: Add conda to system path
|
||||
run: |
|
||||
# $CONDA is an environment variable pointing to the root of the miniconda directory
|
||||
echo $CONDA/bin >> $GITHUB_PATH
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
conda env create --file environment.yaml --name pso
|
||||
conda activate pso
|
||||
@@ -1,30 +1,25 @@
|
||||
# %%
|
||||
import os
|
||||
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
||||
|
||||
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
|
||||
|
||||
import tensorflow as tf
|
||||
|
||||
tf.random.set_seed(777) # for reproducibility
|
||||
|
||||
from tensorflow import keras
|
||||
from keras.datasets import mnist
|
||||
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 pso_tf import PSO
|
||||
from pso import Optimizer
|
||||
import gc
|
||||
from datetime import date
|
||||
|
||||
import numpy as np
|
||||
|
||||
from datetime import date
|
||||
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 tensorflow import keras
|
||||
from tqdm import tqdm
|
||||
|
||||
import gc
|
||||
from pso import Optimizer
|
||||
|
||||
# print(tf.__version__)
|
||||
# print(tf.config.list_physical_devices())
|
||||
# print(f"Num GPUs Available: {len(tf.config.list_physical_devices('GPU'))}")
|
||||
|
||||
def get_data():
|
||||
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
||||
@@ -37,26 +32,30 @@ 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()
|
||||
@@ -67,12 +66,23 @@ x_test, y_test = get_data_test()
|
||||
# loss = 'poisson'
|
||||
# loss = 'cosine_similarity'
|
||||
# loss = 'log_cosh'
|
||||
# loss = 'huber_loss'
|
||||
# loss = 'huber_loss'
|
||||
# loss = 'mean_absolute_error'
|
||||
# loss = 'mean_absolute_percentage_error'
|
||||
# loss = 'mean_squared_error'
|
||||
|
||||
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",
|
||||
]
|
||||
n_particles = [50, 75, 100]
|
||||
c0 = [0.25, 0.35, 0.45, 0.55]
|
||||
c1 = [0.5, 0.6, 0.7, 0.8, 0.9]
|
||||
@@ -93,31 +103,31 @@ if __name__ == "__main__":
|
||||
for n_s in negative_swarm:
|
||||
pso_mnist = Optimizer(
|
||||
model,
|
||||
loss=loss_,
|
||||
loss=loss_,
|
||||
n_particles=n,
|
||||
c0=c_0,
|
||||
c1=c_1,
|
||||
c0=c_0,
|
||||
c1=c_1,
|
||||
w_min=w_m,
|
||||
w_max=w_M,
|
||||
negative_swarm=n_s
|
||||
)
|
||||
negative_swarm=n_s,
|
||||
)
|
||||
|
||||
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,
|
||||
)
|
||||
|
||||
del pso_mnist
|
||||
gc.collect()
|
||||
gc.collect()
|
||||
tf.keras.backend.clear_session()
|
||||
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("KeyboardInterrupt")
|
||||
finally:
|
||||
|
||||
@@ -10,8 +10,8 @@ dependencies:
|
||||
- pandas=1.5.3=py39h417a72b_0
|
||||
- pip=23.0.1=py39h06a4308_0
|
||||
- python=3.9.16=h7a1cb2a_2
|
||||
- tqdm=4.65.0=py39hb070fc8_0
|
||||
- pip:
|
||||
- numpy==1.23.5
|
||||
- nvidia-cudnn-cu11==8.6.0.163
|
||||
- tensorflow==2.12.0
|
||||
- tqdm==4.65.1.dev3+g5587f0d
|
||||
57
example.py
57
example.py
@@ -7,19 +7,19 @@ results with a number of independent runs of standard Backpropagation algorithm
|
||||
@author Mike Holcomb (mjh170630@utdallas.edu)
|
||||
"""
|
||||
|
||||
import tensorflow as tf
|
||||
from sklearn.datasets import load_iris
|
||||
from sklearn.model_selection import train_test_split
|
||||
import tensorflow as tf
|
||||
from tensorflow import keras
|
||||
from tensorflow.keras.models import Sequential
|
||||
from tensorflow.keras.layers import Dense
|
||||
from tensorflow.keras.models import Sequential
|
||||
|
||||
from psokeras import Optimizer
|
||||
|
||||
N = 50 # number of particles
|
||||
STEPS = 500 # number of steps
|
||||
LOSS = 'mse' # Loss function
|
||||
BATCH_SIZE = 32 # Size of batches to train on
|
||||
N = 50 # number of particles
|
||||
STEPS = 500 # number of steps
|
||||
LOSS = "mse" # Loss function
|
||||
BATCH_SIZE = 32 # Size of batches to train on
|
||||
|
||||
|
||||
def build_model(loss):
|
||||
@@ -30,12 +30,11 @@ def build_model(loss):
|
||||
:return: Keras dense model of predefined structure
|
||||
"""
|
||||
model = Sequential()
|
||||
model.add(Dense(4, activation='sigmoid', input_dim=4, use_bias=True))
|
||||
model.add(Dense(4, activation='sigmoid', use_bias=True))
|
||||
model.add(Dense(3, activation='softmax', use_bias=True))
|
||||
model.add(Dense(4, activation="sigmoid", input_dim=4, use_bias=True))
|
||||
model.add(Dense(4, activation="sigmoid", use_bias=True))
|
||||
model.add(Dense(3, activation="softmax", use_bias=True))
|
||||
|
||||
model.compile(loss=loss,
|
||||
optimizer='adam')
|
||||
model.compile(loss=loss, optimizer="adam")
|
||||
|
||||
return model
|
||||
|
||||
@@ -52,11 +51,10 @@ def vanilla_backpropagation(x_train, y_train):
|
||||
|
||||
for i in range(N):
|
||||
model_s = build_model(LOSS)
|
||||
model_s.fit(x_train, y_train,
|
||||
epochs=STEPS,
|
||||
batch_size=BATCH_SIZE,
|
||||
verbose=0)
|
||||
train_score = model_s.evaluate(x_train, y_train, batch_size=BATCH_SIZE, verbose=0)
|
||||
model_s.fit(x_train, y_train, epochs=STEPS, batch_size=BATCH_SIZE, verbose=0)
|
||||
train_score = model_s.evaluate(
|
||||
x_train, y_train, batch_size=BATCH_SIZE, verbose=0
|
||||
)
|
||||
if train_score < best_score:
|
||||
best_model = model_s
|
||||
best_score = train_score
|
||||
@@ -66,11 +64,13 @@ def vanilla_backpropagation(x_train, y_train):
|
||||
if __name__ == "__main__":
|
||||
# Section I: Build the data set
|
||||
iris = load_iris()
|
||||
x_train, x_test, y_train, y_test = train_test_split(iris.data,
|
||||
keras.utils.to_categorical(iris.target, num_classes=None),
|
||||
test_size=0.5,
|
||||
random_state=0,
|
||||
stratify=iris.target)
|
||||
x_train, x_test, y_train, y_test = train_test_split(
|
||||
iris.data,
|
||||
keras.utils.to_categorical(iris.target, num_classes=None),
|
||||
test_size=0.5,
|
||||
random_state=0,
|
||||
stratify=iris.target,
|
||||
)
|
||||
|
||||
# Section II: First run the backpropagation simulation
|
||||
model_s = vanilla_backpropagation(x_train=x_train, y_train=y_train)
|
||||
@@ -84,13 +84,14 @@ if __name__ == "__main__":
|
||||
model_p = build_model(LOSS)
|
||||
|
||||
# Instantiate optimizer with model, loss function, and hyperparameters
|
||||
pso = Optimizer(model=model_p,
|
||||
loss=LOSS,
|
||||
n=N, # Number of particles
|
||||
acceleration=1.0, # Contribution of recursive particle velocity (acceleration)
|
||||
local_rate=0.6, # Contribution of locally best weights to new velocity
|
||||
global_rate=0.4 # Contribution of globally best weights to new velocity
|
||||
)
|
||||
pso = Optimizer(
|
||||
model=model_p,
|
||||
loss=LOSS,
|
||||
n=N, # Number of particles
|
||||
acceleration=1.0, # Contribution of recursive particle velocity (acceleration)
|
||||
local_rate=0.6, # Contribution of locally best weights to new velocity
|
||||
global_rate=0.4, # Contribution of globally best weights to new velocity
|
||||
)
|
||||
|
||||
# Train model on provided data
|
||||
pso.fit(x_train, y_train, steps=STEPS, batch_size=BATCH_SIZE)
|
||||
|
||||
5
iris.py
5
iris.py
@@ -4,15 +4,14 @@ os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
|
||||
|
||||
import gc
|
||||
|
||||
import numpy as np
|
||||
import tensorflow as tf
|
||||
from pso import Optimizer
|
||||
from sklearn.datasets import load_iris
|
||||
from sklearn.model_selection import train_test_split
|
||||
from tensorflow import keras
|
||||
from tensorflow.keras import layers
|
||||
from tensorflow.keras.models import Sequential
|
||||
|
||||
from pso import Optimizer
|
||||
|
||||
|
||||
def make_model():
|
||||
model = Sequential()
|
||||
|
||||
32
iris_tf.py
32
iris_tf.py
@@ -1,5 +1,6 @@
|
||||
import os
|
||||
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
||||
|
||||
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
|
||||
|
||||
import tensorflow as tf
|
||||
|
||||
@@ -10,23 +11,23 @@ if gpus:
|
||||
tf.config.experimental.set_memory_growth(gpus[0], True)
|
||||
except RuntimeError as e:
|
||||
print(e)
|
||||
|
||||
|
||||
from sklearn.datasets import load_iris
|
||||
from sklearn.model_selection import train_test_split
|
||||
from tensorflow import keras
|
||||
from tensorflow.keras import layers
|
||||
from tensorflow.keras.models import Sequential
|
||||
|
||||
from sklearn.datasets import load_iris
|
||||
from sklearn.model_selection import train_test_split
|
||||
|
||||
|
||||
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
|
||||
@@ -34,18 +35,21 @@ 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
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
model = make_model()
|
||||
x_train, x_test, y_train, y_test = load_data()
|
||||
print(x_train.shape, y_train.shape)
|
||||
|
||||
loss = ['categorical_crossentropy', 'accuracy','mse']
|
||||
metrics = ['accuracy']
|
||||
|
||||
model.compile(optimizer='sgd', loss=loss[0], metrics=metrics[0])
|
||||
loss = ["categorical_crossentropy", "accuracy", "mse"]
|
||||
metrics = ["accuracy"]
|
||||
|
||||
model.compile(optimizer="sgd", loss=loss[0], metrics=metrics[0])
|
||||
model.fit(x_train, y_train, epochs=200, batch_size=32, validation_split=0.2)
|
||||
model.evaluate(x_test, y_test, batch_size=32)
|
||||
model.evaluate(x_test, y_test, batch_size=32)
|
||||
|
||||
7
mnist.py
7
mnist.py
@@ -5,14 +5,11 @@ os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
|
||||
|
||||
import gc
|
||||
|
||||
import tensorflow as tf
|
||||
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 import Optimizer
|
||||
from tensorflow import keras
|
||||
from tqdm import tqdm
|
||||
|
||||
|
||||
def get_data():
|
||||
@@ -24,6 +21,7 @@ def get_data():
|
||||
|
||||
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}")
|
||||
|
||||
return x_train, y_train, x_test, y_test
|
||||
|
||||
|
||||
@@ -92,6 +90,7 @@ if __name__ == "__main__":
|
||||
Dispersion=False,
|
||||
check_point=25,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
finally:
|
||||
|
||||
@@ -73,7 +73,11 @@ class Optimizer:
|
||||
self.g_best = None # 최고 점수를 받은 가중치
|
||||
self.g_best_ = None # 최고 점수를 받은 가중치 - 값의 분산을 위한 변수
|
||||
self.avg_score = 0 # 평균 점수
|
||||
|
||||
self.save_path = None # 저장 위치
|
||||
self.renewal = "acc"
|
||||
self.Dispersion = False
|
||||
self.day = datetime.now().strftime("%m-%d-%H-%M")
|
||||
|
||||
negative_count = 0
|
||||
|
||||
@@ -225,7 +229,6 @@ class Optimizer:
|
||||
self.save_path = save_path
|
||||
if not os.path.exists(save_path):
|
||||
os.makedirs(save_path, exist_ok=True)
|
||||
self.day = datetime.now().strftime("%m-%d-%H-%M")
|
||||
except ValueError as e:
|
||||
print(e)
|
||||
sys.exit(1)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import gc
|
||||
|
||||
import numpy as np
|
||||
import tensorflow as tf
|
||||
from tensorflow import keras
|
||||
|
||||
|
||||
|
||||
16
xor.py
16
xor.py
@@ -5,15 +5,12 @@ 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
|
||||
from tensorflow.keras import layers
|
||||
from tensorflow.keras.layers import Dense
|
||||
from tensorflow.keras.models import Sequential
|
||||
|
||||
print(tf.__version__)
|
||||
print(tf.config.list_physical_devices())
|
||||
from pso import Optimizer
|
||||
|
||||
|
||||
def get_data():
|
||||
@@ -23,12 +20,9 @@ def get_data():
|
||||
|
||||
|
||||
def make_model():
|
||||
leyer = []
|
||||
leyer.append(layers.Dense(2, activation="sigmoid", input_shape=(2,)))
|
||||
# leyer.append(layers.Dense(2, activation='sigmoid'))
|
||||
leyer.append(layers.Dense(1, activation="sigmoid"))
|
||||
|
||||
model = Sequential(leyer)
|
||||
model = Sequential()
|
||||
model.add(layers.Dense(2, activation="sigmoid", input_shape=(2,)))
|
||||
model.add(layers.Dense(1, activation="sigmoid"))
|
||||
|
||||
return model
|
||||
|
||||
|
||||
Reference in New Issue
Block a user