Ejemplo n.º 1
0
    def test_updateWeights(self):
        weights1 = tf.constant([4, 5, 6])

        target = Neuron(weights1, 7)
        tf.debugging.assert_equal(target._weights, weights1)

        weights2 = tf.constant([8, 9, 10])
        target.updateWeights(weights2)
        tf.debugging.assert_equal(target._weights, weights2)
Ejemplo n.º 2
0
 def generate_random(self, count, layer_size=3):
     for i in range(count):
         if random.randrange(0, 10) == 5:
             self.connect(
                 self.add_neuron(
                     Neuron(random.uniform(0.1, 0.3), random.uniform(0.6, 1.2), random.uniform(0.2, 0.7))),
                 self.get_rand_neurons(random.randrange(1, layer_size), len(self.neurons)))
         else:
             self.connect(self.add_neuron(ResistNeuron(random.uniform(0.7, 1.1), random.uniform(0.9, 1.5))),
                          self.get_rand_neurons(random.randrange(1, layer_size), len(self.neurons)))
 def __init__(self):
     ## @var neurons
     # List of neurons in the structure
     self.neurons = [Neuron() for index in range(10)]
     ## @var carry_over
     # Boolean variable. True signals a carry over.
     self.carry_over = False
     ## @var index
     # Structure index. Points to one of the ten neurons in the structure.
     self.index = 0
Ejemplo n.º 4
0
    def __init__(self):
        self.rs = RS_232(wrk_dir='/tmp/Cortix', filename='ir_7040')
        self.neuron = Neuron(task=self.rs.ir_7040,wrk_dir='/tmp/Cortix',name='ir_7040')\

        self.socket_list = []
        self.init_port = 60000
        self.port = 60001
        #self.host= str(socket.gethostbyname(socket.gethostname()))
        self.host = '10.253.90.99'
        print(self.host)
Ejemplo n.º 5
0
    def test_init(self):
        weights = tf.constant([1, 2, 3])
        bias = tf.constant(4)

        target = Neuron(weights, bias)

        tf.debugging.assert_equal(target._weights, weights)
        tf.debugging.assert_equal(target.weights, weights)
        tf.debugging.assert_equal(target._bias, bias)
        tf.debugging.assert_equal(target.bias, bias)
Ejemplo n.º 6
0
 def loadptcs(self):
     """Load neurons from a single .ptcs file"""
     self.header = PTCSHeader()
     with open(self.path, 'rb') as f:
         self.header.read(f)
         for i in range(self.header.nneurons):
             neuron = Neuron(self.path, sort=self)
             neuron.loadptcs(f, self.header)
             self.alln[neuron.id] = neuron # save it
         assert eof(f), 'File %s has unexpected length' % self.path
Ejemplo n.º 7
0
    def __init__(self, num_neurons, bias):

        # Every neuron in a layer shares the same bias 一层中的所有神经元共享一个bias
        self.bias = bias if bias else random.random()
        random.random()
        # 生成0和1之间的随机浮点数float,它其实是一个隐藏的random.Random类的实例的random方法。
        # random.random()和random.Random().random()作用是一样的。
        self.neurons = []
        for i in range(num_neurons):
            self.neurons.append(Neuron(self.bias))
Ejemplo n.º 8
0
    def __init__(self, layer_indx):
        '''
        Constructor
        '''
        self.layer_indx = layer_indx

        # create neurons
        self.neurons = []

        # if input layer
        if (layer_indx == 0):
            for k in range(0, varis.x.shape[0]):
                numOf_weights = 0
                neuron = Neuron(numOf_weights)
                self.neurons.append(neuron)

        # if middle layers
        elif (layer_indx <= varis.hidden_layers):

            if (layer_indx == 1):
                # first hidden layer

                # +1 for bias weight
                numOf_weights = varis.x.shape[0] + 1
                neuron = Neuron(numOf_weights)
                self.neurons.append(neuron)

            else:
                # rest of the layers
                for k in range(0, varis.hidden_neurons):

                    # +1 for bias weight
                    numOf_weights = varis.hidden_neurons + 1
                    neuron = Neuron(numOf_weights)
                    self.neurons.append(neuron)

        # if last layer
        else:
            for k in range(0, varis.y.shape[0]):
                numOf_weights = varis.hidden_neurons
                neuron = Neuron(numOf_weights)
                self.neurons.append(neuron)
