Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.41 KB | None | 0 0
  1. [root@cobalt examples]# cd iris/
  2. [root@cobalt iris]# ls
  3. iris.c iris.data Makefile
  4. [root@cobalt iris]# make
  5. gcc -Wall -ansi -pedantic -o irisclassifier iris.c -ldeep -lm -fopenmp
  6. [root@cobalt iris]# ls
  7. iris.c irisclassifier iris.data Makefile
  8. [root@cobalt iris]# ./irisclassifier
  9. Loading data set
  10. Number of training examples: 120
  11. Number of labeled training examples: 119
  12. Number of test examples: 31
  13. Number of Inputs: 4
  14. Training Completed
  15. Test data set performance is 83.3%
  16. [root@cobalt iris]# ls
  17. iris.c irisclassifier iris_classifier.c iris_classifier.py iris.data Makefile training.png
  18. [root@cobalt iris]# cat iris_classifier.c
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <math.h>
  23.  
  24. const int no_of_input_fields = 4;
  25. const int no_of_inputs = 4;
  26. const int no_of_hiddens = 16;
  27. const int no_of_outputs = 3;
  28. const int hidden_layers = 3;
  29.  
  30. int field_length[] = {
  31. 0,0,0,0
  32. };
  33.  
  34. float input_range_min[] = {
  35. 0.0000000000,2.0000000000,1.0000000000,0.1000000015
  36. };
  37.  
  38. float input_range_max[] = {
  39. 7.9000000954,4.4000000954,6.9000000954,2.5000000000
  40. };
  41.  
  42. float output_range_min[] = {
  43. 0.2500000000,0.2500000000,0.2500000000
  44. };
  45.  
  46. float output_range_max[] = {
  47. 0.7500000000,0.7500000000,0.7500000000
  48. };
  49.  
  50. float hidden_layer_0_weights[] = {
  51. -2.2484962940,-0.7666497231,4.0467872620,3.8693499565,-2.8199477196,-0.2944447100,4.2845869064,3.5924100876,-0.1872782260,0.2340631634,-2.2634098530,-2.6295108795,2.5386443138,1.9927604198,-4.9753031731,-5.9636578560,2.6333551407,-0.3905405998,-4.0220298767,-2.1359536648,-1.8713860512,0.1583160162,-1.8444797993,-1.5544142723,-1.7583038807,0.2567679286,1.2585419416,0.4215927720,1.3341944218,0.7511479259,-0.8167550564,-3.3668096066,-1.0437803268,-0.7197110653,-1.7914518118,-0.2218833864,1.0302518606,-0.1271219403,1.4542108774,1.3056194782,0.8255313039,-1.5389255285,2.7201731205,2.0579991341,1.4212317467,0.3980617523,-4.2001056671,-2.7071132660,-0.7312296629,0.0311985668,2.8313605785,2.6895420551,0.4264544547,0.5461354852,0.1987769753,2.0541367531,2.0137908459,-0.1889492869,-0.3180530369,0.0182365570,1.6449592113,-0.5261655450,-0.4516257644,-2.7478616238
  52. };
  53.  
  54. float hidden_layer_1_weights[] = {
  55. -0.0232810900,0.3408840001,0.3033671081,1.1241682768,-0.4909847975,0.5684629679,0.5276624560,0.3360899985,0.0968111604,-0.2019942403,-1.0551794767,0.1476308554,0.2540937364,0.2849839330,-0.3211167753,-0.5861447453,0.0910250247,-0.1553321034,-1.0618195534,0.1213743985,-0.0298507437,-1.7259865999,-0.6105370522,0.6473513842,-1.3686170578,1.0735118389,0.6452174783,-0.7200524807,0.7164253592,0.2934871018,1.0531808138,0.4404775202,-0.6236241460,-0.4271782041,0.4298268259,1.1708241701,0.3139403760,0.3564032018,-0.0971516296,0.3997506201,0.0711647868,-0.0378369391,-0.4462291300,0.5742602944,-0.2853918374,0.0783989877,0.1139859185,0.1140840277,1.5572481155,1.2409085035,-1.0526626110,-2.6589336395,-0.3456998467,-1.3134971857,-0.1012098417,-0.5873133540,-0.3048662543,1.2486742735,2.1414186954,-1.2180148363,1.0608552694,0.2313914448,0.7469719648,0.6155664921,2.7093839645,2.7406356335,-1.6853177547,-3.1624882221,-2.3313438892,-0.8757214546,0.5886667967,-1.4420732260,-0.7900702357,0.7597433329,0.2260827124,-2.4515097141,2.0156552792,0.9235076904,-0.3187447786,-1.6215766668,1.2978073359,1.5434989929,-0.7949154973,-1.0773376226,-1.2390960455,-0.1671756357,0.4630544484,-0.5346933603,-0.3318194151,0.3925703764,-0.2161244154,-0.9999114871,1.1529189348,0.7678143382,-0.0793540329,-1.0703725815,0.2567297220,0.0201897006,-0.0632854179,-0.2421106249,0.1207396761,-0.1917719543,-0.1135950089,-0.1471788585,0.0135019980,0.1301901191,0.2813262641,-0.0239238776,0.1091249660,0.1241452321,0.2551106215,0.0996424705,-0.5482644439,-0.6317408681,0.4053698480,0.6102766395,0.5963914990,0.1907812357,-0.1390360147,0.3015565574,0.1798570007,-0.1073290631,-0.0264435802,0.4976094961,-0.4003952444,-0.2235261202,0.1677937955,0.4242960811,0.4966800213,0.2775073647,0.1395058036,-1.5982586145,0.0053279605,0.3422848880,0.1329246610,-0.6906701922,0.5984010100,-0.0945130661,0.6410507560,-0.0829531625,-0.3143962026,-0.4619703889,-0.2853811085,0.1949690431,-1.2272109985,-1.4144355059,0.5674192905,1.4778079987,1.2681986094,0.1782380641,-0.5375916362,0.5897597075,0.2893729210,-0.1382935047,-0.0646863654,0.9987096190,-0.8593811393,-0.3848586977,0.3586251140,0.8372020125,0.7741051912,0.9640524387,0.3801533282,-1.1315820217,-0.7929027677,0.9451073408,0.8473040462,-0.4755859077,0.7785373330,-0.5211157203,0.0084308442,-0.2788935006,-0.0440711565,-0.3443664014,-0.8953132629,-0.2304409146,-0.7581404448,-0.5649037957,0.1885556132,2.3113605976,0.2828852832,0.2915476263,-0.0623752512,0.6592720151,-0.1592806429,0.0510800928,-1.3540903330,0.7401369810,0.0636665523,0.7202312350,0.1408720911,-0.5857794881
  56. };
  57.  
  58. float hidden_layer_2_weights[] = {
  59. -0.5130499005,1.1936224699,1.1778210402,0.7151303887,-5.5233631134,-2.6083045006,0.2845487595,1.4449765682,-0.5273015499,2.9208521843,-1.8173320293,0.5922161937,0.3057911694,-1.0449562073,0.4244196117,-2.0929286480,-0.6825383902,-0.3918687403,-0.4312022328,0.1530327350,-0.5589534640,0.0907979682,-0.1587066799,0.6566745639,0.5476092100,0.5977876782,0.4206638336,-0.7182656527,0.6080450416,0.4995968640,0.0870804414,0.0439830907,-0.8125756383,0.0194195360,-0.5444428921,1.0064588785,0.9927483201,-1.0969948769,0.8868638873,-1.2038147449,-0.0422149450,0.0532471836,0.0776947886,0.5503459573,0.0696697906,0.5498805046,0.6061909795,0.9576824307,0.6038115025,-1.3648213148,0.7694389820,-3.3010346889,-1.1330194473,-0.8428072333,-0.5287845731,0.4382665753,-0.8027935028,0.6186729074,-0.5494892597,1.2481387854,-0.9044218659,0.2024586201,-1.2809584141,1.3963338137,2.0980718136,0.8012092113,0.0229756590,-0.8683573604,0.6482414603,-1.5702165365,0.4020436108,-1.7509285212,-1.3339611292,-1.2270605564,-0.3001786470,0.9926843047,-3.1163618565,-1.9915896654,-0.2122564912,0.2571521103,0.8677150607,0.9087898135,0.1977701932,-1.8432596922,-0.1235489398,0.3149477541,-0.1226410195,-0.0598434508,0.7792931795,0.4207096696,0.0546933375,-0.1383654177,-0.1413376182,-0.4811075330,-0.2792027891,0.0774362460
  60. };
  61.  
  62. float hidden_layer_0_bias[] = {
  63. -2.3744347095,-2.3307080269,2.2593233585,2.8564050198,1.9045099020,1.9814764261,0.0645935535,1.1027538776,1.3328714371,-1.1325021982,-1.0410507917,2.5527098179,-2.2922053337,-0.7167294621,-0.3331150115,1.4093509912
  64. };
  65.  
  66. float hidden_layer_1_bias[] = {
  67. 0.0294300169,-0.2067327946,0.0371490605,0.2018041015,-0.3858766556,-0.1195494160,-0.0931838080,0.1055648997,-0.0472167060,0.0019632615,0.0741529539,-0.0052029621
  68. };
  69.  
  70. float hidden_layer_2_bias[] = {
  71. 0.6420252919,-0.0998118967,-0.0253237989,0.4309089184,-0.3033515215,-0.7262651324,-0.0981767029,-0.1039361283
  72. };
  73.  
  74. float output_layer_weights[] = {
  75. -0.2351148278,0.9643393159,0.2387799174,-0.1686123013,2.1102364063,-0.5528243184,-2.2048568726,0.2434643060,3.1452608109,-1.0663056374,-0.7640323639,-1.3355526924,-1.6563444138,0.8858467340,2.4830100536,-0.3591814041,-3.0175192356,0.0509094633,0.4828285575,1.4115371704,-0.4516018033,-0.4342475235,-0.6309458017,-0.2578376830
  76. };
  77.  
  78. float output_layer_bias[] = {
  79. -0.8848422170,-0.4227862656,0.6248210669
  80. };
  81.  
  82. float inputs[4];
  83. float network_inputs[4];
  84. float prev_hiddens[16];
  85. float hiddens[16];
  86. float outputs[3];
  87.  
  88. /* Encode some text into the input units */
  89. void encode_text(char * text,
  90. float * inputs, int no_of_inputs,
  91. int offset, int max_field_length_chars)
  92. {
  93. int pos = offset, i, bit, max_chars = strlen(text);
  94.  
  95. if (max_chars > (no_of_inputs-offset)/8) {
  96. max_chars = ((no_of_inputs-offset)/8);
  97. }
  98. if (max_chars > max_field_length_chars) {
  99. max_chars = max_field_length_chars;
  100. }
  101.  
  102. /* for each character in the string */
  103. for (i = 0; i < max_chars; i++) {
  104. /* set the bits for this character */
  105. for (bit = 0; bit < 8; bit++, pos++) {
  106. if (text[i] & (1<<bit)) {
  107. inputs[pos] = 0.75f;
  108. }
  109. else {
  110. inputs[pos] = 0.25f;
  111. }
  112. }
  113. }
  114. /* set the remaining inputs within the field to neutral */
  115. while (i < max_field_length_chars) {
  116. for (bit = 0; bit < 8; bit++) {
  117. if (pos >= no_of_inputs) {
  118. i = max_field_length_chars;
  119. break;
  120. }
  121. inputs[pos++] = 0.5f;
  122. }
  123. i++;
  124. }
  125. }
  126.  
  127. int main(int argc, char* argv[])
  128. {
  129. int i,j,pos;
  130. float sum;
  131.  
  132. if (argc < 4) return -1;
  133.  
  134. /* Obtain input values from command arguments */
  135. for (i = 1; i < argc; i++) {
  136. if (i > no_of_inputs) return -2;
  137. inputs[i-1] = atof(argv[i]);
  138. }
  139.  
  140. pos = 0;
  141. for (i = 0; i < no_of_input_fields; i++) {
  142. if (field_length[i] == 0) {
  143. /* Normalise numeric inputs into a 0.25 - 0.75 range */
  144. network_inputs[pos] = 0.25f + ((inputs[i] - input_range_min[i])*0.5f/(input_range_max[i] - input_range_min[i]));
  145. if (network_inputs[pos] < 0.25f) network_inputs[pos] = 0.25f;
  146. if (network_inputs[pos] > 0.75f) network_inputs[pos] = 0.75f;
  147. pos++;
  148. }
  149. else {
  150. /* text value */
  151. encode_text(argv[i+1], network_inputs, no_of_inputs,
  152. pos, field_length[i]/8);
  153. pos += field_length[i];
  154. }
  155. }
  156.  
  157. /* Hidden layer 0 */
  158. for (i = 0; i < no_of_hiddens; i++) {
  159. sum = hidden_layer_0_bias[i];
  160. for (j = 0; j < no_of_inputs; j++) {
  161. sum += hidden_layer_0_weights[i*no_of_inputs+j]*network_inputs[j];
  162. }
  163. hiddens[i] = 1.0f / (1.0f + exp(-sum));
  164. }
  165. for (i = 0; i < no_of_hiddens; i++) {
  166. prev_hiddens[i] = hiddens[i];
  167. }
  168.  
  169. /* Hidden layer 1 */
  170. for (i = 0; i < 12; i++) {
  171. sum = hidden_layer_1_bias[i];
  172. for (j = 0; j < 16; j++) {
  173. sum += hidden_layer_1_weights[i*16+j]*prev_hiddens[j];
  174. }
  175. hiddens[i] = 1.0f / (1.0f + exp(-sum));
  176. }
  177. for (i = 0; i < 12; i++) {
  178. prev_hiddens[i] = hiddens[i];
  179. }
  180.  
  181. /* Hidden layer 2 */
  182. for (i = 0; i < 8; i++) {
  183. sum = hidden_layer_2_bias[i];
  184. for (j = 0; j < 12; j++) {
  185. sum += hidden_layer_2_weights[i*12+j]*prev_hiddens[j];
  186. }
  187. hiddens[i] = 1.0f / (1.0f + exp(-sum));
  188. }
  189. for (i = 0; i < 8; i++) {
  190. prev_hiddens[i] = hiddens[i];
  191. }
  192.  
  193. /* Output layer */
  194. for (i = 0; i < no_of_outputs; i++) {
  195. sum = output_layer_bias[i];
  196. for (j = 0; j < 8; j++) {
  197. sum += output_layer_weights[i*8+j]*prev_hiddens[j];
  198. }
  199. outputs[i] = 1.0f / (1.0f + exp(-sum));
  200. }
  201.  
  202. for (i = 0; i < no_of_outputs; i++) {
  203. /* Convert outputs from 0.25 - 0.75 back to their original range */
  204. outputs[i] = output_range_min[i] + ((outputs[i]-0.25f)*(output_range_max[i] - output_range_min[i])/0.5f);
  205. /* Send the outputs to stdout */
  206. printf("%.10f",outputs[i]);
  207. if (i < no_of_outputs-1) {
  208. printf(" ");
  209. }
  210. }
  211.  
  212. printf("\n");
  213. return 0;
  214. }
  215. [root@cobalt iris]#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement