streaming-deep-rl 0.2.1__tar.gz → 0.2.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: streaming-deep-rl
3
- Version: 0.2.1
3
+ Version: 0.2.2
4
4
  Summary: Streaming Deep Reinforcement Learning
5
5
  Project-URL: Homepage, https://pypi.org/project/streaming-deep-rl/
6
6
  Project-URL: Repository, https://codeberg.org/lucidrains/streaming-deep-rl
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "streaming-deep-rl"
3
- version = "0.2.1"
3
+ version = "0.2.2"
4
4
  description = "Streaming Deep Reinforcement Learning"
5
5
  authors = [
6
6
  { name = "Phil Wang", email = "lucidrains@gmail.com" }
@@ -1,6 +1,7 @@
1
1
  from __future__ import annotations
2
2
  import math
3
3
  from collections import deque
4
+ from contextlib import nullcontext
4
5
  from typing import Callable, NamedTuple
5
6
 
6
7
  import torch
@@ -25,12 +26,21 @@ from streaming_deep_rl.buffer_dict import BufferDict
25
26
  def exists(v):
26
27
  return v is not None
27
28
 
29
+ def identity(t):
30
+ return t
31
+
28
32
  def divisible_by(num, den):
29
33
  return (num % den) == 0
30
34
 
31
35
  def default(v, d):
32
36
  return v if exists(v) else d
33
37
 
38
+ def no_grad_detach(fn):
39
+ def inner(t):
40
+ with torch.no_grad():
41
+ return fn(t).detach()
42
+ return inner
43
+
34
44
  def cast_tensor(t):
35
45
  return tensor(t) if not is_tensor(t) else t
36
46
 
@@ -104,7 +114,8 @@ class SelfPredictRepr(Module):
104
114
  dim_embed,
105
115
  num_discrete_actions = 0,
106
116
  num_continuous_actions = 0,
107
- dim_predict = None
117
+ dim_predict = None,
118
+ target_embed_from_ema = True # whether the target embed is from an EMA, or from the model itself (traditional vs sigreg from lejepa)
108
119
  ):
109
120
  super().__init__()
110
121
  assert num_discrete_actions > 0 or num_continuous_actions > 0
@@ -113,6 +124,8 @@ class SelfPredictRepr(Module):
113
124
 
114
125
  self.to_action_embed = Embed(dim_embed, num_discrete = num_discrete_actions, num_continuous = num_continuous_actions)
115
126
 
127
+ self.target_embed_from_ema = target_embed_from_ema
128
+
116
129
  self.to_projection = nn.Sequential(
117
130
  nn.RMSNorm(dim_embed),
118
131
  nn.Linear(dim_embed, dim_predict, bias = False)
@@ -130,8 +143,11 @@ class SelfPredictRepr(Module):
130
143
  ):
131
144
  projected = self.to_projection(state_embed)
132
145
 
133
- with torch.no_grad():
134
- next_projected = self.to_projection(next_state_embed.detach())
146
+ # maybe no
147
+
148
+ decorator = no_grad_detach if self.target_embed_from_ema else identity
149
+
150
+ next_projected = decorator(self.to_projection)(next_state_embed)
135
151
 
136
152
  action_embed = self.to_action_embed(actions)
137
153