NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import numpy as np

def sigmoid(x):
return 1 / (1 + np.exp(-x))

def sigmoid_derivative(x):
return x * (1 - x)

class LogicGate:
def __init__(self, gate_name, xdata, tdata):
self.name = gate_name
self._xdata = xdata
self._tdata = tdata.reshape(4, 1)

# Network architecture
self._W2 = np.random.rand(2, 6)
self._b2 = np.random.rand(6)
self._W3 = np.random.rand(6, 1)
self._b3 = np.random.rand(1)
self._learning_rate = 1e-2

def feed_forward(self):
z2 = np.dot(self._xdata, self._W2) + self._b2
a2 = sigmoid(z2)
z3 = np.dot(a2, self._W3) + self._b3
a3 = sigmoid(z3)
return a2, a3

def loss_val(self):
delta = 1e-7
_, a3 = self.feed_forward()
return -np.sum(self._tdata * np.log(a3 + delta) + (1 - self._tdata) * np.log(1 - a3 + delta))

def train(self):
for step in range(10001):
a2, a3 = self.feed_forward()
error_output = a3 - self._tdata
dW3 = np.dot(a2.T, error_output * sigmoid_derivative(a3))
db3 = np.sum(error_output * sigmoid_derivative(a3), axis=0)
error_hidden = np.dot(error_output * sigmoid_derivative(a3), self._W3.T)
dW2 = np.dot(self._xdata.T, error_hidden * sigmoid_derivative(a2))
db2 = np.sum(error_hidden * sigmoid_derivative(a2), axis=0)
self._W2 -= self._learning_rate * dW2
self._b2 -= self._learning_rate * db2
self._W3 -= self._learning_rate * dW3
self._b3 -= self._learning_rate * db3

if step % 400 == 0:
print(f"step = {step}, loss value = {self.loss_val()}")

def predict(self, input_data):
z2 = np.dot(input_data, self._W2) + self._b2
a2 = sigmoid(z2)
z3 = np.dot(a2, self._W3) + self._b3
a3 = sigmoid(z3)
result = 1 if a3 > 0.5 else 0
return a3, result
xdata = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
tdata = np.array([0, 0, 0, 1])
AND_obj = LogicGate("AND_GATE", xdata, tdata)
AND_obj.train()
print(AND_obj.name, "Output")
test_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
for data in test_data:
prediction = AND_obj.predict(data)
print(f"Input: {data}, Prediction: {prediction}")
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.