{"id":6685,"date":"2016-11-01T14:51:58","date_gmt":"2016-11-01T18:51:58","guid":{"rendered":"http:\/\/blog.originlab.com\/?p=6685"},"modified":"2018-11-21T13:43:15","modified_gmt":"2018-11-21T18:43:15","slug":"create-a-simple-neural-network-from-scratch","status":"publish","type":"post","link":"https:\/\/blog.originlab.com\/ja\/create-a-simple-neural-network-from-scratch","title":{"rendered":"Create a simple Neural Network from scratch using Origin C"},"content":{"rendered":"<p>When learning\u00a0artificial intelligence, I&#8217;m always amazed by those who provide all kinds of supporting packages for Python and R and whatever programming languages people are using. However, in order to make sure I really understand how this machine thinking works, I always want to implement a neural network without using packages, totally from zero. That&#8217;s why I choose Origin C because seems like no one has done this on this platform yet and there will be definitely no 3rd party packages that I can use. Here in this blog, I will explain step by step how I did (Thanks to <a href=\"https:\/\/medium.com\/technology-invention-and-more\/how-to-build-a-simple-neural-network-in-9-lines-of-python-code-cc8f23647ca1#.hl4yrnuzh\">one of Milo Spencer-Harper&#8217;s\u00a0blogs<\/a>).<\/p>\n<p>First, we need to understand what is <a href=\"https:\/\/en.wikipedia.org\/wiki\/Artificial_neural_network\"><strong>Neural Network<\/strong><\/a>. The human brain is made of hundreds of billions of neurons. Once the\u00a0synapse gets enough\u00a0impulses, it will turn the current\u00a0neuron on and pass the impulse\u00a0to the next\u00a0neuron. We call this process &#8220;think&#8221;.<\/p>\n<p><a href=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/NeuImg_01.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-6718\" src=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/NeuImg_01.png\" alt=\"NeuImg_01\" width=\"400\" height=\"400\" srcset=\"https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/NeuImg_01.png 500w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/NeuImg_01-150x150.png 150w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/NeuImg_01-300x300.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/a><\/p>\n<p>We can simulate this process programmablly, not by\u00a0building a brain but by simulating the high-level rules. Let&#8217;s use a 2D data table, and in order to simplify the task, we only simulate a\u00a0neural network with 4 inputs and a single output.<\/p>\n<p>So first, we need to train the\u00a0neurons for solving unknown cases. In the following table, the first seven\u00a0cases will be used as\u00a0training set. Have you\u00a0noticed the pattern?<\/p>\n<table style=\"border-color: #000000; width: 520px; height: 203px;\" border=\"1.5\">\n<thead>\n<tr style=\"border-color: #000000; background-color: #ffffff;\">\n<td><strong>\u00a0<\/strong><\/td>\n<td style=\"text-align: center;\" colspan=\"4\"><strong>Input<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>Output<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><strong>Sample 1<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>0<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>0<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>0<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>1<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>0<\/strong><\/td>\n<\/tr>\n<tr style=\"border-color: #000000;\">\n<td><strong>Sample 2<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>1<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>1<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>1<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>1<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>1<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>Sample 3<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>1<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>0<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>1<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>0<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>0<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>Sample 4<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>0<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>1<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>1<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>0<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>1<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>Sample 5<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>0<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>0<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>1<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>1<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>0<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>Sample 6<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>0<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>1<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>0<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>1<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>1<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><strong>Sample 7<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>1<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>0<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>0<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>1<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>0<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><strong>New situation<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>0<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>1<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>0<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>0<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>?<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Yes, the output will always follow the second input, so &#8220;?&#8221; shall be 1.<\/p>\n<p>&nbsp;<\/p>\n<h3>Training Process<\/h3>\n<p>So how can we train the network to return\u00a0a good perdition? The idea is to give each input a weight, either positive or negative. Thus, the input that has the larger\u00a0weight will have\u00a0more impact on the output. So first we can give each\u00a0neuron a random weighting value and let the network adjust them during the process:<\/p>\n<ol>\n<li>Take a training sample, calculate the output based on\u00a0a special function by the current weights.<\/li>\n<li>Calculate the error, which is the difference between the sample output and the result from step1.<\/li>\n<li>Adjust the weight slightly according to the error value.<\/li>\n<li>Repeat step 1~3 for &#8230;\u00a0ten thousand times.<\/li>\n<\/ol>\n<p><a href=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/NeuImg_02.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-6726 aligncenter\" src=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/NeuImg_02.png\" alt=\"NeuImg_02\" width=\"600\" height=\"400\" srcset=\"https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/NeuImg_02.png 750w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/NeuImg_02-300x200.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>Then the weighting values will finally become an optimal solution for the training sets. Now if we use it for an unknown case, it will provide a close-enough prediction. We call this process &#8220;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Backpropagation\"><strong>Back Propagation<\/strong><\/a>&#8220;.<\/p>\n<p>&nbsp;<\/p>\n<h3>The function for calculating network output<\/h3>\n<p>You may think what will be the function that we use to calculate the output? First, let&#8217;s get the weighted sum of all\u00a0neurons.<br \/>\n<a href=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/imgtemp-cc8w0k-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-6728 aligncenter\" src=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/imgtemp-cc8w0k-1.png\" alt=\"imgtemp-cc8w0k-1\" width=\"1000\" height=\"40\" srcset=\"https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/imgtemp-cc8w0k-1.png 1588w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/imgtemp-cc8w0k-1-300x12.png 300w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/imgtemp-cc8w0k-1-768x30.png 768w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/imgtemp-cc8w0k-1-1024x41.png 1024w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/a><\/p>\n<p>Normalize the result\u00a0so that\u00a0it will fall between 0 and 1, by Sigmoid function.<br \/>\n<a href=\"https:\/\/www.codecogs.com\/eqnedit.php?latex=\\dpi{150}&amp;space;\\large&amp;space;\\frac{1}{1+e^{-x}}\"><img decoding=\"async\" class=\"aligncenter\" title=\"\\large \\frac{1}{1+e^{-x}}\" src=\"https:\/\/latex.codecogs.com\/png.latex?\\dpi{150}&amp;space;\\large&amp;space;\\frac{1}{1+e^{-x}}\" \/><\/a><\/p>\n<p>Sigmoid function is an S-shaped curve<\/p>\n<p><a href=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/SigmoidCurve.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-6729 aligncenter\" src=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/SigmoidCurve.png\" alt=\"SigmoidCurve\" width=\"544\" height=\"455\" srcset=\"https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/SigmoidCurve.png 544w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/SigmoidCurve-300x251.png 300w\" sizes=\"auto, (max-width: 544px) 100vw, 544px\" \/><\/a><\/p>\n<p>Embed the first equation into the second, we will have:<br \/>\n<a href=\"https:\/\/www.codecogs.com\/eqnedit.php?latex=\\dpi{150}&amp;space;\\large&amp;space;output\\;&amp;space;of\\;&amp;space;neuron=\\frac{1}{1+e^{\\left&amp;space;(&amp;space;\\sum&amp;space;weight_{i}\\cdot&amp;space;input_{i}&amp;space;\\right&amp;space;)}}\"><img decoding=\"async\" class=\"aligncenter\" title=\"\\large output\\; of\\; neuron=\\frac{1}{1+e^{\\left ( \\sum weight_{i}\\cdot input_{i} \\right )}}\" src=\"https:\/\/latex.codecogs.com\/png.latex?\\dpi{150}&amp;space;\\large&amp;space;output\\;&amp;space;of\\;&amp;space;neuron=\\frac{1}{1+e^{\\left&amp;space;(&amp;space;\\sum&amp;space;weight_{i}\\cdot&amp;space;input_{i}&amp;space;\\right&amp;space;)}}\" \/><\/a><br \/>\nAgain, in order to simply the task, we didn&#8217;t take\u00a0lowest <a href=\"https:\/\/en.wikipedia.org\/wiki\/Threshold_potential\">excitation threshold<\/a> into consider.<\/p>\n<p>&nbsp;<\/p>\n<h3>Weight Adjustment Function<\/h3>\n<p>So how to modify the weighting value during the process? We can use <a href=\"https:\/\/en.wikipedia.org\/wiki\/Delta_rule\"><strong>Error Weighted Derivative<\/strong><\/a> method (A.K.A Delta Rule).<br \/>\n<a href=\"https:\/\/www.codecogs.com\/eqnedit.php?latex=\\dpi{150}&amp;space;\\large&amp;space;Adjust\\;&amp;space;weights\\;&amp;space;by=error\\cdot&amp;space;input\\cdot&amp;space;Sigmoid^{-1}\\left&amp;space;(&amp;space;output&amp;space;\\right&amp;space;)\"><img decoding=\"async\" class=\"aligncenter\" title=\"\\large Adjust\\; weights\\; by=error\\cdot input\\cdot Sigmoid^{-1}\\left ( output \\right )\" src=\"https:\/\/latex.codecogs.com\/png.latex?\\dpi{150}&amp;space;\\large&amp;space;Adjust\\;&amp;space;weights\\;&amp;space;by=error\\cdot&amp;space;input\\cdot&amp;space;Sigmoid^{-1}\\left&amp;space;(&amp;space;output&amp;space;\\right&amp;space;)\" \/><\/a><\/p>\n<p>Why choose this equation? First, we want the adjustment to be\u00a0proportional to the error. Second, when there is no input (input = 0), the current weight will not be modified. At last, we multiply it by the slope of the sigmoid curve. In order to understand the slope part, consider the following:<\/p>\n<ol>\n<li>The same function (Sigmoid) is been used to calculate the output<\/li>\n<li>If the calculated output is a large positive (or negative) value, it means that the\u00a0neural network is taking this (or the opposite) decision<\/li>\n<li>For the curve we can see that the larger the output value be, the smaller the slope is.<\/li>\n<li>If the neural network thinks the current weight is correct (error \u2248 0), it will not be adjusted too much. By multiplying the slope will be able to implement this.<\/li>\n<\/ol>\n<p>Take the derivative of sigmoid equation, we can get<br \/>\n<a href=\"https:\/\/www.codecogs.com\/eqnedit.php?latex=\\dpi{150}&amp;space;\\large&amp;space;Sigmoid^{-1}\\left&amp;space;(&amp;space;output&amp;space;\\right&amp;space;)=output\\cdot&amp;space;\\left&amp;space;(&amp;space;1-output&amp;space;\\right&amp;space;)\"><img decoding=\"async\" class=\"aligncenter\" title=\"\\large Sigmoid^{-1}\\left ( output \\right )=output\\cdot \\left ( 1-output \\right )\" src=\"https:\/\/latex.codecogs.com\/png.latex?\\dpi{150}&amp;space;\\large&amp;space;Sigmoid^{-1}\\left&amp;space;(&amp;space;output&amp;space;\\right&amp;space;)=output\\cdot&amp;space;\\left&amp;space;(&amp;space;1-output&amp;space;\\right&amp;space;)\" \/><\/a><br \/>\nPut it into the adjustment equation, then we have<br \/>\n<a href=\"https:\/\/www.codecogs.com\/eqnedit.php?latex=\\dpi{150}&amp;space;\\large&amp;space;Adjust\\;&amp;space;weights\\;&amp;space;by=error\\cdot&amp;space;input\\cdot&amp;space;output\\cdot&amp;space;\\left&amp;space;(&amp;space;1-output&amp;space;\\right&amp;space;)\"><img decoding=\"async\" class=\"aligncenter\" title=\"\\large Adjust\\; weights\\; by=error\\cdot input\\cdot output\\cdot \\left ( 1-output \\right )\" src=\"https:\/\/latex.codecogs.com\/png.latex?\\dpi{150}&amp;space;\\large&amp;space;Adjust\\;&amp;space;weights\\;&amp;space;by=error\\cdot&amp;space;input\\cdot&amp;space;output\\cdot&amp;space;\\left&amp;space;(&amp;space;1-output&amp;space;\\right&amp;space;)\" \/><\/a><br \/>\nOf course, there are other equations you can find that have better performance,\u00a0while\u00a0the advantage of this equation is, it&#8217;s simple enough to understand.<\/p>\n<p>&nbsp;<\/p>\n<h3>Building your Origin C code<\/h3>\n<pre class=\"width:800 lang:c++ decode:true \">#define NUM_NEURONS 4\r\n#define NUM_MAX_INTERATION 10000\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/ Main function\r\n\r\nvoid Simple_Neural_Network_Main(vector vs) {\r\n\tvector&lt;double&gt; vsWeight;\r\n\tvsWeight = Neural_Network_Trainning(Get_Trainning_Matrix(), Get_Trainning_Result());  \/\/ Train the network to optimize the weighting vector\r\n\tprintf(\"New synaptic weights after trainning:\\n[%f, %f, %f, %f]\\n\\n\", vsWeight[0], vsWeight[1], vsWeight[2], vsWeight[3]);  \/\/ print out the optimized weights\r\n\t\r\n\tmatrix mat(1,4);\r\n\tmat.SetRow(vs, 0);\r\n\tvector&lt;double&gt; vsPred;\r\n\tvsPred = Neural_Network_Think(mat, vsWeight);  \/\/ Re-use think function to provide predicted output\r\n\tprintf(\"Considering new situation [%.0f, %.0f, %.0f, %.0f] --&gt;\\n%f\\n\\n\", mat[0][0], mat[0][1], mat[0][2], mat[0][3], vsPred[0]);\r\n}\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/ Training\r\n\r\nstatic vector&lt;double&gt; Neural_Network_Trainning(matrix TrainningMat, vector TrainningResult) {\r\n\t\/\/ Initialize with a random weight\r\n\tvector&lt;double&gt; vsWeight(NUM_NEURONS);\r\n\tvsWeight = Get_Random_Vector(NUM_NEURONS);  \/\/ generate random weights for starter \r\n\tprintf(\"Random starting synaptic weights:\\n[%f, %f, %f, %f]\\n\\n\", vsWeight[0], vsWeight[1], vsWeight[2], vsWeight[3]);  \/\/ print initial weights\r\n\t\r\n\tfor (int ii = 0; ii &lt; NUM_MAX_INTERATION; ii++) {\r\n\t\tvector&lt;double&gt; vsPred, vsError, vsAdjustment;\r\n\t\tvsPred = Neural_Network_Think(TrainningMat, vsWeight);\t\/\/ Get prediction\r\n\t\tvsError = TrainningResult - vsPred;\t\t\/\/ Compare with trainning result to get error\r\n\t\t\r\n\t\tmatrix mat(TrainningMat);\r\n\t\tmat.Transpose(); \r\n\t\tvsAdjustment = Matrix_Dot(mat, vsError * Sigmoid_Dev(vsPred));\r\n\t\tvsWeight += vsAdjustment;\r\n\t};\r\n\t\r\n\treturn vsWeight;\r\n}\r\n\r\nstatic vector&lt;double&gt; Neural_Network_Think(matrix mat, vector&lt;double&gt; vsWeight) {\r\n\tvector&lt;double&gt; vsResult;\r\n\tvsResult = Sigmoid(Matrix_Dot(mat, vsWeight));\r\n\treturn vsResult;\r\n}\r\n\r\nstatic vector&lt;double&gt; Matrix_Dot(matrix mat, vector&lt;double&gt; vs) {\r\n        \/\/ Seems like Origin C does not have a matirx dot multiply operation. \r\n        \/\/ But we can create one by our own. No big deal.\r\n\tvector&lt;double&gt; vsResult(0);\r\n\tfor (int ii = 0; ii &lt; mat.GetNumRows(); ii++) {\r\n\t\tvector v;\r\n\t\tmat.GetRow(v, ii);\r\n\t\tvsResult.Add(Vector_Dot(v, vs));\r\n\t};\r\n\treturn vsResult;\r\n}\r\n\r\nstatic double Vector_Dot(vector&lt;double&gt; vs1, vector&lt;double&gt; vs2) {\r\n\tint nSize = min(vs1.GetSize(), vs2.GetSize()); \r\n\tdouble dResult = 0;\r\n\tfor (int ii = 0; ii &lt; nSize; ii++) {\r\n\t\tdResult += vs1[ii] * vs2[ii];\r\n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\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/ Random\r\n\r\nstatic vector&lt;double&gt; Get_Random_Vector(int nNum) {\r\n\t\/\/ return a vector with nNum random numbers in [-1, 1] with mean=0\r\n\tvector v(nNum);\r\n\tint a = 0, b = 1;\r\n\tint nRet = unifrnd(v, nNum, a, b);\r\n\treturn 2*v-1;\r\n}\r\n<\/pre>\n<h3>Epilogue<\/h3>\n<p>Compile the OriginC code above and call\u00a0the main function in Script Window as following (you can change the input vector to other 4-dig combinations):<\/p>\n<p><a href=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/ScriptWindow_1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6734\" src=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/ScriptWindow_1.png\" alt=\"ScriptWindow_1\" width=\"516\" height=\"246\" srcset=\"https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/ScriptWindow_1.png 516w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/ScriptWindow_1-300x143.png 300w\" sizes=\"auto, (max-width: 516px) 100vw, 516px\" \/><\/a><\/p>\n<p>you should be able to see results like:<\/p>\n<p><a href=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/ScriptWindow_2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6735\" src=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/ScriptWindow_2.png\" alt=\"ScriptWindow_2\" width=\"516\" height=\"335\" srcset=\"https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/ScriptWindow_2.png 516w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/ScriptWindow_2-300x195.png 300w\" sizes=\"auto, (max-width: 516px) 100vw, 516px\" \/><\/a><\/p>\n<p>You\u00a0did it !!!!!! You&#8217;ve built a simple neural network by plain Origin C !!!!<\/p>\n<p>You can see the network trained itself, considered a new case {0, 1, 0, 0} and gives its prediction 0.999998. It&#8217;s almost 1 !!!!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When learning\u00a0artificial intelligence, I&#8217;m always amazed by those who provide &hellip; <\/p>","protected":false},"author":1617,"featured_media":6744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[5],"tags":[],"class_list":["post-6685","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming-oc"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7DvU2-1JP","jetpack_likes_enabled":true,"jetpack_featured_media_url":"https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/3401.jpg","post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/posts\/6685","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/users\/1617"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/comments?post=6685"}],"version-history":[{"count":35,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/posts\/6685\/revisions"}],"predecessor-version":[{"id":7445,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/posts\/6685\/revisions\/7445"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/media\/6744"}],"wp:attachment":[{"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/media?parent=6685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/categories?post=6685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/tags?post=6685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}