如何在 Caffe 中定义常量向量层?
How can I define a constant vector layer in Caffe?
我有一个带有 10 维标签向量的数据,我想使用 caffe 模型对这些具有 10 维输出的数据进行回归。但是现在,我只想检查一些输出的损失(例如,10-d 向量的 1、3、4、5、6-d),所以我在最后一个底部定义了一个具有 5-d 输出的层输出层,但我不知道如何获得相应的 5-d 标签向量 groundtruth,我想也许我可以定义一个常量层来指示我想要获取哪些条目。如果您有任何想法,请帮助我。
更新:例子
这是我原来的 InnerProduct 和 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
}
}
我比较关心10维输出的$n_1$(like 1,3,4,5,6)个entry和它们的loss,所以想取这些entry的loss,喜欢
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
}
}
如何直接从 score 中得到 score1?
根据我对你的问题的解释,我认为你想计算输出层的损失 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);
我有一个带有 10 维标签向量的数据,我想使用 caffe 模型对这些具有 10 维输出的数据进行回归。但是现在,我只想检查一些输出的损失(例如,10-d 向量的 1、3、4、5、6-d),所以我在最后一个底部定义了一个具有 5-d 输出的层输出层,但我不知道如何获得相应的 5-d 标签向量 groundtruth,我想也许我可以定义一个常量层来指示我想要获取哪些条目。如果您有任何想法,请帮助我。
更新:例子
这是我原来的 InnerProduct 和 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
}
}
我比较关心10维输出的$n_1$(like 1,3,4,5,6)个entry和它们的loss,所以想取这些entry的loss,喜欢
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
}
}
如何直接从 score 中得到 score1?
根据我对你的问题的解释,我认为你想计算输出层的损失 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);