Ejemplo n.º 1
0
    def __init__(self,
                 n_filters,
                 filter_size,
                 strides=1,
                 input_shape=(None, None, None, None),
                 use_biases=True,
                 weight_initializer=UniformInitializer(),
                 bias_initializer=None,
                 padding=None,
                 name=''):
        super(ConvolutionBase, self).__init__(input_shape, name)
        self.ch = n_filters

        if isinstance(filter_size, int):
            self.filter_size = filter_size, filter_size
        else:
            self.filter_size = filter_size
        if isinstance(strides, int):
            self.strides = strides, strides
        else:
            self.strides = strides
        self.weight_initializer = weight_initializer
        self.bias_initializer = bias_initializer
        self.padding = padding if padding == 'same' else None
        self.W_shape = None
        self.b_shape = None
        self.use_biases = use_biases
        self.weights = None
        self.current_layer_input = None
Ejemplo n.º 2
0
 def initialize(self, fitness_function, search_space):
     self.fitness_function = self._fitnessCounter(fitness_function)
     self.search_space = search_space
     self.initializer = UniformInitializer(search_space)
     self.iteration = 0
     self.fitness_evaluations = 0
     self.best_solution = np.array([np.NaN]*self.num_dims), np.inf
     self.employed = np.zeros((self.num_employed, self.num_dims))
     self.foodsources_fitness = np.ones((self.num_employed, 1))*np.inf
     self.attempts_remaining = np.zeros(self.num_employed)
     self.history = []
Ejemplo n.º 3
0
def gradient_teacher_test():
    uniform = UniformInitializer(seed=2019)
    inputs = uniform((5, 1))
    outputInitializer = ConstShapeInitializer(
        [function(value) for value in inputs])
    outputs = outputInitializer((5, 1))
    dataStore = Store(np.concatenate((inputs, outputs), axis=1))
    square_error = SquareError()
    gradient = Gradient()
    teacher = GradientTeacher()

    teacher(network, gradient, square_error, dataStore, 1000, 5)
Ejemplo n.º 4
0
def test_algorithms(algorithms, objective_function, num_experiments,
                    num_iterations, search_bounds):
    results = []
    for experiment in range(num_experiments):
        print('Experiment {}/{}'.format(experiment + 1, num_experiments))
        algorithm_results = []
        for algorithm in algorithms:
            algorithm.initialize(UniformInitializer(search_bounds))
            curve = []
            for _ in range(num_iterations):
                algorithm.minimize(objective_function)
                curve.append(algorithm.getBestSolution()[1][0])
            algorithm_results.append(curve)
        results.append(algorithm_results)
    return np.array(results)
Ejemplo n.º 5
0
    def __init__(self,
                 output_size,
                 input_shape=(None, None),
                 weight_initializer=UniformInitializer(),
                 bias_initializer=None,
                 name='',
                 reg=0.0):

        super(Affine, self).__init__(input_shape, name)
        # self.input_shape = input_shape  # first is batch size, second is dimensionality

        self.output_size = output_size
        self.weight_initializer = weight_initializer
        self.bias_initializer = bias_initializer
        self.weights = None  # matrix of concatenated column-wise weights (including biases)
        self.current_layer_input = None
        self.reg = reg
def main():
    algoritms = tqdm([(pso, 'PSO'), (abc, 'ABC'), (fss, 'FSS')], 'Algorithms')
    functions = tqdm([(sphereFunction, [[-100, 100]], 'Sphere'),
                      (rastriginsFunction, [[-5.12, 5.12]], 'Rastrigin'),
                      (rosenbrockFunction, [[-30, 30]], 'Rosenbrock')],
                     'Functions')
    results = pd.DataFrame(columns=[
        'best_fitness', 'fitness_evaluations', 'iterations', 'experiment',
        'algorithm', 'test_function'
    ])
    for function, search_space, function_name in functions:
        functions.set_postfix({'Function': function_name})
        for algoritm, algorithm_name in algoritms:
            algoritms.set_postfix({'Algorithm': algorithm_name})
            for simulation in trange(num_simulations, desc='Runs'):
                if algorithm_name == 'ABC':
                    algoritm.initialize(function, search_space=search_space)
                else:
                    algoritm.initialize(
                        function, initializer=UniformInitializer(search_space))
                with tqdm() as pgb:
                    while algoritm.fitness_evaluations <= num_fitness_evaluations:
                        algoritm.minimize()
                        progress = round(100 * algoritm.fitness_evaluations /
                                         num_fitness_evaluations)
                        pgb.update(progress)

                history = algoritm.getHistory()
                history.insert(loc=history.shape[1],
                               column='experiment',
                               value=simulation)
                history.insert(loc=history.shape[1],
                               column='algorithm',
                               value=algorithm_name)
                history.insert(loc=history.shape[1],
                               column='test_function',
                               value=function_name)
                results = results.append(history,
                                         sort=False,
                                         ignore_index=True)

    print('\n\nSaving results...')
    file_name = 'results_N2.csv'
    results.to_csv(file_name)
    print('Results saved to ' + file_name)
Ejemplo n.º 7
0
from graphics.neural_network_chart_widget import NeuralNetworkChartWidget
from graphics.neural_network_teaching_controller_widget import NeuralNetworkTeachingControllerWidget
from graphics.neural_network_model_controller_widget import NeuralNetworkModelControllerWidget
from initializers import RangeInitializer, UniformInitializer, ConstInitializer
from store import Store

__all__ = ['NeuralNetworkWidget']

CHART_UPDATE_INTERVAL = 100
ORIGINAL_POINTS_COUNT = 1000
TRAIN_POINTS_COUNT = 10

zero_initializer = ConstInitializer(0.)
range_initializer = RangeInitializer(-2., 2.)
uniform_initializer = UniformInitializer(-.1, .1)


def noise(values):
    x_delta = zero_initializer((values.shape[0], 1))
    y_delta = uniform_initializer((values.shape[0], 1))
    delta = np.concatenate((x_delta, y_delta), axis=1)
    return values + delta


class NeuralNetworkWidget(QWidget):
    def __init__(self,
                 function_code,
                 model,
                 teacher,
                 gradient,
Ejemplo n.º 8
0
def test_uniform_initializer():
    initializer = UniformInitializer(
        seed=2019
    )
    expected = [0.80696443, -0.21383899,  0.24793992,  0.2757548]
    equals(expected, initializer(4))