Skip to content

velora.models.nf

Velora's dedicated RL agents.

NeuroFlow

Bases: RLModuleAgent

Documentation

User Guide - Tutorials: NeuroFlow - Discrete

A custom Liquid RL agent that combines a variety of RL techniques.

Designed for discrete action spaces.

Source code in velora/models/nf/agent.py
Python
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
class NeuroFlow(RLModuleAgent):
    """
    ???+ abstract "Documentation"

        > [User Guide - Tutorials: NeuroFlow - Discrete](https://velora.achronus.dev/learn/tutorial/agents/nf2)

    A custom Liquid RL agent that combines a variety of RL techniques.

    Designed for `discrete` action spaces.
    """

    def __init__(
        self,
        env_id: Union[str, "DiscreteGymNames"],
        actor_neurons: int,
        critic_neurons: int,
        *,
        optim: Type[optim.Optimizer] = optim.Adam,
        buffer_size: int = 1_000_000,
        actor_lr: float = 3e-4,
        critic_lr: float = 3e-4,
        alpha_lr: float = 3e-4,
        initial_alpha: float = 1.0,
        tau: float = 0.005,
        gamma: float = 0.99,
        device: torch.device | None = None,
        seed: int | None = None,
    ) -> None:
        """
        Parameters:
            env_id (str): the Gymnasium environment ID to train the model on
            actor_neurons (int): number of decision nodes (inter and command nodes)
                for the actor
            critic_neurons (int): number of decision nodes (inter and command nodes)
                for the critic
            optim (Type[torch.optim.Optimizer], optional): the type of `PyTorch`
                optimizer to use
            buffer_size (int, optional): the maximum size of the `ReplayBuffer`
            actor_lr (float, optional): the actor optimizer learning rate
            critic_lr (float, optional): the critic optimizer learning rate
            alpha_lr (float, optional): the entropy parameter learning rate
            initial_alpha (float, optional): the starting entropy coefficient value
            tau (float, optional): the soft update factor used to slowly update
                the target networks
            gamma (float, optional): the reward discount factor
            device (torch.device, optional): the device to perform computations on
            seed (int, optional): random number seed for experiment
                reproducibility. When `None` generates a seed automatically
        """
        env = gym.make(env_id, render_mode="rgb_array")

        if not isinstance(env.action_space, gym.spaces.Discrete):
            raise EnvironmentError(
                f"Invalid '{env.action_space=}'. Must be 'gym.spaces.Discrete'."
            )

        super().__init__(
            env,
            actor_neurons,
            critic_neurons,
            buffer_size,
            optim,
            device,
            seed,
        )

        self.initial_alpha = initial_alpha
        self.gamma = gamma
        self.tau = tau

        self.actor: ActorModuleDiscrete = ActorModuleDiscrete(
            self.state_dim,
            self.actor_neurons,
            self.action_dim,
            optim=optim,
            lr=actor_lr,
            device=self.device,
        )

        self.critic: CriticModuleDiscrete = CriticModuleDiscrete(
            self.state_dim,
            self.critic_neurons,
            self.action_dim,
            optim=optim,
            lr=critic_lr,
            tau=tau,
            device=self.device,
        )

        self.hidden_dim = self.actor.hidden_size

        self.entropy: EntropyModuleDiscrete = EntropyModuleDiscrete(
            self.action_dim,
            initial_alpha=initial_alpha,
            optim=optim,
            lr=alpha_lr,
            device=device,
        )

        self.loss = nn.MSELoss()
        self.buffer: ReplayBuffer = ReplayBuffer(
            buffer_size,
            self.state_dim,
            1,
            self.actor.hidden_size,
            device=device,
        )

        self.active_params = self.actor.active_params + self.critic.active_params
        self.total_params = self.actor.total_params + self.critic.total_params

        # Init config details
        self.config = RLAgentConfig(
            agent=self.__class__.__name__,
            env=env_id,
            seed=self.seed,
            model_details=ModelDetails(
                **locals(),
                state_dim=self.state_dim,
                action_dim=self.action_dim,
                action_type="discrete",
                exploration_type="Entropy",
                actor=self.actor.config,
                critic=self.critic.config,
                entropy=self.entropy.config(),
            ),
            buffer=self.buffer.config(),
            torch=TorchConfig(
                device=str(self.device),
                optimizer=optim.__name__,
                loss=self.loss.__class__.__name__,
            ),
        )

        self.metadata = self.set_metadata(locals(), self.seed)

    def _update_critics(self, batch: "BatchExperience") -> torch.Tensor:
        """
        Helper method. Performs Critic network updates.

        Parameters:
            batch (BatchExperience): an object containing a batch of experience
                with `(states, actions, rewards, next_states, dones, hidden)`
                from the buffer

        Returns:
            critic_loss (torch.Tensor): total Critic network loss `(c1_loss + c2_loss)`.
        """
        with torch.no_grad():
            _, next_probs, _, _ = self.actor.forward(batch.next_states, batch.hiddens)
            next_log_probs = torch.log(next_probs + 1e-8)

            # Compute target Q-value (all actions)
            min_next_q = self.critic.target_predict(batch.next_states)  # [b, a_dim]

            next_q = next_probs * (min_next_q - self.entropy.alpha * next_log_probs)
            next_q = torch.sum(next_q, dim=-1, keepdim=True)
            target_q = batch.rewards + (1 - batch.dones) * self.gamma * next_q  # [b, 1]

        # Compute Q-value predictions for current critics (all actions)
        current_q1, current_q2 = self.critic.predict(batch.states)  # [b, a_dim]

        # Select Q-values for the actions taken in the batch - shape: (b, 1)
        current_q1 = current_q1.gather(1, batch.actions.long())
        current_q2 = current_q2.gather(1, batch.actions.long())

        # Calculate loss
        c1_loss: torch.Tensor = self.loss(current_q1, target_q)
        c2_loss: torch.Tensor = self.loss(current_q2, target_q)
        critic_loss: torch.Tensor = c1_loss + c2_loss

        # Update critics
        self.critic.gradient_step(c1_loss, c2_loss)

        return critic_loss

    def _train_step(self, batch_size: int) -> Dict[str, torch.Tensor]:
        """
        Helper method. Performs a single training step.

        Parameters:
            batch_size (int): number of samples in a batch

        Returns:
            losses (Dict[str, torch.Tensor]): a dictionary of losses -

            - critic: the total critic loss.
            - actor: the actor loss.
            - entropy: the entropy loss.
        """
        batch = self.buffer.sample(batch_size)

        # Compute critic loss
        critic_loss = self._update_critics(batch)

        # Make predictions
        _, probs, log_probs, _ = self.actor.forward(batch.states, batch.hiddens)
        q1, q2 = self.critic.predict(batch.states)

        # Compute actor and entropy losses
        next_q = torch.min(q1, q2)  # [b, a_dim]
        actor_loss = probs * (self.entropy.alpha * log_probs - next_q)
        actor_loss = torch.sum(actor_loss, dim=-1, keepdim=False).mean()

        entropy_loss = self.entropy.compute_loss(
            probs,
            torch.log(probs + 1e-8),
        )

        # Update gradients
        self.actor.gradient_step(actor_loss)
        self.entropy.gradient_step(entropy_loss)

        # Update target networks
        self.critic.update_targets()

        return {
            "critic": critic_loss.detach(),
            "actor": actor_loss.detach(),
            "entropy": entropy_loss.detach(),
        }

    @override
    def train(
        self,
        batch_size: int,
        *,
        n_episodes: int = 10_000,
        callbacks: List["TrainCallback"] | None = None,
        log_freq: int = 10,
        display_count: int = 100,
        window_size: int = 100,
        max_steps: int = 1000,
        warmup_steps: int | None = None,
    ) -> None:
        """
        Trains the agent on a Gymnasium environment using a `ReplayBuffer`.

        Parameters:
            batch_size (int): the number of features in a single batch
            n_episodes (int, optional): the total number of episodes to train for
            callbacks (List[TrainCallback], optional): a list of training callbacks
                that are applied during the training process
            log_freq (int, optional): metric logging frequency (in episodes)
            display_count (int, optional): console training progress frequency
                (in episodes)
            window_size (int, optional): the reward moving average size
                (in episodes)
            max_steps (int, optional): the total number of steps per episode
            warmup_steps (int, optional): the number of samples to generate in the
                buffer before starting training. If `None` uses `batch_size * 2`
        """
        warmup_steps = batch_size * 2 if warmup_steps is None else warmup_steps

        # Add training details to config
        self.config = self.config.update(self._set_train_params(locals()))

        # Display console details
        self.env.reset(seed=self.seed)  # Set seed
        training_info(
            self,
            n_episodes,
            batch_size,
            window_size,
            warmup_steps,
            callbacks or [],
        )

        if warmup_steps > 0:
            self.buffer.warm(self, warmup_steps, 2 if warmup_steps < 8 else 8)

        with TrainHandler(
            self, n_episodes, max_steps, log_freq, window_size, callbacks
        ) as handler:
            for current_ep in range(1, n_episodes + 1):
                ep_reward = 0.0
                hidden = None

                state, _ = handler.env.reset()

                for current_step in range(1, max_steps + 1):
                    action, hidden = self.predict(state, hidden, train_mode=True)
                    next_state, reward, terminated, truncated, info = handler.env.step(
                        action
                    )
                    done = terminated or truncated

                    self.buffer.add(state, action, reward, next_state, done, hidden)

                    losses = self._train_step(batch_size)

                    handler.metrics.add_step(**losses)
                    handler.step(current_step)

                    state = next_state

                    if done:
                        ep_reward = info["episode"]["r"].item()

                        handler.metrics.add_episode(
                            current_ep,
                            info["episode"]["r"],
                            info["episode"]["l"],
                        )
                        break

                if current_ep % log_freq == 0 or current_ep == n_episodes:
                    handler.log(current_ep, "episode")

                if (
                    current_ep % display_count == 0
                    or current_ep == n_episodes
                    or handler.stop()
                ):
                    handler.metrics.info(current_ep)

                handler.episode(current_ep, ep_reward)

                # Terminate on early stopping
                if handler.stop():
                    break

    @override
    def predict(
        self,
        state: torch.Tensor,
        hidden: torch.Tensor | None = None,
        *,
        train_mode: bool = False,
    ) -> Tuple[torch.Tensor, torch.Tensor]:
        """
        Makes an action prediction using the Actor network.

        Parameters:
            state (torch.Tensor): the current state
            hidden (torch.Tensor, optional): the current hidden state
            train_mode (bool, optional): whether to make deterministic (when
                `False`) or stochastic (when `True`) action predictions

        Returns:
            action (torch.Tensor): the action prediction on the given state
            hidden (torch.Tensor): the Actor network's new hidden state
        """
        self.actor.eval_mode()
        with torch.no_grad():
            state = state.unsqueeze(0) if state.dim() < 2 else state

            if not train_mode:
                action, hidden = self.actor.predict(state, hidden)
            else:
                action, _, _, hidden = self.actor.forward(state, hidden)

        self.actor.train_mode()
        return action, hidden

    def save(
        self,
        dirpath: str | Path,
        *,
        buffer: bool = False,
        config: bool = False,
    ) -> None:
        save_model(self, dirpath, buffer=buffer, config=config)

    @classmethod
    def load(cls, dirpath: str | Path, *, buffer: bool = False) -> Self:
        return load_model(cls, dirpath, buffer=buffer)

    def __repr__(self) -> str:
        return (
            f"{self.__class__.__name__}("
            + ", ".join([f"{key}={val}" for key, val in self.metadata.items()])
            + ")"
        )

