機械学習で四則演算 2【差】

![差.png](https://qiita-image-store.s3.amazonaws.com/0/207542/d630cc99-7b35-8879-be96-5e39a2dce2ba.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_sub_b = a - b;

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

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

                //記録
                using (StreamWriter sw = new StreamWriter(path, true, Encoding.Unicode))
                {
                    sw.Write(
                        a.ToString() + "," + b.ToString() + "," + a_sub_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();
                label1.Text = sw.Elapsed.ToString();
                button1.Enabled = true;
            }));
        }

        Stopwatch sw = new Stopwatch();
        private delegate void dldl();
```
```math
乱数で重みの初期値が
\begin{pmatrix}
0.569274364770052\\
0.031159881982561
\end{pmatrix}
と決定され\\
100回学習させてみましたが最終的には
\begin{pmatrix}
0.999980547941049\\
-0.999987981692304
\end{pmatrix}
と\\
ひき算でも
\begin{pmatrix}
1\\
-1
\end{pmatrix}
まで重みが近似されました。\\
重みの推移グラフを掲載します。
```

![差重み.png](https://qiita-image-store.s3.amazonaws.com/0/207542/158a9bb5-487e-0ec7-fd68-08274ac15f9a.png)

||a|b|a-b|w[0]|w[1]|Y|ΔE|
|---|---|---|---|---|---|---|---|
|1|0.84634401455817|0.680053877029593|0.166290137528577|0.569274364770052|0.031159881982561|0.502992349814563|0.336702212285986|
|2|0.844342519922341|0.968672198228851|-0.12432967830651|0.284308462713313|-0.197815762886965|0.048435093962572|0.172764772269082|
|3|0.802462208924099|0.938649923512083|-0.136187714587985|0.138435819541827|-0.365168194617364|-0.231675584402881|-0.095487869814897|
|4|0.819457468492658|0.605762529003323|0.213694939489334|0.215061226478946|-0.275538512919279|0.009322621797566|-0.204372317691768|
|5|0.455969781361506|0.699470792291439|-0.243501010929933|0.38253564856462|-0.151737420896043|0.068288802024577|0.31178981295451|
|6|0.12910706229932|0.387504314718537|-0.258397252419217|0.240368915721007|-0.369825288391734|-0.112275570367012|0.146121682052205|
|7|0.098039181482996|0.90920312419031|-0.811163942707313|0.221503574613011|-0.426448070660893|-0.366011888999197|0.445152053708117|
|8|0.430195516641343|0.587986916111776|-0.157791399470433|0.177861231631993|-0.831181708632045|-0.412208865154683|-0.254417465684249|
|9|0.570975760263845|0.988265416113783|-0.417289655849938|0.287310484724609|-0.68158756757939|-0.50954209864442|-0.092252442794482|
|10|0.701955172560157|0.366814441684081|0.335140730876075|0.339984393385385|-0.590417668813589|0.022080075980325|-0.31306065489575|
|11|0.115525251308235|0.5034356971753|-0.387910445867065|0.559738939414528|-0.475582499474751|-0.174761225544575|0.21314922032249|
|12|0.700535585498687|0.130034404401683|0.570501181097003|0.535114822170618|-0.582889425810175|0.299071295941054|-0.271429885155949|
|13|0.987057544284993|0.308584319571305|0.678473224713687|0.725261115690182|-0.547594202357104|0.546895471482986|-0.131577753230701|
|14|0.24559802247472|0.896921643007976|-0.651323620533256|0.855135929676614|-0.506991370905687|-0.244711840107937|0.406611780425319|
|15|0.879109178613457|0.631550400346308|0.247558778267148|0.755272880489231|-0.871690277071162|0.113450978133631|-0.134107800133518|
|16|0.349375404114544|0.685859217162178|-0.336483813047634|0.873168278510265|-0.786994442207276|-0.234703871878743|0.101779941168891|
|17|0.010143078402683|0.323895652929272|-0.31375257452659|0.83760887043363|-0.856801152980185|-0.269018236431479|0.04473433809511|
|18|0.391793866358601|0.629592209416252|-0.23779834305765|0.837155126535039|-0.871290410625859|-0.220565410902041|0.017232932155609|
|19|0.456639075398743|0.723705168684807|-0.267066093286064|0.830403369417097|-0.88214013045643|-0.259214745096984|0.00785134818908|
|20|0.905527679671313|0.754829624087936|0.150698055583377|0.826818137039402|-0.887822191722011|0.078552217908995|-0.072145837674383|
|21|0.775999036513269|0.024183572281238|0.751815464232031|0.892148190026629|-0.833364376190747|0.672152408259503|-0.079663055972528|
|22|0.959457012805835|0.269724646243139|0.689732366562696|0.953966644707013|-0.831437838918492|0.691030710271546|0.00129834370885|
|23|0.695203811253982|0.163878418115842|0.531325393138139|0.952720939730525|-0.831788034216063|0.526023121107121|-0.005302272031018|
|24|0.921940651220242|0.231822647727943|0.690118003492298|0.956407099454794|-0.8309191062632|0.689124716841344|-0.000993286650955|
|25|0.987655197730127|0.053042046284788|0.934613151445339|0.957322850796623|-0.830688839921823|0.901443453599718|-0.033169697845621|
|26|0.804407995103117|0.337735786259983|0.466672208843134|0.990083075280989|-0.828929451273443|0.516471601592417|0.049799392749282|
|27|0.603756707908472|0.202459923085971|0.401296784822502|0.950024045602186|-0.845748488338891|0.402353216307496|0.001056431484995|
|28|0.915634630674326|0.34290831738287|0.572726313291456|0.949386218006675|-0.845962373376089|0.579203365068223|0.006477051776767|
|29|0.979518291530906|0.714308115520658|0.265210176010248|0.943455605095196|-0.848183408302462|0.318267730437684|0.053057554427435|
|30|0.981614653943858|0.493877889352794|0.487736764591065|0.891484760029627|-0.886082850019658|0.437477776453289|-0.050258988137776|
|31|0.693051901037363|0.922535825484682|-0.229483924447319|0.940819719278058|-0.861261047037166|-0.142507276007137|0.086976648440182|
|32|0.726653350389867|0.824419381015198|-0.097766030625331|0.880540387730732|-0.94150012120382|-0.136343324250459|-0.038577293625127|
|33|0.350095388176895|0.443247845137142|-0.093152456960246|0.908572707392404|-0.909696252672151|-0.085133789044787|0.008018667915459|
|34|0.377361236315854|0.774143528553724|-0.39678229223787|0.90576540873588|-0.913250509946549|-0.365186217770802|0.031596074467067|
|35|0.517409257831708|0.800603307225091|-0.283194049393383|0.89384227501226|-0.93771040652293|-0.288251784548944|-0.005057735155561|
|36|0.131347840713033|0.847270591579038|-0.715922750866005|0.896459194005408|-0.93366116703032|-0.673315669904197|0.042607080961807|
|37|0.08273773644247|0.947125548472221|-0.864387812029751|0.890862845921989|-0.969760893722286|-0.844777343001349|0.019610469028402|
|38|0.590815405170813|0.091341679026998|0.499473726143815|0.889240320104004|-0.988334469956609|0.435100750090376|-0.064372976053439|
|39|0.543978684369465|0.430810939255548|0.113167745113917|0.927272866033068|-0.982454534239923|0.081164513044398|-0.032003232069519|
|40|0.434830593147702|0.82317912616915|-0.388348533021449|0.944681942109815|-0.96866719177284|-0.386610003248757|0.001738529772691|
|41|0.386643668351063|0.013775374746777|0.372868293604287|0.943925976177551|-0.970098313191943|0.351599534275712|-0.021268759328575|
|42|0.909266445277849|0.826067132328668|0.08319931294918|0.952149407305627|-0.969805328061793|0.064633200685129|-0.018566112264051|
|43|0.827142405243191|0.011639617854562|0.81550278738863|0.96903095020659|-0.954468472945336|0.790416942629663|-0.025085844758966|
|44|0.082102509253706|0.807683155316712|-0.725580646063006|0.989780516178079|-0.954176483298783|-0.689408808771116|0.03617183729189|
|45|0.408261022254946|0.048885548975731|0.359375473279215|0.986810717572098|-0.983391866976299|0.354802701052716|-0.004572772226499|
|46|0.650073917885345|0.742330959878085|-0.092257041992739|0.988677602235827|-0.983168324495666|-0.087122763633662|0.005134278359077|
|47|0.742090514740949|0.41164979031852|0.330440724422429|0.985339941787428|-0.986979658278241|0.324921455216967|-0.005519269205462|
|48|0.061777723050573|0.853463211028587|-0.791685487978014|0.989435739113103|-0.984707652267101|-0.779286667761033|0.012398820216981|
|49|0.297530221891371|0.135037631790637|0.162492590100734|0.988669768231585|-0.995289589182452|0.159757586450159|-0.002735003650575|
|50|0.919629258066243|0.25195596611684|0.667673291949403|0.989483514474614|-0.994920260766539|0.659281894774416|-0.008391397174987|
|51|0.786517181799988|0.147797122666518|0.63872005913347|0.997200488832786|-0.992805998184245|0.637581448268642|-0.001138610864828|
|52|0.714413389896235|0.187051890505036|0.527361499391199|0.998096025841358|-0.992637714774587|0.527378404128099|1.69047369006359E-05|
|53|0.921694205571755|0.957137403524079|-0.035443197952324|0.998083948870963|-0.992640876837583|-0.030165519139646|0.005277678812678|
|54|0.254998049817513|0.656631159436252|-0.401633109618739|0.993219542890449|-0.997692340632983|-0.401846831912797|-0.000213722294058|
|55|0.611644517915158|0.824292951647329|-0.212648433732171|0.993274041658637|-0.997552003915238|-0.214744463361062|-0.002096029628891|
|56|0.798929633013406|0.548707352275358|0.250222280738048|0.994556066690536|-0.995824261465699|0.248164219531918|-0.002058061206129|
|57|0.279477522372956|0.158435527774708|0.121041994598248|0.996200312774668|-0.994694988150463|0.120820569778952|-0.000221424819296|
|58|0.10723422100173|0.679276526290586|-0.572042305288856|0.996262196034556|-0.994659906592356|-0.568815725685336|0.00322657960352|
|59|0.013269380206833|0.900586218061199|-0.887316837854365|0.995916196284273|-0.996851646377235|-0.884535663516314|0.002781174338051|
|60|0.997276975772007|0.338455770322334|0.658821205449673|0.99587929182456|-0.999356333656109|0.654929570650683|-0.00389163479899|
|61|0.361145335883436|0.569678080999143|-0.208532745115707|0.999760329607705|-0.998039187402404|-0.207502269002236|0.001030476113471|
|62|0.365882506298778|0.468523708855977|-0.102641202557199|0.999388177965586|-0.998626227057242|-0.102221412342292|0.000419790214907|
|63|0.150529389339746|0.389016880834949|-0.238487491495203|0.999234584069636|-0.998822908725671|-0.238144800711794|0.000342690783409|
|64|0.105180076838089|0.67108794612395|-0.565907869285861|0.999182999035277|-0.998956221225324|-0.565293334156002|0.000614535129859|
|65|0.885347825421648|0.737352194142226|0.147995631279422|0.999118362183099|-0.999368628343442|0.147680618431701|-0.00031501284772|
|66|0.793811457135627|0.408941052578828|0.384870404556799|0.999397258122808|-0.999136352928992|0.384745121891263|-0.000125282665536|
|67|0.511181681189305|0.182874627030862|0.328307054158443|0.999496708938091|-0.999085119703878|0.32821708938022|-8.9964778222984E-05|
|68|0.036661944369162|0.238846545684546|-0.202184601315383|0.999542697284671|-0.999068667428615|-0.201978921354534|0.00020567996085|
|69|0.299361984850542|0.722214554772812|-0.42285256992227|0.999535156657388|-0.99911779337678|-0.422354583884353|0.000497986037918|
|70|0.904372926756913|0.870005001719112|0.034367925037801|0.999386078568649|-0.999477446141438|0.034267335586749|-0.000100589451052|
|71|0.756641568037049|0.425471349817454|0.331170218219594|0.999477048944899|-0.999389932815902|0.331034097821553|-0.000136120398041|
|72|0.196038637401554|0.583968070607618|-0.387929433206063|0.999580043296314|-0.99933201748641|-0.387621680486361|0.000307752719703|
|73|0.249955915962325|0.101366565609987|0.148589350352338|0.999519711872487|-0.999511735248359|0.148518793214482|-7.0557137856031E-05|
|74|0.462853437505129|0.583654986966241|-0.120801549461112|0.999537348046508|-0.999504583113615|-0.120726536971789|7.50124893234561E-05|
|75|0.304415569316789|0.382501196759986|-0.078085627443197|0.999502628257968|-0.999548364527093|-0.078064284036324|2.13434068734308E-05|
|76|0.840193830821753|0.396944003830172|0.443249826991581|0.999496130992614|-0.999556528405765|0.443002512750234|-0.000247314241348|
|77|0.13776065601863|0.581173487743909|-0.443412831725279|0.999703922892469|-0.999458358500601|-0.443138831822532|0.000273999902747|
|78|0.890810630233405|0.216839100800845|0.673971529432559|0.999666176486117|-0.999617599979722|0.673757075174314|-0.000214454258245|
|79|0.78191461636774|0.245889681040258|0.536024935327482|0.999857214619061|-0.999571097911201|0.536018751948934|-6.1833785475196E-06|
|80|0.036522501165291|0.532677324736806|-0.496154823571516|0.999862049493126|-0.999569577482222|-0.495930585553787|0.000224238017728|
|81|0.85195732668599|0.583910103693563|0.268047222992427|0.999853859759862|-0.99968902398961|0.268104299778591|5.70767861639565E-05|
|82|0.771283334014604|0.874292323772932|-0.103008989758328|0.999805232773706|-0.999722351701737|-0.102916464698101|9.25250602269445E-05|
|83|0.030481494511702|0.389988580434578|-0.359507085922876|0.999733869736774|-0.99980324565165|-0.359438466022027|6.86199008493316E-05|
|84|0.037525671086053|0.096602895342094|-0.059077224256041|0.999731778099643|-0.999830006629372|-0.05907086761106|6.35664498074145E-06|
|85|0.121773583405546|0.868387675317185|-0.746614091911639|0.999731539562274|-0.999830620699682|-0.746499696404294|0.000114395507346|
|86|0.379914541440045|0.291054825434021|0.088859716006024|0.999717609211419|-0.999929960348373|0.088772817017651|-8.68989883726079E-05|
|87|0.996213291304285|0.127271974052895|0.86894131725139|0.999750623400739|-0.999904667978481|0.868705018063235|-0.000236299188155|
|88|0.581814202285285|0.935673986065981|-0.353859783780696|0.999986027792703|-0.999874593714338|-0.353750573610155|0.000109210170541|
|89|0.373639443597588|0.78191443569116|-0.408274992093572|0.999922487764449|-0.999976778829927|-0.408285796754041|-1.08046604693768E-05|
|90|0.218567792893652|0.049162823729759|0.169404969163893|0.999926524811775|-0.999968330509933|0.169390466815728|-1.45023481650797E-05|
|91|0.92616344332982|0.521525089871849|0.404638353457972|0.999929694558005|-0.999967617533546|0.404590127396456|-4.8226061515444E-05|
|92|0.366672376807161|0.502846628661662|-0.136174251854501|0.999974359773196|-0.99994246643248|-0.136154722856943|1.95289975580826E-05|
|93|0.897158363320473|0.396867453305455|0.500290910015018|0.999967199029245|-0.999952286523064|0.50028041827586|-1.04917391576809E-05|
|94|0.271180396560198|0.930008193445396|-0.658827796885198|0.999976611780776|-0.999948122693263|-0.658785892991443|4.19038937550242E-05|
|95|0.292063996797457|0.819822843568317|-0.52775884677086|0.99996524826625|-0.999987093657793|-0.527758415586946|4.3118391379604E-07|
|96|0.456931929316806|0.053472303810284|0.403459625506522|0.999965122332953|-0.999987447152215|0.403444360016519|-1.52654900033578E-05|
|97|0.685692053607522|0.371695602020107|0.313996451587415|0.999972097622752|-0.999986630871296|0.313982288395401|-1.41631920133056E-05|
|98|0.018047675033122|0.172731464343486|-0.154683789310364|0.99998180921097|-0.999981366475114|-0.154680899015774|2.89029459041301E-06|
|99|0.654188015802851|0.321746313628622|0.332441702174228|0.999981757047872|-0.999981865719931|0.332435602491336|-6.09968289239093E-06|
|100|0.553486576095916|0.859967335527748|-0.306480759431832|0.99998574738732|-0.999979903169446|-0.3064713654438|9.39398803168068E-06|
|||||0.999980547941049|-0.999987981692304|||

次回
[積 失敗1](https://qiita.com/imaginary_truth/items/0507111e6024e71b8d0e)