{"id":20181,"date":"2023-12-26T07:11:50","date_gmt":"2023-12-26T07:11:50","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=10721"},"modified":"2023-12-26T07:11:50","modified_gmt":"2023-12-26T07:11:50","slug":"creating-your-own-network","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2023\/12\/26\/creating-your-own-network\/","title":{"rendered":"Creating Your Own Network"},"content":{"rendered":"\n<p>In this lesson, you will learn to define a&nbsp;<strong>single layer neural network (NN)<\/strong>&nbsp;in Caffe2 and run it on a randomly generated dataset. We will write code to graphically depict the network architecture, print input, output, weights, and bias values. To understand this lesson, you must be familiar with&nbsp;<strong>neural network architectures<\/strong>, its&nbsp;<strong>terms<\/strong>&nbsp;and&nbsp;<strong>mathematics<\/strong>&nbsp;used in them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Network Architecture<\/h2>\n\n\n\n<p>Let us consider that we want to build a single layer NN as shown in the figure below \u2212<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.tutorialspoint.com\/caffe2\/images\/network_architecture.jpg\" alt=\"Network Architecture\"\/><\/figure>\n\n\n\n<p>Mathematically, this network is represented by the following Python code \u2212Y = X * W^T + b<\/p>\n\n\n\n<p>Where&nbsp;<strong>X, W, b<\/strong>&nbsp;are tensors and&nbsp;<strong>Y<\/strong>&nbsp;is the output. We will fill all three tensors with some random data, run the network and examine the&nbsp;<strong>Y<\/strong>&nbsp;output. To define the network and tensors, Caffe2 provides several&nbsp;<strong>Operator<\/strong>&nbsp;functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Caffe2 Operators<\/h2>\n\n\n\n<p>In Caffe2,&nbsp;<strong>Operator<\/strong>&nbsp;is the basic unit of computation. The Caffe2&nbsp;<strong>Operator<\/strong>&nbsp;is represented as follows.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.tutorialspoint.com\/caffe2\/images\/caffe_operators.jpg\" alt=\"Caffe2 Operators\"\/><\/figure>\n\n\n\n<p>Caffe2 provides an exhaustive list of operators. For the network that we are designing currently, we will use the operator called FC, which computes the result of passing an input vector&nbsp;<strong>X<\/strong>&nbsp;into a fully connected network with a two-dimensional weight matrix&nbsp;<strong>W<\/strong>&nbsp;and a single-dimensional bias vector&nbsp;<strong>b<\/strong>. In other words, it computes the following mathematical equationY = X * W^T + b<\/p>\n\n\n\n<p>Where&nbsp;<strong>X<\/strong>&nbsp;has dimensions&nbsp;<strong>(M x k), W<\/strong>&nbsp;has dimensions&nbsp;<strong>(n x k)<\/strong>&nbsp;and&nbsp;<strong>b<\/strong>&nbsp;is&nbsp;<strong>(1 x n)<\/strong>. The output&nbsp;<strong>Y<\/strong>&nbsp;will be of dimension&nbsp;<strong>(M x n)<\/strong>, where&nbsp;<strong>M<\/strong>&nbsp;is the batch size.<\/p>\n\n\n\n<p>For the vectors&nbsp;<strong>X<\/strong>&nbsp;and&nbsp;<strong>W<\/strong>, we will use the&nbsp;<strong>GaussianFill<\/strong>&nbsp;operator to create some random data. For generating bias values&nbsp;<strong>b<\/strong>, we will use&nbsp;<strong>ConstantFill<\/strong>&nbsp;operator.<\/p>\n\n\n\n<p>We will now proceed to define our network.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating Network<\/h2>\n\n\n\n<p>First of all, import the required packages \u2212from caffe2.python import core, workspace<\/p>\n\n\n\n<p>Next, define the network by calling&nbsp;<strong>core.Net<\/strong>&nbsp;as follows \u2212net = core.Net(&#8220;SingleLayerFC&#8221;)<\/p>\n\n\n\n<p>The name of the network is specified as&nbsp;<strong>SingleLayerFC<\/strong>. At this point, the network object called net is created. It does not contain any layers so far.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating Tensors<\/h2>\n\n\n\n<p>We will now create the three vectors required by our network. First, we will create X tensor by calling&nbsp;<strong>GaussianFill<\/strong>&nbsp;operator as follows \u2212X = net.GaussianFill([], [&#8220;X&#8221;], mean=0.0, std=1.0, shape=[2, 3], run_once=0)<\/p>\n\n\n\n<p>The&nbsp;<strong>X<\/strong>&nbsp;vector has dimensions&nbsp;<strong>2 x 3<\/strong>&nbsp;with the mean data value of 0,0 and standard deviation of&nbsp;<strong>1.0<\/strong>.<\/p>\n\n\n\n<p>Likewise, we create&nbsp;<strong>W<\/strong>&nbsp;tensor as follows \u2212W = net.GaussianFill([], [&#8220;W&#8221;], mean=0.0, std=1.0, shape=[5, 3], run_once=0)<\/p>\n\n\n\n<p>The&nbsp;<strong>W<\/strong>&nbsp;vector is of size&nbsp;<strong>5 x 3<\/strong>.<\/p>\n\n\n\n<p>Finally, we create bias&nbsp;<strong>b<\/strong>&nbsp;matrix of size 5.b = net.ConstantFill([], [&#8220;b&#8221;], shape=[5,], value=1.0, run_once=0)<\/p>\n\n\n\n<p>Now, comes the most important part of the code and that is defining the network itself.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Defining Network<\/h3>\n\n\n\n<p>We define the network in the following Python statement \u2212Y = X.FC([W, b], [&#8220;Y&#8221;])<\/p>\n\n\n\n<p>We call&nbsp;<strong>FC<\/strong>&nbsp;operator on the input data&nbsp;<strong>X<\/strong>. The weights are specified in&nbsp;<strong>W<\/strong>&nbsp;and bias in b. The output is&nbsp;<strong>Y<\/strong>. Alternatively, you may create the network using the following Python statement, which is more verbose.Y = net.FC([X, W, b], [&#8220;Y&#8221;])<\/p>\n\n\n\n<p>At this point, the network is simply created. Until we run the network at least once, it will not contain any data. Before running the network, we will examine its architecture.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Printing Network Architecture<\/h2>\n\n\n\n<p>Caffe2 defines the network architecture in a JSON file, which can be examined by calling the Proto method on the created&nbsp;<strong>net<\/strong>&nbsp;object.print (net.Proto())<\/p>\n\n\n\n<p>This produces the following output \u2212name: &#8220;SingleLayerFC&#8221; op { output: &#8220;X&#8221; name: &#8220;&#8221; type: &#8220;GaussianFill&#8221; arg { name: &#8220;mean&#8221; f: 0.0 } arg { name: &#8220;std&#8221; f: 1.0 } arg { name: &#8220;shape&#8221; ints: 2 ints: 3 } arg { name: &#8220;run_once&#8221; i: 0 } } op { output: &#8220;W&#8221; name: &#8220;&#8221; type: &#8220;GaussianFill&#8221; arg { name: &#8220;mean&#8221; f: 0.0 } arg { name: &#8220;std&#8221; f: 1.0 } arg { name: &#8220;shape&#8221; ints: 5 ints: 3 } arg { name: &#8220;run_once&#8221; i: 0 } } op { output: &#8220;b&#8221; name: &#8220;&#8221; type: &#8220;ConstantFill&#8221; arg { name: &#8220;shape&#8221; ints: 5 } arg { name: &#8220;value&#8221; f: 1.0 } arg { name: &#8220;run_once&#8221; i: 0 } } op { input: &#8220;X&#8221; input: &#8220;W&#8221; input: &#8220;b&#8221; output: &#8220;Y&#8221; name: &#8220;&#8221; type: &#8220;FC&#8221; }<\/p>\n\n\n\n<p>As you can see in the above listing, it first defines the operators&nbsp;<strong>X, W<\/strong>&nbsp;and&nbsp;<strong>b<\/strong>. Let us examine the definition of&nbsp;<strong>W<\/strong>&nbsp;as an example. The type of&nbsp;<strong>W<\/strong>&nbsp;is specified as&nbsp;<strong>GausianFill<\/strong>. The&nbsp;<strong>mean<\/strong>&nbsp;is defined as float&nbsp;<strong>0.0<\/strong>, the standard deviation is defined as float&nbsp;<strong>1.0<\/strong>, and the&nbsp;<strong>shape<\/strong>&nbsp;is&nbsp;<strong>5 x 3<\/strong>.op { output: &#8220;W&#8221; name: &#8220;&#8221; type: &#8220;GaussianFill&#8221; arg { name: &#8220;mean&#8221; f: 0.0 } arg { name: &#8220;std&#8221; f: 1.0 } arg { name: &#8220;shape&#8221; ints: 5 ints: 3 } &#8230; }<\/p>\n\n\n\n<p>Examine the definitions of&nbsp;<strong>X<\/strong>&nbsp;and&nbsp;<strong>b<\/strong>&nbsp;for your own understanding. Finally, let us look at the definition of our single layer network, which is reproduced hereop { input: &#8220;X&#8221; input: &#8220;W&#8221; input: &#8220;b&#8221; output: &#8220;Y&#8221; name: &#8220;&#8221; type: &#8220;FC&#8221; }<\/p>\n\n\n\n<p>Here, the network type is&nbsp;<strong>FC<\/strong>&nbsp;(Fully Connected) with&nbsp;<strong>X, W, b<\/strong>&nbsp;as inputs and&nbsp;<strong>Y<\/strong>&nbsp;is the output. This network definition is too verbose and for large networks, it will become tedious to examine its contents. Fortunately, Caffe2 provides a graphical representation for the created networks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Network Graphical Representation<\/h2>\n\n\n\n<p>To get the graphical representation of the network, run the following code snippet, which is essentially only two lines of Python code.from caffe2.python import net_drawer from IPython import display graph = net_drawer.GetPydotGraph(net, rankdir=&#8221;LR&#8221;) display.Image(graph.create_png(), width=800)<\/p>\n\n\n\n<p>When you run the code, you will see the following output \u2212<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.tutorialspoint.com\/caffe2\/images\/graphical_representation.jpg\" alt=\"Graphical Representation\"\/><\/figure>\n\n\n\n<p>For large networks, the graphical representation becomes extremely useful in visualizing and debugging network definition errors.<\/p>\n\n\n\n<p>Finally, it is now time to run the network.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Running Network<\/h2>\n\n\n\n<p>You run the network by calling the&nbsp;<strong>RunNetOnce<\/strong>&nbsp;method on the&nbsp;<strong>workspace<\/strong>&nbsp;object \u2212workspace.RunNetOnce(net)<\/p>\n\n\n\n<p>After the network is run once, all our data that is generated at random would be created, fed into the network and the output will be created. The tensors which are created after running the network are called&nbsp;<strong>blobs<\/strong>&nbsp;in Caffe2. The workspace consists of the&nbsp;<strong>blobs<\/strong>&nbsp;you create and store in memory. This is quite similar to Matlab.<\/p>\n\n\n\n<p>After running the network, you can examine the&nbsp;<strong>blobs<\/strong>&nbsp;that the workspace contains using the following&nbsp;<strong>print<\/strong>&nbsp;commandprint(&#8220;Blobs in the workspace: {}&#8221;.format(workspace.Blobs()))<\/p>\n\n\n\n<p>You will see the following output \u2212Blobs in the workspace: [&#8216;W&#8217;, &#8216;X&#8217;, &#8216;Y&#8217;, &#8216;b&#8217;]<\/p>\n\n\n\n<p>Note that the workspace consists of three input blobs \u2212&nbsp;<strong>X, W<\/strong>&nbsp;and&nbsp;<strong>b<\/strong>. It also contains the output blob called&nbsp;<strong>Y<\/strong>. Let us now examine the contents of these blobs.for name in workspace.Blobs(): print(&#8220;{}:\\n{}&#8221;.format(name, workspace.FetchBlob(name)))<\/p>\n\n\n\n<p>You will see the following output \u2212W: [[ 1.0426593 0.15479846 0.25635982] [-2.2461145 1.4581774 0.16827184] [-0.12009818 0.30771437 0.00791338] [ 1.2274994 -0.903331 -0.68799865] [ 0.30834186 -0.53060573 0.88776857]] X: [[ 1.6588869e+00 1.5279824e+00 1.1889904e+00] [ 6.7048723e-01 -9.7490678e-04 2.5114202e-01]] Y: [[ 3.2709925 -0.297907 1.2803618 0.837985 1.7562964] [ 1.7633215 -0.4651525 0.9211631 1.6511179 1.4302125]] b: [1. 1. 1. 1. 1.]<\/p>\n\n\n\n<p>Note that the data on your machine or as a matter of fact on every run of the network would be different as all inputs are created at random. You have now successfully defined a network and run it on your computer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, you will learn to define a&nbsp;single layer neural network (NN)&nbsp;in Caffe2 and run it on a randomly generated dataset. We will write code to graphically depict the network architecture, print input, output, weights, and bias values. To understand this lesson, you must be familiar with&nbsp;neural network architectures, its&nbsp;terms&nbsp;and&nbsp;mathematics&nbsp;used in them. Network Architecture [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[761],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/20181"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=20181"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/20181\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=20181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=20181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=20181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}