SAC
robot_nav.models.SAC.SAC
SAC
Bases: object
Soft Actor-Critic (SAC) implementation.
This class implements the SAC algorithm using a Gaussian policy actor and double Q-learning critic. It supports automatic entropy tuning, model saving/loading, and logging via TensorBoard.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state_dim
|
int
|
Dimension of the observation/state space. |
required |
action_dim
|
int
|
Dimension of the action space. |
required |
device
|
str
|
PyTorch device (e.g., 'cpu' or 'cuda'). |
required |
max_action
|
float
|
Maximum magnitude of actions. |
required |
discount
|
float
|
Discount factor for rewards. |
0.99
|
init_temperature
|
float
|
Initial entropy temperature. |
0.1
|
alpha_lr
|
float
|
Learning rate for entropy temperature alpha. |
0.0001
|
alpha_betas
|
tuple
|
Adam optimizer betas for alpha. |
(0.9, 0.999)
|
actor_lr
|
float
|
Learning rate for actor network. |
0.0001
|
actor_betas
|
tuple
|
Adam optimizer betas for actor. |
(0.9, 0.999)
|
actor_update_frequency
|
int
|
Frequency of actor updates. |
1
|
critic_lr
|
float
|
Learning rate for critic network. |
0.0001
|
critic_betas
|
tuple
|
Adam optimizer betas for critic. |
(0.9, 0.999)
|
critic_tau
|
float
|
Soft update parameter for critic target. |
0.005
|
critic_target_update_frequency
|
int
|
Frequency of critic target updates. |
2
|
learnable_temperature
|
bool
|
Whether alpha is learnable. |
True
|
save_every
|
int
|
Save model every N training steps. Set 0 to disable. |
0
|
load_model
|
bool
|
Whether to load model from disk at init. |
False
|
log_dist_and_hist
|
bool
|
Log distribution and histogram if True. |
False
|
save_directory
|
Path
|
Directory to save models. |
Path('robot_nav/models/SAC/checkpoint')
|
model_name
|
str
|
Name for model checkpoints. |
'SAC'
|
load_directory
|
Path
|
Directory to load model checkpoints from. |
Path('robot_nav/models/SAC/checkpoint')
|
Source code in robot_nav/models/SAC/SAC.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 |
|
alpha
property
Returns:
Type | Description |
---|---|
torch.Tensor: Current value of the entropy temperature alpha. |
act(obs, sample=False)
Generate an action from the actor network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obs
|
ndarray
|
Input observation. |
required |
sample
|
bool
|
If True, sample from the policy; otherwise use the mean. |
False
|
Returns:
Type | Description |
---|---|
np.ndarray: Action vector. |
Source code in robot_nav/models/SAC/SAC.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
|
get_action(obs, add_noise)
Select an action given an observation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obs
|
ndarray
|
Input observation. |
required |
add_noise
|
bool
|
Whether to add exploration noise. |
required |
Returns:
Type | Description |
---|---|
np.ndarray: Action vector. |
Source code in robot_nav/models/SAC/SAC.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
|
load(filename, directory)
Load the actor, critic, and target critic models from the specified directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename
|
str
|
Base name of the saved files. |
required |
directory
|
Path
|
Directory where models are loaded from. |
required |
Source code in robot_nav/models/SAC/SAC.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
|
prepare_state(latest_scan, distance, cos, sin, collision, goal, action)
Convert raw sensor input into a normalized state vector.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
latest_scan
|
list or ndarray
|
Laser scan distances. |
required |
distance
|
float
|
Distance to goal. |
required |
cos
|
float
|
Cosine of heading angle to goal. |
required |
sin
|
float
|
Sine of heading angle to goal. |
required |
collision
|
bool
|
Whether the robot has collided. |
required |
goal
|
bool
|
Whether the goal has been reached. |
required |
action
|
list
|
Last action taken [linear_vel, angular_vel]. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
(state vector as list, terminal flag as int) |
Source code in robot_nav/models/SAC/SAC.py
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 |
|
save(filename, directory)
Save the actor, critic, and target critic models to the specified directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename
|
str
|
Base name of the saved files. |
required |
directory
|
Path
|
Directory where models are saved. |
required |
Source code in robot_nav/models/SAC/SAC.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
train(replay_buffer, iterations, batch_size)
Run multiple training updates using data from the replay buffer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
replay_buffer
|
Buffer from which to sample training data. |
required | |
iterations
|
int
|
Number of training iterations to run. |
required |
batch_size
|
int
|
Batch size for each update. |
required |
Source code in robot_nav/models/SAC/SAC.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
update(replay_buffer, step, batch_size)
Perform a full update step (critic, actor, alpha, target critic).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
replay_buffer
|
Buffer to sample from. |
required | |
step
|
int
|
Current training step. |
required |
batch_size
|
int
|
Size of sample batch. |
required |
Source code in robot_nav/models/SAC/SAC.py
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 |
|
update_actor_and_alpha(obs, step)
Update the actor and optionally the entropy temperature.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obs
|
Tensor
|
Batch of observations. |
required |
step
|
int
|
Current training step (for logging). |
required |
Source code in robot_nav/models/SAC/SAC.py
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 |
|
update_critic(obs, action, reward, next_obs, done, step)
Update the critic network based on a batch of transitions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obs
|
Tensor
|
Batch of current observations. |
required |
action
|
Tensor
|
Batch of actions taken. |
required |
reward
|
Tensor
|
Batch of received rewards. |
required |
next_obs
|
Tensor
|
Batch of next observations. |
required |
done
|
Tensor
|
Batch of done flags. |
required |
step
|
int
|
Current training step (for logging). |
required |
Source code in robot_nav/models/SAC/SAC.py
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 |
|
robot_nav.models.SAC.SAC_actor
DiagGaussianActor
Bases: Module
Diagonal Gaussian policy network with tanh squashing.
This network outputs a squashed Gaussian distribution given an observation, suitable for continuous control tasks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obs_dim
|
int
|
Dimension of the observation space. |
required |
action_dim
|
int
|
Dimension of the action space. |
required |
hidden_dim
|
int
|
Number of units in hidden layers. |
required |
hidden_depth
|
int
|
Number of hidden layers. |
required |
log_std_bounds
|
list
|
Min and max bounds for log standard deviation. |
required |
Source code in robot_nav/models/SAC/SAC_actor.py
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 |
|
__init__(obs_dim, action_dim, hidden_dim, hidden_depth, log_std_bounds)
Initialize the actor network.
Source code in robot_nav/models/SAC/SAC_actor.py
153 154 155 156 157 158 159 160 161 162 163 |
|
forward(obs)
Forward pass through the network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obs
|
Tensor
|
Observation input. |
required |
Returns:
Name | Type | Description |
---|---|---|
SquashedNormal |
Action distribution with mean and std tracked in |
Source code in robot_nav/models/SAC/SAC_actor.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
|
log(writer, step)
Log network outputs (mu and std histograms) to TensorBoard.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
writer
|
SummaryWriter
|
TensorBoard writer instance. |
required |
step
|
int
|
Current global training step. |
required |
Source code in robot_nav/models/SAC/SAC_actor.py
190 191 192 193 194 195 196 197 198 199 |
|
SquashedNormal
Bases: TransformedDistribution
A squashed (tanh-transformed) diagonal Gaussian distribution.
This is used for stochastic policies where actions must be within bounded intervals.
Source code in robot_nav/models/SAC/SAC_actor.py
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 |
|
mean
property
Compute the mean of the transformed distribution.
Returns:
Name | Type | Description |
---|---|---|
Tensor |
Mean of the squashed distribution. |
__init__(loc, scale)
Initialize the squashed normal distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
loc
|
Tensor
|
Mean of the Gaussian. |
required |
scale
|
Tensor
|
Standard deviation of the Gaussian. |
required |
Source code in robot_nav/models/SAC/SAC_actor.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
TanhTransform
Bases: Transform
A bijective transformation that applies the hyperbolic tangent function.
This is used to squash the output of a normal distribution to be within [-1, 1], making it suitable for bounded continuous action spaces.
Attributes:
Name | Type | Description |
---|---|---|
domain |
The input domain (real numbers). |
|
codomain |
The output codomain (interval between -1 and 1). |
|
bijective |
Whether the transform is bijective (True). |
|
sign |
The sign of the Jacobian determinant (positive). |
Source code in robot_nav/models/SAC/SAC_actor.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 |
|
__eq__(other)
Equality check for the transform.
Returns:
Name | Type | Description |
---|---|---|
bool |
True if the other object is also a TanhTransform. |
Source code in robot_nav/models/SAC/SAC_actor.py
51 52 53 54 55 56 57 58 |
|
__init__(cache_size=1)
Initialize the TanhTransform.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cache_size
|
int
|
Size of the cache for storing intermediate values. |
1
|
Source code in robot_nav/models/SAC/SAC_actor.py
29 30 31 32 33 34 35 36 |
|
atanh(x)
staticmethod
Inverse hyperbolic tangent function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
Name | Type | Description |
---|---|---|
Tensor |
atanh(x) |
Source code in robot_nav/models/SAC/SAC_actor.py
38 39 40 41 42 43 44 45 46 47 48 49 |
|
log_abs_det_jacobian(x, y)
Log absolute determinant of the Jacobian of the transformation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
y
|
Tensor
|
Output tensor. |
required |
Returns:
Name | Type | Description |
---|---|---|
Tensor |
log|det(Jacobian)| |
Source code in robot_nav/models/SAC/SAC_actor.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
robot_nav.models.SAC.SAC_critic
DoubleQCritic
Bases: Module
Double Q-learning critic network.
Implements two independent Q-functions (Q1 and Q2) to mitigate overestimation bias in value estimates, as introduced in the Twin Delayed Deep Deterministic Policy Gradient (TD3) and Soft Actor-Critic (SAC) algorithms.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obs_dim
|
int
|
Dimension of the observation space. |
required |
action_dim
|
int
|
Dimension of the action space. |
required |
hidden_dim
|
int
|
Number of units in each hidden layer. |
required |
hidden_depth
|
int
|
Number of hidden layers. |
required |
Source code in robot_nav/models/SAC/SAC_critic.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 |
|
__init__(obs_dim, action_dim, hidden_dim, hidden_depth)
Initialize the Double Q-critic network with two MLPs.
Q1 and Q2 share the same architecture but have separate weights.
Source code in robot_nav/models/SAC/SAC_critic.py
21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
forward(obs, action)
Compute Q-values for the given observation-action pairs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obs
|
Tensor
|
Observations of shape (batch_size, obs_dim). |
required |
action
|
Tensor
|
Actions of shape (batch_size, action_dim). |
required |
Returns:
Type | Description |
---|---|
Tuple[Tensor, Tensor]: Q1 and Q2 values, each of shape (batch_size, 1). |
Source code in robot_nav/models/SAC/SAC_critic.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
log(writer, step)
Log histograms of Q-value distributions to TensorBoard.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
writer
|
SummaryWriter
|
TensorBoard writer instance. |
required |
step
|
int
|
Current training step (global). |
required |
Source code in robot_nav/models/SAC/SAC_critic.py
57 58 59 60 61 62 63 64 65 66 |
|
robot_nav.models.SAC.SAC_utils
MLP
Bases: Module
Multi-layer perceptron (MLP) with configurable depth and optional output activation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_dim
|
int
|
Number of input features. |
required |
hidden_dim
|
int
|
Number of hidden units in each hidden layer. |
required |
output_dim
|
int
|
Number of output features. |
required |
hidden_depth
|
int
|
Number of hidden layers. |
required |
output_mod
|
Module
|
Optional output activation module (e.g., Tanh, Sigmoid). |
None
|
Source code in robot_nav/models/SAC/SAC_utils.py
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 |
|
forward(x)
Forward pass through the MLP.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor of shape (batch_size, input_dim). |
required |
Returns:
Name | Type | Description |
---|---|---|
Tensor |
Output tensor of shape (batch_size, output_dim). |
Source code in robot_nav/models/SAC/SAC_utils.py
92 93 94 95 96 97 98 99 100 101 102 |
|
make_dir(*path_parts)
Create a directory if it does not exist.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*path_parts
|
str
|
Components of the path to be joined into the directory. |
()
|
Returns:
Name | Type | Description |
---|---|---|
str |
The full path of the created or existing directory. |
Source code in robot_nav/models/SAC/SAC_utils.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
mlp(input_dim, hidden_dim, output_dim, hidden_depth, output_mod=None)
Create an MLP as a nn.Sequential
module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_dim
|
int
|
Input feature dimension. |
required |
hidden_dim
|
int
|
Hidden layer size. |
required |
output_dim
|
int
|
Output feature dimension. |
required |
hidden_depth
|
int
|
Number of hidden layers. |
required |
output_mod
|
Module
|
Output activation module. |
None
|
Returns:
Type | Description |
---|---|
nn.Sequential: The constructed MLP. |
Source code in robot_nav/models/SAC/SAC_utils.py
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 |
|
set_seed_everywhere(seed)
Set random seed for reproducibility across NumPy, random, and PyTorch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seed
|
int
|
Random seed. |
required |
Source code in robot_nav/models/SAC/SAC_utils.py
26 27 28 29 30 31 32 33 34 35 36 37 |
|
soft_update_params(net, target_net, tau)
Perform a soft update of the parameters of the target network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
net
|
Module
|
Source network whose parameters are used for updating. |
required |
target_net
|
Module
|
Target network to be updated. |
required |
tau
|
float
|
Interpolation parameter (0 < tau < 1) for soft updates. A value closer to 1 means faster updates. |
required |
Source code in robot_nav/models/SAC/SAC_utils.py
12 13 14 15 16 17 18 19 20 21 22 23 |
|
weight_init(m)
Custom weight initialization for layers.
Applies orthogonal initialization to Linear layers and zero initialization to biases.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
m
|
Module
|
Layer to initialize. |
required |
Source code in robot_nav/models/SAC/SAC_utils.py
58 59 60 61 62 63 64 65 66 67 68 69 70 |
|