Editing
User:Elgreengeeto/Python Linear Perceptron
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
<pre> #a dot product is the sum of the products of aligned-elements from two #same-lengthed arrays def dot_product(a, b): return sum(x*y for x,y in zip(a,b)) class Perceptron: #percieve based on a list of inputs def percieve(self, inputs): #if there is not a list of weights, make one of = length to list of inputs if len(self.weights) < len(inputs): for ip in inputs: self.weights.append(self.defualt_weight) #get the dot product of those inputs with the bias and the input's weights with the bias's weight sum = dot_product(inputs + [self.bias], self.weights + [self.bias_weight]) return sum #learn method compares output of percieve() to the expected output and adjusts weights by plus or minus #the learn rate which is incrementally decreased by a factor of 0.99 def learn(self, inputs, expected): if self.learn_rate < 0.001: return self.learn_rate = self.learn_rate * 0.999 print "learn rate is %s" % (self.learn_rate) train_step = (expected - self.percieve(inputs)) * self.learn_rate self.bias_weight += self.bias_weight * train_step print "bias weight is: %s" % (self.bias_weight) for i in xrange(len(inputs)): self.weights[i] += inputs[i] * train_step print "input %s weight is: %s" % (i, self.weights[i]) #this defines how a Perceptron object represents itself in the interpreter def __str__(self): return "Weights: %s. Threshhold: %s. Learn rate: %s." % (self.weights, self.threshhold, self.learn_rate) #same as above __repr__ = __str__ #defines initial values for a new Perceptron object def __init__(self): self.bias = 1.0 self.bias_weight = 0.0 self.defualt_weight = 0.0 self.weights = [] self.learn_rate = 1 #trains a perceptron according to data def train(data, perceptron): for case in data: perceptron.learn(case[0],case[1]) #define a dataset to try and train a perceptron data = [[[1.0],2.0],[[2.0],4.0],[[3.0],6.0]] #do the damned thing if __name__ == '__main__': subject = Perceptron() for i in range(100000): train(data, subject) print "final bias weight: %s, final input weights: %s" % (subject.bias_weight, subject.weights) </pre>
Summary:
Please note that all contributions to Noisebridge are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
Noisebridge:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Request account
Namespaces
User page
Discussion
English
Views
Read
Edit
View history
More
Search
Dig in!
Noisebridge
- Status: MOVED
- Donate
- ABOUT
- Accessibility
- Vision
- Blog
Manual
MANUAL
Visitors
Participation
Community Standards
Channels
Operations
Events
EVENTS
Guilds
GUILDS
- Meta
- Electronics
- Fabrication
- Games
- Music
- Library
- Neuro
- Philosophy
- Funding
- Art
- Crypto
- Documentation/Wiki
Wiki
Recent Changes
Random Page
Help
Categories
(Edit)
Tools
What links here
Related changes
User contributions
Logs
View user groups
Special pages
Page information