<jupyter_start><jupyter_text>Perceptron ModelPerceptron model has an accumulator<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import tqdm
# Transfer/Activation Functions
def step(x):
  return x >= 0
class Perceptron:
  def __init__(self,features=1, alpha=0.1, bias=np.random.normal(loc=0.0,scale=0.01), weights=None, activation=step):
    if weights is None:
      self.weights=np.random.normal(loc=0.0, scale=0.01, size=features).transpose()
    else: self.weights = weights 
    self.activation = activation
    self.alpha = alpha
    self.bias=bias
    self.features = features

  def __predict(self, inputn): # Private Function
    # implement prediction of 1 instance
    if self.features is 1:
      return self.activation(self.weights*inputn + self.bias)[0]
    else:
     return self.activation(self.weights.dot(inputn.transpose()) + self.bias)
    
      
      
  def cost(self, inputn, outputn ):
    # Implement the cost function
    return self.__predict(inputn) - outputn
  

  def predict(self, inputs):
      if self.features is 1:
        return self.activation(self.weights*(inputs) + self.bias)
      else:
        return self.activation(self.weights.dot(inputs.transpose()) + self.bias)
      
        
  

  def train(self, inputs, outputs, epochs=1):   
    t = tqdm.tqdm(range(epochs))
    for epoch in t:
      for i, inputn in enumerate(inputs):
        delta = self.__predict(inputn) - outputs[i]
        t.set_postfix({"loss": delta})
        #print(delta, inputn, outputs[i], self.weights, self.bias)
        self.weight_update(delta, inputn)

  
  def weight_update(self, delta, inputs):
      self.weights = self.weights - self.alpha*delta*(inputs)
      self.bias = self.bias - self.alpha*delta
      #print(self.weights, self.bias)<jupyter_output><empty_output><jupyter_text>And Gate<jupyter_code>perceptron=Perceptron(features=2) # Single Perceptron
# Input output for AND Gate
X = np.array([[0,0],
              [0,1],
              [1,0],
              [1,1]
              ])
Y = np.array([0,
              0,
              0,
              1
              ])<jupyter_output><empty_output><jupyter_text>Before Training<jupyter_code>x=np.linspace(-0.5, 1.5, 200)

xx, yy = np.meshgrid(x, x)
Z = perceptron.predict(np.array([xx.ravel(),yy.ravel()]).T)

plt.contourf(xx, yy, Z.reshape(xx.shape), levels=1)
plt.scatter(X[:,0],X[:,1])
plt.xlim([-0.5,1.5])
plt.ylim([-0.5,1.5])
plt.colorbar();
perceptron.predict(X)<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>perceptron.train(X,Y,epochs=20)<jupyter_output>100%|██████████| 20/20 [00:00<00:00, 273.45it/s, loss=0]<jupyter_text>After Training<jupyter_code>x=np.linspace(-0.5, 1.5, 200)

xx, yy = np.meshgrid(x, x)
Z = perceptron.predict(np.array([xx.ravel(),yy.ravel()]).T)

plt.contourf(xx, yy, Z.reshape(xx.shape), levels=1)
plt.scatter(X[:,0],X[:,1])
plt.xlim([-0.5,1.5])
plt.ylim([-0.5,1.5])
plt.colorbar();
perceptron.predict(X)<jupyter_output><empty_output><jupyter_text>Or Gate<jupyter_code># Input output for AND Gate
X = np.array([[0,0],
              [0,1],
              [1,0],
              [1,1]
              ])
Y = np.array([1,
              1,
              1,
              0
              ])
perceptron=Perceptron(features=2)<jupyter_output><empty_output><jupyter_text>Before Training<jupyter_code>x=np.linspace(-0.5, 1.5, 200)

xx, yy = np.meshgrid(x, x)
Z = perceptron.predict(np.array([xx.ravel(),yy.ravel()]).T)

plt.contourf(xx, yy, Z.reshape(xx.shape), levels=1)
plt.scatter(X[:,0],X[:,1])
plt.xlim([-0.5,1.5])
plt.ylim([-0.5,1.5])
plt.colorbar();
perceptron.predict(X)<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>perceptron.train(X,Y,epochs=20)<jupyter_output>100%|██████████| 20/20 [00:00<00:00, 316.23it/s, loss=0]<jupyter_text>After Training<jupyter_code>x=np.linspace(-0.5, 1.5, 200)

xx, yy = np.meshgrid(x, x)
Z = perceptron.predict(np.array([xx.ravel(),yy.ravel()]).T)

plt.contourf(xx, yy, Z.reshape(xx.shape), levels=1)
plt.scatter(X[:,0],X[:,1])
plt.xlim([-0.5,1.5])
plt.ylim([-0.5,1.5])
plt.colorbar();
perceptron.predict(X)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17510293049164447, 'fraction_numerical': 0.03947687091305401, 'mean_word_length': 3.5787139689578713, 'pattern_counts': {'":': 1, '<': 37, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '175426', 'license': 'MIT', 'n_tokens_mistral': 1738, 'n_tokens_neox': 1633, 'n_words': 249, 'path': 'Assignments/002_Gates_Using_Perceptron_Model.ipynb', 'repo_name': 'vsemwal/Deep-Learning-2021', 'size': '4129'}