Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c1d2dee

Browse files
Add files via upload
1 parent b263475 commit c1d2dee

File tree

4 files changed

+852
-0
lines changed

4 files changed

+852
-0
lines changed
Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"<h1>Manual Neural Network</h1>"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"<h3>Basics of object oriented programming to understand the manual way of creating a neural network</h3>"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 1,
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"# to create a class we use the keyword class followed up by classname() in paranthesis\n",
24+
"\n",
25+
"class SimpleClass():\n",
26+
" def __init__(self): # initializes the class, acts as a constrcutor, calls when object is created\n",
27+
" print(\"hello\")"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 2,
33+
"metadata": {},
34+
"outputs": [
35+
{
36+
"name": "stdout",
37+
"output_type": "stream",
38+
"text": [
39+
"hello\n"
40+
]
41+
}
42+
],
43+
"source": [
44+
"# creating a instance of the class i.e, the object\n",
45+
"x = SimpleClass() # when object is created contents of the constructor is called automatically"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": 5,
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"class SimpleClass():\n",
55+
" def __init__(self): # initializes the class, acts as a constrcutor, calls when object is created\n",
56+
" print(\"hello\")\n",
57+
"\n",
58+
" # The keyword self represents the instance of a class and binds the attributes with the given arguments\n",
59+
" # creating a user defined method inside a class\n",
60+
" def yell(self):\n",
61+
" print(\"Yelling\")"
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": 6,
67+
"metadata": {},
68+
"outputs": [
69+
{
70+
"name": "stdout",
71+
"output_type": "stream",
72+
"text": [
73+
"hello\n",
74+
"Yelling\n"
75+
]
76+
}
77+
],
78+
"source": [
79+
"x = SimpleClass() # object creation, calls the constrcutor __init__()\n",
80+
"x.yell() # to access a method of inside a class"
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": 7,
86+
"metadata": {},
87+
"outputs": [],
88+
"source": [
89+
"# using the concept of inheritance\n",
90+
"class ExtendedClass(SimpleClass): # pass the parent class to be inherited inside the parantheis\n",
91+
" def __init__(self):\n",
92+
" print(\"Extend!!!\")"
93+
]
94+
},
95+
{
96+
"cell_type": "code",
97+
"execution_count": 8,
98+
"metadata": {},
99+
"outputs": [
100+
{
101+
"name": "stdout",
102+
"output_type": "stream",
103+
"text": [
104+
"Extend!!!\n"
105+
]
106+
}
107+
],
108+
"source": [
109+
"y = ExtendedClass() # object creation for the inherited class"
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": 10,
115+
"metadata": {},
116+
"outputs": [
117+
{
118+
"name": "stdout",
119+
"output_type": "stream",
120+
"text": [
121+
"Yelling\n"
122+
]
123+
}
124+
],
125+
"source": [
126+
"y.yell() # contents of parent class can also be inherited using the object of ExtendedClass"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": 11,
132+
"metadata": {},
133+
"outputs": [],
134+
"source": [
135+
"# super keyword: it is used to give access to methods and properties of a parent class\n",
136+
"# as we see here that when instance of a parent class is inherited, we didn't get the hello as output, because it solely belongs to the parent class\n",
137+
"# So, to access all the contents of the parent class including the __init__(constructor) contents, we use super class\n",
138+
"\n",
139+
"class NewClass(SimpleClass): # inheriting from SimpleClass\n",
140+
" def __init__(self):\n",
141+
" super().__init__() # to access all the contents of the parent class\n",
142+
" print(\"Extend!!!\")"
143+
]
144+
},
145+
{
146+
"cell_type": "code",
147+
"execution_count": 13,
148+
"metadata": {},
149+
"outputs": [
150+
{
151+
"name": "stdout",
152+
"output_type": "stream",
153+
"text": [
154+
"hello\n",
155+
"Extend!!!\n",
156+
"Yelling\n"
157+
]
158+
}
159+
],
160+
"source": [
161+
"z = NewClass()\n",
162+
"z.yell()\n",
163+
"\n",
164+
"\n",
165+
"# now we see that even hello is priniting after using super keyword"
166+
]
167+
},
168+
{
169+
"cell_type": "code",
170+
"execution_count": 14,
171+
"metadata": {},
172+
"outputs": [],
173+
"source": [
174+
"# Example code:-\n",
175+
"\n",
176+
"class Example():\n",
177+
" def __init__(self,name):\n",
178+
" print(\"Hello \", name)\n",
179+
" def method1(self,USN):\n",
180+
" print(\"Your USN is \", USN)"
181+
]
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": 17,
186+
"metadata": {},
187+
"outputs": [
188+
{
189+
"name": "stdout",
190+
"output_type": "stream",
191+
"text": [
192+
"Hello Mithun\n",
193+
"Your USN is 19BTRCR006\n"
194+
]
195+
}
196+
],
197+
"source": [
198+
"obj = Example(\"Mithun\")\n",
199+
"obj.method1(\"19BTRCR006\")"
200+
]
201+
},
202+
{
203+
"cell_type": "code",
204+
"execution_count": 18,
205+
"metadata": {},
206+
"outputs": [
207+
{
208+
"data": {
209+
"text/plain": [
210+
"__main__.Example"
211+
]
212+
},
213+
"execution_count": 18,
214+
"metadata": {},
215+
"output_type": "execute_result"
216+
}
217+
],
218+
"source": [
219+
"type(obj)\n",
220+
"\n",
221+
"# from this, we understand that the obj belongss to the type of the class it is created in"
222+
]
223+
}
224+
],
225+
"metadata": {
226+
"interpreter": {
227+
"hash": "69eb92836b941e979072a76c7fcfffe5419cca933cedd02cfafbdfca1a93358c"
228+
},
229+
"kernelspec": {
230+
"display_name": "Python 3.9.10 64-bit",
231+
"language": "python",
232+
"name": "python3"
233+
},
234+
"language_info": {
235+
"codemirror_mode": {
236+
"name": "ipython",
237+
"version": 3
238+
},
239+
"file_extension": ".py",
240+
"mimetype": "text/x-python",
241+
"name": "python",
242+
"nbconvert_exporter": "python",
243+
"pygments_lexer": "ipython3",
244+
"version": "3.9.10"
245+
},
246+
"orig_nbformat": 4
247+
},
248+
"nbformat": 4,
249+
"nbformat_minor": 2
250+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"<h1>Manual Neural Network</h1>"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"<h3>We shall build a operation class</h3>\n",
15+
"\n",
16+
"It has following contents:-\n",
17+
"\n",
18+
"* Input Node \n",
19+
"\n",
20+
"* Output Node \n",
21+
"\n",
22+
"* Global default graph variable\n",
23+
"\n",
24+
"* Compute (which will be overwritten by the extended class) "
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": 4,
30+
"metadata": {},
31+
"outputs": [],
32+
"source": [
33+
"class Operation(): # creating a class\n",
34+
" def __init__(self, input_nodes=[]): # intializing the class using __init__ method\n",
35+
" self.input_nodes = input_nodes\n",
36+
" self.output_node = [] # we shall appened the outputs layer\n",
37+
"\n",
38+
" for node in input_nodes:\n",
39+
" node.output_node.append(self)\n",
40+
"\n",
41+
" def compute(self):\n",
42+
" pass # does nothing, created here so that we can override in extended class"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": 5,
48+
"metadata": {},
49+
"outputs": [],
50+
"source": [
51+
"# Let's do addition operation\n",
52+
"\n",
53+
"class add(Operation): # inheriting from Operation()\n",
54+
" def __init__(self,x,y):\n",
55+
" super().__init__([x,y])\n",
56+
" def compute(self,x_var,y_var):\n",
57+
" self.input_nodes = [x_var,y_var]\n",
58+
" return x_var+y_var"
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": 6,
64+
"metadata": {},
65+
"outputs": [],
66+
"source": [
67+
"# Let's do multiplication operation\n",
68+
"\n",
69+
"class multiply(Operation): # inheriting from Operation()\n",
70+
" def __init__(self,x,y):\n",
71+
" super().__init__([x,y])\n",
72+
" def compute(self,x_var,y_var):\n",
73+
" self.input_nodes = [x_var,y_var]\n",
74+
" return x_var*y_var"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": 7,
80+
"metadata": {},
81+
"outputs": [],
82+
"source": [
83+
"# Let's do matrix multiplication operation\n",
84+
"\n",
85+
"class matmul(Operation): # inheriting from Operation()\n",
86+
" def __init__(self,x,y):\n",
87+
" super().__init__([x,y])\n",
88+
" def compute(self,x_var,y_var):\n",
89+
" self.input_nodes = [x_var,y_var]\n",
90+
" return x_var.dot(y_var)"
91+
]
92+
}
93+
],
94+
"metadata": {
95+
"interpreter": {
96+
"hash": "69eb92836b941e979072a76c7fcfffe5419cca933cedd02cfafbdfca1a93358c"
97+
},
98+
"kernelspec": {
99+
"display_name": "Python 3.9.10 64-bit",
100+
"language": "python",
101+
"name": "python3"
102+
},
103+
"language_info": {
104+
"codemirror_mode": {
105+
"name": "ipython",
106+
"version": 3
107+
},
108+
"file_extension": ".py",
109+
"mimetype": "text/x-python",
110+
"name": "python",
111+
"nbconvert_exporter": "python",
112+
"pygments_lexer": "ipython3",
113+
"version": "3.9.10"
114+
},
115+
"orig_nbformat": 4
116+
},
117+
"nbformat": 4,
118+
"nbformat_minor": 2
119+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /