Tuesday 8 October 2019

Python code for a simple neural network.

import numpy as np


###########  Single Layer Neuron with FF Learning (AND, OR, NAND)


class  basic_NN:
  def __init__(self,iv,ov,it=20):
    self.input_vector = np.array(iv)
    self.output_vector = np.array(ov)
    self.weight_vector = np.array([[2,3,-1]])  ### Any random weights
    self.output = np.array([])
    self.err_vector = np.zeros((1,3))
    self.eta = 0.3
    self.iterations = it
   
  def compute(self):
    mfv = np.c_[np.ones(4),self.input_vector]
    for i in range(0,self.iterations):
      self.result = mfv.dot(self.weight_vector.transpose())
      self.output = [0 if result < 0 else 1 for result in self.result]
      print("output after ",i," iterations : ",self.output)
      self.weight_vector = self.update_weight(mfv)

  def update_weight(self,wv):
    sum = 0
    for i in range(self.output_vector.size):
      sum = sum + (self.output[i]-self.output_vector[i])*wv[i]
    updated_weight = self.weight_vector - self.eta*sum
    return updated_weight

   
  def show_result(self):
    print("Input Vector : \n",self.input_vector)
    print("Output : \n",self.output)
   
  def get_output(self):
    return self.output
###########  End of basic_NN

######  As AND
 
n = basic_NN(np.array([[0,0],[0,1],[1,0],[1,1]]),np.array([0,0,0,1]))

n.compute()
n.show_result() 


######  As OR
  
n = basic_NN(np.array([[0,0],[0,1],[1,0],[1,1]]),np.array([0,1,1,1]))

n.compute()
n.show_result()  

######  As NAND
  
n = basic_NN(np.array([[0,0],[0,1],[1,0],[1,1]]),np.array([1,1,1,0]))

n.compute()
n.show_result()  


### xor NN 
### Using basic NN for or, nand and and
### a xor b = (a or b) and (a nand b)
### or and nand in the first layer, and in the second layer

class xor_NN:
  def __init__(self,iv,ov):
    self.input_vector = np.array(iv)
    self.output_vector = np.array(ov)
    self.output = np.array([])
    
  def compute(self):
    or_neuron = basic_NN(self.input_vector,np.array([0,1,1,1]))
    nand_neuron = basic_NN(self.input_vector,np.array([1,1,1,0]))
    or_neuron.compute()
    nand_neuron.compute()
    h1 = or_neuron.get_output()
    h2 = nand_neuron.get_output()
    h = np.concatenate((h1,h2))
    iv_l2= h.reshape(4,2)
    and_neuron = basic_NN(iv_l2,self.output_vector)
    and_neuron.compute()
    self.output = and_neuron.get_output()
    
  def show_result(self):
    print("Input Vector : \n",self.input_vector)
    print("Output : \n",self.output)
  
  def get_output(self):
    return self.output

n = xor_NN(np.array([[0,0],[0,1],[1,0],[1,1]]),np.array([0,1,1,0]))

n.compute()
n.show_result()