機械学習で四則演算 1【和】

![和.png](https://qiita-image-store.s3.amazonaws.com/0/207542/12fece59-1af5-8b14-e704-2a19812adfc9.png)

```math
深層学習の形式でたし算をする回路を考えてみました。\\
上記のように中間層無しで
重りを
\begin{pmatrix}
1\\
1
\end{pmatrix}
とすればたし算ができます。\\
\begin{pmatrix}
a & b
\end{pmatrix}
\begin{pmatrix}
1\\
1
\end{pmatrix}
=a+b\\
　\\
重りの初期値を乱数で決めてから学習を繰り返すと\\
重りが
\begin{pmatrix}
1\\
1
\end{pmatrix}
になるのか試してみました。
```

```csharp
        string path = @"D:\開発\AI\" + DateTime.Now.ToString("yyyyMMdd") + "和.csv";
        double[] w = new double[2];

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            sw.Start();
            Thread th = new Thread(nnw);
            th.Start();
        }

        private void nnw()
        {
            using (StreamWriter sw = new StreamWriter(path, true, Encoding.Unicode))
            {//列見出し
                sw.Write(
                    "a,b,a+b," +
                    "w[0],w[1]," +
                    "Y,ΔE" +
                    Environment.NewLine);
            }

            // 0.0 以上 1.0 以下の乱数を取得
            Random cRandom = new Random();
            for (int n = 0; n < w.Length; n++)
            {
                w[n] = cRandom.NextDouble();
            }

            //学習
            for (int p = 0; p < 100; p++)
            {
                //値設定
                double a = cRandom.NextDouble();
                double b = cRandom.NextDouble();

                double a_add_b = a + b;

                //順伝播
                double Y = a * w[0] + b * w[1];

                //最小二乗誤差
                double dE = Y - a_add_b;//計算省略のため二乗誤差微分後の値

                //記録
                using (StreamWriter sw = new StreamWriter(path, true, Encoding.Unicode))
                {
                    sw.Write(
                        a.ToString() + "," + b.ToString() + "," + a_add_b.ToString() + "," +
                        w[0].ToString() + "," + w[1].ToString() + "," +
                        Y.ToString() + "," + dE.ToString() +
                        Environment.NewLine);
                }

                //逆伝播
                w[0] = w[0] - (a * dE);
                w[1] = w[1] - (b * dE);
            }

            //最終記録
            using (StreamWriter sw = new StreamWriter(path, true, Encoding.Unicode))
            {
                sw.Write(
                    ",,," +
                    w[0].ToString() + "," + w[1].ToString() + "," +
                    "," +
                    Environment.NewLine);
            }

            //読取専用
            File.SetAttributes(path, FileAttributes.ReadOnly);

            Invoke(new dldl(delegate
            {
                sw.Stop();
                label2.Text = sw.Elapsed.ToString();
                button1.Enabled = true;
            }));
        }

        Stopwatch sw = new Stopwatch();
        private delegate void dldl();
```
```math
乱数で重みの初期値が
\begin{pmatrix}
0.650900189136574\\
0.343834649931562
\end{pmatrix}
と決定され\\
100回学習させてみましたが最終的には
\begin{pmatrix}
1.00000071817137\\
0.999999714540082
\end{pmatrix}
と\\
見事、徐々に
\begin{pmatrix}
1\\
1
\end{pmatrix}
の近似値にまで重みが修正されました。\\
重みの推移グラフを掲載します。
```

![和重み.png](https://qiita-image-store.s3.amazonaws.com/0/207542/16f82433-af18-5637-01a0-9dc4df5b4eae.png)

||a|b|a+b|w[0]|w[1]|Y|ΔE|
|---|---|---|---|---|---|---|---|
|1|0.683191321177032|0.481515911631992|1.16470723280902|0.650900189136574|0.343834649931562|0.610251215083059|-0.554456017725965|
|2|0.059863644679945|0.405779822918484|0.465643467598429|1.02969972842133|0.610814044766724|0.309497593590815|-0.156145874007614|
|3|0.628775622057158|0.439168247133106|1.06794386919026|1.03904718954116|0.674174889870986|0.949403747596282|-0.118540121593982|
|4|0.723503041418038|0.812763529742492|1.53626657116053|1.11358232823515|0.72623394728636|1.3959366677628|-0.140329903397734|
|5|0.94756354715096|0.914543969982557|1.86210751713352|1.21511144014531|0.840288974900325|1.91987652144572|0.057769004312201|
|6|0.493606372966248|0.604670146296113|1.09827651926236|1.16037163750386|0.787456680354705|1.04891838139312|-0.049358137869243|
|7|0.190439433879424|0.919450586158526|1.10989002003795|1.18473512891387|0.817302072801004|0.977089157152884|-0.132800862885066|
|8|0.445464146530937|0.296863115065202|0.742327261596139|1.2100256500604|0.939405904023036|0.817898006463618|0.075570744867479|
|9|0.21897144672413|0.497346186776341|0.716317633500471|1.1763615926953|0.916971737293878|0.713641996747978|-0.002675636752493|
|10|0.990401521786303|0.491479034298788|1.48188055608509|1.1769474807459|0.918302455029929|1.61697697978561|0.13509642370052|
|11|0.474847008229628|0.467522951526345|0.942369959755973|1.04314777712502|0.851905395172378|0.893620925881409|-0.048749033874564|
|12|0.070774346157338|0.952326489124599|1.02310083528194|1.06629611001444|0.874696687373472|0.90846323533168|-0.114637599950257|
|13|0.959042876474114|0.257870299861706|1.21691317633582|1.07440951119597|0.98386911045577|1.28411541066642|0.067202234330605|
|14|0.489296777401723|0.865342737578481|1.3546395149802|1.00995968707805|0.96653965013756|1.33055808702113|-0.024081427959075|
|15|0.564851948788786|0.30704285824068|0.871894807029467|1.02174265217366|0.987378338932466|0.880300795591676|0.008405988562209|
|16|0.368937836666097|0.38892472739747|0.757862564063567|1.0169945131528|0.984797340177987|0.758219792654362|0.000357228590795|
|17|0.068729318244722|0.443619993721889|0.512349311966612|1.01686271800932|0.984658405145693|0.506702436866194|-0.005646875100418|
|18|0.629254897418085|0.363545642403674|0.992800539821759|1.01725082388518|0.987163471842289|0.99898904136068|0.006188501538921|
|19|0.34703746593885|0.383850896444102|0.730888362382952|1.01335667898414|0.984913669074806|0.729732728761278|-0.001155633621674|
|20|0.075143174303297|0.413087185198947|0.488230359502244|1.01375772714776|0.985357260076446|0.483215430572704|-0.00501492892954|
|21|0.518711826074269|0.120073279887472|0.638785105961741|1.01413456482643|0.987428862951923|0.644607414236345|0.005822308274605|
|22|0.105407021523177|0.883874247727857|0.989281269251034|1.01111446466934|0.986729759300875|0.978723587852547|-0.010557681398487|
|23|0.465303556278955|0.176727782551538|0.642031338830493|1.01222731841975|0.996061422004713|0.647024697419443|0.00499335858895|
|24|0.968339733764687|0.421823305274278|1.39016303903897|1.00990389091053|0.995178956813803|1.39771974175483|0.007556702715868|
|25|0.78485160450677|0.281333837323512|1.06618544183028|1.00258643541451|0.991991363497221|1.06596230937626|-0.000223132454025|
|26|0.029223583186615|0.935654411528099|0.964877994714714|1.00276156127907|0.992054138206743|0.957524116790224|-0.00735387792449|
|27|0.327340567636928|0.159421154837786|0.486761722474714|1.00297646794234|0.998934826528631|0.487566229995604|0.00080450752089|
|28|0.868987641701935|0.215668369650686|1.08465601135262|1.00271311999378|0.998806571010575|1.08675629421323|0.002100282860609|
|29|0.410741565474654|0.107537155555346|0.51827872103|1.00088800014384|0.998353606430222|0.518466411117802|0.000187690087802|
|30|0.44673694644437|0.370905322195452|0.817642268639823|1.00081090802335|0.998333422772054|0.817386388850325|-0.000255879789497|
|31|0.949871469731383|0.858160303839557|1.80803177357094|1.00092521897916|0.998428329947821|1.80756186783299|-0.000469905737951|
|32|0.247911580953706|0.64052068891028|0.888432269863985|1.00137156903311|0.998831584398677|0.888023903345478|-0.000408366518508|
|33|0.54560613331646|0.104681645568778|0.650287778885238|1.00147280782232|0.99909315160244|0.650996421483754|0.000708642598516|
|34|0.535940828051391|0.746819440157534|1.28276026820893|1.00108616807424|0.999018969729107|1.28260973754835|-0.000150530660575|
|35|0.290195688740441|0.081918504127263|0.372114192867705|1.00116684360112|0.999131388952764|0.372381650532525|0.00026745766482|
|36|0.949825649126352|0.835120550280027|1.78494619940638|1.00108922853986|0.999109479220945|1.78523708440826|0.000290885001883|
|37|0.422203012473044|0.577314336587356|0.9995173490604|1.00081293850413|0.998866555178104|0.999206220200388|-0.000311128860012|
|38|0.97276720822452|0.354773728342156|1.32754093656668|1.00094429804609|0.999046174329515|1.3281211264514|0.000580189884725|
|39|0.362115930003168|0.186900098429481|0.549016028432649|1.00037990835169|0.998840338200965|0.548936858394353|-7.91700382960503E-05|
|40|0.487022932845644|0.782315093922575|1.26933802676822|1.00040857708374|0.998855135088915|1.26864136807739|-0.000696658690829|
|41|0.289530566097019|0.067473472127446|0.357004038224464|1.00074786584254|0.999400141698063|0.357180093722802|0.000176055498338|
|42|0.080491133071711|0.283518111465274|0.364009244536985|1.00069689239444|0.999388262622303|0.363891899569405|-0.00011734496758|
|43|0.277819275054065|0.433283425137998|0.711102700192063|1.00070633762384|0.999421532045901|0.711048293822177|-5.44063698860731E-05|
|44|0.061584159760542|0.909308163406937|0.970892323167479|1.00072145276208|0.999445105424195|0.970432183062029|-0.00046014010545|
|45|0.574632352019955|0.413586527767399|0.988218879787353|1.00074979010385|0.999863514578392|0.988593284906633|0.00037440511928|
|46|0.95280407692902|0.239500310383504|1.19230438731252|1.00053464480955|0.99970866566513|1.19274402440314|0.000439637090617|
|47|0.937113949999732|0.704237852107844|1.64135180210758|1.00011575679724|0.999603372445472|1.64118095927999|-0.00017084282759|
|48|0.463215715467565|0.660551522234712|1.12376723770228|1.00027585599423|0.999723686431422|1.12371249918567|-5.47385166058678E-05|
|49|0.872161698933766|0.464886332612897|1.33704803154666|1.00030121173536|0.999759844041891|1.33719909166289|0.000151060116231|
|50|0.338268114411397|0.360621910710177|0.698890025121574|1.00016946288775|0.999689618258452|0.698835418556389|-5.46065651854111E-05|
|51|0.640786954500148|0.221520293607153|0.862307248107301|1.00018793454759|0.999709310582326|0.862363280508543|5.60324012427449E-05|
|52|0.131627616533836|0.702933100379507|0.834560716913343|1.00015202971584|0.999696898268352|0.834367667982523|-0.000193048930819|
|53|0.33150818819716|0.194103032906588|0.525611221103748|1.00017744028648|0.999832598751817|0.525637550921648|2.63298178996285E-05|
|54|0.513451399055054|0.750026231049572|1.26347763010463|1.00016871173625|0.999827488054307|1.2634348668972|-4.27632074231354E-05|
|55|0.186653946613266|0.514213913359779|0.700867859973045|1.00019066856493|0.999859561581598|0.700831233624472|-3.66263485725993E-05|
|56|0.853552439647518|0.223841467045174|1.07739390669269|1.00019750501744|0.99987839535963|1.07753526742107|0.000141360728381|
|57|0.620514222709701|0.664421852521795|1.2849360752315|1.00007684622286|0.999846752966806|1.28488193872806|-5.41365034398833E-05|
|58|0.081374756098434|0.882397997603937|0.963772753702371|1.00011043869322|0.999882722442711|0.963678255142379|-9.44985599916626E-05|
|59|0.777669170767846|0.964393469488431|1.74206264025628|1.00011812849049|0.999966107782824|1.74212181970861|5.9179452329694E-05|
|60|0.362701413856215|0.112322708178462|0.475024122034676|1.00007210645487|0.999909035505469|0.475040057769431|1.59357347545019E-05|
|61|0.043097485808235|0.626632664644454|0.669730150452689|1.00006632654134|0.999907245560585|0.669674885998335|-5.52644543542202E-05|
|62|0.17218708813758|0.876609160973043|1.04879624911062|1.00006870830038|0.999941876072877|1.04875712782581|-3.91212848147582E-05|
|63|0.129114427198244|0.710325338742847|0.839439765941091|1.00007544448049|0.999976170149535|0.839432579965371|-7.18597571969237E-06|
|64|0.898006600280295|0.7179651603652|1.6159717606455|1.00007637229363|0.999981274530172|1.61602689923431|5.51385888130085E-05|
|65|0.114098621119791|0.312215685989808|0.426314307109599|1.00002685747695|0.999941686944413|0.426299165260033|-1.51418495657474E-05|
|66|0.48261202940839|0.897229597390271|1.37984162679866|1.00002858514111|0.999946414467362|1.37980734380575|-3.42829929149691E-05|
|67|0.848631984018084|0.126401022135467|0.97503300615355|1.00004513052589|0.999977174183292|0.975068420154714|3.54140011632476E-05|
|68|0.141528139422428|0.068010272489865|0.209538411912293|1.00001507707182|0.999972697817348|0.209538688913334|2.77001041049108E-07|
|69|0.93584585559361|0.377558032692204|1.31340388828581|1.00001503786838|0.999972678978431|1.31340764614146|3.7578556448814E-06|
|70|0.952009923268114|0.083716242147478|1.03572616541559|1.00001152109475|0.999971260169847|1.03573472762154|8.56220594669388E-06|
|71|0.919832026082944|0.31155792871097|1.23138995479391|1.00000336978972|0.99997054337414|1.23138387698908|-6.07780483252718E-06|
|72|0.72265421679367|0.060418438660176|0.783072655453846|1.00000896034926|0.999972436962425|0.783077465372324|4.80991847806589E-06|
|73|0.56003408811988|0.728962609883846|1.28899669800373|1.00000548444138|0.999972146354661|1.28897946521185|-1.7232791871713E-05|
|74|0.618986087673803|0.272692097477937|0.89167818515174|1.00001513539227|0.999984708415599|0.89168338385476|5.19870301995962E-06|
|75|0.62286309600941|0.580817717863628|1.20368081387304|1.00001191746742|0.999983290770368|1.20367853180707|-2.28206596641911E-06|
|76|0.882882351466865|0.907768229445335|1.7906505809122|1.0000133388821|0.999984616234715|1.79064839268242|-2.18822978403921E-06|
|77|0.102184918756683|0.225945406232935|0.328130324989618|1.00001527083155|0.999986602640192|0.328128858366395|-1.4666232227567E-06|
|78|0.317524424901942|0.014154289855694|0.331678714757636|1.00001542069833|0.999986934016972|0.331683426266293|4.71150865710701E-06|
|79|0.049333205469574|0.255018455560793|0.304351661030367|1.00001392467925|0.999986867328912|0.304348998905931|-2.66212443555958E-06|
|80|0.573818673181263|0.621182849454313|1.19500152263558|1.00001405601038|0.999987546219774|1.19500185216212|3.29526541165137E-07|
|81|0.296096432626292|0.795486238689854|1.09158267131615|1.0000138669219|0.999987341523539|1.09157670761842|-5.96369772165595E-06|
|82|0.635927609929781|0.874438260623458|1.51036587055324|1.00001563275152|0.999992085563008|1.51036889116503|3.02061179402457E-06|
|83|0.031601883951389|0.956322842257248|0.987924726208637|1.00001371186108|0.999989444224485|0.987915064800037|-9.6614086003699E-06|
|84|0.37520948302709|0.494113200574235|0.869322683601325|1.0000140171798|0.999998683650218|0.869327292554305|4.60895298048936E-06|
|85|0.522893713099367|0.013364483608568|0.536258196707935|1.00001228785693|0.999996406305709|0.536264573923203|6.37721526797197E-06|
|86|0.946552142010327|0.400111138075642|1.34666328008597|1.00000895325116|0.99999632107752|1.34667028282717|7.0027412031326E-06|
|87|0.012383546220317|0.278985171708737|0.291368717929054|1.00000232479147|0.999993519202768|0.291366938671888|-1.77925716599026E-06|
|88|0.192490682095518|0.731539557097265|0.924030239192783|1.00000234682499|0.999994015589134|0.924026313101451|-3.92609133215149E-06|
|89|0.903978447850784|0.111768649477404|1.01574709732819|1.00000310256099|0.999996887680248|1.01574955411668|2.45678848886932E-06|
|90|0.316575072853162|0.400151817314397|0.71672689016756|1.00000088167714|0.999996613088317|0.7167258140057|-1.07616185995862E-06|
|91|0.091339251069044|0.625314502802358|0.716653753871403|1.00000122236316|0.99999704371644|0.716652016914154|-1.73695724858813E-06|
|92|0.09510206435579|0.533280903256163|0.628382967611953|1.00000138101553|0.999998129860999|0.628382101639966|-8.65971987562553E-07|
|93|0.429333334522943|0.574843799963055|1.004177134486|1.00000146337126|0.999998591667323|1.00417695318875|-1.81297245882561E-07|
|94|0.795044678633588|0.622686577785149|1.41773125641874|1.00000154120821|0.99999869588492|1.41773166969317|4.13274429345023E-07|
|95|0.808876597233525|0.127373849566734|0.936250446800259|1.00000121263657|0.99999843854448|0.936251228785004|7.81984744624431E-07|
|96|0.018677067020292|0.680827400032816|0.699504467053108|1.00000058010741|0.999998338940073|0.699503346992702|-1.12006040631218E-06|
|97|0.803739359510941|0.330827073813801|1.13456643332474|1.00000060102686|0.999999101507887|1.13456661914817|1.85823425091769E-07|
|98|0.787619809521185|0.12434578739309|0.911965596914275|1.00000045167326|0.999999040032467|0.911965833293161|2.36378885687394E-07|
|99|0.675630318781189|0.848215956170213|1.5238462749514|1.00000026549656|0.999999010639749|1.52384561513778|-6.59813623737904E-07|
|100|0.028816554708787|0.603711245862633|0.632527800571419|1.00000071128665|0.999999570304192|0.632527561656059|-2.38915360606384E-07|
|||||1.00000071817137|0.999999714540082|||
生まれて初めて機械学習を成し遂げることができました。
私のような天才秀才でなくとも機械学習が可能で
しかも、高額なグラボを何個も並列にしなくとも無料で実現することができました。

次回
[差](https://qiita.com/imaginary_truth/items/4415c2577c86df669a2d)