Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "conv_layer.h"
- void layer_init(conv_layer &l,int stride, int filter_size, int filter_number, point insz){
- assert(stride);
- assert(!((insz.x-filter_size)%stride));
- assert(!((insz.y-filter_size)%stride));
- l.type=CONV;
- tensor_init(l.in,insz.x,insz.y,insz.z); //input
- tensor_init(l.out,(insz.x-filter_size)/stride+1,(insz.y-filter_size)/stride+1,insz.z); //output
- tensor_init(l.errs,insz.x,insz.y,insz.z); //erorebi
- l.fn = filter_number;
- l.fil_size = filter_size;
- l.stride = stride;
- l.filters=(tensor*)malloc(l.fn*sizeof(tensor));
- l.f_grads=(tensor*)malloc(l.fn*sizeof(tensor));
- int i,j,x,y,z;
- for(i=0; i<filter_number; i++){
- tensor_init(l.filters[i],filter_size,filter_size,insz.z);
- int mval=filter_size*filter_size*insz.z;
- for(x=0; x<filter_size; x++){
- for(y=0; y<filter_size; y++){
- for(z=0; z<insz.z; z++){
- cout<<i<<" "<<x<<" "<<y<<" "<<z<<" "<<1.0 * mval * rand() / float( RAND_MAX )<<endl;
- tensor_val(l.filters[i],x,y,z) = 1.0 * mval * rand() / float( RAND_MAX );
- }
- }
- }
- }
- for(i=0; i<filter_number; i++){
- tensor_init(l.f_grads[i],filter_size,filter_size,insz.z);
- }
- }
- //int geti(tensor& t, int x,int y,int z){
- // return x*t.sz.y*t.sz.z + y*t.sz.z + z;
- //}
- void feedin(conv_layer &l){
- int i,x,y,z,ox,oy;
- for(i=0; i<l.fn; i++){
- for(ox=0; ox<l.out.sz.x; ox++){
- for(oy=0; oy<l.out.sz.y; oy++){
- float s=0;
- for(x=0; x<l.fil_size; x++){
- for(y=0; y<l.fil_size; y++){
- for(z=0; z<l.in.sz.z; z++){
- s += tensor_val(l.in,x,y,z) * tensor_val(l.filters[i],x,y,z);
- }
- }
- }
- tensor_val(l.out,ox,oy,i) = s;
- }
- }
- }
- }
- void feed(conv_layer &l, tensor &a){
- l.in=a;
- feedin(l);
- }
- void wonashi_moimate(conv_layer &l){
- }
- void calc_err(conv_layer &l, tensor &nle){
- tensor_mset(l.errs,0);
- int x,y,z;
- for(x=0; x<l.in.sz.x; x++){
- for(y=0; y<l.in.sz.y; y++){
- for(z=0; z<l.in.sz.z; z++){
- float serr=0;
- for(ex=max(0,x-l.fil_size+1); ex<min(l.out.sz.x,x+1); ex++){
- for(ey=max(0,y-l.fil_size+1); ey<min(l.out.sz.y,y+1); ey++){
- for(ez=0; ez<l.fn; ez++){
- float w = tensor_val(l.filters[ez],x-ex,y-ey,z);
- serr += w * tensor_val(nle,ex,ey,ez);
- 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
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement