当前位置: 首页>>代码示例>>Python>>正文


Python Neuron.activate方法代码示例

本文整理汇总了Python中neuron.Neuron.activate方法的典型用法代码示例。如果您正苦于以下问题:Python Neuron.activate方法的具体用法?Python Neuron.activate怎么用?Python Neuron.activate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在neuron.Neuron的用法示例。


在下文中一共展示了Neuron.activate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from neuron import Neuron [as 别名]
# 或者: from neuron.Neuron import activate [as 别名]
def main(train, test, out):
  
  TRAIN_FILE = train
  TEST_FILE  = test
  OUT_FILE   = out
  
  img = Image.open(TRAIN_FILE) # read double moons image from .png file
  pixels = img.load()          # generate pixel map
  width = img.size[0]
  height = img.size[1]
  
  training_set = dict()
  
  for i in range(width):
    for j in range(height):
      if pixels[i,j] == BLUE:   # if pixel is blue
        training_set[i,j] = BOT # set value to bottom
      elif pixels[i,j] == RED:  # if pixel is red
        training_set[i,j] = TOP # set value to top
  
  
  # create neuron with 2 input nodes
  n = Neuron(2) # x-input, y-input
  print "Neuron created."
  # training
  print "Training..."
  counter = 0
  while True:
    errors = 0
    for p in training_set:
      errors += n.train_step(p, training_set[p])
      counter += 1
      print "====="
      
    if errors < n.get_margin() * len(training_set):
      break
  
  print "Length of training set: " + str(len(training_set))
  print "Iterations: " + str(counter)
  
  # test cases
  img = Image.open(TEST_FILE)
  pixels = img.load()
  width = img.size[0]
  height = img.size[1]
  
  for i in range(width):
    for j in range(height):
      if pixels[i,j] == BLACK:
        n.set_input(0, i)
        n.set_input(1, j)
        n.activate()
        ans = n.get_output()
        if ans == TOP:
          pixels[i,j] = RED
        elif ans == BOT:
          pixels[i,j] = BLUE
  
  img.save(OUT_FILE, "PNG")
开发者ID:hakillha,项目名称:dmc,代码行数:61,代码来源:dmc.py

示例2: __init__

# 需要导入模块: from neuron import Neuron [as 别名]
# 或者: from neuron.Neuron import activate [as 别名]
class Network:
    """
    Represents a neural network using sigmoidal activations.

    """

    def __init__(self, layerCounts, activation=sigmoid.Tanh):
        """Constructs a neural network with a set of layers.
        >>> n = Network([2,3,2])
        >>> len(n.neurons)
        3
        >>> len(n.neurons[0])
        2
        >>> len(n.neurons[1])
        3
        >>> len(n.neurons[2])
        2
        >>> len(n.connections)
        2
        >>> len(n.connections[0])
        9
        >>> len(n.connections[1])
        8
        """
        self.bias = Neuron(sigmoid.Constant)

        self.neurons = []
        self.connections = []

        for layer in range(len(layerCounts)):
            neuron_layer = []
            connection_layer = []
            for i in range(layerCounts[layer]):
                # Input neurons shouldn't activate their input.

                cur_neuron = None
                if layer is 0:
                    # input neurons do not activate.
                    cur_neuron = Neuron(sigmoid.Linear)
                else:
                    # hidden and output neurons use normal sigmoids
                    cur_neuron = Neuron(activation)

                    # for every neuron to the left
                    for anterior in self.neurons[layer-1]:

                        #create an anterior connection to CUR_NEURON
                        connection = Connection(anterior, cur_neuron)
                        connection_layer.append(connection)

                        # add this connection to the posterior connections
                        # of ANTERIOR.
                        anterior.posteriors.append(connection)
                        cur_neuron.anteriors.append(connection)

                    # do the same for the BIAS connection.
                    bias_connection = Connection(self.bias, cur_neuron)
                    connection_layer.append(bias_connection)
                    self.bias.posteriors.append(bias_connection)

                # add the current neuron.
                neuron_layer.append(cur_neuron)

            #if connections were made
            if layer != 0:
                self.connections.append(connection_layer)

            # append the neural layer.
            self.neurons.append(neuron_layer)

    def train(self, datapair, rate):
        """Trains the network with a certain learning rate on a datapair.
        Returns the net error across ouytput neurons.
        Assunmes input matches network size."""
        inp = datapair[0]
        desired = datapair[1]

        self.feedforward(inp)
        error = self.backpropagate(desired, rate)

        return error

    def feedforward(self, inputs):
        """ Passes the input data through
        the network and creates the output """

        assert len(inputs) == len(self.neurons[0]), \
            "Input vector does not match the network intut layer"
        for i in range(len(inputs)):
            self.neurons[0][i].feed(inputs[i])

        self.bias.activate()

        for layer in self.neurons:
            for neuron in layer:
                neuron.activate()

        return [x.output for x in self.neurons[-1]]

    def backpropagate(self, desired, learning_rate):
#.........这里部分代码省略.........
开发者ID:mlberkeley,项目名称:NNWorkshop,代码行数:103,代码来源:network.py


注:本文中的neuron.Neuron.activate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。