Skip to content

Commit

Permalink
Display averaged loss over the last several iterations
Browse files Browse the repository at this point in the history
  • Loading branch information
qipeng authored and sguada committed Dec 22, 2014
1 parent 4926615 commit 9e756bf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/caffe/proto/caffe.proto
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ message NetParameter {
// NOTE
// Update the next available ID when you add a new SolverParameter field.
//
// SolverParameter next available ID: 33 (last added: test_initialization)
// SolverParameter next available ID: 34 (last added: average_loss)
message SolverParameter {
//////////////////////////////////////////////////////////////////////////////
// Specifying the train and test networks
Expand Down Expand Up @@ -113,6 +113,8 @@ message SolverParameter {
// the number of iterations between displaying info. If display = 0, no info
// will be displayed.
optional int32 display = 6;
// Display the cost averaged over the last average_cost iterations
optional int32 average_loss = 33 [default = 1];
optional int32 max_iter = 7; // the maximum number of iterations
optional string lr_policy = 8; // The learning rate decay policy.
optional float gamma = 9; // The parameter to compute the learning rate.
Expand Down
18 changes: 17 additions & 1 deletion src/caffe/solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ void Solver<Dtype>::Solve(const char* resume_file) {
// resume_file above.
const int start_iter = iter_;

int average_loss = this->param_.average_loss();

CHECK_GE(average_loss, 1) << "average_cost should be non-negative.";

vector<Dtype> losses;
Dtype smoothed_loss = 0;

// For a network that is trained by the solver, no bottom or top vecs
// should be given, and we will just provide dummy vecs.
vector<Blob<Dtype>*> bottom_vec;
Expand All @@ -187,8 +194,17 @@ void Solver<Dtype>::Solve(const char* resume_file) {
const bool display = param_.display() && iter_ % param_.display() == 0;
net_->set_debug_info(display && param_.debug_info());
Dtype loss = net_->ForwardBackward(bottom_vec);
if (losses.size() < average_loss) {
losses.push_back(loss);
int size = losses.size();
smoothed_loss = (smoothed_loss * (size - 1) + loss) / size;
} else {
int idx = (iter_ - start_iter) % average_loss;
smoothed_loss += (loss - losses[idx]) / average_loss;
losses[idx] = loss;
}
if (display) {
LOG(INFO) << "Iteration " << iter_ << ", loss = " << loss;
LOG(INFO) << "Iteration " << iter_ << ", loss = " << smoothed_loss;
const vector<Blob<Dtype>*>& result = net_->output_blobs();
int score_index = 0;
for (int j = 0; j < result.size(); ++j) {
Expand Down

0 comments on commit 9e756bf

Please sign in to comment.