Esempio n. 1
0
def test_nbr_neuron(list_test):
    color_list=['r','g','b','k','m','c','y']
    color_list *= 3
    k=0
    for i in list_test :
        my_layer1 = Layer.Linear(6,i)
        my_layer2 = ActivationFunctions.Tanh()
        my_layer5 = Layer.Linear(i,i)
        my_layer6 = ActivationFunctions.Tanh()
        my_layer3 = Layer.Linear(i,1)
        my_layer4 = ActivationFunctions.Sigmoid()
        my_NN = Neural_network.NeuralNet([my_layer1, my_layer2, my_layer5, my_layer6, my_layer3, my_layer4])
        
        
        chi2_list, error_list = User.train(my_NN, data_train_input, data_train_target, num_epochs = num_epoch_max, optimizer = Optimizer.SGD(lr = my_lr), batch_size=my_batch_size)
        
        data_test_prediction = User.prediction(my_NN,data_test_input)
        error_final = Error_round.error_round(data_test_prediction, data_test_target)

        plt.plot(range(num_epoch_max), error_list, label= str(i), c=color_list[k])
        plt.plot([num_epoch_max],[error_final], marker='o', c=color_list[k])
        plt.xlabel('Epoch')
        plt.ylabel('Training round error')
        
        k+=1
    plt.legend(title='Neurons')
    plt.title('Optimisation of the number of neurons')
    plt.show()
Esempio n. 2
0
def test_nbr_layer(list_test, n_neuron):
    color_list=['r','g','b','k','m','c','y']
    color_list *= 3
    k=0
    
    my_layerini1 = Layer.Linear(6,n_neuron)
    my_layerini2 = ActivationFunctions.Tanh()
    my_layerfini1 = Layer.Linear(n_neuron,1)
    my_layerfini2 = ActivationFunctions.Sigmoid()
        
    for i in list_test :
        layers_new = [my_layerini1, my_layerini2]
        for j in range(i) :
            layers_new += [Layer.Linear(n_neuron,n_neuron),ActivationFunctions.Tanh()]
        layers_new += [my_layerfini1, my_layerfini2]
        my_NN = Neural_network.NeuralNet(layers_new)
        
        
        chi2_list, error_list = User.train(my_NN, data_train_input, data_train_target, num_epochs = num_epoch_max,optimizer = Optimizer.SGD(lr = my_lr), batch_size=my_batch_size)
        data_test_prediction = User.prediction(my_NN,data_test_input)
        
        error_final = Error_round.error_round(data_test_prediction, data_test_target)
        
        plt.plot(range(num_epoch_max), error_list, label= str(i),c=color_list[k])
        plt.plot([num_epoch_max],[error_final], marker='o', c=color_list[k])
        plt.xlabel('Epoch')
        plt.ylabel('Training round error')
        
        k+=1
    plt.legend(title='Hidden layers')
    plt.title('Optimisation of the number of hidden layers')
    plt.show()
Esempio n. 3
0
def test_NeuralNet() :
    my_layer1 = Layer.Linear(3,2)
    my_layer2 = ActivationFunctions.Tanh()
    my_NN = Neural_network.NeuralNet([my_layer1,my_layer2])
    
    input = np.array([[1,2,3],[4,5,6]])
    grad =  np.array([[0.5,0.2],[0.1,0.3]])
    
    
    print('forward', my_NN.forward(input))
    print('backward', my_NN.backward(grad))
    '''OK'''
Esempio n. 4
0
 def _create_activations(self):
     funs = []
     for n in self.n_neurons_per_layer:
         F = []
         for i in range(n):
             f = af.Tanh()
             if i % 2 == 0:
                 f = af.Sigmoid()
             F.append(f)
         F = np.array(F)
         funs.append(F)
     self.activation_funs = np.array(funs)
