dreamer4 0.0.91__py3-none-any.whl → 0.0.92__py3-none-any.whl
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.
- dreamer4/dreamer4.py +58 -5
- {dreamer4-0.0.91.dist-info → dreamer4-0.0.92.dist-info}/METADATA +1 -1
- dreamer4-0.0.92.dist-info/RECORD +8 -0
- dreamer4-0.0.91.dist-info/RECORD +0 -8
- {dreamer4-0.0.91.dist-info → dreamer4-0.0.92.dist-info}/WHEEL +0 -0
- {dreamer4-0.0.91.dist-info → dreamer4-0.0.92.dist-info}/licenses/LICENSE +0 -0
dreamer4/dreamer4.py
CHANGED
|
@@ -11,7 +11,7 @@ from dataclasses import dataclass, asdict
|
|
|
11
11
|
import torch
|
|
12
12
|
import torch.nn.functional as F
|
|
13
13
|
from torch.nested import nested_tensor
|
|
14
|
-
from torch.distributions import Normal
|
|
14
|
+
from torch.distributions import Normal, kl
|
|
15
15
|
from torch.nn import Module, ModuleList, Embedding, Parameter, Sequential, Linear, RMSNorm, Identity
|
|
16
16
|
from torch import nn, cat, stack, arange, tensor, Tensor, is_tensor, full, zeros, ones, randint, rand, randn, randn_like, empty, full, linspace, arange
|
|
17
17
|
from torch.utils._pytree import tree_flatten, tree_unflatten
|
|
@@ -198,6 +198,14 @@ def masked_mean(t, mask = None):
|
|
|
198
198
|
def log(t, eps = 1e-20):
|
|
199
199
|
return t.clamp(min = eps).log()
|
|
200
200
|
|
|
201
|
+
def mean_log_var_to_distr(
|
|
202
|
+
mean_log_var: Tensor
|
|
203
|
+
) -> Normal:
|
|
204
|
+
|
|
205
|
+
mean, log_var = mean_log_var.unbind(dim = -1)
|
|
206
|
+
std = (0.5 * log_var).exp()
|
|
207
|
+
return Normal(mean, std)
|
|
208
|
+
|
|
201
209
|
def safe_cat(tensors, dim):
|
|
202
210
|
tensors = [*filter(exists, tensors)]
|
|
203
211
|
|
|
@@ -824,10 +832,7 @@ class ActionEmbedder(Module):
|
|
|
824
832
|
continuous_entropies = None
|
|
825
833
|
|
|
826
834
|
if exists(continuous_targets):
|
|
827
|
-
|
|
828
|
-
std = (0.5 * log_var).exp()
|
|
829
|
-
|
|
830
|
-
distr = Normal(mean, std)
|
|
835
|
+
distr = mean_log_var_to_distr(continuous_action_mean_log_var)
|
|
831
836
|
continuous_log_probs = distr.log_prob(continuous_targets)
|
|
832
837
|
|
|
833
838
|
if return_entropies:
|
|
@@ -842,6 +847,54 @@ class ActionEmbedder(Module):
|
|
|
842
847
|
|
|
843
848
|
return log_probs, entropies
|
|
844
849
|
|
|
850
|
+
def kl_div(
|
|
851
|
+
self,
|
|
852
|
+
src: tuple[Tensor | None, Tensor | None],
|
|
853
|
+
tgt: tuple[Tensor | None, Tensor | None]
|
|
854
|
+
) -> tuple[Tensor | None, Tensor | None]:
|
|
855
|
+
|
|
856
|
+
src_discrete, src_continuous = src
|
|
857
|
+
tgt_discrete, tgt_continuous = tgt
|
|
858
|
+
|
|
859
|
+
discrete_kl_div = None
|
|
860
|
+
|
|
861
|
+
# split discrete if it is not already (multiple discrete actions)
|
|
862
|
+
|
|
863
|
+
if exists(src_discrete):
|
|
864
|
+
|
|
865
|
+
discrete_split = self.num_discrete_actions.tolist()
|
|
866
|
+
|
|
867
|
+
if is_tensor(src_discrete):
|
|
868
|
+
src_discrete = src_discrete.split(discrete_split, dim = -1)
|
|
869
|
+
|
|
870
|
+
if is_tensor(tgt_discrete):
|
|
871
|
+
tgt_discrete = tgt_discrete.split(discrete_split, dim = -1)
|
|
872
|
+
|
|
873
|
+
discrete_kl_divs = []
|
|
874
|
+
|
|
875
|
+
for src_logit, tgt_logit in zip(src_discrete, tgt_discrete):
|
|
876
|
+
|
|
877
|
+
src_log_probs = src_logit.log_softmax(dim = -1)
|
|
878
|
+
tgt_prob = tgt_logit.softmax(dim = -1)
|
|
879
|
+
|
|
880
|
+
one_discrete_kl_div = F.kl_div(src_log_probs, tgt_prob, reduction = 'none')
|
|
881
|
+
|
|
882
|
+
discrete_kl_divs.append(one_discrete_kl_div.sum(dim = -1))
|
|
883
|
+
|
|
884
|
+
discrete_kl_div = stack(discrete_kl_divs, dim = -1)
|
|
885
|
+
|
|
886
|
+
# calculate kl divergence for continuous
|
|
887
|
+
|
|
888
|
+
continuous_kl_div = None
|
|
889
|
+
|
|
890
|
+
if exists(src_continuous):
|
|
891
|
+
src_normal = mean_log_var_to_distr(src_continuous)
|
|
892
|
+
tgt_normal = mean_log_var_to_distr(tgt_continuous)
|
|
893
|
+
|
|
894
|
+
continuous_kl_div = kl.kl_divergence(src_normal, tgt_normal)
|
|
895
|
+
|
|
896
|
+
return discrete_kl_div, continuous_kl_div
|
|
897
|
+
|
|
845
898
|
def forward(
|
|
846
899
|
self,
|
|
847
900
|
*,
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
dreamer4/__init__.py,sha256=Jssh1obzDRtTfBLZl36kXge1cIQlMjf_8DyjPulvKSk,183
|
|
2
|
+
dreamer4/dreamer4.py,sha256=8RIgxdlgphe19w4bdcy2w7qWqVL4CZtoPfL6LXwj7jE,116380
|
|
3
|
+
dreamer4/mocks.py,sha256=TfqOB_Gq6N_GggBYwa6ZAJQx38ntlYbXZe23Ne4jshw,2502
|
|
4
|
+
dreamer4/trainers.py,sha256=D2b7WTgTHElLhIWLFgl2Ct2knGJLTk91HHpC5UkNvG0,14028
|
|
5
|
+
dreamer4-0.0.92.dist-info/METADATA,sha256=we70JHu-GvXjlcb5HR2bgtn8dc6L-uOARiNdqAKqDTU,3065
|
|
6
|
+
dreamer4-0.0.92.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
7
|
+
dreamer4-0.0.92.dist-info/licenses/LICENSE,sha256=1yCiA9b5nhslTavxPjsQAO-wpOnwJR9-l8LTVi7GJuk,1066
|
|
8
|
+
dreamer4-0.0.92.dist-info/RECORD,,
|
dreamer4-0.0.91.dist-info/RECORD
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
dreamer4/__init__.py,sha256=Jssh1obzDRtTfBLZl36kXge1cIQlMjf_8DyjPulvKSk,183
|
|
2
|
-
dreamer4/dreamer4.py,sha256=LnCEoVek0ekyttB3z4axdgs_NszqpOg1LnNwhrfSisM,114742
|
|
3
|
-
dreamer4/mocks.py,sha256=TfqOB_Gq6N_GggBYwa6ZAJQx38ntlYbXZe23Ne4jshw,2502
|
|
4
|
-
dreamer4/trainers.py,sha256=D2b7WTgTHElLhIWLFgl2Ct2knGJLTk91HHpC5UkNvG0,14028
|
|
5
|
-
dreamer4-0.0.91.dist-info/METADATA,sha256=D2RUBJjn7zLpaRH0_duBFaAA0EMw5JuywscwecZUhEc,3065
|
|
6
|
-
dreamer4-0.0.91.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
7
|
-
dreamer4-0.0.91.dist-info/licenses/LICENSE,sha256=1yCiA9b5nhslTavxPjsQAO-wpOnwJR9-l8LTVi7GJuk,1066
|
|
8
|
-
dreamer4-0.0.91.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|