mnist 4, 8, 4-1.1 시도
This commit is contained in:
jung-geun
2023-07-08 19:14:38 +09:00
parent 74c08684cc
commit e12716083c
11 changed files with 1 additions and 772 deletions

View File

@@ -73,7 +73,7 @@ if __name__ == "__main__":
n_particles=100,
c0=0.35,
c1=0.8,
w_min=0.7,
w_min=0.4,
w_max=1.1,
negative_swarm=0.2,
mutation_swarm=0.1,

View File

@@ -1,14 +0,0 @@
# -*- coding: utf-8 -*-
"""PSOkeras - Particle Swarm Optimizer for Keras models
This module implements a particle swarm optimizer for training the weights of Keras models. The
"""
from .version import __version__
from .optimizer import Optimizer
__all__ = [
'Optimizer',
]

View File

@@ -1,67 +0,0 @@
BIG_SCORE = 1.e6 # type: float
import keras
from psokeras.particle import Particle
from .util import ProgressBar
class Optimizer:
def __init__(self, model, loss,
n=10,
acceleration=0.1,
local_rate=1.0,
global_rate=1.0):
self.n_particles = n
self.structure = model.to_json()
self.particles = [None] * n
self.loss = loss
self.length = len(model.get_weights())
params = {'acc': acceleration, 'local_acc': local_rate, 'global_acc': global_rate}
for i in range(n-1):
m = keras.models.model_from_json(self.structure)
m.compile(loss=loss,optimizer='sgd')
self.particles[i] = Particle(m, params)
self.particles[n-1] = Particle(model, params)
self.global_best_weights = None
self.global_best_score = BIG_SCORE
def fit(self, x, y, steps=0, batch_size=32):
num_batches = x.shape[0] // batch_size
for i, p in enumerate(self.particles):
local_score = p.get_score(x, y)
if local_score < self.global_best_score:
self.global_best_score = local_score
self.global_best_weights = p.get_best_weights()
print("PSO -- Initial best score {:0.4f}".format(self.global_best_score))
bar = ProgressBar(steps, updates=20)
for i in range(steps):
for j in range(num_batches):
x_ = x[j*batch_size:(j+1)*batch_size,:]
y_ = y[j*batch_size:(j+1)*batch_size]
for p in self.particles:
local_score = p.step(x_, y_, self.global_best_weights)
if local_score < self.global_best_score:
self.global_best_score = local_score
self.global_best_weights = p.get_best_weights()
bar.update(i)
bar.done()
def get_best_model(self):
best_model = keras.models.model_from_json(self.structure)
best_model.set_weights(self.global_best_weights)
best_model.compile(loss=self.loss,optimizer='sgd')
return best_model

View File

@@ -1,66 +0,0 @@
import random
import numpy as np
from psokeras.optimizer import BIG_SCORE
class Particle:
def __init__(self, model, params):
self.model = model
self.params = params
self.init_weights = model.get_weights()
self.velocities = [None] * len(self.init_weights)
self.length = len(self.init_weights)
for i, layer in enumerate(self.init_weights):
self.velocities[i] = np.random.rand(*layer.shape) / 5 - 0.10
# self.velocities[i] = np.zeros(layer.shape)
self.best_weights = None
self.best_score = BIG_SCORE
def get_score(self, x, y, update=True):
local_score = self.model.evaluate(x, y, verbose=0)
if local_score < self.best_score and update:
self.best_score = local_score
self.best_weights = self.model.get_weights()
return local_score
def _update_velocities(self, global_best_weights, depth):
new_velocities = [None] * len(self.init_weights)
weights = self.model.get_weights()
local_rand, global_rand = random.random(), random.random()
for i, layer in enumerate(weights):
if i >= depth:
new_velocities[i] = self.velocities[i]
continue
new_v = self.params['acc'] * self.velocities[i]
new_v = new_v + self.params['local_acc'] * local_rand * (self.best_weights[i] - layer)
new_v = new_v + self.params['global_acc'] * global_rand * (global_best_weights[i] - layer)
new_velocities[i] = new_v
self.velocities = new_velocities
def _update_weights(self, depth):
old_weights = self.model.get_weights()
new_weights = [None] * len(old_weights)
for i, layer in enumerate(old_weights):
if i>= depth:
new_weights[i] = layer
continue
new_w = layer + self.velocities[i]
new_weights[i] = new_w
self.model.set_weights(new_weights)
def step(self, x, y, global_best_weights,depth=None):
if depth is None:
depth = self.length
self._update_velocities(global_best_weights, depth)
self._update_weights(depth)
return self.get_score(x, y)
def get_best_weights(self):
return self.best_weights

View File

@@ -1,31 +0,0 @@
class ProgressBar:
def __init__(self, steps, updates=10):
self.step = 0
self.step_size = (steps // updates)
self.total_steps = steps
self.updates = updates
bar = self._make_bar(0)
print(bar, end=' ')
def update(self, i):
if i % self.step_size > 0:
return
self.step = i // self.step_size
bar = self._make_bar(i)
print(bar, end=' ')
def done(self):
self.step = self.total_steps
bar = self._make_bar(self.updates)
print(bar)
def _make_bar(self, x):
bar = "["
for x in range(self.updates):
print("\r", end=' ')
bar += "=" if x < self.step else " "
bar += "]"
return bar

View File

@@ -1 +0,0 @@
__version__ = '0.2.0'

File diff suppressed because one or more lines are too long

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.1 KiB

View File

@@ -1,93 +0,0 @@
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)

View File

@@ -1,11 +0,0 @@
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.

View File

@@ -46,10 +46,6 @@ $$
| |-- __init__.py # pso 모듈을 사용하기 위한 초기화 파일
| |-- optimizer.py # pso 알고리즘 이용을 위한 기본 코드
| |-- particle.py # 각 파티클의 정보 및 위치를 저장하는 코드
|-- /psokeras # keras 모델을 이용가능한 PSO 알고리즘 - 다른 사람의 코드
| |-- ***
|-- /pyswarms # pyswarms 라이브러리를 이용가능한 PSO 알고리즘 - 다른 사람의 코드
| |-- ***
|-- examples.py # psokeras 코드를 이용한 예제
|-- xor.ipynb # pso 를 이용한 xor 문제 풀이
|-- iris.py # pso 를 이용한 iris 문제 풀이