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))
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))
# 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)) print(and_gate(False, True)) print(and_gate(True, False)) print(and_gate(True, True)) print("AND GATE WEIGHTS") print(and_gate.model.theta) print("") print("OR GATE") print(or_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))
def test(): #~~~~~~~~~~~~~~~~~Test Neural Network with batch input~~~~~~~~~~~~~~~~~~~# layers = [2, 2, 2] #[in,h1,...,out] #print('layers',layers) model = NeuralNetwork(layers) #initialize neural network ## single input #input = torch.FloatTensor([0.05, 0.1]); target= torch.FloatTensor([0.01, 0.99]) ## bacth input input = torch.FloatTensor([[0.05, 0.1], [0.05, 0.1]]) target = torch.FloatTensor([[0.01, 0.99], [0.01, 0.99]]) if (input.numpy()).ndim is 1: pass else: input = torch.transpose(input, 0, 1) target = torch.transpose(target, 0, 1) #print('input',input) #print('target', target) th0 = model.getLayer(0) #print('th0=', th0) th0[0][0] = 0.35 th0[1][0] = 0.15 th0[2][0] = 0.2 #modify theta th0[0][1] = 0.35 th0[1][1] = 0.25 th0[2][1] = 0.3 # modify theta #print('th0m=', th0) th1 = model.getLayer(1) #print('th1=', th1) th1[0][0] = 0.6 th1[1][0] = 0.4 th1[2][0] = 0.45 # modify theta th1[0][1] = 0.6 th1[1][1] = 0.5 th1[2][1] = 0.55 # modify theta #print('th1m=', th1) print('output', model.forward(input)) #print('Total Err', model.backward(target)) #model.updateParams(0.5) # ~~~~~~~~~~~~~~~~~Train Network~~~~~~~~~~~~~~~~~~~# for i in range(100): print('output', model.forward(input)) print('Total Err', model.backward(target)) model.updateParams(0.5) # ~~~~~~~~~~~~~~~~~Test Logic Gates~~~~~~~~~~~~~~~~~~~# #And=AND() ##print(And(False, False)); print (And(False, True)); print (And(True, False)); print (And(True, True)) #for i in range(500): # And.train() #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)) #for i in range(500): # Or.train() #print(Or(False, False)); print(Or(False, True)); print(Or(True, False)); print(Or(True, True)) #Not = NOT() ##print(Not(False)); print(Not(True)) #for i in range(500): # Not.train() #print(Not(False)); print(Not(True)) Xor = XOR() ##print(Xor(False, False)); print(Xor(False, True)); print(Xor(True, False)); print(Xor(True, True)) for i in range(50000): Xor.train() print(Xor(False, False)) print(Xor(False, True)) print(Xor(True, False)) print(Xor(True, True))
# print("this is ThetaDic: ",ThetaDic) # x = torch.Tensor() # import torch # from neural_network import NeuralNetwork # structureList = [2,4,2] # model = NeuralNetwork(structureList) # input_array = torch.DoubleTensor([[3,4,4], [1,2,3]]) # output = model.forward(input_array) from logic_gates import AND from logic_gates import NOT from logic_gates import OR from logic_gates import XOR And = AND() xor = XOR() Not = NOT() Or = OR() print(And(False, False)) print(xor(False, True)) print(Or(False, True)) print(Not(True))
"Or(True,True)", Or(True, True), "\n", ) Not = NOT() print( "Not(False)", Not(False), "\n", "Not(True)", Not(True), "\n", ) XOr = XOR() print( "XOr(False,False)", XOr(False, False), "\n", "XOr(False,True)", XOr(False, True), "\n", "XOr(True,False)", XOr(True, False), "\n", "XOr(True,True)", XOr(True, True), "\n", )
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()
#print(Or.nn.theta) print(Or(True, True)) print(Or(True, False)) print(Or(False, True)) print(Or(False, False)) # In[4]: Not = NOT() #print(Not.nn.theta) print(Not(False)) print(Not(True)) # In[5]: Xor = XOR() #print(Xor.nn.theta) print(Xor(True, True)) print(Xor(True, False)) print(Xor(False, True)) print(Xor(False, False)) # In[6]: nn = NeuralNetwork(2, 2, 1) #nn2 = NeuralNetwork(2,3,1) # In[7]: print(nn.Theta) #print(nn2.Theta)
#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))
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))