-
Notifications
You must be signed in to change notification settings - Fork 0
/
NNetwork.py
186 lines (135 loc) · 4.21 KB
/
NNetwork.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import numpy as np
from tkinter import *
from tkinter import ttk
P = np.array([[1, 1], [-1, 1], [1, -1], [-1, -1]])
print("This is P : \n", P)
Pt = P.transpose()
print("The transpose of P is : ", Pt)
# 1- Calculate The Wight By:
# w = p*pt
W = np.dot(P, Pt)
print("The Wight is[ w = p * pt] : \n", W)
# 2- Define The Activation function hardlims.
def hardlims(n):
res = np.zeros((n.shape[0], n.shape[1]))
for i in range(len(n)):
for j in range(len(res)):
if (i == j):
if (n[i] < 0):
res[int(j)] -= 1
else:
res[int(j)] += 1
return res
#####################################
def EnterMatricies():
R = int(input("Enter the number of rows:"))
C = int(input("Enter the number of columns:"))
print("Enter the entries in a single line (separated by space): ")
# User input of entries in a
# single line separated by space
global entries
entries = list(map(int, input().split()))
# For printing the matrix
matrix = np.array(entries).reshape(int(R), int(C))
return matrix
p1 = EnterMatricies()
print("The first input you entered : ", p1)
p2 = EnterMatricies()
print("The second input you entered : ", p2)
# check if the two inputs are orthogonal
# 𝑝1𝑇*p2
def orthogonal_Test(p1, p2):
p1t = p1.transpose()
n = np.dot(p2, p1t)
if (n == 0):
return ("P1 And P2 Are Orthogonal")
else:
return ("P1 And P2 Are NoT Orthogonal")
print(orthogonal_Test(p1, p2))
b = 0 # We don't have bais here.
n1 = np.dot(W, p1.transpose()) + b
n2 = np.dot(W, p2.transpose()) + b
print("n if we will test p1 : \n", n1)
print("n if we will test p2 : \n", n2)
a1 = hardlims(n1)
a2 = hardlims(n2)
print("The outPut of P1 is : \n", a1)
print("The outPut of P2 is : \n", a2)
def test(a1, p1):
Pt =p1.transpose()
for m in a1:
for l in Pt:
if(m == l):
return ("Successful Test a = P")
break
else:
return ("Failed test a != P")
break
break
Final_Test1 = test(a1, p1)
Final_Test2= test(a2, p2)
press = int(input(("If you want test with p1 press 1 , \nIf you want test with p2 press 2:")))
if (press == 1):
print(Final_Test1)
else:
print(Final_Test2)
####################################################################
#GUI
root = Tk()
root.title("Auto Assiciator Hebbian Network")
Rp = ttk.Label(root, text="Please Enter Input rows")
Rp.pack()
R = ttk.Entry(root, width=20)
R.pack()
R.focus_set()
Cp = ttk.Label(root, text="Please Enter Input columns")
Cp.pack()
C = ttk.Entry(root, width=20)
C.pack()
C.focus_set()
btn1 = ttk.Button(root, text="Enter")
btn1.pack()
INPUT = ttk.Label(root, text="Enter the entries in a single line (separated by space): ")
INPUT.pack()
C_entry1 = ttk.Entry(root, width=20)
C_entry1.pack()
def T():
IN1 = StringVar()
IN1.set(str(p1))
P1_GUI = ttk.Label(root, text="The Firt Input You entered is :")
P1_GUI.pack()
P1_GUI = ttk.Label(root, text=IN1.get())
P1_GUI.pack()
btn2 = ttk.Button(root, text="Enter", command=T)
btn2.pack()
INPUT2 = ttk.Label(root, text="Enter the entries in a single line (separated by space): ")
INPUT2.pack()
C_entry2 = ttk.Entry(root, width=20)
C_entry2.pack()
def T2():
P2_GUI = ttk.Label(root, text="The Second Input You entered is :")
P2_GUI.pack()
P2_GUI = ttk.Label(root, text=p2)
P2_GUI.pack()
btn3 = ttk.Button(root, text="Enter", command=T2)
btn3.pack()
w =orthogonal_Test(p1, p2)
def onclik():
master=Tk()
master.title("Test orthogonality")
v = StringVar()
v.set(w)
Label(master, text=v.get()).pack()
bu22 = ttk.Button(root, text="Test orthogonality", command=onclik)
bu22.pack(side=LEFT)
def Test():
v2=StringVar()
v2.set(test(a1, p1))
Label(root, text=v2.get()).pack()
def Press_GUI():
Test = ttk.Label(root, text="If you want test with p1 press 1 , \nIf you want test with p2 press 2:").pack()
Test_entry = ttk.Entry(root ,width=20).pack()
Test_btn = ttk.Button(root, text="Test Inputs" ,command=Press_GUI ).pack(side=RIGHT)
Test_btn2 = ttk.Button(root, text="Test Now!" ,command=Test)
Test_btn2.pack(padx=5 , pady=5,anchor=CENTER )
root.mainloop()