-
Notifications
You must be signed in to change notification settings - Fork 6
/
pcn.py
71 lines (48 loc) · 1.72 KB
/
pcn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python
# -*- coding: utf-8 -*-
from global_vars import *
import numpy as ny
class Perceptron:
""" Perceptron
Code based on chapter 4 of 'Machine Learning: An Algorithmic Perspective' by Stephen Marsland.
(http://seat.massey.ac.nz/personal/s.r.marsland/MLBook.html)
"""
def __init__( self, inputs, targets ):
""" Constructor. """
self.nodes_in = len( inputs[0] )
self.nodes_out = len( targets[0] )
self.data_amount = len( inputs )
self.weights = ny.random.rand( self.nodes_in + 1, self.nodes_out ) * 0.1 - 0.05
def train( self, inputs, targets, eta, iterations ):
""" Train the perceptron.
eta -- Learning rate.
iterations -- Number of iterations to train.
"""
# Add bias node
ones = -ny.ones( ( self.data_amount, 1 ) )
inputs = ny.concatenate( ( inputs, ones ), axis = 1 )
change = range( self.data_amount )
for n in range( iterations ):
outputs = self._forward( inputs )
trans_in = ny.transpose( inputs )
self.weights += eta * ny.dot( trans_in, targets - outputs )
ny.random.shuffle( change )
inputs = inputs[change,:]
targets = targets[change,:]
def _forward( self, inputs ):
""" Forward the network.
inputs -- Input data to run forward.
Returns the output of the activation function.
"""
outputs = ny.dot( inputs, self.weights )
# Activation function
return ny.where( outputs > 0, 1, 0 )
def use( self, inputs ):
""" Use the trained network. Which means running it forward.
inputs -- Input data to use the trained perceptron on.
Returns the classification by the perceptron. Which is either 1 or 0.
"""
outputs = ny.dot( inputs, self.weights )[0]
outputs /= max( outputs )
outputs = ny.where( outputs < 0.5, 0, 1 )
return outputs