如何在Caffe中定义常量矢量图层?

时间:2016-04-06 02:33:57

标签: caffe

我有10-d标签向量的数据,我想使用caffe模型以10-d输出对这些数据进行回归。但是现在,我只想检查一些输出的丢失(例如,10-d矢量的1,3,4,5,6-d),所以我在最后一个底部定义了一个5-d输出的层。输出层,但我不知道如何获得相应的5-d标签向量groundtruth,我想可能是我可以定义一个常量层来指示我想要的条目。如果您有任何想法,请帮助我。

更新:示例

这是我原来的InnerProduct and Loss图层

layer {
   name: "score"
   type: "InnerProduct"
   bottom: "fc7"
   top: "score"
   inner_product_param {
      num_output: 10
      weight_filler {
         type: "gaussian"
         std: 0.005
      }
      bias_filler {
         type: "constant"
         value: 1
      }
   }
}

layer {
  name: "loss"
  type: "EuclideanLoss"
  bottom: "score"
  bottom: "label"
  top: "loss"
  include {
    phase: TRAIN
  }
}

我更关心$ n_1 $(如1,3,4,5,6)10维输出的条目及其丢失,所以我想获取这些条目的丢失,比如

layer {
   name: "score1"
   type: "InnerProduct"
   bottom: "fc7"
   top: "score1"
   inner_product_param {
      num_output: 5                  # n_1
      weight_filler {
         type: "gaussian"
         std: 0.005
      }
      bias_filler {
         type: "constant"
         value: 1
      }
   }
}

layer {
  name: "loss1"
  type: "EuclideanLoss"
  bottom: "score1"
  bottom: "label"
  top: "loss1"
  include {
    phase: TRAIN
  }
}

如何直接从得分中获得得分1?

1 个答案:

答案 0 :(得分:0)

根据我对你的问题的解释,我认为你想计算输出层的损失w.r.t回归模型的标签。但是你不想把这些标签带到等式中。

如果我的解释是真的,作为一个回归模型,我期待你的新图层与EuclidieanLayer类似。如果是这样,则图层中的caffe_sub函数可以替换为以下代码段。

  int arrayPos[5] = {1,3,4,5,6};
  int count = 5;
  Dtype *newBottom0=(Dtype*)malloc(sizeof(Dtype)*count);
  Dtype *newBottom1=(Dtype*)malloc(sizeof(Dtype)*count);
  for(int varI=0; varI<count; varI++)
  {
    newBottom0[varI] = (Dtype) bottom[0]->cpu_data()[arrayPos[varI]];
    newBottom1[varI] = (Dtype) bottom[1]->cpu_data()[arrayPos[varI]];
  }
  caffe_sub( count, newBottom0, newBottom1, diff_.mutable_cpu_data());
  free(newBottom0);
  free(newBottom1);
相关问题