This repository was archived by the owner on Nov 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Add FullRecorder for logging an entire optimization #196
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
package optimize | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"math" | ||
|
@@ -104,3 +105,72 @@ func (p *Printer) Record(loc *Location, op Operation, stats *Stats) error { | |
p.lastValue = time.Now() | ||
return nil | ||
} | ||
|
||
// Full Recorder records all of the evaluations that occur during an optimization | ||
// run. If Operation is an Evaulation, FullRecorder records the x location and | ||
// the corresponding field of Location. If Operation is a MajorIteration, or | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. field or fields |
||
// PostIteration, FullRecorder records the full value of the Location struct. | ||
// Otherwise, FullRecorder records the operation type. | ||
type FullRecorder struct { | ||
Writer io.Writer | ||
} | ||
|
||
func (f FullRecorder) Init() error { | ||
return nil | ||
} | ||
|
||
func (f FullRecorder) Record(loc *Location, op Operation, stats *Stats) error { | ||
r := RecordLocation{Op: op.String()} | ||
switch { | ||
case op.isEvaluation(): | ||
r.Loc.X = loc.X | ||
switch op { | ||
default: | ||
panic("optimize: unknown evaluation operation") | ||
case FuncEvaluation: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Evaluation operations can be ORed together which this switch does not take into account. |
||
r.Loc.F = loc.F | ||
case GradEvaluation: | ||
r.Loc.Gradient = loc.Gradient | ||
case HessEvaluation: | ||
r.Loc.Hessian = loc.Hessian | ||
} | ||
case op == MajorIteration || op == PostIteration: | ||
r.Loc = *loc | ||
} | ||
if op.isEvaluation() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A left-over before the above switch was introduced? |
||
|
||
} else if op == MajorIteration { | ||
|
||
} | ||
b, err := json.MarshalIndent(r, "", "\t") | ||
if err != nil { | ||
return nil | ||
} | ||
_, err = f.Writer.Write(b) | ||
return err | ||
} | ||
|
||
// Read reads the stream written to by FullRecorder, and returns the history of | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// the optimization run. | ||
func (f FullRecorder) Read(r io.Reader) ([]RecordLocation, error) { | ||
d := json.NewDecoder(r) | ||
var records []RecordLocation | ||
var err error | ||
for { | ||
var r RecordLocation | ||
err = d.Decode(&r) | ||
if err != nil { | ||
break | ||
} | ||
records = append(records, r) | ||
} | ||
if err == io.EOF { | ||
return records, nil | ||
} | ||
return records, err | ||
} | ||
|
||
type RecordLocation struct { | ||
Op string | ||
Loc Location | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have a justification why this should be in optimize? The functionality seems quite specific to fit general needs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems specific? In what way? It seems like a natural counterpart to Printer. Printer prints to a writer to track optimization, this saves the optimization run to
json
for retrospectionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about what is being recorded and into what format. I can imagine that either will easily not fit the needs and the user will write it's own recorder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FullRecorder
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand your point about things being Ored and this not handling that properly. Do you have other suggestions for making it more general? It seems reasonable to me to provide some built-in way to store the history of the optimization for retrospection (in addition to the human-friendly printing we have now).