mirror of
https://github.com/wassname/pyrobolearn.git
synced 2026-09-09 11:31:38 +08:00
update AC to account for refactored values + update methods
This commit is contained in:
@@ -99,17 +99,49 @@ class ActorCritic(object):
|
||||
"""Compute the action."""
|
||||
return self.act(x)
|
||||
|
||||
def act(self, states=None, deterministic=True):
|
||||
"""Evaluate the given input states."""
|
||||
return self.actor.act(states, deterministic=deterministic)
|
||||
def act(self, state=None, deterministic=True, to_numpy=True, return_logits=False, apply_action=True):
|
||||
"""Evaluate the given input states.
|
||||
|
||||
def evaluate(self, states=None):
|
||||
"""Evaluate the given input states."""
|
||||
return self.critic.compute(states)
|
||||
Args:
|
||||
state (State): current state
|
||||
deterministic (bool): True by default. It can only be set to False, if the policy is stochastic.
|
||||
to_numpy (bool): If True, it will convert the data (torch.Tensors) to numpy arrays.
|
||||
return_logits (bool): If True, in the case of discrete outputs, it will return the logits.
|
||||
apply_action (bool): If True, it will call and execute the action.
|
||||
|
||||
def act_and_evaluate(self, states=None):
|
||||
"""Act and evaluate the given input states."""
|
||||
return self.act(states), self.evaluate(states)
|
||||
Returns:
|
||||
(list of) np.array / torch.Tensor: action data
|
||||
"""
|
||||
return self.actor.act(state, deterministic=deterministic, to_numpy=to_numpy, return_logits=return_logits,
|
||||
apply_action=apply_action)
|
||||
|
||||
def evaluate(self, state=None, to_numpy=False):
|
||||
"""Evaluate the given input state.
|
||||
|
||||
Args:
|
||||
state (None, State, (list of) np.array, (list of) torch.Tensor): state input data. If None, it will get
|
||||
the data from the inputs that were given at the initialization.
|
||||
to_numpy (bool): If True, it will convert the data (torch.Tensors) to numpy arrays.
|
||||
"""
|
||||
return self.critic.evaluate(state, to_numpy=to_numpy)
|
||||
|
||||
def act_and_evaluate(self, state=None, deterministic=True, to_numpy=True, return_logits=False, apply_action=True):
|
||||
"""Act and evaluate the given input states.
|
||||
|
||||
Args:
|
||||
state (State): current state
|
||||
deterministic (bool): True by default. It can only be set to False, if the policy is stochastic.
|
||||
to_numpy (bool): If True, it will convert the data (torch.Tensors) to numpy arrays.
|
||||
return_logits (bool): If True, in the case of discrete outputs, it will return the logits.
|
||||
apply_action (bool): If True, it will call and execute the action.
|
||||
|
||||
Returns:
|
||||
(list of) np.array / torch.Tensor: action data
|
||||
"""
|
||||
action = self.act(state, deterministic=deterministic, to_numpy=to_numpy, return_logits=return_logits,
|
||||
apply_action=apply_action)
|
||||
value = self.evaluate(state, to_numpy=to_numpy)
|
||||
return action, value
|
||||
|
||||
|
||||
class SharedActorCritic(object):
|
||||
|
||||
@@ -10,7 +10,7 @@ import itertools
|
||||
import torch
|
||||
|
||||
from pyrobolearn.policies import LinearPolicy
|
||||
from pyrobolearn.values import LinearStateValue
|
||||
from pyrobolearn.values import LinearValue
|
||||
from pyrobolearn.actorcritics import ActorCritic, SharedActorCritic
|
||||
|
||||
__author__ = "Brian Delhaisse"
|
||||
@@ -42,7 +42,7 @@ class LinearActorCritic(ActorCritic):
|
||||
postprocessors (Processor, list of Processor, None): post-processors to be applied to the policy's output
|
||||
"""
|
||||
policy = LinearPolicy(states, actions, rate=rate, preprocessors=preprocessors, postprocessors=postprocessors)
|
||||
value = LinearStateValue(states, preprocessors=preprocessors)
|
||||
value = LinearValue(states, preprocessors=preprocessors)
|
||||
super(LinearActorCritic, self).__init__(policy, value)
|
||||
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ import itertools
|
||||
import torch
|
||||
|
||||
from pyrobolearn.policies import MLPPolicy
|
||||
from pyrobolearn.values import MLPStateValue
|
||||
from pyrobolearn.values import MLPValue
|
||||
from pyrobolearn.actorcritics import ActorCritic, SharedActorCritic
|
||||
|
||||
__author__ = "Brian Delhaisse"
|
||||
@@ -51,12 +51,12 @@ class MLPActorCritic(ActorCritic):
|
||||
preprocessors (Processor, list of Processor, None): pre-processors to be applied to the given input
|
||||
postprocessors (Processor, list of Processor, None): post-processors to be applied to the policy's output
|
||||
"""
|
||||
policy = MLPPolicy(states, actions, hidden_units=hidden_units, activation_fct=activation_fct,
|
||||
last_activation_fct=last_activation_fct, dropout_prob=dropout_prob, rate=rate,
|
||||
policy = MLPPolicy(states, actions, hidden_units=hidden_units, activation=activation_fct,
|
||||
last_activation=last_activation_fct, dropout=dropout_prob, rate=rate,
|
||||
preprocessors=preprocessors, postprocessors=postprocessors)
|
||||
value = MLPStateValue(states, hidden_units=hidden_units, activation_fct=activation_fct,
|
||||
last_activation_fct=last_activation_fct, dropout_prob=dropout_prob,
|
||||
preprocessors=preprocessors)
|
||||
value = MLPValue(states, hidden_units=hidden_units, activation_fct=activation_fct,
|
||||
last_activation_fct=last_activation_fct, dropout_prob=dropout_prob,
|
||||
preprocessors=preprocessors)
|
||||
super(MLPActorCritic, self).__init__(policy, value)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user