void MyAccuracyLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { Dtype RMSE_lin = 0; int count = bottom[0]->count(); // weighting caffe_mul(count, bottom[0]->cpu_data(), bottom[2]->cpu_data(), bottom[0]->mutable_cpu_data()); caffe_mul(count, bottom[1]->cpu_data(), bottom[2]->cpu_data(), bottom[1]->mutable_cpu_data()); // rescaling caffe_exp(count, bottom[0]->cpu_data(), bottom[0]->mutable_cpu_data()); caffe_exp(count, bottom[1]->cpu_data(), bottom[1]->mutable_cpu_data()); // diff caffe_sub( count, bottom[0]->cpu_data(), bottom[1]->cpu_data(), diff_.mutable_cpu_data()); // sum(diff^2) Dtype ss = caffe_cpu_dot(count, diff_.cpu_data(), diff_.cpu_data()); // n Dtype n = caffe_cpu_asum(count, bottom[2]->cpu_data()); n += std::numeric_limits<Dtype>::min(); // sqrt(ss/n) RMSE_lin = sqrt(ss/n); top[0]->mutable_cpu_data()[0] = RMSE_lin; }
void ExpLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { const int count = bottom[0]->count(); const Dtype* bottom_data = bottom[0]->cpu_data(); Dtype* top_data = top[0]->mutable_cpu_data(); if (inner_scale_ == Dtype(1)) { caffe_exp(count, bottom_data, top_data); } else { caffe_cpu_scale(count, inner_scale_, bottom_data, top_data); caffe_exp(count, top_data, top_data); } if (outer_scale_ != Dtype(1)) { caffe_scal(count, outer_scale_, top_data); } }
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; }
void CRFWithLossLayer<Dtype>::Forward_cpu( const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { //Some prerequisites Reshape(bottom, top); if(!for_training_) { // TODO viterbi decode } else { // Get input featue table, data pointers of parameter and auxilary spaces Dtype* ptr_alpha = alpha_.mutable_cpu_data(); Dtype* ptr_beta = beta_.mutable_cpu_data(); Dtype* ptr_epsilon = epsilon_.mutable_cpu_data(); Dtype* ptr_gamma = gamma_.mutable_cpu_data(); Dtype* ptr_buf_state_energy = buf_state_energy_.mutable_cpu_data(); Dtype* ptr_buf_feature = buf_feature_.mutable_cpu_data(); Dtype* ptr_buf_state = buf_state_.mutable_cpu_data(); const Dtype* feature_table = buf_bottom_transposed_.cpu_data(); const Dtype* pi = this->blobs_[0]->cpu_data(); const Dtype* tr = this->blobs_[1]->cpu_data(); const Dtype* mu = this->blobs_[2]->cpu_data(); // Get each block size of the param and buffer for bias computation needed when // doing matrix multiplication // size of feature table int ts = feature_num_ * max_seq_length_; // size of alpha, beta, gamma table int abgs = max_seq_length_ * state_num_; // size of epsilon table int eps = max_seq_length_ * state_num_ * state_num_; for (int i = 0; i < num_; ++i) { // In comments below we assume: // "*" means element-wise multiply; // "dot" means matrix multiply; // "'" means transpose operation; // and all these operations are of LogSumExp version // sl need to be reconsidered int sl = max_seq_length_; // Compute state energy table by lieaner combination of the feature weights at each // state held in mu matrix and the feature_table from the bottom blob: buf_state_energy = feature_table dot mu' caffe_cpu_mm_logP(CblasNoTrans, CblasTrans, max_seq_length_, state_num_, feature_num_, feature_table + i * ts, mu, ptr_buf_state_energy, ptr_buf_feature); // alpha(0) = pi * buf_state_energy(0) caffe_dmul_logP(state_num_, pi, 1, ptr_buf_state_energy, 1, ptr_alpha + i * abgs); // alpha(j) = alpha(j-1) dot T * buf_state_energy(j) for (int j = 1; j < sl; ++j) { caffe_cpu_mm_logP(CblasNoTrans, CblasNoTrans, 1, state_num_, state_num_, alpha_.cpu_data() + i * abgs + (j - 1) * state_num_, tr, ptr_alpha + i * abgs + j * state_num_, ptr_buf_state); caffe_dmul_logP(state_num_, alpha_.cpu_data() + i * abgs + j * state_num_, 1, ptr_buf_state_energy + j * state_num_ , 1, ptr_alpha + i * abgs + j * state_num_); } // Set beta(sl) as all log1 vector caffe_set(state_num_, (Dtype)0., ptr_beta + (sl - 1) * state_num_); // The value of partition function for this sequence(one Z_ for one instance in batch) double Z_; Dtype* ptr_Z = (Dtype*)&Z_; caffe_cpu_mm_logP(CblasNoTrans, CblasTrans, 1, 1, state_num_, alpha_.cpu_data() + i * abgs + (sl - 1) * state_num_, beta_.cpu_data() + i * abgs + (sl - 1) * state_num_, ptr_Z, ptr_buf_state); // Set gamma(sl) as alpha(sl) * beta(sl) caffe_dmul_logP(state_num_, alpha_.cpu_data() + i * abgs + (sl - 1) * state_num_, 1, beta_.cpu_data() + i * abgs + (sl - 1) * state_num_, 1, ptr_gamma + i * abgs + (sl - 1) * state_num_); // Then we iterately flow back to the beginning from the end for (int j = sl - 2; j >= 0; --j) { // At this step we just store beta(j+1) * state_energy(j) in beta(j) as a buffer for epsilon caffe_dmul_logP(state_num_, beta_.cpu_data() + i * abgs + (j + 1) * state_num_, 1, ptr_buf_state_energy + (j + 1) * state_num_, 1, ptr_beta + i * abgs + j * state_num_); // Compute the epsilon(j) = alpha(j)' dot ( beta(j+1) * state_energy(j) ) double buf_ep; Dtype* ptr_buf_ep = (Dtype*)&buf_ep; caffe_cpu_mm_logP(CblasTrans, CblasNoTrans, state_num_, state_num_, 1, alpha_.cpu_data() + i * abgs + (j - 1) * state_num_, beta_.cpu_data() + i * abgs + j * state_num_, ptr_epsilon + i * eps + j * state_num_ * state_num_, ptr_buf_ep); // Continue to finish the computation in beta(j) = tr dot ( beta(j+1) * state_energy(j) ) caffe_cpu_mm_logP(CblasNoTrans, CblasTrans, state_num_, 1, state_num_, tr, beta_.cpu_data() + i * abgs + j * state_num_, ptr_beta + i * abgs + j * state_num_, ptr_buf_state); // Compute the gamma(j) = beta(j) * alpha(j) caffe_dmul_logP(state_num_, alpha_.cpu_data() + j * state_num_ , 1, beta_.cpu_data() + i * abgs + j * state_num_, 1, ptr_gamma + i * abgs + j * state_num_); } //nomalize for (int j = 0; j < sl; ++j) { for (int k = 0; k < state_num_; ++k) *(ptr_gamma + i * abgs + j * max_seq_length_ + k) -= Z_; if ( j >= sl - 1) continue; for (int k = 0; k < state_num_ * state_num_; ++k) *(ptr_epsilon + i * eps + j * max_seq_length_ + k) -= Z_; } // exponential to probability caffe_exp(abgs, gamma_.cpu_data() + i * abgs, ptr_gamma + i * abgs); caffe_exp(eps, epsilon_.cpu_data() + i * eps, ptr_epsilon + i * eps); } } }