ccc112b / ai

課程:人工智慧 -- 筆記、習題與報告
3 stars 53 forks source link

習題7: 請自己定義一個神經網路模型,並在 MNIST 資料集上訓練並跑出正確率 #7

Open ccckmit opened 2 months ago

ccckmit commented 2 months ago

參考

  1. 深度學習/01-MNIST
  2. https://cs.stanford.edu/people/karpathy/convnetjs/demo/mnist.html

卷積層的 JavaScript 實作

https://github.com/karpathy/convnetjs/blob/master/src/convnet_layers_dotproducts.js

  var ConvLayer = function(opt) {
    var opt = opt || {};

    // required
    this.out_depth = opt.filters;
    this.sx = opt.sx; // filter size. Should be odd if possible, it's cleaner.
    this.in_depth = opt.in_depth;
    this.in_sx = opt.in_sx;
    this.in_sy = opt.in_sy;

    // optional
    this.sy = typeof opt.sy !== 'undefined' ? opt.sy : this.sx;
    this.stride = typeof opt.stride !== 'undefined' ? opt.stride : 1; // stride at which we apply filters to input volume
    this.pad = typeof opt.pad !== 'undefined' ? opt.pad : 0; // amount of 0 padding to add around borders of input volume
    this.l1_decay_mul = typeof opt.l1_decay_mul !== 'undefined' ? opt.l1_decay_mul : 0.0;
    this.l2_decay_mul = typeof opt.l2_decay_mul !== 'undefined' ? opt.l2_decay_mul : 1.0;

    // computed
    // note we are doing floor, so if the strided convolution of the filter doesnt fit into the input
    // volume exactly, the output volume will be trimmed and not contain the (incomplete) computed
    // final application.
    this.out_sx = Math.floor((this.in_sx + this.pad * 2 - this.sx) / this.stride + 1);
    this.out_sy = Math.floor((this.in_sy + this.pad * 2 - this.sy) / this.stride + 1);
    this.layer_type = 'conv';

    // initializations
    var bias = typeof opt.bias_pref !== 'undefined' ? opt.bias_pref : 0.0;
    this.filters = [];
    // 注意下列這行,決定了卷積層的參數數量
    for(var i=0;i<this.out_depth;i++) { this.filters.push(new Vol(this.sx, this.sy, this.in_depth)); }
    this.biases = new Vol(1, 1, this.out_depth, bias);
  }
  ConvLayer.prototype = {
    forward: function(V, is_training) {
      // optimized code by @mdda that achieves 2x speedup over previous version

      this.in_act = V;
      var A = new Vol(this.out_sx |0, this.out_sy |0, this.out_depth |0, 0.0);

      var V_sx = V.sx |0;
      var V_sy = V.sy |0;
      var xy_stride = this.stride |0;

      for(var d=0;d<this.out_depth;d++) {
        var f = this.filters[d];
        var x = -this.pad |0;
        var y = -this.pad |0;
        for(var ay=0; ay<this.out_sy; y+=xy_stride,ay++) {  // xy_stride
          x = -this.pad |0;
          for(var ax=0; ax<this.out_sx; x+=xy_stride,ax++) {  // xy_stride
            // 遮罩累加迴圈開始
            // convolve centered at this particular location
            var a = 0.0;
            for(var fy=0;fy<f.sy;fy++) {
              var oy = y+fy; // coordinates in the original input array coordinates
              for(var fx=0;fx<f.sx;fx++) {
                var ox = x+fx;
                if(oy>=0 && oy<V_sy && ox>=0 && ox<V_sx) {
                  for(var fd=0;fd<f.depth;fd++) {
                    // avoid function call overhead (x2) for efficiency, compromise modularity :(
                    a += f.w[((f.sx * fy)+fx)*f.depth+fd] * V.w[((V_sx * oy)+ox)*V.depth+fd];
                  }
                }
              }
            }
            // 遮罩累加迴圈結束
            a += this.biases.w[d]; // 加上 bias
            A.set(ax, ay, d, a); // 設定進結果格子中
          }
        }
      }
      this.out_act = A;
      return this.out_act;
    },
    backward: function() {

      var V = this.in_act;
      V.dw = global.zeros(V.w.length); // zero out gradient wrt bottom data, we're about to fill it

      var V_sx = V.sx |0;
      var V_sy = V.sy |0;
      var xy_stride = this.stride |0;

      for(var d=0;d<this.out_depth;d++) {
        var f = this.filters[d];
        var x = -this.pad |0;
        var y = -this.pad |0;
        for(var ay=0; ay<this.out_sy; y+=xy_stride,ay++) {  // xy_stride
          x = -this.pad |0;
          for(var ax=0; ax<this.out_sx; x+=xy_stride,ax++) {  // xy_stride

            // convolve centered at this particular location
            var chain_grad = this.out_act.get_grad(ax,ay,d); // gradient from above, from chain rule
            for(var fy=0;fy<f.sy;fy++) {
              var oy = y+fy; // coordinates in the original input array coordinates
              for(var fx=0;fx<f.sx;fx++) {
                var ox = x+fx;
                if(oy>=0 && oy<V_sy && ox>=0 && ox<V_sx) {
                  for(var fd=0;fd<f.depth;fd++) {
                    // avoid function call overhead (x2) for efficiency, compromise modularity :(
                    var ix1 = ((V_sx * oy)+ox)*V.depth+fd;
                    var ix2 = ((f.sx * fy)+fx)*f.depth+fd;
                    f.dw[ix2] += V.w[ix1]*chain_grad;
                    V.dw[ix1] += f.w[ix2]*chain_grad;
                  }
                }
              }
            }
            this.biases.dw[d] += chain_grad;
          }
        }
      }
    },
    getParamsAndGrads: function() {
      var response = [];
      for(var i=0;i<this.out_depth;i++) {
        response.push({params: this.filters[i].w, grads: this.filters[i].dw, l2_decay_mul: this.l2_decay_mul, l1_decay_mul: this.l1_decay_mul});
      }
      response.push({params: this.biases.w, grads: this.biases.dw, l1_decay_mul: 0.0, l2_decay_mul: 0.0});
      return response;
    },
    toJSON: function() {
      var json = {};
      json.sx = this.sx; // filter size in x, y dims
      json.sy = this.sy;
      json.stride = this.stride;
      json.in_depth = this.in_depth;
      json.out_depth = this.out_depth;
      json.out_sx = this.out_sx;
      json.out_sy = this.out_sy;
      json.layer_type = this.layer_type;
      json.l1_decay_mul = this.l1_decay_mul;
      json.l2_decay_mul = this.l2_decay_mul;
      json.pad = this.pad;
      json.filters = [];
      for(var i=0;i<this.filters.length;i++) {
        json.filters.push(this.filters[i].toJSON());
      }
      json.biases = this.biases.toJSON();
      return json;
    },
    fromJSON: function(json) {
      this.out_depth = json.out_depth;
      this.out_sx = json.out_sx;
      this.out_sy = json.out_sy;
      this.layer_type = json.layer_type;
      this.sx = json.sx; // filter size in x, y dims
      this.sy = json.sy;
      this.stride = json.stride;
      this.in_depth = json.in_depth; // depth of input volume
      this.filters = [];
      this.l1_decay_mul = typeof json.l1_decay_mul !== 'undefined' ? json.l1_decay_mul : 1.0;
      this.l2_decay_mul = typeof json.l2_decay_mul !== 'undefined' ? json.l2_decay_mul : 1.0;
      this.pad = typeof json.pad !== 'undefined' ? json.pad : 0;
      for(var i=0;i<json.filters.length;i++) {
        var v = new Vol(0,0,0,0);
        v.fromJSON(json.filters[i]);
        this.filters.push(v);
      }
      this.biases = new Vol(0,0,0,0);
      this.biases.fromJSON(json.biases);
    }
  }
