本文整理汇总了C++中caffe_cpu_axpby函数的典型用法代码示例。如果您正苦于以下问题:C++ caffe_cpu_axpby函数的具体用法?C++ caffe_cpu_axpby怎么用?C++ caffe_cpu_axpby使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了caffe_cpu_axpby函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: memset
void cal_add_item_cpu<float>(const float co, const int N, const int K, const float* W, const float* x1, float* tempX1,
const float* x2, float* tempX2, const float gamma, float* KK) {
memset(tempX1, 0, sizeof(float) * K);
memset(tempX2, 0, sizeof(float) * K);
caffe_cpu_gemv<float>(CblasNoTrans, N, K, 1.0, W, x1, 0.0, tempX1);
caffe_cpu_gemv<float>(CblasNoTrans, N, K, 1.0, W, x2, 0.0, tempX2);
float square_sum = 0;
caffe_cpu_axpby(K, -1.0f, tempX2, 1.0f, tempX1);
caffe_cpu_axpby(K, 1.0f, x1, 0.0f, tempX2);
caffe_cpu_axpby(K, -1.0f, x2, 1.0f, tempX2);
square_sum = caffe_cpu_dot<float>(K, tempX1, tempX1);
//calculate 2 * \gamma * kernel
float kernel = 0.0f;
float tempGamma = gamma / 4.0f;
for(int i = 0; i < 5; ++i) {
float temp = (0.0 - tempGamma) * square_sum;
temp = exp(temp);
kernel += 2 * tempGamma* temp;
tempGamma = tempGamma * 2;
}
/*float kernel = (0.0 - gamma) * square_sum;
kernel = exp(kernel);
kernel = 2 * gamma * kernel;
*/
//calculate KK <- co * kernel * X^T * W + 1 * KK
caffe_cpu_gemm<float>(CblasNoTrans, CblasTrans, 1, N, K, co*kernel, tempX2, W, 0.0, KK);
}
开发者ID:flair2005,项目名称:mmd-caffe,代码行数:32,代码来源:math_functions.cpp
示例2: caffe_cpu_axpby
void TripletClsLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom){
int num = bottom[0]->num();
int channels = bottom[0]->channels();
for (int n = 0; n < num; ++n) {
if (propagate_down[0]) {
if(const_.cpu_data()[n] > Dtype(0.0)){
//derivative in relation to image
caffe_cpu_axpby(channels, Dtype( 2./num), diff_impostor_target.cpu_data() + bottom[0]->offset(n), Dtype(0.0),
bottom[0]->mutable_cpu_diff() + bottom[0]->offset(n));
//derivative in relation to target
caffe_cpu_axpby(channels, Dtype( 2./num), diff_sample_target.cpu_data() + bottom[1]->offset(n), Dtype(0.0),
bottom[1]->mutable_cpu_diff() + bottom[1]->offset(n));
//derivative in relation to impostor
caffe_cpu_axpby(channels, Dtype( -2./num), diff_sample_impostor.cpu_data() + bottom[2]->offset(n), Dtype(0.0),
bottom[2]->mutable_cpu_diff() + bottom[2]->offset(n));
}else{
caffe_set(channels, Dtype(0.0), bottom[0]->mutable_cpu_diff() + bottom[0]->offset(n));
caffe_set(channels, Dtype(0.0), bottom[1]->mutable_cpu_diff() + bottom[1]->offset(n));
caffe_set(channels, Dtype(0.0), bottom[2]->mutable_cpu_diff() + bottom[2]->offset(n));
}
}
}
}
开发者ID:rfsantacruz,项目名称:caffe,代码行数:33,代码来源:triplet_cls_loss_layer.cpp
示例3: caffe_cpu_axpby
void ContrastiveLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
Dtype margin = this->layer_param_.contrastive_loss_param().margin();
for (int i = 0; i < 2; ++i) {
if (propagate_down[i]) {
const Dtype sign = (i == 0) ? 1 : -1;
const Dtype alpha = sign * top[0]->cpu_diff()[0] /
static_cast<Dtype>(bottom[i]->num());
int num = bottom[i]->num();
int channels = bottom[i]->channels();
for (int j = 0; j < num; ++j) {
Dtype* bout = bottom[i]->mutable_cpu_diff();
if (static_cast<int>(bottom[2]->cpu_data()[j])) { // similar pairs
caffe_cpu_axpby(
channels,
alpha,
diff_.cpu_data() + (j*channels),
Dtype(0.0),
bout + (j*channels));
} else { // dissimilar pairs
if ((margin-dist_sq_.cpu_data()[j]) > Dtype(0.0)) {
caffe_cpu_axpby(
channels,
-alpha,
diff_.cpu_data() + (j*channels),
Dtype(0.0),
bout + (j*channels));
} else {
caffe_set(channels, Dtype(0), bout + (j*channels));
}
}
}
}
}
}
开发者ID:52191114,项目名称:caffe,代码行数:35,代码来源:contrastive_loss_layer.cpp
示例4: CHECK
void NesterovSolver<Dtype>::ComputeUpdateValue(int param_id, Dtype rate) {
CHECK(Caffe::root_solver());
const vector<Blob<Dtype>*>& net_params = this->net_->learnable_params();
const vector<float>& net_params_lr = this->net_->params_lr();
Dtype momentum = this->param_.momentum();
Dtype local_rate = rate * net_params_lr[param_id];
switch (Caffe::mode()) {
case Caffe::CPU: {
// save history momentum for stepping back
caffe_copy(net_params[param_id]->count(),
this->history_[param_id]->cpu_data(),
this->update_[param_id]->mutable_cpu_data());
// update history
caffe_cpu_axpby(net_params[param_id]->count(), local_rate,
net_params[param_id]->cpu_diff(), momentum,
this->history_[param_id]->mutable_cpu_data());
// compute update: step back then over step
caffe_cpu_axpby(net_params[param_id]->count(), Dtype(1) + momentum,
this->history_[param_id]->cpu_data(), -momentum,
this->update_[param_id]->mutable_cpu_data());
// copy
caffe_copy(net_params[param_id]->count(),
this->update_[param_id]->cpu_data(),
net_params[param_id]->mutable_cpu_diff());
break;
}
case Caffe::GPU: {
#ifndef CPU_ONLY
// save history momentum for stepping back
caffe_copy(net_params[param_id]->count(),
this->history_[param_id]->gpu_data(),
this->update_[param_id]->mutable_gpu_data());
// update history
caffe_gpu_axpby(net_params[param_id]->count(), local_rate,
net_params[param_id]->gpu_diff(), momentum,
this->history_[param_id]->mutable_gpu_data());
// compute update: step back then over step
caffe_gpu_axpby(net_params[param_id]->count(), Dtype(1) + momentum,
this->history_[param_id]->gpu_data(), -momentum,
this->update_[param_id]->mutable_gpu_data());
// copy
caffe_copy(net_params[param_id]->count(),
this->update_[param_id]->gpu_data(),
net_params[param_id]->mutable_gpu_diff());
#else
NO_GPU;
#endif
break;
}
default:
LOG(FATAL) << "Unknown caffe mode: " << Caffe::mode();
}
}
开发者ID:ALISCIFP,项目名称:caffe-stn,代码行数:59,代码来源:nesterov_solver.cpp
示例5: if
void BinomialDevianceLossLayer<Dtype>::Forward_cpu(
const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
n1 = 0;
n2 = 0;
for (int i = 0; i < bottom[1]->num(); ++i){
if (static_cast<int>(bottom[1]->cpu_data()[i]) == 1){
n1++;
}
else if (static_cast<int>(bottom[1]->cpu_data()[i]) == -1) {
n2++;
}
}
// LOG(INFO) << n1 << " " << n2;
Dtype c = this->layer_param_.binomial_deviance_loss_param().c();
for (int i = 0; i < bottom[1]->num(); ++i){
M_.mutable_cpu_data()[i] = static_cast<int>(bottom[1]->cpu_data()[i]);
if (static_cast<int>(bottom[1]->cpu_data()[i]) == 1)
W_.mutable_cpu_data()[i] = 1.0/n1;
else if (static_cast<int>(bottom[1]->cpu_data()[i]) == -1) {
W_.mutable_cpu_data()[i] = 1.0/n2;
M_.mutable_cpu_data()[i] = -c;
}
else W_.mutable_cpu_data()[i] = 0.0;
}
summer_vec_.Reshape(bottom[0]->num(), 1, 1, 1);
for (int i = 0; i < bottom[0]->num(); ++i){
exp_.mutable_cpu_data()[i] = Dtype(1);
summer_vec_.mutable_cpu_data()[i] = Dtype(1);
}
Dtype alpha = this->layer_param_.binomial_deviance_loss_param().alpha();
Dtype beta = this->layer_param_.binomial_deviance_loss_param().beta();
caffe_cpu_axpby(
bottom[1]->num(),
Dtype(-alpha),
bottom[0]->cpu_data(),
Dtype(alpha * beta),
exp_.mutable_cpu_data());
caffe_mul(bottom[1]->num(), M_.cpu_data(), exp_.cpu_data(), exp_.mutable_cpu_data());
caffe_exp(bottom[1]->num(), exp_.cpu_data(), exp_.mutable_cpu_data());
caffe_cpu_axpby(bottom[1]->num(), Dtype(1), exp_.cpu_data(), Dtype(1), summer_vec_.mutable_cpu_data());
for (int i = 0; i < bottom[0]->num(); ++i){
summer_vec_.mutable_cpu_data()[i] = log(summer_vec_.cpu_data()[i]);
}
//// multiply by elimination array
caffe_mul(bottom[2]->num(), bottom[2]->cpu_data(), summer_vec_.cpu_data(), summer_vec_.mutable_cpu_data());
////
Dtype loss = caffe_cpu_dot(bottom[1]->num(), W_.cpu_data(), summer_vec_.cpu_data());
top[0]->mutable_cpu_data()[0] = loss;
}
开发者ID:madiken,项目名称:caffe-CUDNN-fork,代码行数:58,代码来源:binomial_deviance_loss_layer.cpp
示例6: caffe_cpu_gemm
void TripletLossLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
Dtype eps = this->layer_param_.triplet_loss_param().eps();
Dtype loss = 0;
Dtype margin = this->layer_param_.triplet_loss_param().margin();
caffe_cpu_gemm(CblasNoTrans, CblasTrans, sample_num_, sample_num_,
feature_dim_, Dtype(1), bottom[0]->cpu_data(),
bottom[0]->cpu_data(), Dtype(0),
inner_matrix_.mutable_cpu_data());
for (int i = 0; i < triplet_num_; ++i) {
int a_idx = bottom[1]->cpu_data()[i * 3];
int p_idx = bottom[1]->cpu_data()[i * 3 + 1];
int n_idx = bottom[1]->cpu_data()[i * 3 + 2];
const Dtype *a_pointer = bottom[0]->cpu_data() + a_idx * feature_dim_;
const Dtype *p_pointer = bottom[0]->cpu_data() + p_idx * feature_dim_;
const Dtype *n_pointer = bottom[0]->cpu_data() + n_idx * feature_dim_;
const Dtype *inner_matrix_data = inner_matrix_.cpu_data();
Dtype a_norm = sqrt(inner_matrix_data[a_idx * sample_num_ + a_idx] + eps);
Dtype p_norm = sqrt(inner_matrix_data[p_idx * sample_num_ + p_idx] + eps);
Dtype n_norm = sqrt(inner_matrix_data[n_idx * sample_num_ + n_idx] + eps);
Dtype inner_ap = inner_matrix_data[a_idx * sample_num_ + p_idx];
Dtype inner_an = inner_matrix_data[a_idx * sample_num_ + n_idx];
Dtype dist_ap = inner_ap / (a_norm * p_norm);
Dtype dist_an = inner_an / (a_norm * n_norm);
if (dist_ap - dist_an - margin < 0) {
ComputeDiff_cpu(a_pointer, p_pointer, a_norm,
p_norm, inner_ap, diff_ap_.mutable_cpu_data());
ComputeDiff_cpu(a_pointer, n_pointer, a_norm,
n_norm, inner_an, diff_an_.mutable_cpu_data());
ComputeDiff_cpu(p_pointer, a_pointer, p_norm,
a_norm, inner_ap, diff_pa_.mutable_cpu_data());
ComputeDiff_cpu(n_pointer, a_pointer, n_norm,
a_norm, inner_an, diff_na_.mutable_cpu_data());
caffe_cpu_axpby(feature_dim_, Dtype(1),
diff_an_.cpu_data(), Dtype(1),
bottom_diff_.mutable_cpu_data() + (a_idx * feature_dim_));
caffe_cpu_axpby(feature_dim_, Dtype(-1),
diff_ap_.cpu_data(), Dtype(1),
bottom_diff_.mutable_cpu_data() + (a_idx * feature_dim_));
caffe_cpu_axpby(feature_dim_, Dtype(-1),
diff_pa_.cpu_data(), Dtype(1),
bottom_diff_.mutable_cpu_data() + (p_idx * feature_dim_));
caffe_cpu_axpby(feature_dim_, Dtype(1),
diff_na_.cpu_data(), Dtype(1),
bottom_diff_.mutable_cpu_data() + (n_idx * feature_dim_));
loss += dist_an + margin - dist_ap;
}
}
//Dtype scalar = Dtype(1) / triplet_num_;
Dtype scalar = Dtype(1) / sample_num_;
top[0]->mutable_cpu_data()[0] = loss * scalar;
}
开发者ID:edificewang,项目名称:caffe_facenet,代码行数:55,代码来源:triplet_loss_layer.cpp
示例7: switch
void RMSPropSolver<Dtype>::ComputeUpdateValue(int param_id, Dtype rate) {
const vector<Blob<Dtype>*>& net_params = this->net_->learnable_params();
const vector<float>& net_params_lr = this->net_->params_lr();
// get the learning rate
Dtype delta = this->param_.delta();
Dtype rms_decay = this->param_.rms_decay();
Dtype local_rate = rate * net_params_lr[param_id];
switch (Caffe::mode()) {
case Caffe::CPU:
// compute square of gradient in update
caffe_powx(net_params[param_id]->count(),
net_params[param_id]->cpu_diff(), Dtype(2),
this->update_[param_id]->mutable_cpu_data());
// update history
caffe_cpu_axpby(net_params[param_id] -> count(),
Dtype(1-rms_decay), this->update_[param_id]->cpu_data(),
rms_decay, this->history_[param_id]-> mutable_cpu_data());
// prepare update
caffe_powx(net_params[param_id]->count(),
this->history_[param_id]->cpu_data(), Dtype(0.5),
this->update_[param_id]->mutable_cpu_data());
caffe_add_scalar(net_params[param_id]->count(),
delta, this->update_[param_id]->mutable_cpu_data());
caffe_div(net_params[param_id]->count(),
net_params[param_id]->cpu_diff(), this->update_[param_id]->cpu_data(),
this->update_[param_id]->mutable_cpu_data());
// scale and copy
caffe_cpu_axpby(net_params[param_id]->count(), local_rate,
this->update_[param_id]->cpu_data(), Dtype(0),
net_params[param_id]->mutable_cpu_diff());
break;
case Caffe::GPU:
#ifndef CPU_ONLY
rmsprop_update_gpu(net_params[param_id]->count(),
net_params[param_id]->mutable_gpu_diff(),
this->history_[param_id]->mutable_gpu_data(),
rms_decay, delta, local_rate);
#else
NO_GPU;
#endif
break;
default:
LOG(FATAL) << "Unknown caffe mode: " << Caffe::mode();
}
}
开发者ID:0hm,项目名称:caffe,代码行数:52,代码来源:rmsprop_solver.cpp
示例8: caffe_cpu_axpby
void TripletLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
//Dtype margin = this->layer_param_.contrastive_loss_param().margin();
const Dtype* sampleW = bottom[3]->cpu_data();
for (int i = 0; i < 3; ++i) {
if (propagate_down[i]) {
const Dtype sign = (i < 2) ? -1 : 1;
const Dtype alpha = sign * top[0]->cpu_diff()[0] /
static_cast<Dtype>(bottom[i]->num());
int num = bottom[i]->num();
int channels = bottom[i]->channels();
for (int j = 0; j < num; ++j) {
Dtype* bout = bottom[i]->mutable_cpu_diff();
if (i==0) { // a
//if(dist_binary_.cpu_data()[j]>Dtype(0)){
caffe_cpu_axpby(
channels,
alpha*sampleW[j],
diff_pn_.cpu_data() + (j*channels),
Dtype(0.0),
bout + (j*channels));
//}else{
// caffe_set(channels, Dtype(0), bout + (j*channels));
//}
} else if (i==1) { // p
//if(dist_binary_.cpu_data()[j]>Dtype(0)){
caffe_cpu_axpby(
channels,
alpha*sampleW[j],
diff_ap_.cpu_data() + (j*channels),
Dtype(0.0),
bout + (j*channels));
//}else{
// caffe_set(channels, Dtype(0), bout + (j*channels));
//}
} else if (i==2) { // n
//if(dist_binary_.cpu_data()[j]>Dtype(0)){
caffe_cpu_axpby(
channels,
alpha*sampleW[j],
diff_an_.cpu_data() + (j*channels),
Dtype(0.0),
bout + (j*channels));
//}else{
// caffe_set(channels, Dtype(0), bout + (j*channels));
//}
}
} // for num
} //if propagate_down[i]
} //for i
}
开发者ID:xuqiantong,项目名称:MyCaffe,代码行数:51,代码来源:triplet_loss_layer.cpp
示例9: pow
void CosineSimilarityLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
for (int i = 0; i < 2; ++i) {
if (propagate_down[i]) {
int num = bottom[i]->num();
int channels = bottom[i]->channels();
for (int j = 0; j < num; ++j) {
Dtype denominator = pow(xx_.cpu_data()[j]*yy_.cpu_data()[j], 0.5);
Dtype* bout = bottom[i]->mutable_cpu_diff();
if (i == 0){
caffe_cpu_axpby(
channels,
Dtype(-xy_.cpu_data()[j] /(denominator* xx_.cpu_data()[j])),
bottom[0]->cpu_data() + (j*channels),
Dtype(0.0),
bout + (j*channels));
caffe_cpu_axpby(
channels,
Dtype(1.0/denominator),
bottom[1]->cpu_data() + (j*channels),
Dtype(1.0),
bout + (j*channels));
} else if (i == 1){
caffe_cpu_axpby(
channels,
Dtype(-xy_.cpu_data()[j] /(denominator*yy_.cpu_data()[j])),
bottom[1]->cpu_data() + (j*channels),
Dtype(0.0),
bout + (j*channels));
caffe_cpu_axpby(
channels,
Dtype(1.0/denominator),
bottom[0]->cpu_data() + (j*channels),
Dtype(1.0),
bout + (j*channels));
}
caffe_cpu_axpby(
channels,
Dtype(0.0),
bout + (j*channels),
top[0]->cpu_diff()[j],
bout + (j*channels)
);
}
}
}
}
开发者ID:madiken,项目名称:caffe-CUDNN-fork,代码行数:50,代码来源:cosine_similarity_layer.cpp
示例10: srand
float cal_gamma_cpu<float>(const int K, const int S, const int N, const float* W, const float* X, float* tempX1, float* tempX2) {
srand((unsigned int)time(0));
float gamma = 0;
float temp = 0;
//random sample S pair to calculate gamma
for(int i = 0; i < S; ++i) {
memset(tempX1, 0, sizeof(float) * K);
memset(tempX2, 0, sizeof(float) * K);
int s1 = rand() % S;
int s2 = rand() % S;
s2 = (s1 != s2) ? s2 : (s2 + 1) % S;
const float* x1 = X + s1 * K;
const float* x2 = X + s2 * K;
caffe_cpu_gemv<float>(CblasNoTrans, N, K, 1.0, W, x1, 0.0, tempX1);
caffe_cpu_gemv<float>(CblasNoTrans, N, K, 1.0, W, x2, 0.0, tempX2);
//caffe_cpu_sub<float>(K, tempX1, tempX2, tempX2);
caffe_cpu_axpby(K, 1.0f, tempX1, -1.0f, tempX2);
temp = caffe_cpu_dot<float>(K, tempX2, tempX2);
gamma += temp;
}
return S / gamma;
}
开发者ID:flair2005,项目名称:mmd-caffe,代码行数:26,代码来源:math_functions.cpp
示例11: switch
void SGDSolver<Dtype>::ComputeUpdateValue(int param_id, Dtype rate) {
const vector<Blob<Dtype>*>& net_params = this->net_->learnable_params();
const vector<float>& net_params_lr = this->net_->params_lr();
Dtype momentum = this->param_.momentum();
Dtype local_rate = rate * net_params_lr[param_id];
// Compute the update to history, then copy it to the parameter diff.
switch (Caffe::mode()) {
case Caffe::CPU: {
caffe_cpu_axpby(net_params[param_id]->count(), local_rate,
net_params[param_id]->cpu_diff(), momentum,
history_[param_id]->mutable_cpu_data());
caffe_copy(net_params[param_id]->count(),
history_[param_id]->cpu_data(),
net_params[param_id]->mutable_cpu_diff());
break;
}
case Caffe::GPU: {
#ifndef CPU_ONLY
sgd_update_gpu(net_params[param_id]->count(),
net_params[param_id]->mutable_gpu_diff(),
history_[param_id]->mutable_gpu_data(),
momentum, local_rate);
#else
NO_GPU;
#endif
break;
}
default:
LOG(FATAL) << "Unknown caffe mode: " << Caffe::mode();
}
}
开发者ID:fossabot,项目名称:caffe,代码行数:31,代码来源:sgd_solver.cpp
示例12: caffe_cpu_axpby
void ContrastiveLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
Dtype margin = this->layer_param_.contrastive_loss_param().margin();
bool legacy_version =
this->layer_param_.contrastive_loss_param().legacy_version();
for (int i = 0; i < 2; ++i) {
if (propagate_down[i]) {
const Dtype sign = (i == 0) ? 1 : -1;
const Dtype alpha = sign * top[0]->cpu_diff()[0] /
static_cast<Dtype>(bottom[i]->num());
int num = bottom[i]->num();
int channels = bottom[i]->channels();
for (int j = 0; j < num; ++j) {
Dtype* bout = bottom[i]->mutable_cpu_diff();
if (static_cast<int>(bottom[2]->cpu_data()[j]) == static_cast<int>(bottom[3]->cpu_data()[j])) { // similar pairs
caffe_cpu_axpby(
channels,
alpha,
diff_.cpu_data() + (j*channels),
Dtype(0.0),
bout + (j*channels));
} else { // dissimilar pairs
Dtype mdist(0.0);
Dtype beta(0.0);
if (legacy_version) {
mdist = margin - dist_sq_.cpu_data()[j];
beta = -alpha;
} else {
Dtype dist = sqrt(dist_sq_.cpu_data()[j]);
mdist = margin - dist;
beta = -alpha * mdist / (dist + Dtype(1e-4));
}
if (mdist > Dtype(0.0)) {
caffe_cpu_axpby(
channels,
beta,
diff_.cpu_data() + (j*channels),
Dtype(0.0),
bout + (j*channels));
} else {
caffe_set(channels, Dtype(0), bout + (j*channels));
}
}
}
}
}
}
开发者ID:2php,项目名称:caffe-windows,代码行数:47,代码来源:contrastive_loss_layer.cpp
示例13: caffe_cpu_axpby
void EuclideanLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
int count = (*bottom)[0]->count();
int num = (*bottom)[0]->num();
// Compute the gradient
caffe_cpu_axpby(count, Dtype(1) / num, difference_.cpu_data(), Dtype(0),
(*bottom)[0]->mutable_cpu_diff());
}
开发者ID:lipengyuMachineLearner,项目名称:caffe,代码行数:8,代码来源:loss_layer.cpp
示例14: GetLearningRate
void SGDFeedbackSolver<Dtype>::ComputeUpdateValue() {
vector<shared_ptr<Blob<Dtype> > >& net_params = this->net_->params();
vector<float>& net_params_lr = this->net_->params_lr();
vector<float>& net_params_weight_decay = this->net_->params_weight_decay();
// get the learning rate
Dtype rate = GetLearningRate();
if (this->param_.display() && this->iter_ % this->param_.display() == 0) {
LOG(INFO) << "Iteration " << this->iter_ << ", lr = " << rate;
}
Dtype momentum = this->param_.momentum();
Dtype weight_decay = this->param_.weight_decay();
switch (Caffe::mode()) {
case Caffe::CPU:
for (int param_id = 0; param_id < net_params.size(); ++param_id) {
// Compute the value to history, and then copy them to the blob's diff.
Dtype local_rate = rate * net_params_lr[param_id];
Dtype local_decay = weight_decay * net_params_weight_decay[param_id];
caffe_cpu_axpby(net_params[param_id]->count(), local_rate,
net_params[param_id]->cpu_diff(), momentum,
history_[param_id]->mutable_cpu_data());
if (local_decay) {
// add weight decay
caffe_axpy(net_params[param_id]->count(),
local_decay * local_rate,
net_params[param_id]->cpu_data(),
history_[param_id]->mutable_cpu_data());
}
// copy
caffe_copy(net_params[param_id]->count(),
history_[param_id]->cpu_data(),
net_params[param_id]->mutable_cpu_diff());
}
break;
case Caffe::GPU:
for (int param_id = 0; param_id < net_params.size(); ++param_id) {
// Compute the value to history, and then copy them to the blob's diff.
Dtype local_rate = rate * net_params_lr[param_id];
Dtype local_decay = weight_decay * net_params_weight_decay[param_id];
caffe_gpu_axpby(net_params[param_id]->count(), local_rate,
net_params[param_id]->gpu_diff(), momentum,
history_[param_id]->mutable_gpu_data());
if (local_decay) {
// add weight decay
caffe_gpu_axpy(net_params[param_id]->count(),
local_decay * local_rate,
net_params[param_id]->gpu_data(),
history_[param_id]->mutable_gpu_data());
}
// copy
caffe_gpu_copy(net_params[param_id]->count(),
history_[param_id]->gpu_data(),
net_params[param_id]->mutable_gpu_diff());
}
break;
default:
LOG(FATAL) << "Unknown caffe mode: " << Caffe::mode();
}
}
开发者ID:liuxianming,项目名称:caffe_feedback,代码行数:58,代码来源:feedback_solver.cpp
示例15: caffe_cpu_axpby
void VoxelCustomLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
caffe_cpu_axpby(
(*bottom)[0]->count(), // count
Dtype(1) / (*bottom)[0]->num(), // alpha
diff_.cpu_data(), // a
Dtype(0), // beta
(*bottom)[0]->mutable_cpu_diff()); // b
}
开发者ID:vra,项目名称:C3D,代码行数:9,代码来源:voxel_custom_loss_layer.cpp
示例16: caffe_cpu_scale
void TripletLossLayer<Dtype>::ComputeDiff_cpu(const Dtype *x_1,
const Dtype *x_2, const Dtype x_1_norm, const Dtype x_2_norm,
const Dtype inner_val, Dtype *x_1_diff) {
caffe_cpu_scale(feature_dim_, Dtype(1) / (x_1_norm * x_2_norm),
x_2, x_1_diff);
Dtype x_1_norm_cubic = x_1_norm * x_1_norm * x_1_norm;
caffe_cpu_axpby(feature_dim_, -inner_val / (x_1_norm_cubic * x_2_norm),
x_1, Dtype(1), x_1_diff);
}
开发者ID:edificewang,项目名称:caffe_facenet,代码行数:9,代码来源:triplet_loss_layer.cpp
示例17: caffe_set
void PowerLayer<Dtype>::Backward_cpu(
const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,
const vector<Blob<Dtype>*>& bottom) {
if (propagate_down[0]) {
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
const int count = bottom[0]->count();
const Dtype* top_diff = top[0]->cpu_diff();
if (diff_scale_ == Dtype(0) || power_ == Dtype(1)) {
caffe_set(count, diff_scale_, bottom_diff);
} else {
const Dtype* bottom_data = bottom[0]->cpu_data();
// Compute dy/dx = scale * power * (shift + scale * x)^(power - 1)
// = diff_scale * y / (shift + scale * x)
if (power_ == Dtype(2)) {
// Special case for y = (shift + scale * x)^2
// -> dy/dx = 2 * scale * (shift + scale * x)
// = diff_scale * shift + diff_scale * scale * x
caffe_cpu_axpby(
count,
diff_scale_ * scale_,
bottom_data,
Dtype(0),
bottom_diff);
if (shift_ != Dtype(0)) {
caffe_add_scalar(count, diff_scale_ * shift_, bottom_diff);
}
} else if (shift_ == Dtype(0)) {
// Special case for y = (scale * x)^power
// -> dy/dx = scale * power * (scale * x)^(power - 1)
// = scale * power * (scale * x)^power * (scale * x)^(-1)
// = power * y / x
const Dtype* top_data = top[0]->cpu_data();
caffe_div(count, top_data, bottom_data, bottom_diff);
caffe_scal(count, power_, bottom_diff);
} else {
caffe_copy(count, bottom_data, bottom_diff);
if (scale_ != Dtype(1)) {
caffe_scal(count, scale_, bottom_diff);
}
if (shift_ != Dtype(0)) {
caffe_add_scalar(count, shift_, bottom_diff);
}
const Dtype* top_data = top[0]->cpu_data();
caffe_div<Dtype>(count, top_data, bottom_diff, bottom_diff);
if (diff_scale_ != Dtype(1)) {
caffe_scal(count, diff_scale_, bottom_diff);
}
}
}
if (diff_scale_ != Dtype(0)) {
caffe_mul(count, top_diff, bottom_diff, bottom_diff);
}
}
}
开发者ID:rickyHong,项目名称:CaffeForOpenCL,代码行数:56,代码来源:power_layer.cpp
示例18: caffe_cpu_axpby
void TripletLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
// const Dtype* sampleW = bottom[3]->cpu_data();
//for 3 bottom data
for(int i = 0; i < 3; ++i)
{
if(propagate_down[i]){
const Dtype sign = (i < 2) ? -1: 1;
const Dtype alpha = sign * top[0]->cpu_diff()[0] / static_cast<Dtype>(bottom[i]->num());
int num = bottom[i]->num();
int channels = bottom[i]->channels();
for(int j = 0; j < num; ++j)//for each image
{
Dtype* bout = bottom[i]->mutable_cpu_diff();
if(i == 0){//anchor
caffe_cpu_axpby(
channels,
alpha,// * sampleW[j],
diff_pn_.cpu_data() + (j * channels),
Dtype(0.0),
bout + (j*channels)
);
}else if(i == 1) { //positive
caffe_cpu_axpby(
channels,
alpha, //*sampleW[j],
diff_ap_.cpu_data() + (j*channels),
Dtype(0.0),
bout + (j*channels)
);
}else if(i==2){ //negative
caffe_cpu_axpby(
channels,
alpha, // * sampleW[j],
diff_an_.cpu_data() + (j*channels),
Dtype(0.0),
bout + (j*channels)
);
}
}
}
}
}
开发者ID:bagnikita,项目名称:caffe,代码行数:43,代码来源:triplet_loss_layer.cpp
示例19: LOG
void EuclideanLossWithIgnoreLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
int count = bottom[0]->count();
//Number of elements in the batch
int bCount = bottom[0]->count(1, bottom[0]->num_axes());
int lbCount = bottom[1]->count(1, bottom[1]->num_axes());
int N = bottom[0]->shape(0);
if (lCount_ == Dtype(0)){
LOG(INFO) << "EuclideanLossWithIgnore was Silent for this batch";
return;
}
//Compute the gradients
for (int i = 0; i < 2; ++i) {
const Dtype sign = (i == 0) ? 1 : -1;
const Dtype alpha = sign * top[0]->cpu_diff()[0] / lCount_;
Dtype Z;
const Dtype* botZData = bottom[nc_]->cpu_data();
Dtype* botDiff = bottom[i]->mutable_cpu_diff();
const Dtype* labels = bottom[1]->cpu_data();
Dtype* diff = diff_.mutable_cpu_data();
const Dtype* diffC = diff_.cpu_data();
if (propagate_down[i]) {
for (int n=0; n < N; ++n){
if (labels[bCount] == Dtype(1)){
if (is_normalize_){
Z = caffe_cpu_dot(bCount, botZData, botZData);
if (Z>0){
caffe_scal(count, Z, diff);
}
}
caffe_cpu_axpby(
bCount, // count
alpha, // alpha
diffC, // a
Dtype(0), // beta
botDiff); // b
}
labels += lbCount;
diff += bCount;
diffC += bCount;
if (nc_==0){
botZData += bCount;
}else{
botZData += lbCount;
}
if (i==0){
botDiff += bCount;
}else {
botDiff += lbCount;
}
}
}
}
}
开发者ID:castigliano,项目名称:caffe,代码行数:55,代码来源:euclidean_loss_with_ignore_layer.cpp
示例20: caffe_copy
void SumLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
const Dtype* bottom_data = bottom[0]->cpu_data();
Dtype* top_data = top[0]->mutable_cpu_data();
caffe_copy(bottom[0]->count(), bottom_data, top_data);
for (int i = 1; i < bottom.size(); ++i) {
const Dtype* bottom_data_i = bottom[i]->cpu_data();
caffe_cpu_axpby(bottom[0]->count(), Dtype(1.0), bottom_data_i,
Dtype(1.0), top_data);
}
}
开发者ID:bharath272,项目名称:caffe-sds,代码行数:11,代码来源:sum_layer.cpp
注:本文中的caffe_cpu_axpby函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论