-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
75 lines (62 loc) · 2.61 KB
/
Copy pathutils.py
File metadata and controls
75 lines (62 loc) · 2.61 KB
1
2
3
4
5
6
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
67
68
69
70
71
72
73
74
75
import torch
from torch import nn
from torch import Tensor
import numpy as np
def normalize_advantages(adv: Tensor) -> Tensor:
"""
Normalizes the given advantages tensor by subtracting its mean and dividing by its
standard deviation, ensuring numerical stability by adding a small constant to the
standard deviation.
"""
return (adv - adv.mean()) / (adv.std() + 1e-8)
def ppo_clipped_surrogate(
adv: Tensor,
ratio: Tensor,
clip: float = 0.2
) -> Tensor:
"""
Computes the Proximal Policy Optimization (PPO) clipped surrogate objective function.
The function calculates the PPO loss using a clipped ratio to ensure a balance between
policy updates and avoiding over-optimization, which can destabilize training.
The clipping mechanism limits the change in policy entropy within a fixed range.
"""
obj_unclipped = -adv * ratio
obj_clipped = -adv * torch.clamp(ratio, min=1-clip, max=1+clip)
policy_loss = torch.max(obj_unclipped, obj_clipped).mean()
return policy_loss
def kl_divergence(
ratio: Tensor,
log_ratio
) -> Tensor:
"""
Computes an approximation of the Kullback-Leibler (KL) divergence
between two probability distributions. The method is based on a mean-field
approximation and assumes input tensors representing probabilities or distributions.
"""
with torch.no_grad():
approx_kl = ((ratio - 1) - log_ratio).mean()
return approx_kl
def layer_init(
layer,
std=np.sqrt(2),
bias=0.0
):
"""
Initializes the parameters of a given neural network layer with specific values based on
given standard deviation and bias.
This function adjusts the initial values of the weights and bias of the provided layer to
improve the stability of training and convergence. It uses the provided standard deviation
and bias value to modify these parameters.
:param layer: The neural network layer whose parameters need to be initialized.
Must have weight and bias attributes that can be adjusted.
The 'weight' is a multi-dimensional tensor representing the layer's weights.
:param std: A floating-point value specifying the standard deviation to scale the
layer's weights. It defaults to the square root of 2.
:param bias: A floating-point value specifying the value to initialize the layer's
bias term. Defaults to 0.0.
:return: The layer with its weights and bias initialized based on the given standard
deviation and bias.
"""
nn.init.orthogonal_(layer.weight, std)
nn.init.constant_(layer.bias, bias)
return layer