-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1st_MP_first_gates.py
More file actions
161 lines (132 loc) · 3.69 KB
/
1st_MP_first_gates.py
File metadata and controls
161 lines (132 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import numpy as np
import pandas as pd
'''
Author: Priyansh Soni
y=(W^T).X , (Weight)^Transpose.Inputs
1*n matrix dot with n*1
MP Model only uses integers as weights. After choosing weights we find threshold. Threshold is basically :-
After threshold (>=threshold) next node fires(1) and < threshold does not fire(0)
'''
w1=w2=1
mat_weight=np.matrix([[w1],[w2]])
mat_inputs_X=np.matrix([[0,0],[0,1],[1,0],[1,1]])
print(mat_inputs_X)
mat_weight_transpose=(mat_weight.getT())
dot_mat=(np.dot(mat_inputs_X,mat_weight))
dot_mat=list((dot_mat.getT()).flat)
#print(dot_mat)
##AND GATE
threshold=2
ans_mat=[]
for i in dot_mat:
if(i<threshold):
##Output 0 for AND Gate
ans_mat.append(0)
else:
ans_mat.append(1)
print("------------------------")
print("AND GATE")
print("------------------------")
print("Input_1\tInput_2\tOutput")
print("-------\t-------\t------")
for i in range(len(ans_mat)):
for j in (mat_inputs_X[i]):
temp=list(j.flat)
print(" "+str(temp[0])+"\t "+str(temp[1])+"\t "+str(ans_mat[i]))
##OR GATE
threshold=1
ans_mat=[]
for i in dot_mat:
if(i<threshold):
##Output 0 for AND Gate
ans_mat.append(0)
else:
ans_mat.append(1)
print("------------------------")
print("OR GATE")
print("------------------------")
print("Input_1\tInput_2\tOutput")
print("-------\t-------\t------")
for i in range(len(ans_mat)):
for j in (mat_inputs_X[i]):
temp=list(j.flat)
print(" "+str(temp[0])+"\t "+str(temp[1])+"\t "+str(ans_mat[i]))
###AND NOT GATE
##Updation of weights
w1=2;w2=-1
mat_weight=np.matrix([[w1],[w2]])
mat_weight_transpose=(mat_weight.getT())
dot_mat=(np.dot(mat_inputs_X,mat_weight))
dot_mat=list((dot_mat.getT()).flat)
threshold=2
ans_mat=[]
for i in dot_mat:
if(i<threshold):
##Output 0 for AND Gate
ans_mat.append(0)
else:
ans_mat.append(1)
print("------------------------")
print("AND NOT GATE")
print("------------------------")
print("Input_1\tInput_2\tOutput")
print("-------\t-------\t------")
for i in range(len(ans_mat)):
for j in (mat_inputs_X[i]):
temp=list(j.flat)
print(" "+str(temp[0])+"\t "+str(temp[1])+"\t "+str(ans_mat[i]))
### XOR Gate
### XOR gate will be formed using 2 layers :- x1*comp(x2)+comp(x1)*x2
## for first layer - 2 AND NOT GATES
w1=2;w2=-1
mat_weight_1=np.matrix([[w1],[w2]])
mat_weight_2=np.matrix([[w2],[w1]])
mat_weight_transpose_1=(mat_weight_1.getT())
mat_weight_transpose_2=(mat_weight_2.getT())
z1_dot_mat=(np.dot(mat_inputs_X,mat_weight_1))
z2_dot_mat=(np.dot(mat_inputs_X,mat_weight_2))
z1_dot_mat_1=list((z1_dot_mat.getT()).flat)
z2_dot_mat=list((z2_dot_mat.getT()).flat)
z1_ans_mat=[]
z2_ans_mat=[]
threshold=2
## ACTIVATION OF FIRST LAYER NODES
for i,j in zip(z1_dot_mat,z2_dot_mat):
if(i<threshold):
##Output 0 for AND Gate
z1_ans_mat.append(0)
else:
z1_ans_mat.append(1)
if(j<threshold):
##Output 0 for AND Gate
z2_ans_mat.append(0)
else:
z2_ans_mat.append(1)
print(z1_ans_mat);print(z2_ans_mat)
## for second layer - 1 OR GATE
threshold=1
w1=w2=1
mat_weight_final=np.matrix([[w1],[w2]])
z1_ans_mat=np.matrix(z1_ans_mat)
z2_ans_mat=np.matrix(z2_ans_mat)
combined_input=np.matrix(np.row_stack((z1_ans_mat,z2_ans_mat)))
final_dot_mat=np.dot(combined_input.getT(),mat_weight_final)
final_dot_mat=list((final_dot_mat.getT()).flat)
## FINAL ANSWER ACCORDING TO THRESHOLD
ans_mat=[]
print(final_dot_mat)
for i in final_dot_mat:
if(i<threshold):
##Output 0 for AND Gate
ans_mat.append(0)
else:
ans_mat.append(1)
print("------------------------")
print("XOR GATE")
print("------------------------")
print("Input_1\tInput_2\tOutput")
print("-------\t-------\t------")
for i in range(len(ans_mat)):
for j in (mat_inputs_X[i]):
temp=list(j.flat)
print(" "+str(temp[0])+"\t "+str(temp[1])+"\t "+str(ans_mat[i]))