KiuBios commented 2 months ago

資工二 111110519 胡劭家 https://github.com/KiuBios/ai/tree/master/hw7

weilunh7 commented 2 months ago

資工三 111010514 黃威綸 HW7

wrr606 commented 2 months ago

資工二 111110517 范揚玄 https://github.com/wrr606/ai/tree/master/homework/7

LeeYi-user commented 2 months ago

資工三 李易 111010512 https://github.com/LeeYi-user/ai/tree/master/homework/07

kingpider123 commented 2 months ago

資工三 阮銘福 111010552 https://github.com/kingpider123/ai/tree/master/homework/7

dallas145 commented 1 month ago

資工三 蔡松宏 111010511 習題七

Tzuan1020 commented 1 month ago

資工三 111010517 蘇子安 習題七

ctfpanda17 commented 3 weeks ago

電三甲 111010148 林晏霆 https://github.com/ctfpanda17/ai/tree/master/hw7

tom71012 commented 3 weeks ago

資工三 吳承泰 111010505 習題7

111010557 commented 3 weeks ago

資工三 楊欣潔111010557 https://github.com/111010557/ai/blob/master/hw/07

Ellinaa commented 3 weeks ago

資工四110910558陳玟卉 習題7

lkhagvaa2444 commented 3 weeks ago

