Advertisement
Guest User

Artificial Neuron Python

a guest
Jan 23rd, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | None | 0 0
  1. # !/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Artificial Neuron
  4. # source: www.solodatascience.com
  5.  
  6. __author__ = "Gonzalo Chacaltana Buleje"
  7. __email__ = "gchacaltanab@outlook.com"
  8.  
  9. import sys
  10. import math
  11.  
  12.  
  13. class ArtificialNeuron(object):
  14.     def __init__(self, x, w, u, u_w, transfer_code):
  15.         self.x = x
  16.         self.w = w
  17.         self.u = u
  18.         self.u_w = u_w
  19.         self.transfer_code = transfer_code
  20.         self.ni = 0
  21.         self.y = 0
  22.  
  23.     def execute(self):
  24.         self.execute_sum_xw()
  25.         self.execute_activation()
  26.  
  27.     def execute_sum_xw(self):
  28.         self.ni = 0
  29.         for i in range(len(self.x)):
  30.             self.ni += self.x[i]*self.w[i]
  31.         self.ni += self.u*self.u_w
  32.         self.ni = round(self.ni, 2)
  33.         print("ni : ", self.ni)
  34.  
  35.     def execute_activation(self):
  36.         self.activation_sigmoidal()
  37.         self.activation_escalon_unitario()
  38.  
  39.     def activation_sigmoidal(self):
  40.         if self.transfer_code == 1:
  41.             self.y = round(1/(1+math.pow(math.e, self.ni*-1)), 2)
  42.  
  43.     def activation_escalon_unitario(self):
  44.         if self.transfer_code == 2:
  45.             self.y = round(1 if self.ni > 0 else 0, 2)
  46.  
  47.     def showResult(self):
  48.         print("yi = f(x,w): %s" % self.y)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement