def log_model_predictions(self, itr, all_logs): # model predictions import matplotlib.pyplot as plt self.fig = plt.figure() # sample actions action_sequence = self.agent.actor.sample_action_sequences(num_sequences=1, horizon=10) #20 reacher action_sequence = action_sequence[0] # calculate and log model prediction error mpe, true_states, pred_states = utils.calculate_mean_prediction_error(self.env, action_sequence, self.agent.dyn_models, self.agent.actor.data_statistics) assert self.params['agent_params']['ob_dim'] == true_states.shape[1] == pred_states.shape[1] ob_dim = self.params['agent_params']['ob_dim'] ob_dim = 2*int(ob_dim/2.0) ## skip last state for plotting when state dim is odd # plot the predictions self.fig.clf() for i in range(ob_dim): plt.subplot(ob_dim/2, 2, i+1) plt.plot(true_states[:,i], 'g') plt.plot(pred_states[:,i], 'r') self.fig.suptitle('MPE: ' + str(mpe)) self.fig.savefig(self.params['logdir']+'/itr_'+str(itr)+'_predictions.png', dpi=200, bbox_inches='tight') # plot all intermediate losses during this iteration all_losses = np.array([log['Training Loss'] for log in all_logs]) np.save(self.params['logdir']+'/itr_'+str(itr)+'_losses.npy', all_losses) self.fig.clf() plt.plot(all_losses) self.fig.savefig(self.params['logdir']+'/itr_'+str(itr)+'_losses.png', dpi=200, bbox_inches='tight')
def log_model_predictions(self, itr, all_logs): # model predictions import matplotlib.pyplot as plt self.fig = plt.figure() # sample actions action_sequence = self.agent.actor.sample_action_sequences( num_sequences=1, horizon=10 ) # 20 reacher action_sequence = action_sequence[0] # calculate and log model prediction error mpe, true_states, pred_states = utils.calculate_mean_prediction_error( self.env, action_sequence, self.agent.dyn_models, self.agent.actor.data_statistics, ) assert ( self.params["agent_params"]["ob_dim"] == true_states.shape[1] == pred_states.shape[1] ) ob_dim = self.params["agent_params"]["ob_dim"] ob_dim = 2 * int( ob_dim / 2.0 ) ## skip last state for plotting when state dim is odd # plot the predictions self.fig.clf() for i in range(ob_dim): plt.subplot(ob_dim // 2, 2, i + 1) plt.plot(true_states[:, i], "g") plt.plot(pred_states[:, i], "r") self.fig.suptitle("MPE: " + str(mpe)) self.fig.savefig( self.params["logdir"] + "/itr_" + str(itr) + "_predictions.png", dpi=200, bbox_inches="tight", ) # plot all intermediate losses during this iteration all_losses = np.array([log["Training Loss"] for log in all_logs]) np.save(self.params["logdir"] + "/itr_" + str(itr) + "_losses.npy", all_losses) self.fig.clf() plt.plot(all_losses) self.fig.savefig( self.params["logdir"] + "/itr_" + str(itr) + "_losses.png", dpi=200, bbox_inches="tight", )