資工二 李荷葦 111110544 https://github.com/lkhagvaa2444/ai/tree/master/HW7

siyu0927 commented 3 weeks ago

資工三 葉思妤 111010508 https://github.com/siyu0927/ai/tree/master/HW/hw7

weixiang0470 commented 3 weeks ago

資工三 黃偉祥 111010550 Homework 7

Kruez0 commented 2 weeks ago

資工二劉佩芬111110541 https://github.com/Kruez0/ai/tree/master/h7

RogerChen530 commented 2 weeks ago

資工三111010530陳樂融 Week7

ali1234-56 commented 2 weeks ago

資工三 111010536 林家成 hk7

Jung217 commented 2 weeks ago

資工三 簡志融 111010501 習題七

rossen1020 commented 2 weeks ago

111010516 資工三 賴映羽 07

sam00002135 commented 2 weeks ago

資工三 111010574 白和鑫 https://github.com/sam00002135/ai/tree/master/home7

yihsien924 commented 2 weeks ago

資工三 林羿嫻 111010555 hw7

ruoxin030704 commented 2 weeks ago

資工三 111010576 王若馨 https://github.com/ruoxin030704/ai/tree/master/7

byby9527 commented 2 weeks ago

111010540 資工三 陳先正 https://github.com/byby9527/ai/tree/master/07

weichen11011 commented 2 weeks ago

資工三 顏瑋成 111010529 習題7

Remilia517 commented 2 weeks ago

資工三 111010532 李昱宏 習題7

a920217 commented 2 weeks ago

資工三 111010538 陳紹恩 hw7

Chieh0622 commented 2 weeks ago

資工三 111010515 林弘杰 HW7

mimmm4308 commented 2 weeks ago

資工三 111010525 林品翔 HW7

MitanEXE commented 1 week ago

資工三 111010544 陳彬彬 HW7

gakuplusq commented 1 week ago

110910530 資工四 黃劭騏

patrick0516 commented 1 week ago

資工三 111010575 周辰恩 hw7

yehchen0328 commented 1 week ago

資工三 111010535 葉哲辰 https://github.com/yehchen0328/ai/blob/master/hw7.md https://github.com/yehchen0328/ai/blob/master/hw7.py

guangbo1110105228 commented 1 week ago

資工三 111010528 冉光博 https://github.com/guangbo1110105228/ai/blob/master/hw7.py

HuangAlvin commented 1 week ago

資工三 111010526 黃俊皓 https://github.com/HuangAlvin/ai/tree/master/E7