Esempio n. 5
0
def test_train_prediction() :
    my_layer1 = Layer.Linear(3,2)
    my_layer2 = ActivationFunctions.Tanh()
    my_NN = Neural_network.NeuralNet([my_layer1,my_layer2])
    
    input = np.array([[1,2,3],[4,5,6]])
    target = np.array([[0.5,0.2],[0.1,0.3]])
    
    User.train(my_NN, input, target, batch_size = 1)
    #By careful, we must have size_training = number of rows in our data
    
    input_predict = np.array([[1,1,4],[0.5,2,4]])
    print(User.prediction(my_NN,input_predict))
    ''' OK '''
Esempio n. 6
0
 def gate_transform(self, affine_gates):
     """
     apply gate Non-Linearity
     """
     h = self.hl_size
     affine_gates[0:h, :] = activation_functions.Sigmoid().transform(
         self.i(affine_gates))
     affine_gates[h:2 * h, :] = activation_functions.Sigmoid().transform(
         self.f(affine_gates))
     affine_gates[2 * h:3 *
                  h, :] = activation_functions.Sigmoid().transform(
                      self.o(affine_gates))
     affine_gates[3 * h:, :] = activation_functions.Tanh().transform(
         self.g(affine_gates))
     transformed_gates = affine_gates
     return transformed_gates
Esempio n. 7
0
def test_XOR() :
    my_layer1 = Layer.Linear(2,3)
    my_layer2 = ActivationFunctions.Tanh()
    my_layer3 = Layer.Linear(3,1)
    my_layer4 = ActivationFunctions.Sigmoid()
    #my_layer3 = lib2.Arondi()
    my_NN = Neural_network.NeuralNet([my_layer1,my_layer2,my_layer3,my_layer4])
    
    input =np.array([[0, 0], [1, 0], [0, 1], [1, 1]])
    target = np.array([[0], [1], [1], [0]])
    
    User.train(my_NN, input, target, batch_size = 1,,num_epochs= 1000)
    # By careful, we must have size_training = number of rows in our data
    
    input_predict = np.array([[0, 0], [1, 0], [0, 1], [1, 1]])
    print(User.prediction(my_NN,input_predict))
    ''' OK '''
import fcnetwork

import cost_functions
import activation_functions
import learning_methods
import regularization_functions as reg
import mnist as mnist_loader
import numpy as np
import sys
import batch_norm as bn
network_topology = [784, 30, 10]
#activation_functions.PReLU(learning_methods.Momentum(.01,.7),20),
#activation_functions.PReLU(learning_methods.Momentum(.01,.7),30)
network_activations = [activation_functions.Tanh(), \
activation_functions.Softmax()]


def reduceL(t):
    for index, v in enumerate(t):
        x, y = v
        t[index] = x, np.argmax(y)
    return t