__init__(env_id, actor_neurons, critic_neurons, *, optim=optim.Adam, buffer_size=1000000, actor_lr=0.0003, critic_lr=0.0003, alpha_lr=0.0003, initial_alpha=1.0, tau=0.005, gamma=0.99, device=None, seed=None)

Parameters:

Name Type Description Default
env_id str

the Gymnasium environment ID to train the model on

required
actor_neurons int

number of decision nodes (inter and command nodes) for the actor

required
critic_neurons int

number of decision nodes (inter and command nodes) for the critic

required
optim Type[torch.optim.Optimizer]

the type of PyTorch optimizer to use

optim.Adam
buffer_size int

the maximum size of the ReplayBuffer

1000000
actor_lr float

the actor optimizer learning rate

0.0003
critic_lr float

the critic optimizer learning rate

0.0003
alpha_lr float

the entropy parameter learning rate

0.0003
initial_alpha float

the starting entropy coefficient value

1.0
tau float

the soft update factor used to slowly update the target networks

0.005
gamma float

the reward discount factor

0.99
device torch.device

the device to perform computations on

None
seed int

random number seed for experiment reproducibility. When None generates a seed automatically

None
Source code in velora/models/nf/agent.py
Python
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
def __init__(
    self,
    env_id: Union[str, "DiscreteGymNames"],
    actor_neurons: int,
    critic_neurons: int,
    *,
    optim: Type[optim.Optimizer] = optim.Adam,
    buffer_size: int = 1_000_000,
    actor_lr: float = 3e-4,
    critic_lr: float = 3e-4,
    alpha_lr: float = 3e-4,
    initial_alpha: float = 1.0,
    tau: float = 0.005,
    gamma: float = 0.99,
    device: torch.device | None = None,
    seed: int | None = None,
) -> None:
    """
    Parameters:
        env_id (str): the Gymnasium environment ID to train the model on
        actor_neurons (int): number of decision nodes (inter and command nodes)
            for the actor
        critic_neurons (int): number of decision nodes (inter and command nodes)
            for the critic
        optim (Type[torch.optim.Optimizer], optional): the type of `PyTorch`
            optimizer to use
        buffer_size (int, optional): the maximum size of the `ReplayBuffer`
        actor_lr (float, optional): the actor optimizer learning rate
        critic_lr (float, optional): the critic optimizer learning rate
        alpha_lr (float, optional): the entropy parameter learning rate
        initial_alpha (float, optional): the starting entropy coefficient value
        tau (float, optional): the soft update factor used to slowly update
            the target networks
        gamma (float, optional): the reward discount factor
        device (torch.device, optional): the device to perform computations on
        seed (int, optional): random number seed for experiment
            reproducibility. When `None` generates a seed automatically
    """
    env = gym.make(env_id, render_mode="rgb_array")

    if not isinstance(env.action_space, gym.spaces.Discrete):
        raise EnvironmentError(
            f"Invalid '{env.action_space=}'. Must be 'gym.spaces.Discrete'."
        )

    super().__init__(
        env,
        actor_neurons,
        critic_neurons,
        buffer_size,
        optim,
        device,
        seed,
    )

    self.initial_alpha = initial_alpha
    self.gamma = gamma
    self.tau = tau

    self.actor: ActorModuleDiscrete = ActorModuleDiscrete(
        self.state_dim,
        self.actor_neurons,
        self.action_dim,
        optim=optim,
        lr=actor_lr,
        device=self.device,
    )

    self.critic: CriticModuleDiscrete = CriticModuleDiscrete(
        self.state_dim,
        self.critic_neurons,
        self.action_dim,
        optim=optim,
        lr=critic_lr,
        tau=tau,
        device=self.device,
    )

    self.hidden_dim = self.actor.hidden_size

    self.entropy: EntropyModuleDiscrete = EntropyModuleDiscrete(
        self.action_dim,
        initial_alpha=initial_alpha,
        optim=optim,
        lr=alpha_lr,
        device=device,
    )

    self.loss = nn.MSELoss()
    self.buffer: ReplayBuffer = ReplayBuffer(
        buffer_size,
        self.state_dim,
        1,
        self.actor.hidden_size,
        device=device,
    )

    self.active_params = self.actor.active_params + self.critic.active_params
    self.total_params = self.actor.total_params + self.critic.total_params

    # Init config details
    self.config = RLAgentConfig(
        agent=self.__class__.__name__,
        env=env_id,
        seed=self.seed,
        model_details=ModelDetails(
            **locals(),
            state_dim=self.state_dim,
            action_dim=self.action_dim,
            action_type="discrete",
            exploration_type="Entropy",
            actor=self.actor.config,
            critic=self.critic.config,
            entropy=self.entropy.config(),
        ),
        buffer=self.buffer.config(),
        torch=TorchConfig(
            device=str(self.device),
            optimizer=optim.__name__,
            loss=self.loss.__class__.__name__,
        ),
    )

    self.metadata = self.set_metadata(locals(), self.seed)

predict(state, hidden=None, *, train_mode=False)

Makes an action prediction using the Actor network.

Parameters:

Name Type Description Default
state torch.Tensor

the current state

required
hidden torch.Tensor

the current hidden state

None
train_mode bool

whether to make deterministic (when False) or stochastic (when True) action predictions

False

Returns:

Name Type Description
action torch.Tensor

the action prediction on the given state

hidden torch.Tensor

the Actor network's new hidden state

Source code in velora/models/nf/agent.py
Python
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
@override
def predict(
    self,
    state: torch.Tensor,
    hidden: torch.Tensor | None = None,
    *,
    train_mode: bool = False,
) -> Tuple[torch.Tensor, torch.Tensor]:
    """
    Makes an action prediction using the Actor network.

    Parameters:
        state (torch.Tensor): the current state
        hidden (torch.Tensor, optional): the current hidden state
        train_mode (bool, optional): whether to make deterministic (when
            `False`) or stochastic (when `True`) action predictions

    Returns:
        action (torch.Tensor): the action prediction on the given state
        hidden (torch.Tensor): the Actor network's new hidden state
    """
    self.actor.eval_mode()
    with torch.no_grad():
        state = state.unsqueeze(0) if state.dim() < 2 else state

        if not train_mode:
            action, hidden = self.actor.predict(state, hidden)
        else:
            action, _, _, hidden = self.actor.forward(state, hidden)

    self.actor.train_mode()
    return action, hidden