Ejemplo n.º 9
0
 def __init__(self,
              nbNeurons=5,
              nbConnexions=5,
              nbInputsNeurons=2,
              nbOutputsNeurons=1,
              genes=None):
     self.fitness = 0.0
     if (genes != None):
         neuronGenes = genes[0]
         connexionGenes = genes[1]
         # print(neuronGenes)
         # print(connexionGenes)
         self.nbNeurons = 0
         self.neuronList = []
         self.inputNeuronList = []
         self.outputNeuronList = []
         self.connexionList = []
         for neuronGene in neuronGenes:
             neuron = None
             if neuronGene[1] == 'n':
                 neuron = Neuron(self.nbNeurons, bias=neuronGene[0])
             elif neuronGene[1] == 'i':
                 neuron = InputNeuron(self.nbNeurons, bias=neuronGene[0])
                 self.inputNeuronList.append(neuron)
             elif neuronGene[1] == 'o':
                 neuron = OutputNeuron(self.nbNeurons, bias=neuronGene[0])
                 self.outputNeuronList.append(neuron)
             self.neuronList.append(neuron)
             self.nbNeurons += 1
         for connGene in connexionGenes:
             fromNeuron = self.neuronList[connGene[0]]
             toNeuron = self.neuronList[connGene[1]]
             self.connexionList.append(
                 Connexion(fromNeuron, toNeuron, connGene[2]))
         self.nbConnexions = len(self.connexionList)
         self.species = str(self.nbNeurons) + "_" + str(self.nbConnexions)
     else:
         self.nbNeurons = 0
         self.neuronList = []
         self.inputNeuronList = []
         self.outputNeuronList = []
         self.nbConnexions = 0
         self.connexionList = []
         self.species = str(self.nbNeurons) + "_" + str(self.nbConnexions)
         for idInputNeuron in range(nbInputsNeurons):
             self.createNeuron("input")
         for idNeuron in range(nbNeurons - nbInputsNeurons -
                               nbOutputsNeurons):
             self.createNeuron()
         for idOutputNeuron in range(nbOutputsNeurons):
             self.createNeuron("output")
         for idConnexion in range(nbConnexions):
             self.createConnexion()
         self.clean()
Ejemplo n.º 10
0
 def initilize(self):
     weight_num = self.inputNum
     for i in range(self.hiddenLayerNum):
         self.layers.append(Neuron(weight_num, self.neuronNum[i]))
         weight_num = self.neuronNum[i]
         #self.deltes.append(list(temp2))
     for i in range(self.hiddenLayerNum):
         Y = np.zeros(self.neuronNum[i])
         Y2 = np.zeros(self.neuronNum[i])
         Neuron.neuron_ouput.append(Y)
         Neuron.neuron_deltes.append(Y2)
Ejemplo n.º 11
0
    def __init__(self, learning_rate: float = 0.1, activation_function: callable = None):
        self._learning_rate = learning_rate
        self.activation_function = activation_function

        self.neuron = Neuron(learning_rate, activation_function)

        self.list_sources_target = []

        self.cycles = 0

        self.total_square_error_by_cycle = []
Ejemplo n.º 12
0
def test1():
    test_neuron = Neuron(3)
    test_layer = Layer(3, 0)
    test_layer.neurons[0].alpha = 0
    test_layer.neurons[1].alpha = 1
    test_layer.neurons[2].alpha = 0
    test_neuron.weights[0] = 1
    test_neuron.weights[1] = 1
    test_neuron.weights[2] = 1

    print("This should print out 0.73:")
    print(sig_func(test_neuron, test_layer))