eta = 3
lmbda = 0
epochs = 64
mini_batch = 10
net=fcnetwork.FCNetwork(network_topology, \
                        network_activations, \
                        cost_functions.CrossEntropy(),\
Esempio n. 9
0
def train_simultaneousNN(
        inputs_train: Tensor,
        targets_train: Tensor,
        loss: Loss.Loss = Loss.MeanSquareError(),
        optimizer: OptimizerClass.Optimizer = OptimizerClass.SGD(),
        num_epochs: int = 5000,
        batch_size: int = 32) -> tuple:

    size_training = inputs_train.shape[0]
    Result_chi2 = [[], [], [], [], [], [], [], [], []]
    list_epoch = np.array(range(10, 50, 5)) / 100 * num_epochs
    '''initialisation des 9 NN'''  #verifier question seed()
    list_net = []
    for i in range(9):
        layers = []
        layers.append(Layer.Linear(6, 4))
        layers.append(ActivationFunctions.Tanh())
        layers.append(Layer.Linear(4, 2))
        layers.append(ActivationFunctions.Tanh())
        layers.append(Layer.Linear(2, 1))
        layers.append(ActivationFunctions.Sigmoid())
        list_net.append(Neural_network.NeuralNet(layers))

    destroyed_NN = []
    nbr_batch = size_training // batch_size
    ''' training des 9 NN'''
    for epoch in range(num_epochs):

        for k in range(9):
            if k not in destroyed_NN:
                Chi2_train = 0

                for i in range(0, size_training, batch_size):

                    # 1) feed forward
                    y_actual = list_net[k].forward(inputs_train[i:i +
                                                                batch_size])

                    # 2) compute the loss and the gradients
                    Chi2_train += loss.loss(targets_train[i:i + batch_size],
                                            y_actual)
                    grad_ini = loss.grad(targets_train[i:i + batch_size],
                                         y_actual)

                    # 3)feed backwards
                    grad_fini = list_net[k].backward(grad_ini)

                    # 4) update the net
                    optimizer.step(list_net[k], n_epoch=epoch)

                Chi2_train = Chi2_train / nbr_batch
                Result_chi2[k].append(Chi2_train)
        '''Supression du NN le moins efficace '''
        if epoch in list_epoch:
            Comparaison = [[], []]
            for k in range(9):
                if k not in destroyed_NN:
                    ErrorSlope = np.polyfit(np.array(range(epoch - 49, epoch)),
                                            Result_chi2[k][-50:-1], 1)[0]
                    MixedError = Result_chi2[k][-1] * (1 -
                                                       np.arctan(ErrorSlope) /
                                                       (np.pi / 2))
                    Comparaison[0].append(k)
                    Comparaison[1].append(MixedError)

            k = Comparaison[0][Comparaison[1].index(max(Comparaison[1]))]
            destroyed_NN.append(k)

        if epoch % 100 == 0:
            print('epoch : ' + str(epoch) + "/" + str(num_epochs) + "\r",
                  end="")

    for k in range(9):
        if k not in destroyed_NN:
            my_NN = list_net[k]
    return my_NN, Result_chi2
Esempio n. 10
0
np.random.seed(1)
'''size of the training set and the testing set '''
train_size = 3000
test_size = 1500
'''Maximal number of epochs '''
my_num_epochs = 500
'''size of the batch'''
my_batch_size = 100
''' learning rate'''
my_lr = 0.001

my_initial_lr = 0.1
my_decay_coeff = 1 / 200
'''Construction of the neural network '''
my_layer1 = Layer.Linear(6, 5)
my_layer2 = ActivationFunctions.Tanh()
my_layer3 = Layer.Linear(5, 4)
my_layer4 = ActivationFunctions.Tanh()
my_layer5 = Layer.Linear(4, 3)
my_layer6 = ActivationFunctions.Tanh()
my_layer7 = Layer.Linear(3, 2)
my_layer8 = ActivationFunctions.Tanh()
my_layer9 = Layer.Linear(2, 1)
my_layer10 = ActivationFunctions.Sigmoid()
my_NN = Neural_network.NeuralNet([
    my_layer1, my_layer2, my_layer3, my_layer4, my_layer5, my_layer6,
    my_layer7, my_layer8, my_layer9, my_layer10
])

## Importation of the training and testing data
os.chdir(path_ini[:-4])
Esempio n. 11
0
weights = []
functions = []

# Loop for creating random weights between in range (-2, 2), and activation
# functions alternating between sigmoid and tanh
for i in range(n_layers):
    n = n_neurons_per_layer[i]
    ins = inputs_per_layer[i]
    layer_w = []
    layer_f = []
    for j in range(n):
        layer_w.append(np.random.uniform(-2, 2, ins))
        if i % 2 == 0:
            layer_f.append(af.Sigmoid())
        else:
            layer_f.append(af.Tanh())

    functions.append(np.array(layer_f))
    weights.append(np.array(layer_w))

weights = np.array(weights)
functions = np.array(functions)

#%%

network = nn.NeuralNetwork(n_layers, n_neurons_per_layer, n_in, n_out)

# Set network parameters
network.set_iterations(n_iter)
network.set_learning_rate(0.01)
network.set_weights(weights)