importmatplotlib.pyplotaspltfromIPythonimportdisplayclassPlotCost(object):""" append train and test cost in event_handle and then call plot. """def__init__(self):self.train_costs=([],[])self.test_costs=([],[])defplot(self):plt.plot(*self.train_costs)plt.plot(*self.test_costs)title=[]iflen(self.train_costs[0])>0:title.append('Train Cost')iflen(self.test_costs[0])>0:title.append('Test Cost')plt.legend(title,loc='upper left')display.clear_output(wait=True)display.display(plt.gcf())plt.gcf().clear()defappend_train_cost(self,step,cost):self.train_costs[0].append(step)self.train_costs[1].append(cost)defappend_test_cost(self,step,cost):self.test_costs[0].append(step)self.test_costs[1].append(cost)defreset(self):self.train_costs=([],[])self.test_costs=([],[])