本文整理汇总了Python中neuron.Neuron类的典型用法代码示例。如果您正苦于以下问题:Python Neuron类的具体用法?Python Neuron怎么用?Python Neuron使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Neuron类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_update_weights
def test_update_weights(self):
neuron = Neuron(0, 0, [0.05, 0.05], [0.519053, 1])
neuron.delta_val = -0.1295578
neuron.update_weights(0.001)
self.assertEquals(0.0499327526, round(neuron.weights[0], 10))
self.assertEquals(0.0498704, round(neuron.weights[1], 7))
示例2: test_set_error_output_layer
def test_set_error_output_layer(self):
neuron = Neuron(0, 0, [0.05, 0.05], [1, 1])
neuron.output = 0.518979
neuron.is_output_layer = True
neuron.set_output_layer_error(0)
self.assertEquals(-0.12955, round_to(neuron.delta_val, 5))
示例3: test_step_true
def test_step_true(self):
neuron = Neuron(
weights=[1, 2, 3],
transfer_function=StepTransferFunction,
)
self.assertEqual(neuron.run([1, 2, 3]), 1)
示例4: Perceptron
class Perceptron(object):
def __init__(self, input_size, lrn_rate=1):
"""'input_size' is the length of the input.
'lrn_rate' is the learning rate.
"""
self.neuron = Neuron([0]*input_size, 0, signal)
self.lrn_rate = lrn_rate
self.fire = self.neuron.fire
def training(self, examples):
epochs = 0
while True:
epochs = epochs + 1
error_count = 0
for (input_vector, desired_output) in examples:
actual_output = self.neuron.fire(input_vector)
error = desired_output - actual_output
if error != 0:
learned = self.lrn_rate*error
self.neuron.update(input_vector, learned)
error_count = error_count + 1
if error_count == 0:
break
return epochs
def __str__(self):
ret = 'lrn_rate: %s' % self.lrn_rate
ret = '%s\n%s' % (ret, self.neuron.__str__())
return ret
示例5: testSinglePreviousEvaluate
def testSinglePreviousEvaluate(self):
previousNeuron = InputNeuron()
previousNeuron.setValue(1)
previousRow = [previousNeuron]
neuron = Neuron(previousRow)
self.assertGreater(neuron.evaluate(), 1/2)
示例6: test_step_false
def test_step_false(self):
neuron = Neuron(
weights=[1, 2, 3],
transfer_function=StepTransferFunction,
function=lambda p: p >= 7, # any function can be used here
)
self.assertEqual(neuron.run([1, 1, 1]), 0)
示例7: test_integrator
def test_integrator(self):
neuron = Neuron(
weights=[1, 2, 3],
transfer_function=BaseTransferFunction, # does nothing
)
self.assertEqual(neuron.integrator([1, 1, 1]), 6)
示例8: add_layer
def add_layer(self, neurons, layer_number):
"""
:param neurons:
:param layer_number:
:return:
"""
bias_value = random.randint(1,10)
bias = Neuron(activation_func=lambda x: 0, activation_prime=lambda x: 0, isBias=True)
bias.y_output = bias_value
neurons.append(bias)
self.layers[layer_number] = neurons
if layer_number == 0:
return
if layer_number > self.max_layer:
self.max_layer = layer_number
for input in self.layers[layer_number - 1]:
for output in neurons:
output.add_input_reference(input)
if not output.isBias:
weight = self.randomize_weight()
input.add_output_connection(output, weight)
示例9: test_sigmoid
def test_sigmoid(self):
neuron = Neuron(
weights=[1, 2, 3],
transfer_function=SigmoidTransferFunction,
)
v = neuron.run([0, 0, 0])
self.assertEqual(v, 0.5)
示例10: loadmat
def loadmat(self):
"""Load neurons from a single .mat file"""
self.header = MATHeader()
nrecs = self.header.read(self.path)
for nrec in nrecs:
neuron = Neuron(self.path, sort=self)
neuron.loadmat(nrec)
self.alln[neuron.id] = neuron # save it
示例11: load
def load(path):
"""
Loads a neural network from a json file
@param (String) path - The path to load the neural network from
@returns (Network) - The neural network that was loaded
"""
network = Network()
try:
with open(path, "r+") as f:
network_data = "\n".join(f.readlines())
network_json = json.loads(network_data)
layers = network_json["layers"]
# For every layer in the network ...
for layer in layers:
neurons = []
# For every neuron in the layer ...
for neuron in layer["neurons"]:
weights = neuron["weights"]
bias = neuron["bias"]
activation = neuron["activation"]
# Choose the proper activation function and corresponding derivative
activation_func = None
derivative_func = None
if activation == Network.LINEAR:
activation_func = Network.ACTIVATION_LINEAR
derivative_func = Network.DERIVATIVE_LINEAR
elif activation == Network.SIGMOID:
activation_func = Network.ACTIVATION_SIGMOID
derivative_func = Network.DERIVATIVE_SIGMOID
elif activation == Network.TANH:
activation_func = Network.ACTIVATION_TANH
derivative_func = Network.DERIVATIVE_TANH
elif activation == Network.STEP:
activation_func = Network.ACTIVATION_STEP
derivative_func = Network.DERIVATIVE_STEP
# Create a neuron with the desired info
neuron = Neuron(0, activation_func, derivative_func)
neuron.weights = weights
neuron.bias = bias
# Add the processed neuron to the collection
neurons.append(neuron)
# Create a layer with the desired neurons
layer = Layer(0, 0, None, None)
layer.neurons = neurons
# Add the processed layer to the collection
network.layers.append(layer)
except:
raise Exception("Invalid Neural Network File @ {}!".format(path))
return network
示例12: loadptcs
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
示例13: init
def init(self, neurons_num=3, inputs=3, activation_function="sigmoid"):
self.inputs_num = inputs
self.neurons_num = neurons_num
self.activation_function = activation_function
self.neurons = []
for i in range(self.neurons_num):
neuron = Neuron()
neuron.init(self.inputs_num, self.activation_function)
self.neurons.append(neuron)
示例14: test_1
def test_1(steps):
weights = 3
print "Linear combination of weights {0}, {1} steps".format(weights, steps)
neuron = Neuron(weights, sigm, sigmp, error)
errors = []
for i in range(steps):
inputs = [random.random() for r in range(weights)]
target = 2*inputs[0] + 0.3*inputs[1] - 0.7*inputs[2]
neuron.learn_1(inputs, target)
errors.append(neuron.last_error)
print report(errors)
示例15: test_feed_forward
def test_feed_forward(self):
neuron = Neuron(0, 0, [0.05, 0.05], [1, 1])
next_node1 = Neuron(0, 0, [0.05, 0.05], [0, 0])
next_node2 = Neuron(0, 0, [0.05, 0.05], [0, 0])
nodes = [next_node1, next_node2]
neuron.feed_forward(nodes)
self.assertEquals(0.524, round_to(nodes[0].inputs[0], 3))
self.assertEquals(0, round_to(nodes[0].inputs[1], 3))
self.assertEquals(0.524, round_to(nodes[1].inputs[0], 3))
self.assertEquals(0, round_to(nodes[1].inputs[1], 3))