|
| 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 | +} |
0 commit comments