First page Back Continue Last page Overview Graphics

Neural Network with Bias

import numpy as np

from collections import Counter

class Perceptron:

def __init__(self, input_length, weights=None):

if weights==None:

# input_length + 1 because bias needs a weight as well

self.weights = np.random.random((input_length + 1)) * 2 - 1

self.learning_rate = 0.05

self.bias = 1

@staticmethod

def sigmoid_function(x):

res = 1 / (1 + np.power(np.e, -x))

return 0 if res < 0.5 else 1

def __call__(self, in_data):

weighted_input = self.weights[:-1] * in_data

weighted_sum = weighted_input.sum() + self.bias *self.weights[-1]

return Perceptron.sigmoid_function(weighted_sum)