Ejemplo n.º 13
0
    def addLayer(self, layerSize):
        self.NetworkStruct.append([])
        for i in range(layerSize):
            self.NetworkStruct[-1].append(
                Neuron(len(self.NetworkStruct), i + 1))

        #weights[second layer number, position of 1st neuron on last layer, position of 2nd neuron on current layer]
        self.weights.append([])
        for i in range(len(self.NetworkStruct[-2])):
            self.weights[-1].append([])
            for _ in range(len(self.NetworkStruct[-1])):
                self.weights[-1][-1].append(.5)
Ejemplo n.º 14
0
 def createNeuron(self, *args, **keywordArgs):
     """
     Create a new neuron.
     
     >>> neuron = network.createNeuron(name = 'AVAL')
      
     Returns the :class:`neuron <Network.Neuron.Neuron>` that is created.
     """
     
     neuron = Neuron(self, *args, **keywordArgs)
     self.addObject(neuron)
     return neuron
Ejemplo n.º 15
0
def build_custom_layer(prev_layer_size, layer_size):
    new_layer = []

    for _ in range(layer_size):
        new_neuron = Neuron()
        weights = []
        for _ in range(prev_layer_size):
            weights.append(random.random() * 2 - 1)
        new_neuron.set_weights(weights)
        new_neuron.set_bias(random.random() * 2 - 1)
        new_layer.append(new_neuron)
    return new_layer
Ejemplo n.º 16
0
    def __init__(self, num_neurons, act):
        """ Constructs a Neural Layer
            
            num_neurons: number of Neurons width of layer
            act: layer activation function
        """
        self.num_neurons = num_neurons
        self.neurons = []

        for i in range(self.num_neurons):
            neuron = Neuron(act=act)
            self.neurons.append(neuron)
Ejemplo n.º 17
0
    def __init__(self, input_count, hidden_neurons, output_neurons):
        """Creates and initializes the neural network.
        
        `input_count` -- The number of inputs for this network.
        `hidden_neurons` -- A 2-d list of `NeuronInfo` instances. Each row represents a hidden layer.
        The layers can be of different lenghts.
        `output_neurons` -- a 1-d list of `NeuronInfo` instances. This represents output neurons.
        """
        # Initialize the input layer. The last element of the all layers will be the bias neuron.
        self.__input_layer = [InputNeuron() for i in range(input_count)] + [BiasNeuron()]

        # Initialize the hidden layers.
        self.__hidden_layers = []
        last_layer = self.__input_layer
        for infos in hidden_neurons:
            # The last element of the all layers will be the bias neuron.
            layer = [Neuron(last_layer, info.weights, info.activation_function, info.activation_function_derivative) for info in infos] + [BiasNeuron()]
            last_layer = layer
            self.__hidden_layers.append(layer)

        # Initialize the output layer.
        self.__output_layer = [Neuron(last_layer, info.weights, info.activation_function, info.activation_function_derivative) for info in output_neurons]
Ejemplo n.º 18
0
 def __init__(self, num_neurons, num_inputs, activation, derivative):
     """
         Initializes a layer of neurons
         @param (Number) num_neurons - The number of neurons in the layer
         @param (Number) num_inputs - The number of inputs of each neuron in the layer
         @param (Function) activation - The activation function to be used
         @param (Function) derivative - The derivative of the activation function
         @returns (Layer)
     """
     self.neurons = [
         Neuron(num_inputs, activation, derivative)
         for i in range(0, num_neurons, 1)
     ]
