Advertisement
sandro1234

Untitled

Apr 28th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #include "conv_layer.h"
  2.  
  3. void layer_init(conv_layer &l,int stride, int filter_size, int filter_number, point insz){
  4. assert(stride);
  5.  
  6. assert(!((insz.x-filter_size)%stride));
  7. assert(!((insz.y-filter_size)%stride));
  8. l.type=CONV;
  9. tensor_init(l.in,insz.x,insz.y,insz.z); //input
  10. tensor_init(l.out,(insz.x-filter_size)/stride+1,(insz.y-filter_size)/stride+1,insz.z); //output
  11. tensor_init(l.errs,insz.x,insz.y,insz.z); //erorebi
  12.  
  13. l.fn = filter_number;
  14. l.fil_size = filter_size;
  15. l.stride = stride;
  16.  
  17. l.filters=(tensor*)malloc(l.fn*sizeof(tensor));
  18. l.f_grads=(tensor*)malloc(l.fn*sizeof(tensor));
  19.  
  20. int i,j,x,y,z;
  21. for(i=0; i<filter_number; i++){
  22. tensor_init(l.filters[i],filter_size,filter_size,insz.z);
  23.  
  24. int mval=filter_size*filter_size*insz.z;
  25.  
  26. for(x=0; x<filter_size; x++){
  27. for(y=0; y<filter_size; y++){
  28. for(z=0; z<insz.z; z++){
  29. cout<<i<<" "<<x<<" "<<y<<" "<<z<<" "<<1.0 * mval * rand() / float( RAND_MAX )<<endl;
  30. tensor_val(l.filters[i],x,y,z) = 1.0 * mval * rand() / float( RAND_MAX );
  31. }
  32. }
  33. }
  34. }
  35.  
  36. for(i=0; i<filter_number; i++){
  37. tensor_init(l.f_grads[i],filter_size,filter_size,insz.z);
  38. }
  39. }
  40.  
  41. //int geti(tensor& t, int x,int y,int z){
  42. // return x*t.sz.y*t.sz.z + y*t.sz.z + z;
  43. //}
  44.  
  45.  
  46.  
  47. void feedin(conv_layer &l){
  48. int i,x,y,z,ox,oy;
  49. for(i=0; i<l.fn; i++){
  50. for(ox=0; ox<l.out.sz.x; ox++){
  51. for(oy=0; oy<l.out.sz.y; oy++){
  52.  
  53. float s=0;
  54. for(x=0; x<l.fil_size; x++){
  55. for(y=0; y<l.fil_size; y++){
  56. for(z=0; z<l.in.sz.z; z++){
  57. s += tensor_val(l.in,x,y,z) * tensor_val(l.filters[i],x,y,z);
  58. }
  59. }
  60. }
  61. tensor_val(l.out,ox,oy,i) = s;
  62.  
  63. }
  64. }
  65. }
  66. }
  67.  
  68. void feed(conv_layer &l, tensor &a){
  69. l.in=a;
  70. feedin(l);
  71. }
  72.  
  73. void wonashi_moimate(conv_layer &l){
  74.  
  75. }
  76.  
  77. void calc_err(conv_layer &l, tensor &nle){
  78.  
  79. tensor_mset(l.errs,0);
  80. int x,y,z;
  81. for(x=0; x<l.in.sz.x; x++){
  82. for(y=0; y<l.in.sz.y; y++){
  83. for(z=0; z<l.in.sz.z; z++){
  84. float serr=0;
  85. for(ex=max(0,x-l.fil_size+1); ex<min(l.out.sz.x,x+1); ex++){
  86. for(ey=max(0,y-l.fil_size+1); ey<min(l.out.sz.y,y+1); ey++){
  87. for(ez=0; ez<l.fn; ez++){
  88. float w = tensor_val(l.filters[ez],x-ex,y-ey,z);
  89. serr += w * tensor_val(nle,ex,ey,ez);
  90. tensor_val(l.f_grads[0][ez],x-ex,y-ey,z) += tensor_val(l.in,x,y,z) * tensor_val(nle,ex,ey,ez); //es gaanule sandroo
  91. }
  92. }
  93. }
  94.  
  95. }
  96.  
  97. }
  98. }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement