SAC-pytorch 0.3.2__tar.gz → 0.3.4__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.5
2
2
  Name: SAC-pytorch
3
- Version: 0.3.2
3
+ Version: 0.3.4
4
4
  Summary: Soft Actor Critic - Pytorch
5
5
  Project-URL: Homepage, https://pypi.org/project/SAC-pytorch/
6
6
  Project-URL: Repository, https://github.com/lucidrains/SAC-pytorch
@@ -42,7 +42,7 @@ Requires-Dist: einx[torch]>=0.3.0
42
42
  Requires-Dist: ema-pytorch>=0.6.2
43
43
  Requires-Dist: hl-gauss-pytorch>=0.1.11
44
44
  Requires-Dist: jaxtyping
45
- Requires-Dist: mean-conc-beta>=0.2.1
45
+ Requires-Dist: mean-conc-beta>=0.2.11
46
46
  Requires-Dist: torch-einops-utils>=0.1.0
47
47
  Requires-Dist: torch>=2.0
48
48
  Requires-Dist: x-transformers>=2.19.3
@@ -264,20 +264,13 @@ class SEM(Module):
264
264
  class AttentionResidual(Module):
265
265
  def __init__(self, dim):
266
266
  super().__init__()
267
- self.to_attn_logits = nn.Conv1d(dim, dim, 1, bias = False)
268
-
269
- nn.init.dirac_(self.to_attn_logits.weight)
270
-
271
- with torch.no_grad():
272
- self.to_attn_logits.weight.mul_(2)
267
+ self.to_attn_logits = nn.Linear(dim, dim, bias = False)
268
+ nn.init.zeros_(self.to_attn_logits.weight)
273
269
 
274
270
  def forward(self, history):
275
- values = rearrange(stack(history), 'l b d -> b d l')
276
-
277
- logits = self.to_attn_logits(values)
278
- attn = logits.softmax(dim = -1)
279
-
280
- return einsum('b d l, b d l -> b d', attn, values)
271
+ values = stack(history, dim = -2)
272
+ attn = self.to_attn_logits(values).softmax(dim = -2)
273
+ return (attn * values).sum(dim = -2)
281
274
 
282
275
  # SimBa - Kaist + SonyAI research
283
276
 
@@ -296,7 +289,7 @@ class SimBa(Module):
296
289
  depth = 3,
297
290
  dropout = 0.,
298
291
  expansion_factor = 2,
299
- final_norm = False,
292
+ final_norm = True,
300
293
  simplicial_embed = True
301
294
  ):
302
295
  super().__init__()
@@ -322,7 +315,7 @@ class SimBa(Module):
322
315
  nn.Linear(dim_inner, dim_hidden),
323
316
  )
324
317
 
325
- nn.init.constant_(layer[-1].weight, 1e-5)
318
+ nn.init.orthogonal_(layer[-1].weight, gain = 0.1)
326
319
 
327
320
  layers.append(layer)
328
321
 
@@ -420,12 +413,11 @@ class BetaDistribution(Module):
420
413
  def __init__(
421
414
  self,
422
415
  eps = 1e-5,
423
- init_conc = 2.,
424
416
  detach_entropy_mean = False,
425
417
  **kwargs
426
418
  ):
427
419
  super().__init__()
428
- self.beta = Beta(eps = eps, init_conc = init_conc, detach_entropy_mean = detach_entropy_mean, **kwargs)
420
+ self.beta = Beta(eps = eps, detach_entropy_mean = detach_entropy_mean, **kwargs)
429
421
  self.source_range = tuple(map(float, self.beta.bounds))
430
422
 
431
423
  def process_params(self, params):
@@ -1442,11 +1434,9 @@ class SAC(Module):
1442
1434
  regen_reg_rate = actor_regen_reg_rate
1443
1435
  )
1444
1436
  # based on the actor hyperparameters, init the learned temperature container
1445
- # the beta distribution on (-1, 1) has entropy bounded above by
1446
- # n * log(2), so half the usual gaussian entropy target is used
1447
1437
 
1448
1438
  if not exists(target_continuous_entropy):
1449
- entropy_target_scale = 0.5 if isinstance(actor.cont_dist, BetaDistribution) else 1.
1439
+ entropy_target_scale = actor.cont_dist.beta.target_entropy.mean().item() if isinstance(actor.cont_dist, BetaDistribution) else 1.
1450
1440
  target_continuous_entropy = actor.num_cont_actions * entropy_target_scale
1451
1441
 
1452
1442
  self.learned_entropy_temperature = LearnedEntropyTemperature(
@@ -1526,7 +1516,7 @@ class SAC(Module):
1526
1516
  critics,
1527
1517
  beta = critic_target_ema_decay,
1528
1518
  include_online_model = False,
1529
- **ema_kwargs
1519
+ **(dict(update_every = 1, update_after_step = 0, min_value = critic_target_ema_decay) | ema_kwargs)
1530
1520
  )
1531
1521
 
1532
1522
  # minto - taking the minimum of both online and ema
@@ -1597,6 +1587,9 @@ class SAC(Module):
1597
1587
 
1598
1588
  seq_len = rewards.shape[1]
1599
1589
 
1590
+ if exists(cont_actions):
1591
+ cont_actions = cont_actions.detach()
1592
+
1600
1593
  if exists(cont_actions) and cont_actions.ndim == 3:
1601
1594
  assert cont_actions.shape[1] == seq_len, f'rewards chunk length {seq_len} must match actions chunk length {cont_actions.shape[1]}. did you forget to include rewards for each step in the chunk?'
1602
1595
 
@@ -1748,6 +1741,8 @@ class SAC(Module):
1748
1741
  cont_entropy = actor_output.continuous_entropy
1749
1742
  discrete_logits = actor_output.discrete_action_logits
1750
1743
 
1744
+ self.critics.requires_grad_(False)
1745
+
1751
1746
  cont_q_values, *discrete_q_values = self.critics(
1752
1747
  states,
1753
1748
  cont_actions = actor_output.continuous,
@@ -1777,11 +1772,13 @@ class SAC(Module):
1777
1772
  actor_state_recon_loss = self.state_recon_loss_fn(actor_output.state_recon, states)
1778
1773
  total_actor_loss = total_actor_loss + actor_state_recon_loss * self.actor_state_recon_loss_weight
1779
1774
 
1780
- if self.actor.spr and states.ndim == 2:
1781
- actor_spr_loss = self.actor.spr_loss(states, actor_output.continuous, next_states, reward = reduce(rewards, 'b 1 n -> b', 'sum'))
1775
+ if self.actor.spr and exists(cont_actions) and states.ndim == 2:
1776
+ actor_spr_loss = self.actor.spr_loss(states, cont_actions, next_states, reward = reduce(rewards, 'b ... -> b', 'mean'))
1782
1777
  total_actor_loss = total_actor_loss + actor_spr_loss * self.actor_spr_loss_weight
1783
1778
 
1784
1779
  total_actor_loss.backward()
1780
+ self.critics.requires_grad_(True)
1781
+
1785
1782
  self.actor_optimizer.step()
1786
1783
  self.actor_optimizer.zero_grad()
1787
1784
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "SAC-pytorch"
3
- version = "0.3.2"
3
+ version = "0.3.4"
4
4
  description = "Soft Actor Critic - Pytorch"
5
5
  authors = [
6
6
  { name = "Phil Wang", email = "lucidrains@gmail.com" }
@@ -32,7 +32,7 @@ dependencies = [
32
32
  'ema-pytorch>=0.6.2',
33
33
  'jaxtyping',
34
34
  'hl-gauss-pytorch>=0.1.11',
35
- 'mean-conc-beta>=0.2.1',
35
+ 'mean-conc-beta>=0.2.11',
36
36
  'torch>=2.0',
37
37
  'torch-einops-utils>=0.1.0',
38
38
  'x-transformers>=2.19.3'
File without changes
File without changes
File without changes