Ejemplo n.º 19
0
    def __init__(self, neuronCount, minBias, maxBias, minWeight, maxWeight):
        self.neurons = [[]]
        for j in range(len(neuronCount) - 1, -1, -1):
            amount = neuronCount[j]
            for i in range(amount):
                # First go, output neurons
                if j == len(neuronCount) - 1:
                    self.neurons[0].append(
                        Neuron(f"{j},{i}", random.uniform(minBias, maxBias),
                               []))
                else:
                    nextLayer = []
                    if j != len(neuronCount) - 1:
                        for neuron in self.neurons[len(neuronCount) - 2 - j]:
                            nextLayer.append(
                                Connection(
                                    neuron,
                                    random.uniform(minWeight, maxWeight)))
                    if len(self.neurons) > len(neuronCount) - 1 - j:
                        if j == 0:
                            self.neurons[len(neuronCount) - 1 - j].append(
                                Neuron(f"{j},{i}", 0, nextLayer))
                        else:
                            self.neurons[len(neuronCount) - 1 - j].append(
                                Neuron(f"{j},{i}",
                                       random.uniform(minBias, maxBias),
                                       nextLayer))
                    else:
                        if j == 0:
                            self.neurons.append(
                                [Neuron(f"{j},{i}", 0, nextLayer)])
                        else:
                            self.neurons.append([
                                Neuron(f"{j},{i}",
                                       random.uniform(minBias, maxBias),
                                       nextLayer)
                            ])

        self.neurons = self.neurons[::-1]
Ejemplo n.º 20
0
 def createNeuron(self, type="norm"):
     if type == "input":
         neuron = InputNeuron(self.nbNeurons)
         self.neuronList.append(neuron)
         self.inputNeuronList.append(neuron)
     elif type == "output":
         neuron = OutputNeuron(self.nbNeurons)
         self.neuronList.append(neuron)
         self.outputNeuronList.append(neuron)
     else:
         neuron = Neuron(self.nbNeurons)
         self.neuronList.append(neuron)
     self.nbNeurons += 1
Ejemplo n.º 21
0
    def __init__(self, hiddenActivation="sigmoid"):
        # activation function for hidden layer
        self.hiddenActivation = hiddenActivation

        # Select appropriate weight initialization for activation function
        if self.hiddenActivation == "sigmoid":
            init_technique = "xavier"
        elif self.hiddenActivation in ["ReLU", "leaky ReLU"]:
            init_technique = "he"
        else:
            raise Exception("Activation for hidden layer not recognized")

        # create hidden layer with 5 neurons
        self.hidden = [
            Neuron(initialize(init_technique, 11, 3, 11), 0,
                   self.hiddenActivation) for i in range(5)
        ]

        # create a softmax layer with 3 neurons
        self.soft = [
            Neuron(initialize("xavier", 5, 1, 5), 0, None) for i in range(3)
        ]
Ejemplo n.º 22
0
 def decide_trucada(self, hand):
     cards_in_hand_weight = self.cards_weight(self.cards_in_hand)
     cards_in_hand_mean = cards_in_hand_weight / len(self.cards_in_hand)
     entries_choice = [[
         self.who_made_first,
         self.cards_weight(self.opponent_cards_played), cards_in_hand_mean,
         hand
     ]]
     choice_neuron = Neuron(entries_choice).exec()
     if round(choice_neuron) == 1:
         return ('r', 1)
     else:
         return ('r', 2)
Ejemplo n.º 23
0
 def init_neurons(self):
     for layer_ix, layer_size in enumerate(self.initial_topology):
         layer = []
         if layer_ix == 0:
             _type = 'input'
         elif layer_ix == len(self.initial_topology) - 1:
             _type = 'output'
         else:
             _type = 'hidden'
         for _ in range(layer_size):
             neuron = Neuron(_type)
             layer.append(neuron)
         self.layers.append(layer)
Ejemplo n.º 24
0
    def __init__(self, num_in, num_out, *num_hidden):

        self.num_in = num_in
        self.num_out = num_out
        self.num_hidden = num_hidden
        self.num_layers = 2 + len(num_hidden)

        # Neuron array l, n
        self.layers = []
        self.bias = []

        # setup neurons in layers array
        for l in range(self.num_layers):
            if l == 0:
                layer = [RestrictedNeuron() for x in range(self.num_in)]
                self.layers.append(layer)
            elif l == self.num_layers - 1:
                layer = [Neuron() for x in range(self.num_out)]
                self.layers.append(layer)
            else:
                layer = [Neuron() for x in range(self.num_hidden[l - 1])]
                self.layers.append(layer)

        # setup restricted neurons in bias array
        for l in range(self.num_layers - 1):
            self.bias.append(RestrictedNeuron())

        # setup connections
        for l in range(self.num_layers - 1, 0,
                       -1):  # start at final index / output layer
            k_layer = self.layers[l]
            n_layer = self.layers[l - 1]
            n_bias_neuron = RestrictedNeuron()
            for k_neuron in k_layer:
                for n_neuron in n_layer:
                    k_neuron.add_inbound_connection(n_neuron)

                k_neuron.add_inbound_connection(n_bias_neuron)
Ejemplo n.º 25
0
 def __init__(self, net_config):
     for i in range(len(net_config)):
         self.net.append([])
         if i == 0: funtion_title = 'linear'
         else: funtion_title = 'threshold'
         neuron = Neuron(i, funtion_title)
         for j in range(net_config[i]):
             self.net[i].append(neuron)
     for i in range(len(net_config) - 1):
         self.weight.append([])
         for j in range(len(self.net[i])):
             self.weight[i].append([])
             for k in range(len(self.net[i + 1])):
                 self.weight[i][j].append(uniform(.1, .4))
Ejemplo n.º 26
0
    def __init__(self, neuron_setup, x_dim=5, y_dim=5):
        """
        Constructor of the base layer. A base layer is a flat 2D layer with
        neurons of a given model (parameters) and built-in spike detector.

        :param neuron_setup:    NeuronSetup object with settings for the
                                neurons for the layer
        :param x_dim:           number of neurons in X-dimension
        :param y_dim:           number of neurons in Y-dimension
        """
        self._x_dim = x_dim
        self._y_dim = y_dim

        self._neurons = [Neuron(neuron_setup) for i in range(x_dim * y_dim)]
Ejemplo n.º 27
0
    def __init__(self, n_input, n_output):
        """
            Creates a new sigmoid network

            parameters:
                n_input - int, number of input variables
                n_output - int, number of output variables (classes)
        """

        self._n_output = n_output
        self._n_input = n_input

        #initializes the neurons for the output classes
        self._neurons = [Neuron(n_input) for _ in range(n_output)]
Ejemplo n.º 28
0
 def addNeuron(self, spikeAmplitude, spikeAmplitude2, exciteTimeConstant,
               inhibTimeConstant, designation, delay1, delay2,
               wantBGCurrent):
     '''
     # Adds a single neuron to the network
     # It also automatically adds a section for itself in the adjacency matrix
     '''
     iD = len(self.listOfNeurons)
     neuron = Neuron(spikeAmplitude, spikeAmplitude2, exciteTimeConstant,
                     inhibTimeConstant, designation, iD, delay1, delay2,
                     wantBGCurrent, self.networkID)
     neuron.setTime(self.timeStep, self.duration, self.resolution)
     self.listOfNeurons.append(neuron)
     self.adjacencyMatrix.addNeuron()
Ejemplo n.º 29
0
 def __init__(self, size):
     preneurons = []
     defaultweight = 0.3
     #create neurons
     for i in range(size):
         tempneuron = Neuron('sensory' + str(i), 'inputneuron', actthreshold)
         preneurons.append(tempneuron)
     self.neurons = preneurons
     self.size = size
     #connect neurons
     for neuron1 in self.neurons:
         for neuron2 in self.neurons:
             if neuron1 is not neuron2:
                 neuron1.add_inconnects([neuron2, defaultweight])
                 neuron2.add_outconnects([neuron1, defaultweight])
Ejemplo n.º 30
0
    def simulate(self, process):
        # parallel processing on each setting value
        self.pid = os.getpid()
        self.progress_co = 0
        self.nr = Neuron(**self.parm[process + self.multiproc_co])
        self.nr.parm_dict = self.parm[process + self.multiproc_co]

        for i in range(0, self.nr.allsteps - 1):
            self.nr.propagation()
            if self.progress_co % 100000 == 0:
                logging.warning('process id : %d : %4d steps', self.pid,
                                self.progress_co)

            self.progress_co += 1
        return self.nr