Code Monkey home page Code Monkey logo

perceptron_package's Introduction

perceptron_package

Perceptron Implementation using OR,AND,XOR and NAND gates.

Info about perceptron(Single Layer Neural Network)

Visit analyticsindiamag.com!

Dataset

AND

x1 x2 y
0 0 0
0 1 0
1 0 0
1 1 1

0R

x1 x2 y
0 0 0
0 1 1
1 0 1
1 1 1

Python code for perceptron class

class Perceptron:
  def __init__(self,eta,epochs):
    np.random.seed(42)
    self.weights = np.random.randn(3) * 10**-4
    logging.info(f"intial weights brfore training: {self.weights}")
    self.eta = eta
    self.epochs = epochs    #self declares variable as global for all the methods

  def activationFunction(self,inputs,weights): 
    z = np.dot(inputs,weights)
    return np.where(z>0, 1,0)
  
  def fit(self, X, y):
    self.X = X 
    self.y = y

    X_with_bias = np.c_[self.X, -np.ones((len(self.X), 1))] #concatenation of X and bias 
    logging.info(f"X with bias: {X_with_bias}")

    for epoch in tqdm(range(self.epochs),total=self.epochs, desc="training the model"):
      logging.info("--"*10)
      logging.info(f"for epoch: {epoch}")
      logging.info("--"*10)

      y_hat = self.activationFunction(X_with_bias, self.weights) #forward propogation
      logging.info(f"prdicted value after forward pass: \n{y_hat}")
      self.error = self.y - y_hat
      logging.info(f"error : \n{self.error}")
      self.weights = self.weights + self.eta * np.dot(X_with_bias.T, self.error) #backward propogation
      logging.info(f"updated weights after epoch : \n{epoch}/{self.epochs} : \n{self.weights}")
      logging.info("######"*10)

  def predict(self, X):
    X_with_bias = np.c_[X, -np.ones((len(X), 1))]
    return self.activationFunction(X_with_bias, self.weights)

  def total_loss(self):
    total_loss = np.sum(self.error)
    logging.info(f"total loss : {total_loss}")
    return total_loss

PLOTS:-

  • AND plot: and plot

  • OR plot:

    or plot

Commands used-

conda env list
mkdir utils
touch utils/__init__.py #to treat utility as a package
touch utils/model.py
touch utils/all_utils.py (keep all helper functions in all_utils folder)
from utils.model import class
touch requirements.txt
conda create -n sagar python==3.8 -y
conda activate sagar
pip freeze > requirements.txt
pip install -r requirements.txt
git add . && git commit -m "docstring updated" && git push origin main
git checkout "committed id no" (to go to specific version)

#to see utils as a pckage, just type python in the terminal
import utils
utils.__version__ #check utils package version
cp Sample\ Notebooks/demo.ipynb .

perceptron_package's People

Contributors

sagar-modelling avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.