예제 #1
0
파일: mutation.py 프로젝트: bfl2/EEAckley
def mutation_case2(indiv):
    chromosome = indiv[0]
    sigma = indiv[-1]
    mutationed = chromosome[:]
    n = len(chromosome) 

    epson_0 = 0.0001 
    learning_rate = 1/sqrt(2*n)
    learning_rate_line = 1/sqrt(2*sqrt(n))
    
    var_fix = N(0,1)
    sigma_line = []
    
    for i in range(0,n): ###sigma nao eh o ultimo elemento da lista de cromossomo e sim do individuo
        var_aleat = N(0,1)
        
        sigma_line.append(sigma[i]*exp((learning_rate_line * var_fix) + (learning_rate * var_aleat)))
        
        if sigma_line[i] < epson_0:
            sigma_line[i] = epson_0
            
        mutationed[i] = (chromosome[i] + sigma_line[i] * var_aleat)
        aux = fitness(mutationed)
        if(aux > indiv[1]):
            mutationed[i] = chromosome[i]


    ### Mutacao tem que retornar individuo no formato: [cromossomo, fitness, sigma]
    mutationedF = [mutationed, fitness(mutationed), sigma_line]

    return mutationedF
예제 #2
0
    def make_child(self, neural_net):
        p_mutat_rate = neural_net.mutat_rate
        W = len(neural_net.w)
        weird_coefficient = math.exp(N(0, 1) / ((2**0.5) * (W**0.25)))
        c_mutat_rate = p_mutat_rate * weird_coefficient

        child = RandomNeuralNet(self.node_layers, self.act_func,
                                self.weight_range, c_mutat_rate)

        new_weights = {}
        for key in neural_net.w:
            p_weight = neural_net.w[key]
            new_weights[key] = p_weight + p_mutat_rate * N(0, 1)

        child.set_weights(new_weights)
        return child
예제 #3
0
파일: mutation.py 프로젝트: bfl2/EEAckley
def mutation_case1(indiv):###entrada eh uma lista da forma [cromossomo,fitness, sigma] Onde cromossomo eh uma lista e fitness e sigma sao floats
    chromosome = indiv[0]
    sigma = indiv[-1]
    mutationed = []
    n = len(chromosome)

    epson_0 = 0.2
    learning_rate = 1 / sqrt(n)
    sigma_line = sigma * exp(learning_rate * N(0, 1))

    if sigma_line < epson_0:
        sigma_line = epson_0

    for xi in chromosome: ###sigma nao eh o ultimo elemento da lista de cromossomo e sim do individuo
        mutationed.append(xi + sigma_line * N(0, 1))

    ### Mutacao tem que retornar individuo no formato: [cromossomo, fitness, sigma]
    mutationedF = [mutationed, fitness(mutationed), sigma_line]

    return mutationedF
예제 #4
0
def cross_entropy_method(n, f, p, d, iterations):

    # init parameters
    μ = N(np.zeros(d), np.eye(d) * np.ones(d))
    σ = abs(N(np.zeros(d), 10 * np.eye(d) * np.ones(d)))

    for i in range(iterations):
        # sample new parameters & evaluate fittness
        θ = N(μ, np.eye(d) * σ, n)
        #import ipdb; ipdb.set_trace()

        R = np.array(list(map(f, θ)))

        # sort θ by performance and yield best one
        θ = θ[np.argsort(R)]
        yield θ[0], μ, σ

        # fit new parameter distribution best p parameters
        cut_i = int(len(θ) * p)
        elite = θ[cut_i:]
        μ = np.mean(elite, axis=0)
        σ = np.var(elite, axis=0)
예제 #5
0
def monte_carlo_normal(Niter):
    #Niter = 10000
    y = np.zeros((Niter, ))
    z = 0
    for i in range(Niter):
        x = N(0, 1)

        if x > 0:
            y[i] = 1
        else:
            y[i] = 0

        z += y[i]
    integral = z / Niter

    return integral, y
예제 #6
0
def gen_multilabel_data(num_labels,num_samples_per,size_constraint):
    for i in range(num_labels):
        this_center = np.random.randint(size_constraint,size=2)
        # this_cov_array = make_sparse_spd_matrix(2)
        this_cov_array = np.array([[1,0],[0,1]])

        dX = N(this_center,this_cov_array,size = num_samples_per)
        dy = np.zeros((1,num_samples_per))+i
        
        if i == 0:
            X = dX
            y = dy
        else:
            X = np.append(X,dX,0)
            y = np.append(y,dy)

    return(X,y)
예제 #7
0
from sklearn import datasets
from sklearn.naive_bayes import GaussianNB
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

## a
# write a general function to generate random samples from N(u,E) in d-dimensions

# in this homework u is the mean of a nd dataset and E is the cov matrix of 1 a nd dataset

# dimetionaity is determined by the length of the mean and cov (u,E) vectors
u = np.array([0, 0])
E = np.array([[1, 0], [0, 1]])
num_samples = 100

data = N(u, E, size=num_samples)

## b
# write a procedure of the discriminant:


def g(x, u, E):

    d = len(x)

    a = (-1 / 2) * np.matmul(np.matmul((x - u).T, inv(E)), (x - u))
    b = (-1 * d / 2) * np.log(2 * np.pi)
    c = (-1 / 2) * np.log(det(E)) + np.log(1 / len(x))

    return (a + b + c)
예제 #8
0
def rand_bimodal(m0, m1, s0=1, s1=1, f=U):
    p = f(0,1)
    x = p * N(m0, s0) + (1 - p) * N(m1,s1)
    return x
예제 #9
0
"""

from utils import draw_ellipse
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import multivariate_normal as N

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import matplotlib.transforms as transforms

mean = np.array([1., 2.])
cov = np.array([[2, 0.1], [0.1, 0.5]])

sample_points = N(mean, cov, 100000)

plt.plot(sample_points[:, 0], sample_points[:, 1], 'rx', alpha=0.1)

x, y = draw_ellipse(mean, cov, num_sd=1)
plt.plot(x, y, 'k')
x, y = draw_ellipse(mean, cov, num_sd=2)
plt.plot(x, y, 'k')
x, y = draw_ellipse(mean, cov, num_sd=3)
plt.plot(x, y, 'k')

# It doesn't f*****g work!!!


def confidence_ellipse(x, y, ax, n_std=3.0, facecolor='none', **kwargs):
    """
예제 #10
0
# What is the impact of adding in this penalty?
# Write your own implementation of logistic regression and
# implement your model on either real-world
# (see Github data sets:https://github.com/gditzler/UA-ECE-523-Sp2018/tree/master/data),
# or synthetic data. If you simply use Scikit-learn’s implementation of the logistic
# regression classifier, then you’ll receive zero points.
# A full 10/10 will be awarded to those that implement logistic
# regression using the optimization of cross-entropy using stochastic gradient descent

# creating my own synthetic data
u1 = np.array([0, 2])
u2 = np.array([2, 0])
E = np.array([[1, 0], [0, 1]])
num_samples = 100

data1 = N(u1, E, size=num_samples)
data2 = N(u2, E, size=num_samples)

data_full = np.append(data1, data2, axis=0)
labels_full = np.append(np.zeros(len(data1)), np.ones(len(data2)), axis=0)


def my_sigmoid(x):
    return 1 / (1 + np.exp(-x))


epochs = 200
learning_rate = 0.1
X = np.asarray(data_full).T
y = np.asarray(labels_full).T
X = np.concatenate([X, np.ones([1, X.shape[-1]])], axis=0)