Exemplo n.º 1
0
def main():
    # nn = NN(2, 1)
    # input = torch.DoubleTensor([[1,1,0,0],[1,0,1,0]])
    # print("input", input)
    # output = nn.forward(input)
    # print("output", output)

    And = AND()
    Or = OR()
    Not = NOT()
    Xor = XOR()
    
    print(And(True, True), And(True, False), And(False, True), And(False, False))
    print(Or(True, True), Or(True, False), Or(False, True), Or(False, False))
    print(Not(True), Not(False))
    print(Xor(True, True), Xor(True, False), Xor(False, True), Xor(False, False))
Exemplo n.º 2
0
def test():
    # ~~~~~~~~~~~~~~~~~Test Neural Network with batch input~~~~~~~~~~~~~~~~~~~#
    layers = [3, 4, 2]  # [in,h1,...,out]
    print('layers', layers)
    model = NeuralNetwork(layers)  # initialize neural network
    # input = torch.DoubleTensor([[1, 3, 1], [2, 2, 2]])	#2D DoubleTensor
    input = torch.DoubleTensor([1, 3, 1])  # 1D DoubleTensor
    if (input.numpy()).ndim is 1:
        pass
    else:
        input = torch.transpose(input, 0, 1)
    print('input', input)
    print('output', model.forward(input))
    # ~~~~~~~~~~~~~~~~~Test getLayer method~~~~~~~~~~~~~~~~~~~#
    th0 = model.getLayer(0)
    print('th0=', th0)
    # th0[0]=-20; 	th0[1] =15;		#th0[2] =15		#modify theta
    # print('th0m=', th0)

    # ~~~~~~~~~~~~~~~~~Test Logic Gates~~~~~~~~~~~~~~~~~~~#
    And = AND()
    print(And(False, False))
    print(And(False, True))
    print(And(True, False))
    print(And(True, True))
    Or = OR()
    print(Or(False, False))
    print(Or(False, True))
    print(Or(True, False))
    print(Or(True, True))
    Not = NOT()
    print(Not(False))
    print(Not(True))
    Xor = XOR()
    print(Xor(False, False))
    print(Xor(False, True))
    print(Xor(True, False))
    print(Xor(True, True))
Exemplo n.º 3
0
# for i in range(100):
#     xor_gate = XOR()
#     xor_gate.train()
#     mse = xor_gate._mse()
#     if mse >= .01:
#         wrong += 1
#     else:
#         correct += 1

# print(wrong)
# print(correct)
# print(correct / (wrong + correct))

# exit()

and_gate = AND()
and_gate.train()

or_gate = OR()
or_gate.train()

not_gate = NOT()
not_gate.train()

xor_gate = XOR()
xor_gate.train()

print("")

print("AND GATE")
print(and_gate(False, False))
from logic_gates import AND
from logic_gates import OR
from logic_gates import NOT
from logic_gates import XOR

print("------- AND -------")
And = AND()
print(And(True, True))
print(And(True, False))
print(And(False, True))
print(And(False, False))

print("------- OR -------")
Or = OR()
print(Or(True, True))
print(Or(True, False))
print(Or(False, True))
print(Or(False, False))

print("------- NOT -------")
Not = NOT()
print(Not(True))
print(Not(False))

print("------- XOR -------")
Xor = XOR()
print(Xor(True, True))
print(Xor(True, False))
print(Xor(False, True))
print(Xor(False, False))
Exemplo n.º 5
0
    print 'Test Input:', test_inp
    print 'Output:', model.forward(test_inp)

    print 'PART A Completed'
    print '---------------------------------------------------------------------'

    print '\nPART B: \t Neural Networks - Logic Gates'
    # Test Inputs
    test_input_1 = [[False, False], [False, True], [True, False], [True, True]]

    test_input_2 = [False, True]

    # AND Gate -----------------------------------------------------------------
    print '\nInitializing Object for AND Gate ...'
    ANDCurr = AND()

    print 'Testing Output ...'

    for test_inp in test_input_1:
        print 'Input 0: ', test_inp[0],'  \tInput 1: ', test_inp[1], '  \tOutput: ', \
              ANDCurr(test_inp[0],test_inp[1])

    # OR Gate -----------------------------------------------------------------
    print '\nInitializing Object for OR Gate ...'
    ORCurr = OR()

    print 'Testing Output ...'

    for test_inp in test_input_1:
        print 'Input 0: ', test_inp[0],'  \tInput 1: ', test_inp[1], '  \tOutput: ', \
