1. 程式人生 > >Caffe層系列:Eltwise Layer

Caffe層系列:Eltwise Layer

Eltwise Layer是對多個bottom進行操作計算並將結果賦值給top,一般特點:多個輸入一個輸出,多個輸入維度要求一致
首先看下Eltwise層的引數:

message EltwiseParameter {
  enum EltwiseOp {
    PROD = 0;       //點乘
    SUM = 1;        //加減(預設)
    MAX = 2;        //最大值
  }
  optional EltwiseOp operation = 1 [default = SUM];  // element-wise operation
  repeated float coeff = 2; // blob-wise coefficient for SUM operation

  // Whether to use an asymptotically slower (for >2 inputs) but stabler method
  // of computing the gradient for the PROD operation. (No effect for SUM op.)
  optional bool stable_prod_grad = 3 [default = true];
}

則Eltwise層的操作有三個:product(點乘), sum(相加減) 和 max(取大值),其中sum是預設操作。

假設輸入bottom為A和B,
1)如果要實現element_wise的A+B,即A和B的對應元素相加,prototxt檔案如下:

layer {
	  name: "eltwise_layer"
	  bottom: "A"
	  bottom: "B"
	  top: "diff"
	  type: "Eltwise"
	  eltwise_param {
	    operation: SUM
	  }
}​

2)如果實現A-B,則prototxt為:

layer {
	  name: "eltwise_layer"
	  bottom: "A"
	  bottom: "B"
	  top: "diff"
	  type: "Eltwise"
	  eltwise_param {
	    operation: SUM
	    coeff: 1
	    coeff: -1
	  }
}​

注意:其中A和B的係數(coefficient)都要給出。

3)如果實現A.*B,則prototxt為:

layer {
	  name: "eltwise_layer"
	  bottom: "A"
	  bottom: "B"
	  top: "diff"
	  type: "Eltwise"
	  eltwise_param {
	    operation: PROD
	  }
}​