train(batch_size, *, n_episodes=10000, callbacks=None, log_freq=10, display_count=100, window_size=100, max_steps=1000, warmup_steps=None)

Trains the agent on a Gymnasium environment using a ReplayBuffer.

Parameters:

Name Type Description Default
batch_size int

the number of features in a single batch

required
n_episodes int

the total number of episodes to train for

10000
callbacks List[TrainCallback]

a list of training callbacks that are applied during the training process

None
log_freq int

metric logging frequency (in episodes)

10
display_count int

console training progress frequency (in episodes)

100
window_size int

the reward moving average size (in episodes)

100
max_steps int

the total number of steps per episode

1000
warmup_steps int

the number of samples to generate in the buffer before starting training. If None uses batch_size * 2

None
Source code in velora/models/nf/agent.py
Python
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
@override
def train(
    self,
    batch_size: int,
    *,
    n_episodes: int = 10_000,
    callbacks: List["TrainCallback"] | None = None,
    log_freq: int = 10,
    display_count: int = 100,
    window_size: int = 100,
    max_steps: int = 1000,
    warmup_steps: int | None = None,
) -> None:
    """
    Trains the agent on a Gymnasium environment using a `ReplayBuffer`.

    Parameters:
        batch_size (int): the number of features in a single batch
        n_episodes (int, optional): the total number of episodes to train for
        callbacks (List[TrainCallback], optional): a list of training callbacks
            that are applied during the training process
        log_freq (int, optional): metric logging frequency (in episodes)
        display_count (int, optional): console training progress frequency
            (in episodes)
        window_size (int, optional): the reward moving average size
            (in episodes)
        max_steps (int, optional): the total number of steps per episode
        warmup_steps (int, optional): the number of samples to generate in the
            buffer before starting training. If `None` uses `batch_size * 2`
    """
    warmup_steps = batch_size * 2 if warmup_steps is None else warmup_steps

    # Add training details to config
    self.config = self.config.update(self._set_train_params(locals()))

    # Display console details
    self.env.reset(seed=self.seed)  # Set seed
    training_info(
        self,
        n_episodes,
        batch_size,
        window_size,
        warmup_steps,
        callbacks or [],
    )

    if warmup_steps > 0:
        self.buffer.warm(self, warmup_steps, 2 if warmup_steps < 8 else 8)

    with TrainHandler(
        self, n_episodes, max_steps, log_freq, window_size, callbacks
    ) as handler:
        for current_ep in range(1, n_episodes + 1):
            ep_reward = 0.0
            hidden = None

            state, _ = handler.env.reset()

            for current_step in range(1, max_steps + 1):
                action, hidden = self.predict(state, hidden, train_mode=True)
                next_state, reward, terminated, truncated, info = handler.env.step(
                    action
                )
                done = terminated or truncated

                self.buffer.add(state, action, reward, next_state, done, hidden)

                losses = self._train_step(batch_size)

                handler.metrics.add_step(**losses)
                handler.step(current_step)

                state = next_state

                if done:
                    ep_reward = info["episode"]["r"].item()

                    handler.metrics.add_episode(
                        current_ep,
                        info["episode"]["r"],
                        info["episode"]["l"],
                    )
                    break

            if current_ep % log_freq == 0 or current_ep == n_episodes:
                handler.log(current_ep, "episode")

            if (
                current_ep % display_count == 0
                or current_ep == n_episodes
                or handler.stop()
            ):
                handler.metrics.info(current_ep)

            handler.episode(current_ep, ep_reward)

            # Terminate on early stopping
            if handler.stop():
                break

NeuroFlowCT

Bases: RLModuleAgent

Documentation

User Guide - Tutorials: NeuroFlow - Continuous

A custom Liquid RL agent that combines a variety of RL techniques.

Designed for continuous action spaces.

Source code in velora/models/nf/agent.py
Python
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
class NeuroFlowCT(RLModuleAgent):
    """
    ???+ abstract "Documentation"

        > [User Guide - Tutorials: NeuroFlow - Continuous](https://velora.achronus.dev/learn/tutorial/agents/nf)

    A custom Liquid RL agent that combines a variety of RL techniques.

    Designed for `continuous` action spaces.
    """

    def __init__(
        self,
        env_id: Union[str, "ContinuousGymNames"],
        actor_neurons: int,
        critic_neurons: int,
        *,
        optim: Type[optim.Optimizer] = optim.Adam,
        buffer_size: int = 1_000_000,
        actor_lr: float = 3e-4,
        critic_lr: float = 3e-4,
        alpha_lr: float = 3e-4,
        initial_alpha: float = 1.0,
        log_std: Tuple[float, float] = (-5, 2),
        tau: float = 0.005,
        gamma: float = 0.99,
        device: torch.device | None = None,
        seed: int | None = None,
    ) -> None:
        """
        Parameters:
            env_id (str): the Gymnasium environment ID to train the model on
            actor_neurons (int): number of decision nodes (inter and command nodes)
                for the actor
            critic_neurons (int): number of decision nodes (inter and command nodes)
                for the critic
            optim (Type[torch.optim.Optimizer], optional): the type of `PyTorch`
                optimizer to use
            buffer_size (int, optional): the maximum size of the `ReplayBuffer`
            actor_lr (float, optional): the actor optimizer learning rate
            critic_lr (float, optional): the critic optimizer learning rate
            alpha_lr (float, optional): the entropy parameter learning rate
            initial_alpha (float, optional): the starting entropy coefficient value
            log_std (Tuple[float, float], optional): `(low, high)` bounds for the
                log standard deviation of the action distribution. Controls the
                variance of actions
            tau (float, optional): the soft update factor used to slowly update
                the target networks
            gamma (float, optional): the reward discount factor
            device (torch.device, optional): the device to perform computations on
            seed (int, optional): random number seed for experiment
                reproducibility. When `None` generates a seed automatically
        """
        env = gym.make(env_id, render_mode="rgb_array")

        if not isinstance(env.action_space, gym.spaces.Box):
            raise EnvironmentError(
                f"Invalid '{env.action_space=}'. Must be 'gym.spaces.Box'."
            )

        super().__init__(
            env,
            actor_neurons,
            critic_neurons,
            buffer_size,
            optim,
            device,
            seed,
        )

        self.initial_alpha = initial_alpha
        self.log_std = log_std
        self.gamma = gamma
        self.tau = tau

        self.actor: ActorModule = ActorModule(
            self.state_dim,
            self.actor_neurons,
            self.action_dim,
            self.action_scale,
            self.action_bias,
            log_std_min=log_std[0],
            log_std_max=log_std[1],
            optim=optim,
            lr=actor_lr,
            device=self.device,
        )

        self.critic: CriticModule = CriticModule(
            self.state_dim,
            self.critic_neurons,
            self.action_dim,
            optim=optim,
            lr=critic_lr,
            tau=tau,
            device=self.device,
        )

        self.hidden_dim = self.actor.hidden_size

        self.entropy: EntropyModule = EntropyModule(
            self.action_dim,
            initial_alpha=initial_alpha,
            optim=optim,
            lr=alpha_lr,
            device=device,
        )

        self.loss = nn.MSELoss()
        self.buffer: ReplayBuffer = ReplayBuffer(
            buffer_size,
            self.state_dim,
            self.action_dim,
            self.actor.hidden_size,
            device=device,
        )

        self.active_params = self.actor.active_params + self.critic.active_params
        self.total_params = self.actor.total_params + self.critic.total_params

        # Init config details
        self.config = RLAgentConfig(
            agent=self.__class__.__name__,
            env=env_id,
            seed=self.seed,
            model_details=ModelDetails(
                **locals(),
                state_dim=self.state_dim,
                action_dim=self.action_dim,
                exploration_type="Entropy",
                actor=self.actor.config,
                critic=self.critic.config,
                entropy=self.entropy.config(),
            ),
            buffer=self.buffer.config(),
            torch=TorchConfig(
                device=str(self.device),
                optimizer=optim.__name__,
                loss=self.loss.__class__.__name__,
            ),
        )

        self.metadata = self.set_metadata(locals(), self.seed)

    def _update_critics(self, batch: "BatchExperience") -> torch.Tensor:
        """
        Helper method. Performs Critic network updates.

        Parameters:
            batch (BatchExperience): an object containing a batch of experience
                with `(states, actions, rewards, next_states, dones, hidden)`
                from the buffer

        Returns:
            critic_loss (torch.Tensor): total Critic network loss `(c1_loss + c2_loss)`.
        """
        with torch.no_grad():
            next_actions, next_log_probs, _ = self.actor.forward(
                batch.next_states, batch.hiddens
            )

            # Compute target Q-value
            next_q = self.critic.target_predict(batch.next_states, next_actions)
            next_q = next_q - self.entropy.alpha * next_log_probs
            target_q = batch.rewards + (1 - batch.dones) * self.gamma * next_q

        current_q1, current_q2 = self.critic.predict(batch.states, batch.actions)

        # Calculate loss
        c1_loss: torch.Tensor = self.loss(current_q1, target_q)
        c2_loss: torch.Tensor = self.loss(current_q2, target_q)
        critic_loss: torch.Tensor = c1_loss + c2_loss

        # Update critics
        self.critic.gradient_step(c1_loss, c2_loss)

        return critic_loss

    def _train_step(self, batch_size: int) -> Dict[str, torch.Tensor]:
        """
        Helper method. Performs a single training step.

        Parameters:
            batch_size (int): number of samples in a batch

        Returns:
            losses (Dict[str, torch.Tensor]): a dictionary of losses -

            - critic: the total critic loss.
            - actor: the actor loss.
            - entropy: the entropy loss.
        """
        batch = self.buffer.sample(batch_size)

        # Compute critic loss
        critic_loss = self._update_critics(batch)

        # Make predictions
        actions, log_probs, _ = self.actor.forward(batch.states, batch.hiddens)
        q1, q2 = self.critic.predict(batch.states, actions)

        # Compute actor and entropy losses
        next_q = torch.min(q1, q2)
        actor_loss = (self.entropy.alpha * log_probs - next_q).mean()
        entropy_loss = self.entropy.compute_loss(log_probs)

        # Update gradients
        self.actor.gradient_step(actor_loss)
        self.entropy.gradient_step(entropy_loss)

        # Update target networks
        self.critic.update_targets()

        return {
            "critic": critic_loss.detach(),
            "actor": actor_loss.detach(),
            "entropy": entropy_loss.detach(),
        }

    @override
    def train(
        self,
        batch_size: int,
        *,
        n_episodes: int = 10_000,
        callbacks: List["TrainCallback"] | None = None,
        log_freq: int = 10,
        display_count: int = 100,
        window_size: int = 100,
        max_steps: int = 1000,
        warmup_steps: int | None = None,
    ) -> None:
        """
        Trains the agent on a Gymnasium environment using a `ReplayBuffer`.

        Parameters:
            batch_size (int): the number of features in a single batch
            n_episodes (int, optional): the total number of episodes to train for
            callbacks (List[TrainCallback], optional): a list of training callbacks
                that are applied during the training process
            log_freq (int, optional): metric logging frequency (in episodes)
            display_count (int, optional): console training progress frequency
                (in episodes)
            window_size (int, optional): the reward moving average size
                (in episodes)
            max_steps (int, optional): the total number of steps per episode
            warmup_steps (int, optional): the number of samples to generate in the
                buffer before starting training. If `None` uses `batch_size * 2`
        """
        warmup_steps = batch_size * 2 if warmup_steps is None else warmup_steps

        # Add training details to config
        self.config = self.config.update(self._set_train_params(locals()))

        # Display console details
        self.env.reset(seed=self.seed)  # Set seed
        training_info(
            self,
            n_episodes,
            batch_size,
            window_size,
            warmup_steps,
            callbacks or [],
        )

        if warmup_steps > 0:
            self.buffer.warm(self, warmup_steps, 2 if warmup_steps < 8 else 8)

        with TrainHandler(
            self, n_episodes, max_steps, log_freq, window_size, callbacks
        ) as handler:
            for current_ep in range(1, n_episodes + 1):
                ep_reward = 0.0
                hidden = None

                state, _ = handler.env.reset()

                for current_step in range(1, max_steps + 1):
                    action, hidden = self.predict(state, hidden, train_mode=True)
                    next_state, reward, terminated, truncated, info = handler.env.step(
                        action
                    )
                    done = terminated or truncated

                    self.buffer.add(state, action, reward, next_state, done, hidden)

                    losses = self._train_step(batch_size)

                    handler.metrics.add_step(**losses)
                    handler.step(current_step)

                    state = next_state

                    if done:
                        ep_reward = info["episode"]["r"].item()

                        handler.metrics.add_episode(
                            current_ep,
                            info["episode"]["r"],
                            info["episode"]["l"],
                        )
                        break

                if current_ep % log_freq == 0 or current_ep == n_episodes:
                    handler.log(current_ep, "episode")

                if (
                    current_ep % display_count == 0
                    or current_ep == n_episodes
                    or handler.stop()
                ):
                    handler.metrics.info(current_ep)

                handler.episode(current_ep, ep_reward)

                # Terminate on early stopping
                if handler.stop():
                    break

    @override
    def predict(
        self,
        state: torch.Tensor,
        hidden: torch.Tensor | None = None,
        *,
        train_mode: bool = False,
    ) -> Tuple[torch.Tensor, torch.Tensor]:
        """
        Makes an action prediction using the Actor network.

        Parameters:
            state (torch.Tensor): the current state
            hidden (torch.Tensor, optional): the current hidden state
            train_mode (bool, optional): whether to make deterministic (when
                `False`) or stochastic (when `True`) action predictions

        Returns:
            action (torch.Tensor): the action prediction on the given state
            hidden (torch.Tensor): the Actor network's new hidden state
        """
        self.actor.eval_mode()
        with torch.no_grad():
            state = state.unsqueeze(0) if state.dim() < 2 else state

            if not train_mode:
                action, hidden = self.actor.predict(state, hidden)
            else:
                action, _, hidden = self.actor.forward(state, hidden)

        self.actor.train_mode()
        return action, hidden

    def save(
        self,
        dirpath: str | Path,
        *,
        buffer: bool = False,
        config: bool = False,
    ) -> None:
        save_model(self, dirpath, buffer=buffer, config=config)

    @classmethod
    def load(cls, dirpath: str | Path, *, buffer: bool = False) -> Self:
        return load_model(cls, dirpath, buffer=buffer)

    def __repr__(self) -> str:
        return (
            f"{self.__class__.__name__}("
            + ", ".join([f"{key}={val}" for key, val in self.metadata.items()])
            + ")"
        )

__init__(env_id, actor_neurons, critic_neurons, *, optim=optim.Adam, buffer_size=1000000, actor_lr=0.0003, critic_lr=0.0003, alpha_lr=0.0003, initial_alpha=1.0, log_std=(-5, 2), tau=0.005, gamma=0.99, device=None, seed=None)

Parameters:

Name Type Description Default
env_id str

the Gymnasium environment ID to train the model on

required
actor_neurons int

number of decision nodes (inter and command nodes) for the actor

required
critic_neurons int

number of decision nodes (inter and command nodes) for the critic

required
optim Type[torch.optim.Optimizer]

the type of PyTorch optimizer to use

optim.Adam
buffer_size int

the maximum size of the ReplayBuffer

1000000
actor_lr float

the actor optimizer learning rate

0.0003
critic_lr float

the critic optimizer learning rate

0.0003
alpha_lr float

the entropy parameter learning rate

0.0003
initial_alpha float

the starting entropy coefficient value

1.0
log_std Tuple[float, float]

(low, high) bounds for the log standard deviation of the action distribution. Controls the variance of actions

(-5, 2)
tau float

the soft update factor used to slowly update the target networks

0.005
gamma float

the reward discount factor

0.99
device torch.device

the device to perform computations on

None
seed int

random number seed for experiment reproducibility. When None generates a seed automatically