Exemplo n.º 6
0
import numpy as np
import torch
from logic_gates import AND,OR,NOT,XOR

# Testing
a=AND()

c=OR()

b=XOR()

e=NOT()

i=[[True,False],[True,True]]
i1=[True,True]
i2=[False,False]
i3=[False,True]
print("The input sequence for testing is:")
print("1.",i)
print("2.",i1)
print("3.",i2)
print("4.",i3)
print("Training of AND gate")
a.train()
print("Testing of AND gate")
print(a.forward(i))
print(a.forward(i1))
print(a.forward(i2))
print(a.forward(i3))
print("Training of XOR gate")
b.train()
Exemplo n.º 7
0
# coding: utf-8

# In[1]:

from neural_network import NeuralNetwork
from nn2 import NeuralNetwork2
import matplotlib.pyplot as plt
import torch
from logic_gates import AND
from logic_gates import OR
from logic_gates import NOT
from logic_gates import XOR

# In[2]:

And = AND()
#print(And.nn.theta)
print(And(True, True))
print(And(True, False))
print(And(False, True))
print(And(False, False))

# In[3]:

Or = OR()
#print(Or.nn.theta)
print(Or(True, True))
print(Or(True, False))
print(Or(False, True))
print(Or(False, False))
Exemplo n.º 8
0
#from __future__ import print_function as print
from neural_network import NeuralNetwork as NN
import torch
from logic_gates import AND, OR, NOT, XOR

if __name__ == "__main__":

    nn = NN([2, 1])
    and_gate = AND()
    or_gate = OR()
    not_gate = NOT()
    xor_gate = XOR()

    print(and_gate(False, False))
    print(and_gate(False, True))
    print(and_gate(True, False))
    print(and_gate(True, True))
    print(" ")

    print(or_gate(False, False))
    print(or_gate(False, True))
    print(or_gate(True, False))
    print(or_gate(True, True))
    print("")

    print(not_gate(True))
    print(not_gate(False))
    print("")

    print(xor_gate(False, False))
    print(xor_gate(False, True))
Exemplo n.º 9
0
from neural_network import NeuralNetwork
from logic_gates import AND
from logic_gates import OR
from logic_gates import NOT
from logic_gates import XOR

print "Testing AND gate implementation.\nAND Truth Table:"
_and = AND()
print "INPUT_1\tINPUT_2\tAND_RESULT"
print "True\tTrue\t" + str(_and(True, True))
print "True\tFalse\t" + str(_and(True, False))
print "False\tTrue\t" + str(_and(False, True))
print "False\tFalse\t" + str(_and(False, False))

print "\nTesting OR gate implementation.\nOR Truth Table:"
_or = OR()
print "INPUT_1\tINPUT_2\tOR_RESULT"
print "True\tTrue\t" + str(_or(True, True))
print "True\tFalse\t" + str(_or(True, False))
print "False\tTrue\t" + str(_or(False, True))
print "False\tFalse\t" + str(_or(False, False))

print "\nTesting NOT gate implementation.\nNOT Truth Table:"
_not = NOT()
print "INPUT_1\tNOT_RESULT"
print "True\t" + str(_not(True))
print "False\t" + str(_not(False))

print "\nTesting XOR gate implementation.\nXOR Truth Table:"
_xor = XOR()
print "INPUT_1\tINPUT_2\tXOR_RESULT"
Exemplo n.º 10
0
from logic_gates import AND
from logic_gates import OR
from logic_gates import NOT
from logic_gates import XOR

# Initialize 4 types of Gates
And = AND()
And.train()
Or = OR()
Or.train()
Not = NOT()
Not.train()
Xor = XOR()
Xor.train()

' Test cases for 4 Gates for 1D Input'

# Test cases for AND
print(
    "\nDemonstrating AND Gate functionality using FeedForward Neural Network")
print("And(False, False) = %r" % And.forward(False, False))
print("And(False, True) = %r" % And.forward(False, True))
print("And(True, False) = %r" % And.forward(True, False))
print("And(True, True) = %r" % And.forward(True, True))

# Test cases for OR
print("\nDemonstrating OR Gate functionality using FeedForward Neural Network")
print("Or(False, False) = %r" % Or.forward(False, False))
print("Or(False, True) = %r" % Or.forward(False, True))
print("Or(True, False) = %r" % Or.forward(True, False))
print("Or(True, True) = %r" % Or.forward(True, True))