If you want CSVLogger to append line every epoch

HI, I have refactored the CSVLogger Callback code so that it appends line after each epoch.

The original callback does not write anything until training is finished, but it’s helpful sometimes to be able to access output during long learning process, as well as not to lose it if training was stopped.

class MyCSVLogger(Callback):
run_after=Recorder
"Log the results displayed in `learn.path/fname`"
def __init__(self, fname='history.csv'):
    self.fname= Path(fname)

def read_log(self):
    "Convenience method to quickly access the log."
    return pd.read_csv(self.fname)

def before_fit(self):
    "Prepare file with metric names."
    if hasattr(self, "gather_preds"): return
    self.path.parent.mkdir(parents=True, exist_ok=True)
    self.file = (self.fname).open('a')
    self.file.write(','.join(self.recorder.metric_names) + '\n')
    self.file.close()

def after_epoch(self):
    "white log after every epoch"
    pd.DataFrame([[*self.recorder.log]],index=[0]).to_csv(self.fname ,mode = 'a',header=False,index=False)
2 Likes