None
Source code in velora/models/nf/agent.py
Python
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
def __init__(
    self,
    env_id: Union[str, "ContinuousGymNames"],
    actor_neurons: int,
    critic_neurons: int,
    *,
    optim: Type[optim.Optimizer] = optim.Adam,
    buffer_size: int = 1_000_000,
    actor_lr: float = 3e-4,
    critic_lr: float = 3e-4,
    alpha_lr: float = 3e-4,
    initial_alpha: float = 1.0,
    log_std: Tuple[float, float] = (-5, 2),
    tau: float = 0.005,
    gamma: float = 0.99,
    device: torch.device | None = None,
    seed: int | None = None,
) -> None:
    """
    Parameters:
        env_id (str): the Gymnasium environment ID to train the model on
        actor_neurons (int): number of decision nodes (inter and command nodes)
            for the actor
        critic_neurons (int): number of decision nodes (inter and command nodes)
            for the critic
        optim (Type[torch.optim.Optimizer], optional): the type of `PyTorch`
            optimizer to use
        buffer_size (int, optional): the maximum size of the `ReplayBuffer`
        actor_lr (float, optional): the actor optimizer learning rate
        critic_lr (float, optional): the critic optimizer learning rate
        alpha_lr (float, optional): the entropy parameter learning rate
        initial_alpha (float, optional): the starting entropy coefficient value
        log_std (Tuple[float, float], optional): `(low, high)` bounds for the
            log standard deviation of the action distribution. Controls the
            variance of actions
        tau (float, optional): the soft update factor used to slowly update
            the target networks
        gamma (float, optional): the reward discount factor
        device (torch.device, optional): the device to perform computations on
        seed (int, optional): random number seed for experiment
            reproducibility. When `None` generates a seed automatically
    """
    env = gym.make(env_id, render_mode="rgb_array")

    if not isinstance(env.action_space, gym.spaces.Box):
        raise EnvironmentError(
            f"Invalid '{env.action_space=}'. Must be 'gym.spaces.Box'."
        )

    super().__init__(
        env,
        actor_neurons,
        critic_neurons,
        buffer_size,
        optim,
        device,
        seed,
    )

    self.initial_alpha = initial_alpha
    self.log_std = log_std
    self.gamma = gamma
    self.tau = tau

    self.actor: ActorModule = ActorModule(
        self.state_dim,
        self.actor_neurons,
        self.action_dim,
        self.action_scale,
        self.action_bias,
        log_std_min=log_std[0],
        log_std_max=log_std[1],
        optim=optim,
        lr=actor_lr,
        device=self.device,
    )

    self.critic: CriticModule = CriticModule(
        self.state_dim,
        self.critic_neurons,
        self.action_dim,
        optim=optim,
        lr=critic_lr,
        tau=tau,
        device=self.device,
    )

    self.hidden_dim = self.actor.hidden_size

    self.entropy: EntropyModule = EntropyModule(
        self.action_dim,
        initial_alpha=initial_alpha,
        optim=optim,
        lr=alpha_lr,
        device=device,
    )

    self.loss = nn.MSELoss()
    self.buffer: ReplayBuffer = ReplayBuffer(
        buffer_size,
        self.state_dim,
        self.action_dim,
        self.actor.hidden_size,
        device=device,
    )

    self.active_params = self.actor.active_params + self.critic.active_params
    self.total_params = self.actor.total_params + self.critic.total_params

    # Init config details
    self.config = RLAgentConfig(
        agent=self.__class__.__name__,
        env=env_id,
        seed=self.seed,
        model_details=ModelDetails(
            **locals(),
            state_dim=self.state_dim,
            action_dim=self.action_dim,
            exploration_type="Entropy",
            actor=self.actor.config,
            critic=self.critic.config,
            entropy=self.entropy.config(),
        ),
        buffer=self.buffer.config(),
        torch=TorchConfig(
            device=str(self.device),
            optimizer=optim.__name__,
            loss=self.loss.__class__.__name__,
        ),
    )

    self.metadata = self.set_metadata(locals(), self.seed)

predict(state, hidden=None, *, train_mode=False)

Makes an action prediction using the Actor network.

Parameters:

Name Type Description Default
state torch.Tensor

the current state

required
hidden torch.Tensor

the current hidden state

None
train_mode bool

whether to make deterministic (when False) or stochastic (when True) action predictions

False

Returns:

Name Type Description
action torch.Tensor

the action prediction on the given state

hidden torch.Tensor

the Actor network's new hidden state

Source code in velora/models/nf/agent.py
Python
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
@override
def predict(
    self,
    state: torch.Tensor,
    hidden: torch.Tensor | None = None,
    *,
    train_mode: bool = False,
) -> Tuple[torch.Tensor, torch.Tensor]:
    """
    Makes an action prediction using the Actor network.

    Parameters:
        state (torch.Tensor): the current state
        hidden (torch.Tensor, optional): the current hidden state
        train_mode (bool, optional): whether to make deterministic (when
            `False`) or stochastic (when `True`) action predictions

    Returns:
        action (torch.Tensor): the action prediction on the given state
        hidden (torch.Tensor): the Actor network's new hidden state
    """
    self.actor.eval_mode()
    with torch.no_grad():
        state = state.unsqueeze(0) if state.dim() < 2 else state

        if not train_mode:
            action, hidden = self.actor.predict(state, hidden)
        else:
            action, _, hidden = self.actor.forward(state, hidden)

    self.actor.train_mode()
    return action, hidden

train(batch_size, *, n_episodes=10000, callbacks=None, log_freq=10, display_count=100, window_size=100, max_steps=1000, warmup_steps=None)

Trains the agent on a Gymnasium environment using a ReplayBuffer.

Parameters:

Name Type Description Default
batch_size int

the number of features in a single batch

required
n_episodes int

the total number of episodes to train for

10000
callbacks List[TrainCallback]

a list of training callbacks that are applied during the training process

None
log_freq int

metric logging frequency (in episodes)

10
display_count int

console training progress frequency (in episodes)

100
window_size int

the reward moving average size (in episodes)

100
max_steps int

the total number of steps per episode

1000
warmup_steps int

the number of samples to generate in the buffer before starting training. If None uses batch_size * 2

None
Source code in velora/models/nf/agent.py
Python
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
@override
def train(
    self,
    batch_size: int,
    *,
    n_episodes: int = 10_000,
    callbacks: List["TrainCallback"] | None = None,
    log_freq: int = 10,
    display_count: int = 100,
    window_size: int = 100,
    max_steps: int = 1000,
    warmup_steps: int | None = None,
) -> None:
    """
    Trains the agent on a Gymnasium environment using a `ReplayBuffer`.

    Parameters:
        batch_size (int): the number of features in a single batch
        n_episodes (int, optional): the total number of episodes to train for
        callbacks (List[TrainCallback], optional): a list of training callbacks
            that are applied during the training process
        log_freq (int, optional): metric logging frequency (in episodes)
        display_count (int, optional): console training progress frequency
            (in episodes)
        window_size (int, optional): the reward moving average size
            (in episodes)
        max_steps (int, optional): the total number of steps per episode
        warmup_steps (int, optional): the number of samples to generate in the
            buffer before starting training. If `None` uses `batch_size * 2`
    """
    warmup_steps = batch_size * 2 if warmup_steps is None else warmup_steps

    # Add training details to config
    self.config = self.config.update(self._set_train_params(locals()))

    # Display console details
    self.env.reset(seed=self.seed)  # Set seed
    training_info(
        self,
        n_episodes,
        batch_size,
        window_size,
        warmup_steps,
        callbacks or [],
    )

    if warmup_steps > 0:
        self.buffer.warm(self, warmup_steps, 2 if warmup_steps < 8 else 8)

    with TrainHandler(
        self, n_episodes, max_steps, log_freq, window_size, callbacks
    ) as handler:
        for current_ep in range(1, n_episodes + 1):
            ep_reward = 0.0
            hidden = None

            state, _ = handler.env.reset()

            for current_step in range(1, max_steps + 1):
                action, hidden = self.predict(state, hidden, train_mode=True)
                next_state, reward, terminated, truncated, info = handler.env.step(
                    action
                )
                done = terminated or truncated

                self.buffer.add(state, action, reward, next_state, done, hidden)

                losses = self._train_step(batch_size)

                handler.metrics.add_step(**losses)
                handler.step(current_step)

                state = next_state

                if done:
                    ep_reward = info["episode"]["r"].item()

                    handler.metrics.add_episode(
                        current_ep,
                        info["episode"]["r"],
                        info["episode"]["l"],
                    )
                    break

            if current_ep % log_freq == 0 or current_ep == n_episodes:
                handler.log(current_ep, "episode")

            if (
                current_ep % display_count == 0
                or current_ep == n_episodes
                or handler.stop()
            ):
                handler.metrics.info(current_ep)

            handler.episode(current_ep, ep_reward)

            # Terminate on early stopping
            if handler.stop():
                break