survival-rl 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- survival_rl-0.0.1/.gitignore +220 -0
- survival_rl-0.0.1/LICENSE +21 -0
- survival_rl-0.0.1/PKG-INFO +72 -0
- survival_rl-0.0.1/README.md +26 -0
- survival_rl-0.0.1/pyproject.toml +69 -0
- survival_rl-0.0.1/survival_rl/__init__.py +7 -0
- survival_rl-0.0.1/survival_rl/survival_rl.py +135 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
*.sh
|
|
2
|
+
|
|
3
|
+
# Byte-compiled / optimized / DLL files
|
|
4
|
+
__pycache__/
|
|
5
|
+
*.py[codz]
|
|
6
|
+
*$py.class
|
|
7
|
+
|
|
8
|
+
# C extensions
|
|
9
|
+
*.so
|
|
10
|
+
|
|
11
|
+
# Distribution / packaging
|
|
12
|
+
.Python
|
|
13
|
+
build/
|
|
14
|
+
develop-eggs/
|
|
15
|
+
dist/
|
|
16
|
+
downloads/
|
|
17
|
+
eggs/
|
|
18
|
+
.eggs/
|
|
19
|
+
lib/
|
|
20
|
+
lib64/
|
|
21
|
+
parts/
|
|
22
|
+
sdist/
|
|
23
|
+
var/
|
|
24
|
+
wheels/
|
|
25
|
+
share/python-wheels/
|
|
26
|
+
*.egg-info/
|
|
27
|
+
.installed.cfg
|
|
28
|
+
*.egg
|
|
29
|
+
MANIFEST
|
|
30
|
+
|
|
31
|
+
# PyInstaller
|
|
32
|
+
# Usually these files are written by a python script from a template
|
|
33
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
34
|
+
*.manifest
|
|
35
|
+
*.spec
|
|
36
|
+
|
|
37
|
+
# Installer logs
|
|
38
|
+
pip-log.txt
|
|
39
|
+
pip-delete-this-directory.txt
|
|
40
|
+
|
|
41
|
+
# Unit test / coverage reports
|
|
42
|
+
htmlcov/
|
|
43
|
+
.tox/
|
|
44
|
+
.nox/
|
|
45
|
+
.coverage
|
|
46
|
+
.coverage.*
|
|
47
|
+
.cache
|
|
48
|
+
nosetests.xml
|
|
49
|
+
coverage.xml
|
|
50
|
+
*.cover
|
|
51
|
+
*.py.cover
|
|
52
|
+
.hypothesis/
|
|
53
|
+
.pytest_cache/
|
|
54
|
+
cover/
|
|
55
|
+
|
|
56
|
+
# Translations
|
|
57
|
+
*.mo
|
|
58
|
+
*.pot
|
|
59
|
+
|
|
60
|
+
# Django stuff:
|
|
61
|
+
*.log
|
|
62
|
+
local_settings.py
|
|
63
|
+
db.sqlite3
|
|
64
|
+
db.sqlite3-journal
|
|
65
|
+
|
|
66
|
+
# Flask stuff:
|
|
67
|
+
instance/
|
|
68
|
+
.webassets-cache
|
|
69
|
+
|
|
70
|
+
# Scrapy stuff:
|
|
71
|
+
.scrapy
|
|
72
|
+
|
|
73
|
+
# Sphinx documentation
|
|
74
|
+
docs/_build/
|
|
75
|
+
|
|
76
|
+
# PyBuilder
|
|
77
|
+
.pybuilder/
|
|
78
|
+
target/
|
|
79
|
+
|
|
80
|
+
# Jupyter Notebook
|
|
81
|
+
.ipynb_checkpoints
|
|
82
|
+
|
|
83
|
+
# IPython
|
|
84
|
+
profile_default/
|
|
85
|
+
ipython_config.py
|
|
86
|
+
|
|
87
|
+
# pyenv
|
|
88
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
89
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
90
|
+
# .python-version
|
|
91
|
+
|
|
92
|
+
# pipenv
|
|
93
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
94
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
95
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
96
|
+
# install all needed dependencies.
|
|
97
|
+
# Pipfile.lock
|
|
98
|
+
|
|
99
|
+
# UV
|
|
100
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
101
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
102
|
+
# commonly ignored for libraries.
|
|
103
|
+
# uv.lock
|
|
104
|
+
|
|
105
|
+
# poetry
|
|
106
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
107
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
108
|
+
# commonly ignored for libraries.
|
|
109
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
110
|
+
# poetry.lock
|
|
111
|
+
# poetry.toml
|
|
112
|
+
|
|
113
|
+
# pdm
|
|
114
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
115
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
116
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
117
|
+
# pdm.lock
|
|
118
|
+
# pdm.toml
|
|
119
|
+
.pdm-python
|
|
120
|
+
.pdm-build/
|
|
121
|
+
|
|
122
|
+
# pixi
|
|
123
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
124
|
+
# pixi.lock
|
|
125
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
126
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
127
|
+
.pixi
|
|
128
|
+
|
|
129
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
130
|
+
__pypackages__/
|
|
131
|
+
|
|
132
|
+
# Celery stuff
|
|
133
|
+
celerybeat-schedule
|
|
134
|
+
celerybeat.pid
|
|
135
|
+
|
|
136
|
+
# Redis
|
|
137
|
+
*.rdb
|
|
138
|
+
*.aof
|
|
139
|
+
*.pid
|
|
140
|
+
|
|
141
|
+
# RabbitMQ
|
|
142
|
+
mnesia/
|
|
143
|
+
rabbitmq/
|
|
144
|
+
rabbitmq-data/
|
|
145
|
+
|
|
146
|
+
# ActiveMQ
|
|
147
|
+
activemq-data/
|
|
148
|
+
|
|
149
|
+
# SageMath parsed files
|
|
150
|
+
*.sage.py
|
|
151
|
+
|
|
152
|
+
# Environments
|
|
153
|
+
.env
|
|
154
|
+
.envrc
|
|
155
|
+
.venv
|
|
156
|
+
env/
|
|
157
|
+
venv/
|
|
158
|
+
ENV/
|
|
159
|
+
env.bak/
|
|
160
|
+
venv.bak/
|
|
161
|
+
|
|
162
|
+
# Spyder project settings
|
|
163
|
+
.spyderproject
|
|
164
|
+
.spyproject
|
|
165
|
+
|
|
166
|
+
# Rope project settings
|
|
167
|
+
.ropeproject
|
|
168
|
+
|
|
169
|
+
# mkdocs documentation
|
|
170
|
+
/site
|
|
171
|
+
|
|
172
|
+
# mypy
|
|
173
|
+
.mypy_cache/
|
|
174
|
+
.dmypy.json
|
|
175
|
+
dmypy.json
|
|
176
|
+
|
|
177
|
+
# Pyre type checker
|
|
178
|
+
.pyre/
|
|
179
|
+
|
|
180
|
+
# pytype static type analyzer
|
|
181
|
+
.pytype/
|
|
182
|
+
|
|
183
|
+
# Cython debug symbols
|
|
184
|
+
cython_debug/
|
|
185
|
+
|
|
186
|
+
# PyCharm
|
|
187
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
188
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
189
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
190
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
191
|
+
# .idea/
|
|
192
|
+
|
|
193
|
+
# Abstra
|
|
194
|
+
# Abstra is an AI-powered process automation framework.
|
|
195
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
196
|
+
# Learn more at https://abstra.io/docs
|
|
197
|
+
.abstra/
|
|
198
|
+
|
|
199
|
+
# Visual Studio Code
|
|
200
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
201
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
202
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
203
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
204
|
+
# .vscode/
|
|
205
|
+
# Temporary file for partial code execution
|
|
206
|
+
tempCodeRunnerFile.py
|
|
207
|
+
|
|
208
|
+
# Ruff stuff:
|
|
209
|
+
.ruff_cache/
|
|
210
|
+
|
|
211
|
+
# PyPI configuration file
|
|
212
|
+
.pypirc
|
|
213
|
+
|
|
214
|
+
# Marimo
|
|
215
|
+
marimo/_static/
|
|
216
|
+
marimo/_lsp/
|
|
217
|
+
__marimo__/
|
|
218
|
+
|
|
219
|
+
# Streamlit
|
|
220
|
+
.streamlit/secrets.toml
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Phil Wang
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: survival-rl
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Explorations into Survival Reinforcement Learning
|
|
5
|
+
Project-URL: Homepage, https://pypi.org/project/survival-rl/
|
|
6
|
+
Project-URL: Repository, https://github.com/lucidrains/survival-rl
|
|
7
|
+
Author-email: Phil Wang <lucidrains@gmail.com>
|
|
8
|
+
License: MIT License
|
|
9
|
+
|
|
10
|
+
Copyright (c) 2026 Phil Wang
|
|
11
|
+
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
in the Software without restriction, including without limitation the rights
|
|
15
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
furnished to do so, subject to the following conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
copies or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
SOFTWARE.
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Keywords: artificial intelligence,deep learning,reinforcement learning,survival analysis
|
|
31
|
+
Classifier: Development Status :: 4 - Beta
|
|
32
|
+
Classifier: Intended Audience :: Developers
|
|
33
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
34
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
35
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
36
|
+
Requires-Python: >=3.10
|
|
37
|
+
Requires-Dist: einops>=0.8.2
|
|
38
|
+
Requires-Dist: einx>=0.4.3
|
|
39
|
+
Requires-Dist: torch-einops-utils>=0.1.31
|
|
40
|
+
Requires-Dist: torch>=2.5
|
|
41
|
+
Requires-Dist: x-mlps-pytorch>=0.6.4
|
|
42
|
+
Provides-Extra: examples
|
|
43
|
+
Provides-Extra: test
|
|
44
|
+
Requires-Dist: pytest; extra == 'test'
|
|
45
|
+
Description-Content-Type: text/markdown
|
|
46
|
+
|
|
47
|
+
## Survival RL (wip)
|
|
48
|
+
|
|
49
|
+
Explorations into an application of survival analysis to RL, proposed by Tiofack et al. earlier this year
|
|
50
|
+
|
|
51
|
+
## Citations
|
|
52
|
+
|
|
53
|
+
```bibtex
|
|
54
|
+
@inproceedings{Tiofack2026SVL,
|
|
55
|
+
title = {SVL: Goal-Conditioned Reinforcement Learning as Survival Learning},
|
|
56
|
+
author = {Franki Nguimatsia Tiofack and Fabian Schramm and Th{\'e}otime Le Hellard and Justin Carpentier},
|
|
57
|
+
booktitle = {International Conference on Machine Learning (ICML)},
|
|
58
|
+
year = {2026},
|
|
59
|
+
url = {https://arxiv.org/abs/2604.17551}
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
```bibtex
|
|
64
|
+
@article{Tiofack2026SRL,
|
|
65
|
+
title = {Survival Reinforcement Learning: Toward Scalable Self-Supervised RL},
|
|
66
|
+
author = {Franki Nguimatsia-Tiofack and Fabian Schramm and Th{\'e}otime Le Hellard and Justin Carpentier},
|
|
67
|
+
journal = {arXiv preprint arXiv:2605.31273},
|
|
68
|
+
year = {2026},
|
|
69
|
+
note = {19th European Workshop on Reinforcement Learning (EWRL), Oral},
|
|
70
|
+
url = {https://arxiv.org/abs/2605.31273}
|
|
71
|
+
}
|
|
72
|
+
```
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
## Survival RL (wip)
|
|
2
|
+
|
|
3
|
+
Explorations into an application of survival analysis to RL, proposed by Tiofack et al. earlier this year
|
|
4
|
+
|
|
5
|
+
## Citations
|
|
6
|
+
|
|
7
|
+
```bibtex
|
|
8
|
+
@inproceedings{Tiofack2026SVL,
|
|
9
|
+
title = {SVL: Goal-Conditioned Reinforcement Learning as Survival Learning},
|
|
10
|
+
author = {Franki Nguimatsia Tiofack and Fabian Schramm and Th{\'e}otime Le Hellard and Justin Carpentier},
|
|
11
|
+
booktitle = {International Conference on Machine Learning (ICML)},
|
|
12
|
+
year = {2026},
|
|
13
|
+
url = {https://arxiv.org/abs/2604.17551}
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```bibtex
|
|
18
|
+
@article{Tiofack2026SRL,
|
|
19
|
+
title = {Survival Reinforcement Learning: Toward Scalable Self-Supervised RL},
|
|
20
|
+
author = {Franki Nguimatsia-Tiofack and Fabian Schramm and Th{\'e}otime Le Hellard and Justin Carpentier},
|
|
21
|
+
journal = {arXiv preprint arXiv:2605.31273},
|
|
22
|
+
year = {2026},
|
|
23
|
+
note = {19th European Workshop on Reinforcement Learning (EWRL), Oral},
|
|
24
|
+
url = {https://arxiv.org/abs/2605.31273}
|
|
25
|
+
}
|
|
26
|
+
```
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "survival-rl"
|
|
3
|
+
version = "0.0.1"
|
|
4
|
+
description = "Explorations into Survival Reinforcement Learning"
|
|
5
|
+
authors = [
|
|
6
|
+
{ name = "Phil Wang", email = "lucidrains@gmail.com" }
|
|
7
|
+
]
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
requires-python = ">= 3.10"
|
|
10
|
+
license = { file = "LICENSE" }
|
|
11
|
+
keywords = [
|
|
12
|
+
'artificial intelligence',
|
|
13
|
+
'deep learning',
|
|
14
|
+
'reinforcement learning',
|
|
15
|
+
'survival analysis'
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
classifiers=[
|
|
19
|
+
'Development Status :: 4 - Beta',
|
|
20
|
+
'Intended Audience :: Developers',
|
|
21
|
+
'Topic :: Scientific/Engineering :: Artificial Intelligence',
|
|
22
|
+
'License :: OSI Approved :: MIT License',
|
|
23
|
+
'Programming Language :: Python :: 3.10',
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
dependencies = [
|
|
27
|
+
"einx>=0.4.3",
|
|
28
|
+
"einops>=0.8.2",
|
|
29
|
+
"torch>=2.5",
|
|
30
|
+
"x-mlps-pytorch>=0.6.4",
|
|
31
|
+
"torch-einops-utils>=0.1.31"
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[project.urls]
|
|
35
|
+
Homepage = "https://pypi.org/project/survival-rl/"
|
|
36
|
+
Repository = "https://github.com/lucidrains/survival-rl"
|
|
37
|
+
|
|
38
|
+
[project.optional-dependencies]
|
|
39
|
+
examples = []
|
|
40
|
+
test = [
|
|
41
|
+
"pytest"
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
[tool.pytest.ini_options]
|
|
45
|
+
pythonpath = [
|
|
46
|
+
"."
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
[build-system]
|
|
50
|
+
requires = ["hatchling"]
|
|
51
|
+
build-backend = "hatchling.build"
|
|
52
|
+
|
|
53
|
+
[tool.rye]
|
|
54
|
+
managed = true
|
|
55
|
+
dev-dependencies = []
|
|
56
|
+
|
|
57
|
+
[tool.hatch.metadata]
|
|
58
|
+
allow-direct-references = true
|
|
59
|
+
|
|
60
|
+
[tool.hatch.build.targets.wheel]
|
|
61
|
+
packages = ["survival_rl"]
|
|
62
|
+
|
|
63
|
+
[tool.hatch.build.targets.sdist]
|
|
64
|
+
include = [
|
|
65
|
+
"/survival_rl",
|
|
66
|
+
"/pyproject.toml",
|
|
67
|
+
"/README.md",
|
|
68
|
+
"/LICENSE"
|
|
69
|
+
]
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
from math import log
|
|
3
|
+
|
|
4
|
+
import torch
|
|
5
|
+
from torch.nn.functional import (
|
|
6
|
+
logsigmoid,
|
|
7
|
+
binary_cross_entropy_with_logits
|
|
8
|
+
)
|
|
9
|
+
from torch.nn import Module, Linear
|
|
10
|
+
|
|
11
|
+
import einx
|
|
12
|
+
|
|
13
|
+
from torch_einops_utils import (
|
|
14
|
+
lens_to_mask,
|
|
15
|
+
masked_sum
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
from x_mlps_pytorch import (
|
|
19
|
+
MLP,
|
|
20
|
+
ResidualNormedMLP,
|
|
21
|
+
AttnResidualNormedMLP
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
# helpers
|
|
25
|
+
|
|
26
|
+
def exists(v):
|
|
27
|
+
return v is not None
|
|
28
|
+
|
|
29
|
+
def default(v, d):
|
|
30
|
+
return v if exists(v) else d
|
|
31
|
+
|
|
32
|
+
# classes
|
|
33
|
+
|
|
34
|
+
class HazardCritic(Module):
|
|
35
|
+
def __init__(
|
|
36
|
+
self,
|
|
37
|
+
*,
|
|
38
|
+
dim,
|
|
39
|
+
depth,
|
|
40
|
+
dim_state,
|
|
41
|
+
num_actions,
|
|
42
|
+
pred_time_bins,
|
|
43
|
+
dim_action = None,
|
|
44
|
+
dim_goal = None,
|
|
45
|
+
attn_residual = True,
|
|
46
|
+
discount_factor = 0.99
|
|
47
|
+
):
|
|
48
|
+
super().__init__()
|
|
49
|
+
|
|
50
|
+
mlp_klass = AttnResidualNormedMLP if attn_residual else ResidualNormedMLP
|
|
51
|
+
|
|
52
|
+
dim_goal = default(dim_goal, dim_state)
|
|
53
|
+
dim_action = default(dim_action, dim // 2)
|
|
54
|
+
|
|
55
|
+
self.action_encoder = MLP(num_actions, dim_action, dim_action)
|
|
56
|
+
|
|
57
|
+
mlp_kwargs = dict(
|
|
58
|
+
dim_in = dim_state + dim_goal + dim_action,
|
|
59
|
+
dim = dim,
|
|
60
|
+
depth = depth
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
if not attn_residual:
|
|
64
|
+
mlp_kwargs['residual_every'] = next(e for e in (4, 3, 2, 1) if depth % e == 0)
|
|
65
|
+
|
|
66
|
+
self.mlp = mlp_klass(**mlp_kwargs)
|
|
67
|
+
|
|
68
|
+
self.pred_time_bins = Linear(dim, pred_time_bins)
|
|
69
|
+
|
|
70
|
+
self.num_time_bins = pred_time_bins
|
|
71
|
+
|
|
72
|
+
self.register_buffer('times', torch.arange(pred_time_bins))
|
|
73
|
+
|
|
74
|
+
# calculate log discount factors upfront
|
|
75
|
+
|
|
76
|
+
self.register_buffer('log_discounts', torch.arange(pred_time_bins) * log(discount_factor))
|
|
77
|
+
|
|
78
|
+
def extract_policy(
|
|
79
|
+
self,
|
|
80
|
+
actor: Module,
|
|
81
|
+
state,
|
|
82
|
+
goal,
|
|
83
|
+
):
|
|
84
|
+
actions = actor(state, goal)
|
|
85
|
+
values = self.forward(state, actions, goal, return_values = True)
|
|
86
|
+
return -values
|
|
87
|
+
|
|
88
|
+
def forward(
|
|
89
|
+
self,
|
|
90
|
+
state,
|
|
91
|
+
actions,
|
|
92
|
+
goal,
|
|
93
|
+
reach_goal_index = None, # -1 or >= horizon_cutoff treated as right-censored
|
|
94
|
+
horizon_cutoff = None,
|
|
95
|
+
return_values = False
|
|
96
|
+
):
|
|
97
|
+
horizon_cutoff = default(horizon_cutoff, self.num_time_bins)
|
|
98
|
+
|
|
99
|
+
# encode actions, embed with deep mlp and predict bins
|
|
100
|
+
|
|
101
|
+
encoded_action = self.action_encoder(actions)
|
|
102
|
+
|
|
103
|
+
embed = self.mlp((state, encoded_action, goal))
|
|
104
|
+
|
|
105
|
+
time_bin_logits = self.pred_time_bins(embed)
|
|
106
|
+
|
|
107
|
+
# log survival
|
|
108
|
+
|
|
109
|
+
log_one_minus_hazard = logsigmoid(-time_bin_logits)
|
|
110
|
+
|
|
111
|
+
log_survival = log_one_minus_hazard.cumsum(dim = -1)
|
|
112
|
+
|
|
113
|
+
# returning values, as discounted sum of survival probabilities
|
|
114
|
+
|
|
115
|
+
if return_values:
|
|
116
|
+
return -(log_survival + self.log_discounts).exp().sum(dim = -1)
|
|
117
|
+
|
|
118
|
+
# if no reach goal index given, return the log survival function
|
|
119
|
+
|
|
120
|
+
if not exists(reach_goal_index):
|
|
121
|
+
return log_survival
|
|
122
|
+
|
|
123
|
+
# bce loss on logits
|
|
124
|
+
|
|
125
|
+
reached_goal = reach_goal_index < horizon_cutoff
|
|
126
|
+
reached_goal_bin = einx.equal('b, t -> b t', reach_goal_index, self.times) & reached_goal[..., None]
|
|
127
|
+
|
|
128
|
+
losses = binary_cross_entropy_with_logits(time_bin_logits, reached_goal_bin.to(time_bin_logits.dtype), reduction = 'none')
|
|
129
|
+
|
|
130
|
+
# nll loss - time after cutoff is masked out
|
|
131
|
+
|
|
132
|
+
lens = torch.where(reach_goal_index >= 0, reach_goal_index + 1, horizon_cutoff).clamp(max = horizon_cutoff)
|
|
133
|
+
mask = lens_to_mask(lens, self.num_time_bins)
|
|
134
|
+
|
|
135
|
+
return masked_sum(losses, mask, dim = -1).mean()
|