eleurent / rl-agents

Implementations of Reinforcement Learning and Planning algorithms
MIT License
582 stars 152 forks source link

Model Weights and Biases - Histogram Tensorboard #64

Closed skynox03 closed 3 years ago

skynox03 commented 3 years ago

Hi Eluerent,

I have some doubts regarding the analyse of weights and biases:

1) I wanted to know, how can I create a histogram of weights and biases, in order to better visualize the trained model?

2) Is it possible to print the parameters (weights and biases) ?

3) Is there also any other way to analyse weights and biases distribution?

It would be great if you could answer them! :D

eleurent commented 3 years ago

Hi,

  1. You should edit the agent code to include something like this:
    for name, param in self.model.named_parameters():
    self.writer.add_histogram(f'{name}_weight', param.weight.detach().cpu().numpy(), episode)
    self.writer.add_histogram(f'{name}_bias', param.bias.detach().cpu().numpy(), episode)
  2. Same thing, just use print() or logger.info() instead of writer.add_histogram()
  3. Possibly, but not that I know of...
skynox03 commented 3 years ago

Hi,

I tried implementing it but I get this error:

'Parameter' object has no attribute 'weight'

Implemented Function:

def weight_bias(self,episode):
    for name, param in self.value_net.named_parameters():
        self.writer.add_histogram(f'{name}_weight', param.weight.detach().cpu().numpy(), episode)
        self.writer.add_histogram(f'{name}_bias', param.bias.detach().cpu().numpy(), episode)

I have implemented it in "pytorch.py" as a new function, and then called it in "evalution.py" at the end of "def run_episodes" like other tensorboard graphs. But it always gives this error. However i am able to print the weigths using the following function (written similarly in pytorch.py and called in evalution.py at the end of def run_episodes):

def weights(self): for param in self.value_net.parameters(): print(param.data)

I have tried other implementations too, like implementing it directly in evalution.py in the function " def after_all_episodes". But it is always the same problem. Can you maybe comprehend where the error lies?

eleurent commented 3 years ago

yeah I didn't really check my code, it was just to give you the main idea.

param.weight and param.bias do not exist because param already iterates all weights and biases, so simply do:

self.writer.add_histogram(f'{name}_weight', param.data, episode)

instead and it should work I guess

skynox03 commented 3 years ago

Okay, I will try it!