mirror of
https://github.com/jung-geun/PSO.git
synced 2025-12-20 04:50:45 +09:00
23-05-29
EBPSO 알고리즘 구현 - 선택지로 추가 random 으로 분산시키는 방법 구현 - 선택지로 추가 iris 기준 98퍼센트로 나오나 정확한 결과를 지켜봐야 할것으로 보임
This commit is contained in:
484
pyswarms/example.ipynb
Normal file
484
pyswarms/example.ipynb
Normal file
File diff suppressed because one or more lines are too long
BIN
pyswarms/pso.gif
Normal file
BIN
pyswarms/pso.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.1 KiB |
93
pyswarms/pyswarm.py
Normal file
93
pyswarms/pyswarm.py
Normal file
@@ -0,0 +1,93 @@
|
||||
from sklearn.datasets import load_iris
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.preprocessing import OneHotEncoder, StandardScaler
|
||||
|
||||
import tensorflow as tf
|
||||
from tensorflow import keras
|
||||
from tensorflow.keras.models import Sequential
|
||||
from tensorflow.keras.layers import Dense, Dropout, Flatten
|
||||
|
||||
import time as time
|
||||
import numpy as np
|
||||
|
||||
|
||||
iris = load_iris()
|
||||
iris = load_iris()
|
||||
X = iris['data']
|
||||
y = iris['target']
|
||||
names = iris['target_names']
|
||||
feature_names = iris['feature_names']
|
||||
enc = OneHotEncoder()
|
||||
Y = enc.fit_transform(y[:, np.newaxis]).toarray()
|
||||
scaler = StandardScaler()
|
||||
X_scaled = scaler.fit_transform(X)
|
||||
X_train, X_test, Y_train, Y_test = train_test_split(
|
||||
X_scaled, Y, test_size=0.5, random_state=2)
|
||||
n_features = X.shape[1]
|
||||
n_classes = Y.shape[1]
|
||||
|
||||
def create_custom_model(input_dim, output_dim, nodes, n=1, name='model'):
|
||||
model = Sequential(name=name)
|
||||
for i in range(n):
|
||||
model.add(Dense(nodes, input_dim=input_dim, activation='relu'))
|
||||
model.add(Dense(output_dim, activation='softmax'))
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
optimizer='adam',
|
||||
metrics=['accuracy'])
|
||||
return model
|
||||
|
||||
|
||||
# n_layers = 1
|
||||
# model = create_custom_model(n_features, n_classes,
|
||||
# 4, n_layers)
|
||||
# model.summary()
|
||||
|
||||
# start_time = time.time()
|
||||
# print('Model name:', model.name)
|
||||
# history_callback = model.fit(X_train, Y_train,
|
||||
# batch_size=5,
|
||||
# epochs=400,
|
||||
# verbose=0,
|
||||
# validation_data=(X_test, Y_test)
|
||||
# )
|
||||
# score = model.evaluate(X_test, Y_test)
|
||||
# print('Test loss:', score[0])
|
||||
# print('Test accuracy:', score[1])
|
||||
# print("--- %s seconds ---" % (time.time() - start_time))
|
||||
|
||||
|
||||
def get_shape(model):
|
||||
weights_layer = model.get_weights()
|
||||
shapes = []
|
||||
for weights in weights_layer:
|
||||
shapes.append(weights.shape)
|
||||
return shapes
|
||||
|
||||
def set_shape(weights,shapes):
|
||||
new_weights = []
|
||||
index=0
|
||||
for shape in shapes:
|
||||
if(len(shape)>1):
|
||||
n_nodes = np.prod(shape)+index
|
||||
else:
|
||||
n_nodes=shape[0]+index
|
||||
tmp = np.array(weights[index:n_nodes]).reshape(shape)
|
||||
new_weights.append(tmp)
|
||||
index=n_nodes
|
||||
return new_weights
|
||||
|
||||
def evaluate_nn(W, shape,X_train=X_train, Y_train=Y_train):
|
||||
results = []
|
||||
for weights in W:
|
||||
model.set_weights(set_shape(weights,shape))
|
||||
score = model.evaluate(X_train, Y_train, verbose=0)
|
||||
results.append(1-score[1])
|
||||
return results
|
||||
|
||||
shape = get_shape(model)
|
||||
x_max = 1.0 * np.ones(83)
|
||||
x_min = -1.0 * x_max
|
||||
bounds = (x_min, x_max)
|
||||
options = {'c1': 0.4, 'c2': 0.8, 'w': 0.4}
|
||||
optimizer = GlobalBestPSO(n_particles=25, dimensions=83,
|
||||
options=options, bounds=bounds)
|
||||
11
pyswarms/report.log
Normal file
11
pyswarms/report.log
Normal file
@@ -0,0 +1,11 @@
|
||||
2023-05-28 17:29:35,386 - pyswarms.single.global_best - INFO - Optimize for 20 iters with {'c1': 0.4, 'c2': 0.6, 'w': 0.4}
|
||||
2023-05-28 17:30:07,637 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 0.013333320617675781, best pos: [ 0.17027965 0.17696722 -0.07395054 0.31544984 0.17052408 -0.37810479
|
||||
0.24267479 0.16931148 0.65606942 -0.24207116 -0.66562722 0.02191478
|
||||
0.5870387 0.78966943 -0.4457816 0.0907434 -0.1808341 0.29282655
|
||||
0.61472003 0.90660508 0.16469465 -0.55057763 0.54702005 -0.22636745
|
||||
0.01125538 0.62431828 0.02128613 -0.26723577 -0.43527016 0.51223244
|
||||
0.76388399 -0.02073011 0.15949622 0.45878514 0.01787211]
|
||||
2023-05-28 17:30:08,140 - matplotlib.animation - WARNING - MovieWriter pillowwritter unavailable; using Pillow instead.
|
||||
2023-05-28 17:30:08,141 - matplotlib.animation - INFO - Animation.save using <class 'matplotlib.animation.PillowWriter'>
|
||||
2023-05-28 17:31:01,885 - tensorflow - WARNING - 5 out of the last 3010 calls to <function Model.make_test_function.<locals>.test_function at 0x7fee6622df70> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has reduce_retracing=True option that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details.
|
||||
2023-05-28 17:31:08,794 - tensorflow - WARNING - 6 out of the last 3011 calls to <function Model.make_test_function.<locals>.test_function at 0x7fee66257b80> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has reduce_retracing=True option that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details.
|
||||
Reference in New Issue
Block a user