Reranker Framework (ReFr)
Reranking framework for structure prediction and discriminative language modeling
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
training-time.H
Go to the documentation of this file.
1 // Copyright 2012, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 // -----------------------------------------------------------------------------
30 //
31 //
37 
38 #ifndef RERANKER_TRAINING_TIME_H_
39 #define RERANKER_TRAINING_TIME_H_
40 
41 #include <ctime>
42 #include <iostream>
43 #include <string>
44 #include <sstream>
45 
46 namespace reranker {
47 
48 using std::string;
49 using std::ostream;
50 
56 class Time {
57  public:
59  Time() : epoch_(-1), index_(-1), absolute_index_(-1),
60  absolute_time_(clock()), time_since_last_epoch_(absolute_time_) { }
66  Time(int epoch, int index, int absolute_index) :
67  epoch_(epoch),
68  index_(index),
69  absolute_index_(absolute_index),
70  absolute_time_(clock()), time_since_last_epoch_(absolute_time_) { }
71 
72  // accessors
74  int epoch() const { return epoch_; }
77  int index() const { return index_; }
79  int absolute_index() const { return absolute_index_; }
80 
81  double absolute_seconds() const {
82  clock_t diff_time = clock() - absolute_time_;
83  return ((double)diff_time) / CLOCKS_PER_SEC;
84  }
85  double seconds_since_last_epoch() const {
86  clock_t diff_time = clock() - time_since_last_epoch_;
87  return ((double)diff_time) / CLOCKS_PER_SEC;
88  }
89 
90  friend ostream &operator<<(ostream &os, const Time &time) {
91  return os << time.epoch() << "," << time.index() << ";"
92  << time.absolute_index();
93  }
94 
95  string to_string() const {
96  std::ostringstream oss;
97  oss << *this;
98  return oss.str();
99  }
100 
101  // mutators
104  void Tick() {
105  index_++;
106  absolute_index_++;
107  }
109  void NewEpoch() {
110  epoch_++;
111  index_ = -1;
112  time_since_last_epoch_ = clock();
113  }
114  private:
115  int epoch_;
116  int index_;
117  int absolute_index_;
118  clock_t absolute_time_;
119  clock_t time_since_last_epoch_;
120 };
121 
122 } // namespace reranker
123 
124 #endif
void NewEpoch()
Increments the epoch counter.
int absolute_index() const
Returns the total number of training examples seen in all epochs.
Definition: training-time.H:79
A simple class to hold the three notions of time during training: the current epoch, the current time index within the current epoch, and the absolute time index.
Definition: training-time.H:56
Time()
Constructs a new time instance, ready for its indices to be incremented.
Definition: training-time.H:59
friend ostream & operator<<(ostream &os, const Time &time)
Definition: training-time.H:90
double absolute_seconds() const
Definition: training-time.H:81
double seconds_since_last_epoch() const
Definition: training-time.H:85
Time(int epoch, int index, int absolute_index)
Constructs a new time instance with the specified time indices.
Definition: training-time.H:66
void Tick()
Increments both the time index for the current epoch and the absolute time index. ...
int epoch() const
Returns the index of the current epoch.
Definition: training-time.H:74
int index() const
Returns the index of the current training example within the current epoch.
Definition: training-time.H:77
string to_string() const
Definition: training-time.H:95