Skip to content

Instantly share code, notes, and snippets.

@garland3
Last active May 10, 2021 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garland3/809964c4f176ee16d62927afc0aff5c4 to your computer and use it in GitHub Desktop.
Save garland3/809964c4f176ee16d62927afc0aff5c4 to your computer and use it in GitHub Desktop.
from fastai.vision import *
def try_detach( o):
"""Try to detach and convert to numpy a tensor"""
if issubclass(type(o),torch.Tensor): o= o.detach().cpu().numpy()
if type(o)==np.ndarray: o = float(o)
return o
class MyRecorder(Callback):
"""Pass it values in 'learner' you want to record at each batch
When finished, it puts everything in a pandas data from '.df' that you can use to get the data"""
def __init__(self, things_to_get=None):
self.things_to_get= ['n_iter', 'epoch']
if things_to_get is not None:
self.things_to_get = things_to_get+self.things_to_get
print(self.things_to_get)
def before_fit(self): self.log=[]
def after_step(self):
values_to_log = {a:try_detach(getattr(self.learn,a, 0)) for a in self.things_to_get}
self.log.append(values_to_log)
def create_df(self): self.df =pd.DataFrame(self.log)
def after_fit(self): self.create_df()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment