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

Dueling #66

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# vscode
.vscode/
wandb/
.DS_Store

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
2 changes: 1 addition & 1 deletion examples/dueling_navix_empty.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def main(argv):

# environment
env = nx.environments.Room(5, 5, 100, observation_fn=nx.observations.categorical)
env = helx.environment.to_helx(env) # type: ignore
env = helx.envs.interop.to_helx(env)

# optimiser
optimiser = optax.rmsprop(
Expand Down
1 change: 0 additions & 1 deletion helx/agents/dqn.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from helx.base.mdp import TERMINATION, Timestep
from helx.base.memory import ReplayBuffer
from helx.base.spaces import Discrete
from helx.base import losses
from .agent import Agent, HParams, Log, AgentState


Expand Down
9 changes: 7 additions & 2 deletions helx/agents/dueling_dqn.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from functools import partial

import jax.numpy as jnp
import optax
Expand All @@ -36,6 +35,9 @@ class DuelingDQNState(DQNState):


class DuelingDQN(DQN):
"""Dueling DQN agent as described in https://arxiv.org/abs/1511.06581
Uses the average operator version to combine the advantage and value functions."""

hparams: DuelingDQNHParams = struct.field(pytree_node=True)
optimiser: optax.GradientTransformation = struct.field(pytree_node=True)
critic: nn.Module = struct.field(pytree_node=True)
Expand All @@ -52,7 +54,10 @@ def create(
backbone,
Split(2),
Parallel((nn.Dense(1), nn.Dense(hparams.action_space.maximum))), # v, A
Merge(partial(jnp.sum, axis=-1)) # q = v + A
Merge(
lambda inputs: inputs[0]
+ (inputs[1] - jnp.mean(inputs[1], axis=-1))
), # q = v + (A - mean(A))
]
)
return DuelingDQN(
Expand Down
6 changes: 3 additions & 3 deletions helx/base/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from functools import partial

from functools import partial
from typing import Callable, Sequence, Tuple

import flax.linen as nn
from jax import Array
import jax.numpy as jnp
import jax.tree_util as jtu
from jax import Array
import flax.linen as nn


class Split(nn.Module):
Expand Down
2 changes: 1 addition & 1 deletion helx/envs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
gymnasium,
gymnax,
interop,
# navix
navix
)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ minigrid
procgen
gymnax
brax
navix
wandb
Loading