Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible bug: state visitation frequency #1

Open
magnusja opened this issue Nov 24, 2017 · 4 comments
Open

Possible bug: state visitation frequency #1

magnusja opened this issue Nov 24, 2017 · 4 comments

Comments

@magnusja
Copy link

magnusja commented Nov 24, 2017

Hey there,

I am not a 100% sure but I feel like there is something wrong with calculating the state visitation frequency (https://github.com/stormmax/irl-imitation/blob/master/deep_maxent_irl.py#L93).

You iterate over all the states and calculate the frequency for every timestep then.

for s in range(N_STATES):
    for t in range(T-1):
      if deterministic:
        mu[s, t+1] = sum([mu[pre_s, t]*P_a[pre_s, s, int(policy[pre_s])] for pre_s in range(N_STATES)])
      else:
mu[s, t+1] = sum([sum([mu[pre_s, t]*P_a[pre_s, s, a1]*policy[pre_s, a1] for a1 in range(N_ACTIONS)]) for pre_s in range(N_STATES)])

In my opinion the loops should be switched:

for t in range(T-1):
    for s in range(N_STATES):
      if deterministic:
        mu[s, t+1] = sum([mu[pre_s, t]*P_a[pre_s, s, int(policy[pre_s])] for pre_s in range(N_STATES)])
      else:
mu[s, t+1] = sum([sum([mu[pre_s, t]*P_a[pre_s, s, a1]*policy[pre_s, a1] for a1 in range(N_ACTIONS)]) for pre_s in range(N_STATES)])

Because the visitation frequency of timestep t+1 depends on all the state frequencies of timestamp t. This also reflects the formular from the original MaxEnt paper (Ziebart et al, 2008):
image

Unfortunately if I change the loop heads, the reward is not recovered correctly anymore. Do you have any hints on this?

@Zhousiyuhit
Copy link

Hello, I have encountered the same question as you. Have you solved it?

@magnusja
Copy link
Author

magnusja commented Sep 18, 2019

Hello there,

please refer to my fork of this repository, which not only fixes that but also implements highly efficient methods for calculating the state visitation frequency, in tf but also vectorized using numpy. The code in this repository is completely unusable when you need more states than the 5 by 5 example grid ;D

The trick to fix the bug is essentially to take the average over timestamps. This is not mentioned anywhere except this video: https://youtu.be/d9DlQSJQAoI?t=973 (watch for a minute or so then Chelsea mentions that the calculation is missing an average).

See this note of mine as well:
https://github.com/magnusja/irl-imitation/blob/master/deep_maxent_irl.py#L340-L348

Let me know if you have further questions.

@Zhousiyuhit
Copy link

Zhousiyuhit commented Sep 19, 2019 via email

@Zhousiyuhit
Copy link

I modified the code based on tensorflow 2.0, and now there are no other problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants