Skip to content

Instantly share code, notes, and snippets.

@gurix
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gurix/1f67c04ef99bc759b3a4 to your computer and use it in GitHub Desktop.
Save gurix/1f67c04ef99bc759b3a4 to your computer and use it in GitHub Desktop.
Perceptron Learning with 3 inputs
1 x0: 1 x1: 0 x2: 1 desired: 1 current: 0 w0: -0.4 w1: 0.5 w2: -0.4 sum: -0.8
w0: -0.1 w2: -0.1
2 x0: 0 x1: 1 x2: 1 desired: 1 current: 1 w0: -0.1 w1: 0.5 w2: -0.1 sum: 0.4
3 x0: 1 x1: 1 x2: 1 desired: 1 current: 1 w0: -0.1 w1: 0.5 w2: -0.1 sum: 0.3
4 x0: 0 x1: 0 x2: 1 desired: 0 current: 0 w0: -0.1 w1: 0.5 w2: -0.1 sum: -0.1
5 x0: 1 x1: 0 x2: 1 desired: 1 current: 0 w0: -0.1 w1: 0.5 w2: -0.1 sum: -0.2
w0: 0.2 w2: -0.4
6 x0: 0 x1: 1 x2: 1 desired: 1 current: 1 w0: 0.2 w1: 0.5 w2: -0.4 sum: 0.1
7 x0: 1 x1: 1 x2: 1 desired: 1 current: 1 w0: 0.2 w1: 0.5 w2: -0.4 sum: 0.3
8 x0: 0 x1: 0 x2: 1 desired: 0 current: 0 w0: 0.2 w1: 0.5 w2: -0.4 sum: -0.4
9 x0: 1 x1: 0 x2: 1 desired: 1 current: 0 w0: 0.2 w1: 0.5 w2: -0.4 sum: -0.2
w0: 0.5 w2: -0.7
10 x0: 0 x1: 1 x2: 1 desired: 1 current: 0 w0: 0.5 w1: 0.5 w2: -0.7 sum: -0.2
w1: 0.8 w2: -1.0
11 x0: 1 x1: 1 x2: 1 desired: 1 current: 1 w0: 0.5 w1: 0.8 w2: -1.0 sum: 0.3
12 x0: 0 x1: 0 x2: 1 desired: 0 current: 0 w0: 0.5 w1: 0.8 w2: -1.0 sum: -1.0
13 x0: 1 x1: 0 x2: 1 desired: 1 current: 0 w0: 0.5 w1: 0.8 w2: -1.0 sum: -0.5
w0: 0.8 w2: -0.7
14 x0: 0 x1: 1 x2: 1 desired: 1 current: 1 w0: 0.8 w1: 0.8 w2: -0.7 sum: 0.1
15 x0: 1 x1: 1 x2: 1 desired: 1 current: 1 w0: 0.8 w1: 0.8 w2: -0.7 sum: 0.9
16 x0: 0 x1: 0 x2: 1 desired: 0 current: 0 w0: 0.8 w1: 0.8 w2: -0.7 sum: -0.7
17 x0: 1 x1: 0 x2: 1 desired: 1 current: 1 w0: 0.8 w1: 0.8 w2: -0.7 sum: 0.1
18 x0: 0 x1: 1 x2: 1 desired: 1 current: 1 w0: 0.8 w1: 0.8 w2: -0.7 sum: 0.1
19 x0: 1 x1: 1 x2: 1 desired: 1 current: 1 w0: 0.8 w1: 0.8 w2: -0.7 sum: 0.9
require 'colorize'
class Perceptron
def initialize(weights, learningrate, threshold)
@weights, @learningrate, @threshold = weights, learningrate, threshold
@step = 1
end
def learn(inputs, desired_output)
output = "#{@step}\t"
inputs.each_with_index do |value, index|
output += "x#{index}: #{value}\t"
end
output += "desired: #{desired_output}\t"
output += "current: #{current(inputs)}\t"
@weights.each_with_index do |value, index|
output += "w#{index}: #{value.round 4} \t"
end
output += "sum: #{sum(inputs).round 4}"
if correct?(inputs, desired_output)
puts output.green
else
puts output.red
adjust_weights(inputs, desired_output)
end
@step += 1
end
def adjust_weights(inputs, desired_output)
output = ""
(inputs.size + 5).times do
output += " \t"
end
inputs.each_with_index do |value, index|
if value == 1
adjustment = value * @learningrate
current(inputs) < desired_output ? @weights[index] += adjustment : @weights[index] -= adjustment
output += "w#{index}: #{@weights[index].round 4} \t"
else
output += "\t\t"
end
end
puts output
end
def sum(inputs)
sum = 0
inputs.each_with_index do |value, index|
sum += value * @weights[index]
end
sum
end
def current(inputs)
sum(inputs) >= @threshold ? 1 : 0
end
def correct?(inputs, desired_output)
current(inputs) == desired_output
end
end
neuron = Perceptron.new [-0.4, 0.5, -0.4], 0.3, 0
10.times do
neuron.learn [1, 0, 1], 1
neuron.learn [0, 1, 1], 1
neuron.learn [1, 1, 1], 1
neuron.learn [0, 0, 1], 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment