From afaf44fc068e996c2690a0c4e2c5f1710eaf4f1b Mon Sep 17 00:00:00 2001 From: aaaa Date: Mon, 10 Dec 2018 15:52:07 +0100 Subject: [PATCH] C NN added --- VanillaCNeuralNet/cnet.c | 8 +++ VanillaCNeuralNet/hello | Bin 0 -> 8640 bytes VanillaCNeuralNet/nn | Bin 0 -> 12928 bytes VanillaCNeuralNet/nn.c | 121 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 VanillaCNeuralNet/cnet.c create mode 100755 VanillaCNeuralNet/hello create mode 100755 VanillaCNeuralNet/nn create mode 100644 VanillaCNeuralNet/nn.c diff --git a/VanillaCNeuralNet/cnet.c b/VanillaCNeuralNet/cnet.c new file mode 100644 index 0000000..4e87b95 --- /dev/null +++ b/VanillaCNeuralNet/cnet.c @@ -0,0 +1,8 @@ +#include + +main() { +for(int i=0; i<1000000; i++){ + printf("number %d\n", i); + +} +} diff --git a/VanillaCNeuralNet/hello b/VanillaCNeuralNet/hello new file mode 100755 index 0000000000000000000000000000000000000000..eabae9503355081c71dc2a8fa476f3edf16a3b8d GIT binary patch literal 8640 zcmeHMU2Igx6`u9tAHb{^2NL7Za+^X6O?_FL;FuE1?%IZHWCJxON!6Ik_3pL3?f$sC zcd3o~Q5Em69SQNN&_rh7!dQEusqX_MExr ztnc2vF6mQW80+qt`Of*yoS8c_cV_&P?%q9BKA+&^7vB_Enyj{wFe{olBSctqh~@CR zL3~lv0F0(#QoH3{a?P@i?ED~?!P8M?18%vV>{xPyC@D0uC>MXC3RnuENCV_ytk+h_ zB1lDMSUyW}93{y-EFy27c=N<#IZJ`Dm|IOZp7WwAm& zWy69c^S%WkeM|MrW{RIBHCJp+CWhLd*qV&DBopcUc*}T4drSKhYA&O; z$#$cEwB5V^u&jyuQGpW1yapZ|1E_!h%rmF^c4y{Zn{K*w{pPLj538^5MJ_*({oH9A zSSlXYD*wL_^kAVJ*VRyBQv1}r34GUe5jT`!KB5foS z$4w}rg`TsFtfi-nL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I(On0++A%kgzW@x+@&$P?$)YtakN(`Dk5 z;v!<_a9#9?%J^|TtP%ln&h=iOZI$^AdB#G zTV^W9XALyK=P@4E1KtSzf0V{8FB4w}Tw$jUc|ziq<3|HL2wy$-`1uOpjiPdX9gw)g zF=b;*;LnqP_qaU`xKimx`~vXRRqF(=zijer#353i*Srk90XX)?dj5@>vmYA<*945ATxwIr1;hdf@v#z!j>?DLw`q<9GY{bP4;l@ZuhGI-vX| zuy6EQKVw-dXXS^7)filY?d$Bc3LxC7JU8GR&~88VW3+{$Eg zx{)6ju}o?#X8kMGi_xj#BkO~nR+~*N=*R8QPRQEDl?6x zO-shNr$5ryt#|M5f&lGkIbX(!3%%>x`y+ilonYnA$jd;0;L=p@)^yvO*3~ccy}bvb zkzW14o;`=U2lRnRw6_~;kXMMYTwX^1-!Bl&xM3M|7a0yauLLV@BHe0a+EfK^?c5RCN4&YO@#7LE=O+c`%bS*OdYO0xdZV&)bm2K{3^9P zT%qPBQkF3U(#qO$lyh(oYi7rUn$B3J8j1F_SjGsIM$&n8D4&4)?nGRuSQ<5Qqe6{O zq`{vpt*os%W@dAVOuDR~gDz_(4J1%*ENKZh#_O4dqD90s?f(mg#n*}LF_Tj{$~ikdoVTO+h-@< z&#~n50I8)d=TUwc9$d1S&-*@>3h11P*Ez;oA>-up{*dKHHYDlhcL9C}_T*guS<0~F z`1!N$`aFvLz{fKR^UqU;VKM2d8fzHVvBR)%xmtDBa zKX>u@JIwL}*9+%*+25~Rd_K3c|to^RS(1bvfsH*>*K#y!f9`1@7_h=Klw4 Ci&m)s literal 0 HcmV?d00001 diff --git a/VanillaCNeuralNet/nn b/VanillaCNeuralNet/nn new file mode 100755 index 0000000000000000000000000000000000000000..1591edf751ec2abc0e2bb99dff9cf87afe23a6dc GIT binary patch literal 12928 zcmeHNdvH|Oc|WU_j1aK9BEk_^lv`(23xd`H8zQg?TCsvFvb+~tY6Z)pT_M$a%|489 zJt>(Lj<;edX+18JAsHrh+%)6y(21v|wqr}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lJ|K9M_<>Z`2NXU&(Nf=94|yR%=Otu-fx}f#FuGGI0GGl!cGw+4H`FA;-&Z^6Bs8*$?Z6Fx<`?XzhUO zXMca2XV1SoFw8TTeZ{r}|6bd3{~5XhhQp*}Df#vP17wsxwja|94DYqr5}KcXIzC~w z=lv1Gdr4?1`T2VpG6ya_ZL<3)K98NR&oo1Nf2LP7w&(ZjQGH&W^`IgP(+uANmqKKF z-j`kc9rntb5bpw^E@yk*zwvqbN!@-P-&`;E!!g)e>*sym=pt1^odVfA+cW$i&px{! zG?Ynw)>0vC$8ZX4YyJG5U2iCTj#E+B=P%ROfRHb4Kfi~XhSK<5_I6ZG_2;*f?Q4~r z?45L}SRs#HB~kYQLqVR0)SY+Y;&#(oL}ex}j$JbJt^N*${8^rJu9tP`=YT@?KUu6a M==`m< +#include +#include +#include +#include + +#define NUMPAT 4 +#define NUMIN 2 +#define NUMHID 2 +#define NUMOUT 1 + +#define rando() ((double)rand()/((double)RAND_MAX+1)) + +main() { + int i, j, k, p, np, op, ranpat[NUMPAT+1], epoch; + int NumPattern = NUMPAT, NumInput = NUMIN, NumHidden = NUMHID, NumOutput = NUMOUT; + double Input[NUMPAT+1][NUMIN+1] = { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1 }; + double Target[NUMPAT+1][NUMOUT+1] = { 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 }; + double SumH[NUMPAT+1][NUMHID+1], WeightIH[NUMIN+1][NUMHID+1], Hidden[NUMPAT+1][NUMHID+1]; + double SumO[NUMPAT+1][NUMOUT+1], WeightHO[NUMHID+1][NUMOUT+1], Output[NUMPAT+1][NUMOUT+1]; + double DeltaO[NUMOUT+1], SumDOW[NUMHID+1], DeltaH[NUMHID+1]; + double DeltaWeightIH[NUMIN+1][NUMHID+1], DeltaWeightHO[NUMHID+1][NUMOUT+1]; + double Error, eta = 0.5, alpha = 0.9, smallwt = 0.5; + + /*random values for the networks weights*/ + for( j = 1 ; j <= NumHidden ; j++ ) { /* initialize WeightIH and DeltaWeightIH */ + for( i = 0 ; i <= NumInput ; i++ ) { + DeltaWeightIH[i][j] = 0.0 ; + WeightIH[i][j] = 2.0 * ( rando() - 0.5 ) * smallwt ; + } + } + + for( k = 1 ; k <= NumOutput ; k ++ ) { /* initialize WeightHO and DeltaWeightHO */ + for( j = 0 ; j <= NumHidden ; j++ ) { + DeltaWeightHO[j][k] = 0.0 ; + WeightHO[j][k] = 2.0 * ( rando() - 0.5 ) * smallwt ; + } + } + + + for( epoch = 0 ; epoch < 10000000 ; epoch++) { /* iterate weight updates */ + for( p = 1 ; p <= NumPattern ; p++ ) { /* randomize order of individuals */ + ranpat[p] = p ; + } + for( p = 1 ; p <= NumPattern ; p++) { + np = p + rando() * ( NumPattern + 1 - p ) ; + op = ranpat[p] ; ranpat[p] = ranpat[np] ; ranpat[np] = op ; + } + Error = 0.0 ; + for( np = 1 ; np <= NumPattern ; np++ ) { /* repeat for all the training patterns */ + p = ranpat[np]; + for( j = 1 ; j <= NumHidden ; j++ ) { /* compute hidden unit activations */ + SumH[p][j] = WeightIH[0][j] ; + for( i = 1 ; i <= NumInput ; i++ ) { + SumH[p][j] += Input[p][i] * WeightIH[i][j] ; + } + /*Thats the sigmoid function*/ + Hidden[p][j] = 1.0/(1.0 + exp(-SumH[p][j])) ; + } + for( k = 1 ; k <= NumOutput ; k++ ) { /* compute output unit activations and errors */ + SumO[p][k] = WeightHO[0][k] ; + for( j = 1 ; j <= NumHidden ; j++ ) { + SumO[p][k] += Hidden[p][j] * WeightHO[j][k] ; + } + Output[p][k] = 1.0/(1.0 + exp(-SumO[p][k])) ; /* Sigmoidal Outputs */ +/* Output[p][k] = SumO[p][k]; Linear Outputs */ + Error += 0.5 * (Target[p][k] - Output[p][k]) * (Target[p][k] - Output[p][k]) ; /* SSE */ +/* Error -= ( Target[p][k] * log( Output[p][k] ) + ( 1.0 - Target[p][k] ) * log( 1.0 - Output[p][k] ) ) ; Cross-Entropy Error */ + DeltaO[k] = (Target[p][k] - Output[p][k]) * Output[p][k] * (1.0 - Output[p][k]) ; /* Sigmoidal Outputs, SSE */ +/* DeltaO[k] = Target[p][k] - Output[p][k]; Sigmoidal Outputs, Cross-Entropy Error */ +/* DeltaO[k] = Target[p][k] - Output[p][k]; Linear Outputs, SSE */ + } + for( j = 1 ; j <= NumHidden ; j++ ) { /* 'back-propagate' errors to hidden layer */ + SumDOW[j] = 0.0 ; + for( k = 1 ; k <= NumOutput ; k++ ) { + SumDOW[j] += WeightHO[j][k] * DeltaO[k] ; + } + DeltaH[j] = SumDOW[j] * Hidden[p][j] * (1.0 - Hidden[p][j]) ; + } + for( j = 1 ; j <= NumHidden ; j++ ) { /* update weights WeightIH */ + DeltaWeightIH[0][j] = eta * DeltaH[j] + alpha * DeltaWeightIH[0][j] ; + WeightIH[0][j] += DeltaWeightIH[0][j] ; + for( i = 1 ; i <= NumInput ; i++ ) { + DeltaWeightIH[i][j] = eta * Input[p][i] * DeltaH[j] + alpha * DeltaWeightIH[i][j]; + WeightIH[i][j] += DeltaWeightIH[i][j] ; + } + } + for( k = 1 ; k <= NumOutput ; k ++ ) { /* update weights WeightHO */ + DeltaWeightHO[0][k] = eta * DeltaO[k] + alpha * DeltaWeightHO[0][k] ; + WeightHO[0][k] += DeltaWeightHO[0][k] ; + for( j = 1 ; j <= NumHidden ; j++ ) { + DeltaWeightHO[j][k] = eta * Hidden[p][j] * DeltaO[k] + alpha * DeltaWeightHO[j][k] ; + WeightHO[j][k] += DeltaWeightHO[j][k] ; + } + } + } + if( epoch%100 == 0 ) fprintf(stdout, "\nEpoch %-5d : Error = %f", epoch, Error) ; + if( Error < 0.0004 ) break ; /* stop learning when 'near enough' */ + } + + fprintf(stdout, "\n\nNETWORK DATA - EPOCH %d\n\nPat\t", epoch) ; /* print network outputs */ + for( i = 1 ; i <= NumInput ; i++ ) { + fprintf(stdout, "Input%-4d\t", i) ; + } + for( k = 1 ; k <= NumOutput ; k++ ) { + fprintf(stdout, "Target%-4d\tOutput%-4d\t", k, k) ; + } + for( p = 1 ; p <= NumPattern ; p++ ) { + fprintf(stdout, "\n%d\t", p) ; + for( i = 1 ; i <= NumInput ; i++ ) { + fprintf(stdout, "%f\t", Input[p][i]) ; + } + for( k = 1 ; k <= NumOutput ; k++ ) { + fprintf(stdout, "%f\t%f\t", Target[p][k], Output[p][k]) ; + } + } + fprintf(stdout, "\n\nDONE\n\n") ; + return 1 ; +} + +/*******************************************************************************/