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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""NeoChessPPO.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1wfdi_MmS5cEnvU_IIomlNObjzoGodqIC
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
!git clone https://huggingface.co/sigmoidneuron123/NeoChess
|
| 11 |
+
|
| 12 |
+
!pip install torchrl
|
| 13 |
+
!pip install tensordict
|
| 14 |
+
!pip install gymnasium
|
| 15 |
+
!pip install chess
|
| 16 |
+
|
| 17 |
+
import torchrl
|
| 18 |
+
import torch
|
| 19 |
+
import chess
|
| 20 |
+
import chess.engine
|
| 21 |
+
import gymnasium
|
| 22 |
+
import numpy as np
|
| 23 |
+
import tensordict
|
| 24 |
+
from collections import defaultdict
|
| 25 |
+
from tensordict.nn import TensorDictModule
|
| 26 |
+
from tensordict.nn.distributions import NormalParamExtractor
|
| 27 |
+
from torch import nn
|
| 28 |
+
from torchrl.collectors import SyncDataCollector
|
| 29 |
+
from torchrl.data.replay_buffers import ReplayBuffer
|
| 30 |
+
from torchrl.data.replay_buffers.samplers import SamplerWithoutReplacement
|
| 31 |
+
from torchrl.data.replay_buffers.storages import LazyTensorStorage
|
| 32 |
+
import torch.nn.functional as F
|
| 33 |
+
from torch.distributions import Categorical
|
| 34 |
+
from torchrl.envs import (
|
| 35 |
+
Compose,
|
| 36 |
+
DoubleToFloat,
|
| 37 |
+
ObservationNorm,
|
| 38 |
+
StepCounter,
|
| 39 |
+
TransformedEnv,
|
| 40 |
+
)
|
| 41 |
+
from torchrl.envs.libs.gym import GymEnv
|
| 42 |
+
from torchrl.envs.utils import check_env_specs, ExplorationType, set_exploration_type
|
| 43 |
+
from torchrl.modules import ProbabilisticActor, TanhNormal, ValueOperator, MaskedCategorical, ActorCriticWrapper
|
| 44 |
+
from torchrl.objectives import ClipPPOLoss
|
| 45 |
+
from torchrl.objectives.value import GAE
|
| 46 |
+
from tqdm import tqdm
|
| 47 |
+
from torchrl.envs.custom.chess import ChessEnv
|
| 48 |
+
from torchrl.envs.libs.gym import set_gym_backend, GymWrapper
|
| 49 |
+
from torchrl.envs import GymEnv
|
| 50 |
+
from tensordict import TensorDict
|
| 51 |
+
|
| 52 |
+
!git clone https://huggingface.co/sigmoidneuron123/NeoChess
|
| 53 |
+
|
| 54 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 55 |
+
|
| 56 |
+
def board_to_tensor(board):
|
| 57 |
+
piece_encoding = {
|
| 58 |
+
'P': 1, 'N': 2, 'B': 3, 'R': 4, 'Q': 5, 'K': 6,
|
| 59 |
+
'p': 7, 'n': 8, 'b': 9, 'r': 10, 'q': 11, 'k': 12
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
tensor = torch.zeros(64, dtype=torch.long)
|
| 63 |
+
for square in chess.SQUARES:
|
| 64 |
+
piece = board.piece_at(square)
|
| 65 |
+
if piece:
|
| 66 |
+
tensor[square] = piece_encoding[piece.symbol()]
|
| 67 |
+
else:
|
| 68 |
+
tensor[square] = 0
|
| 69 |
+
|
| 70 |
+
return tensor.unsqueeze(0)
|
| 71 |
+
|
| 72 |
+
class Policy(nn.Module):
|
| 73 |
+
def __init__(self):
|
| 74 |
+
super().__init__()
|
| 75 |
+
self.embedding = nn.Embedding(13, 32)
|
| 76 |
+
self.attention = nn.MultiheadAttention(embed_dim=32, num_heads=16)
|
| 77 |
+
self.neu = 256
|
| 78 |
+
self.neurons = nn.Sequential(
|
| 79 |
+
nn.Linear(64*32, self.neu),
|
| 80 |
+
nn.ReLU(),
|
| 81 |
+
nn.Linear(self.neu, self.neu),
|
| 82 |
+
nn.ReLU(),
|
| 83 |
+
nn.Linear(self.neu, self.neu),
|
| 84 |
+
nn.ReLU(),
|
| 85 |
+
nn.Linear(self.neu, self.neu),
|
| 86 |
+
nn.ReLU(),
|
| 87 |
+
nn.Linear(self.neu, 128),
|
| 88 |
+
nn.ReLU(),
|
| 89 |
+
nn.Linear(128, 29275),
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
def forward(self, x):
|
| 93 |
+
x = chess.Board(x)
|
| 94 |
+
color = x.turn
|
| 95 |
+
x = board_to_tensor(x)
|
| 96 |
+
x = self.embedding(x)
|
| 97 |
+
x = x.permute(1, 0, 2)
|
| 98 |
+
attn_output, _ = self.attention(x, x, x)
|
| 99 |
+
x = attn_output.permute(1, 0, 2).contiguous()
|
| 100 |
+
x = x.view(x.size(0), -1)
|
| 101 |
+
x = self.neurons(x) * color
|
| 102 |
+
return x
|
| 103 |
+
|
| 104 |
+
class Value(nn.Module):
|
| 105 |
+
def __init__(self):
|
| 106 |
+
super().__init__()
|
| 107 |
+
self.embedding = nn.Embedding(13, 64)
|
| 108 |
+
self.attention = nn.MultiheadAttention(embed_dim=64, num_heads=16)
|
| 109 |
+
self.neu = 512
|
| 110 |
+
self.neurons = nn.Sequential(
|
| 111 |
+
nn.Linear(64*64, self.neu),
|
| 112 |
+
nn.ReLU(),
|
| 113 |
+
nn.Linear(self.neu, self.neu),
|
| 114 |
+
nn.ReLU(),
|
| 115 |
+
nn.Linear(self.neu, self.neu),
|
| 116 |
+
nn.ReLU(),
|
| 117 |
+
nn.Linear(self.neu, self.neu),
|
| 118 |
+
nn.ReLU(),
|
| 119 |
+
nn.Linear(self.neu, self.neu),
|
| 120 |
+
nn.ReLU(),
|
| 121 |
+
nn.Linear(self.neu, self.neu),
|
| 122 |
+
nn.ReLU(),
|
| 123 |
+
nn.Linear(self.neu, self.neu),
|
| 124 |
+
nn.ReLU(),
|
| 125 |
+
nn.Linear(self.neu, self.neu),
|
| 126 |
+
nn.ReLU(),
|
| 127 |
+
nn.Linear(self.neu, self.neu),
|
| 128 |
+
nn.ReLU(),
|
| 129 |
+
nn.Linear(self.neu, self.neu),
|
| 130 |
+
nn.ReLU(),
|
| 131 |
+
nn.Linear(self.neu, self.neu),
|
| 132 |
+
nn.ReLU(),
|
| 133 |
+
nn.Linear(self.neu, self.neu),
|
| 134 |
+
nn.ReLU(),
|
| 135 |
+
nn.Linear(self.neu, self.neu),
|
| 136 |
+
nn.ReLU(),
|
| 137 |
+
nn.Linear(self.neu, 64),
|
| 138 |
+
nn.ReLU(),
|
| 139 |
+
nn.Linear(64, 4)
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
def forward(self, x):
|
| 143 |
+
x = chess.Board(x)
|
| 144 |
+
color = x.turn
|
| 145 |
+
x = board_to_tensor(x)
|
| 146 |
+
x = self.embedding(x)
|
| 147 |
+
x = x.permute(1, 0, 2)
|
| 148 |
+
attn_output, _ = self.attention(x, x, x)
|
| 149 |
+
x = attn_output.permute(1, 0, 2).contiguous()
|
| 150 |
+
x = x.view(x.size(0), -1)
|
| 151 |
+
x = self.neurons(x)
|
| 152 |
+
x = x[0][0]/10
|
| 153 |
+
if color == chess.WHITE:
|
| 154 |
+
x = -x
|
| 155 |
+
return x
|
| 156 |
+
|
| 157 |
+
with set_gym_backend("gymnasium"):
|
| 158 |
+
env = ChessEnv(
|
| 159 |
+
stateful=True,
|
| 160 |
+
include_fen=True,
|
| 161 |
+
include_san=False,
|
| 162 |
+
)
|
| 163 |
+
obs = env.reset()
|
| 164 |
+
|
| 165 |
+
!mv san_moves.txt /usr/local/lib/python3.11/dist-packages/torchrl/envs/custom/
|
| 166 |
+
|
| 167 |
+
!pip show torchrl gymnasium
|
| 168 |
+
|
| 169 |
+
obs = env.reset()
|
| 170 |
+
|
| 171 |
+
for _ in range(10):
|
| 172 |
+
legal_moves = obs["action_mask"].nonzero(as_tuple=False).squeeze(-1)
|
| 173 |
+
action = legal_moves[0] # example: pick first legal move
|
| 174 |
+
td_action = TensorDict({"action": action}, batch_size=obs.batch_size)
|
| 175 |
+
|
| 176 |
+
obs = env.step(td_action) # obs is the nested TensorDict
|
| 177 |
+
|
| 178 |
+
# Use the next observation for the next step:
|
| 179 |
+
obs = obs.get("next") # move to next state
|
| 180 |
+
|
| 181 |
+
board = chess.Board(obs["fen"])
|
| 182 |
+
print(board)
|
| 183 |
+
|
| 184 |
+
obs = env.reset()
|
| 185 |
+
obs
|
| 186 |
+
|
| 187 |
+
policy = Policy().to(device)
|
| 188 |
+
value = Value().to(device)
|
| 189 |
+
valweight = torch.load("NeoChess/chessy_model.pth",map_location=device)
|
| 190 |
+
value.load_state_dict(valweight)
|
| 191 |
+
|
| 192 |
+
def sample_masked_action(logits, mask):
|
| 193 |
+
masked_logits = logits.clone()
|
| 194 |
+
masked_logits[~mask] = float('-inf') # Illegal moves
|
| 195 |
+
probs = F.softmax(masked_logits, dim=-1)
|
| 196 |
+
dist = Categorical(probs=probs)
|
| 197 |
+
action = dist.sample()
|
| 198 |
+
log_prob = dist.log_prob(action)
|
| 199 |
+
return action, log_prob
|
| 200 |
+
|
| 201 |
+
class FENPolicyWrapper(nn.Module):
|
| 202 |
+
def __init__(self, policy_net):
|
| 203 |
+
super().__init__()
|
| 204 |
+
self.policy_net = policy_net
|
| 205 |
+
|
| 206 |
+
def forward(self, fens, action_mask=None) -> torch.tensor:
|
| 207 |
+
if isinstance(fens, (TensorDict, dict)):
|
| 208 |
+
fens = fens["fen"]
|
| 209 |
+
|
| 210 |
+
# Normalize to list of strings
|
| 211 |
+
if isinstance(fens, str):
|
| 212 |
+
fens = [fens]
|
| 213 |
+
|
| 214 |
+
# Flatten nested list
|
| 215 |
+
while isinstance(fens[0], list):
|
| 216 |
+
fens = fens[0]
|
| 217 |
+
|
| 218 |
+
# Ensure action_mask is a list of tensors (or None)
|
| 219 |
+
if action_mask is not None:
|
| 220 |
+
if isinstance(action_mask, torch.Tensor):
|
| 221 |
+
action_mask = action_mask.unsqueeze(0) if action_mask.ndim == 1 else action_mask
|
| 222 |
+
if not isinstance(action_mask, list):
|
| 223 |
+
action_mask = [action_mask[i] for i in range(len(fens))]
|
| 224 |
+
|
| 225 |
+
logits_list = []
|
| 226 |
+
|
| 227 |
+
for i, fen in enumerate(fens):
|
| 228 |
+
logits = self.policy_net(fen) # shape: [4672]
|
| 229 |
+
|
| 230 |
+
# Apply masking if provided
|
| 231 |
+
if action_mask is not None:
|
| 232 |
+
mask = action_mask[i].bool() # shape: [4672]
|
| 233 |
+
logits = logits.masked_fill(~mask, float("-inf"))
|
| 234 |
+
|
| 235 |
+
logits_list.append(logits)
|
| 236 |
+
|
| 237 |
+
return torch.stack(logits_list).squeeze(-2).squeeze(-2) # shape: [batch_size, 4672]
|
| 238 |
+
|
| 239 |
+
class FENValueWrapper(nn.Module):
|
| 240 |
+
def __init__(self, value_net):
|
| 241 |
+
super().__init__()
|
| 242 |
+
self.value_net = value_net
|
| 243 |
+
|
| 244 |
+
def forward(self, fens) -> torch.tensor:
|
| 245 |
+
if isinstance(fens, TensorDict) or isinstance(fens,dict):
|
| 246 |
+
fens = fens["fen"]
|
| 247 |
+
if isinstance(fens, str):
|
| 248 |
+
fens = [fens] # Wrap single string in a list
|
| 249 |
+
while isinstance(fens[0], list):
|
| 250 |
+
fens = fens[0]
|
| 251 |
+
state_value = []
|
| 252 |
+
for fen in fens:
|
| 253 |
+
state_value += [self.value_net(fen)]
|
| 254 |
+
state_value = torch.stack(state_value)
|
| 255 |
+
# Ensure output has a batch dimension of 1 if it's a single sample
|
| 256 |
+
if state_value.ndim == 0:
|
| 257 |
+
state_value = state_value.unsqueeze(0)
|
| 258 |
+
return state_value
|
| 259 |
+
|
| 260 |
+
ACTION_DIM = 64 * 73
|
| 261 |
+
|
| 262 |
+
from functools import partial
|
| 263 |
+
# Wrap policy
|
| 264 |
+
policy_module = TensorDictModule(
|
| 265 |
+
FENPolicyWrapper(policy),
|
| 266 |
+
in_keys=["fen"],
|
| 267 |
+
out_keys=["logits"]
|
| 268 |
+
)
|
| 269 |
+
value_module = TensorDictModule(
|
| 270 |
+
FENValueWrapper(value),
|
| 271 |
+
in_keys=["fen"],
|
| 272 |
+
out_keys=["state_value"]
|
| 273 |
+
)
|
| 274 |
+
|
| 275 |
+
def masked_categorical_factory(logits, action_mask):
|
| 276 |
+
return MaskedCategorical(logits=logits, mask=action_mask)
|
| 277 |
+
|
| 278 |
+
actor = ProbabilisticActor(
|
| 279 |
+
module=policy_module,
|
| 280 |
+
in_keys=["logits", "action_mask"],
|
| 281 |
+
out_keys=["action"],
|
| 282 |
+
distribution_class=masked_categorical_factory,
|
| 283 |
+
return_log_prob=True,
|
| 284 |
+
)
|
| 285 |
+
actor_critic = ActorCriticWrapper(actor,value_module)
|
| 286 |
+
|
| 287 |
+
obs = env.reset()
|
| 288 |
+
print(obs)
|
| 289 |
+
print(policy_module(obs)["logits"])
|
| 290 |
+
print(value_module(obs))
|
| 291 |
+
print(actor(obs))
|
| 292 |
+
|
| 293 |
+
rollout = env.rollout(3)
|
| 294 |
+
|
| 295 |
+
from torchrl.record.loggers import generate_exp_name, get_logger
|
| 296 |
+
def train_ppo_chess(chess_env, num_iterations=1, frames_per_batch=100,
|
| 297 |
+
num_epochs=10, lr=3e-4, gamma=0.99, lmbda=0.95,
|
| 298 |
+
clip_epsilon=0.2, device="cpu"):
|
| 299 |
+
"""
|
| 300 |
+
Main PPO training loop for Chess
|
| 301 |
+
|
| 302 |
+
Args:
|
| 303 |
+
chess_env: Your ChessEnv instance
|
| 304 |
+
num_iterations: Number of training iterations
|
| 305 |
+
frames_per_batch: Number of environment steps per batch
|
| 306 |
+
num_epochs: Number of PPO epochs per iteration
|
| 307 |
+
lr: Learning rate
|
| 308 |
+
gamma: Discount factor
|
| 309 |
+
lmbda: GAE lambda parameter
|
| 310 |
+
clip_epsilon: PPO clipping parameter
|
| 311 |
+
device: Training device
|
| 312 |
+
"""
|
| 313 |
+
|
| 314 |
+
# Wrap the chess environment
|
| 315 |
+
env = chess_env
|
| 316 |
+
# Create actor and value modules
|
| 317 |
+
actor_module = actor
|
| 318 |
+
|
| 319 |
+
collector = SyncDataCollector(
|
| 320 |
+
env,
|
| 321 |
+
actor_module,
|
| 322 |
+
frames_per_batch=frames_per_batch,
|
| 323 |
+
total_frames=-1,
|
| 324 |
+
device=device,
|
| 325 |
+
)
|
| 326 |
+
|
| 327 |
+
# Create replay buffer
|
| 328 |
+
replay_buffer = ReplayBuffer(
|
| 329 |
+
storage=LazyTensorStorage(frames_per_batch),
|
| 330 |
+
sampler=SamplerWithoutReplacement(),
|
| 331 |
+
batch_size=256, # Mini-batch size for PPO updates
|
| 332 |
+
)
|
| 333 |
+
|
| 334 |
+
# Create PPO loss module
|
| 335 |
+
loss_module = ClipPPOLoss(
|
| 336 |
+
actor_network=actor_module,
|
| 337 |
+
critic_network=value_module,
|
| 338 |
+
clip_epsilon=clip_epsilon,
|
| 339 |
+
entropy_bonus=True,
|
| 340 |
+
entropy_coef=0.01,
|
| 341 |
+
critic_coef=1.0,
|
| 342 |
+
normalize_advantage=True,
|
| 343 |
+
)
|
| 344 |
+
|
| 345 |
+
optim = torch.optim.Adam(loss_module.parameters(), lr=lr)
|
| 346 |
+
|
| 347 |
+
# Setup logging
|
| 348 |
+
logger = get_logger("tensorboard", logger_name="ppo_chess", experiment_name=generate_exp_name("PPO", "Chess"))
|
| 349 |
+
|
| 350 |
+
# Training loop
|
| 351 |
+
collected_frames = 0
|
| 352 |
+
|
| 353 |
+
for iteration in range(num_iterations):
|
| 354 |
+
print(f"\n=== Iteration {iteration + 1}/{num_iterations} ===")
|
| 355 |
+
|
| 356 |
+
# Collect data
|
| 357 |
+
batch_data = []
|
| 358 |
+
for i, batch in enumerate(collector):
|
| 359 |
+
batch_data.append(batch)
|
| 360 |
+
collected_frames += batch.numel()
|
| 361 |
+
|
| 362 |
+
# Break after collecting enough frames
|
| 363 |
+
if len(batch_data) * collector.frames_per_batch >= frames_per_batch:
|
| 364 |
+
break
|
| 365 |
+
|
| 366 |
+
# Concatenate all batches
|
| 367 |
+
if batch_data:
|
| 368 |
+
full_batch = torch.cat(batch_data, dim=0)
|
| 369 |
+
|
| 370 |
+
# Add GAE (Generalized Advantage Estimation)
|
| 371 |
+
with torch.no_grad():
|
| 372 |
+
full_batch = loss_module.value_estimator(full_batch)
|
| 373 |
+
|
| 374 |
+
replay_buffer.extend(full_batch)
|
| 375 |
+
|
| 376 |
+
# Training phase
|
| 377 |
+
total_loss = 0
|
| 378 |
+
total_actor_loss = 0
|
| 379 |
+
total_critic_loss = 0
|
| 380 |
+
total_entropy_loss = 0
|
| 381 |
+
|
| 382 |
+
for epoch in range(num_epochs):
|
| 383 |
+
epoch_loss = 0
|
| 384 |
+
epoch_actor_loss = 0
|
| 385 |
+
epoch_critic_loss = 0
|
| 386 |
+
epoch_entropy_loss = 0
|
| 387 |
+
num_batches = 0
|
| 388 |
+
|
| 389 |
+
for batch in replay_buffer:
|
| 390 |
+
print(batch)
|
| 391 |
+
# Ensure batch has correct dimensions
|
| 392 |
+
if "state_value" in batch and batch["state_value"].dim() > 1:
|
| 393 |
+
batch["state_value"] = batch["state_value"].squeeze(-1)
|
| 394 |
+
|
| 395 |
+
batch["value_target"] = batch["value_target"].squeeze(1)
|
| 396 |
+
# Compute losses
|
| 397 |
+
loss_dict = loss_module(batch)
|
| 398 |
+
loss = loss_dict["loss_objective"] + loss_dict["loss_critic"] + loss_dict["loss_entropy"]
|
| 399 |
+
|
| 400 |
+
# Backward pass
|
| 401 |
+
optim.zero_grad()
|
| 402 |
+
loss.backward()
|
| 403 |
+
torch.nn.utils.clip_grad_norm_(loss_module.parameters(), max_norm=0.5)
|
| 404 |
+
optim.step()
|
| 405 |
+
|
| 406 |
+
# Accumulate losses
|
| 407 |
+
epoch_loss += loss.item()
|
| 408 |
+
epoch_actor_loss += loss_dict["loss_objective"].item()
|
| 409 |
+
epoch_critic_loss += loss_dict["loss_critic"].item()
|
| 410 |
+
epoch_entropy_loss += loss_dict["loss_entropy"].item()
|
| 411 |
+
num_batches += 1
|
| 412 |
+
|
| 413 |
+
# Average losses over epoch
|
| 414 |
+
if num_batches > 0:
|
| 415 |
+
total_loss += epoch_loss / num_batches
|
| 416 |
+
total_actor_loss += epoch_actor_loss / num_batches
|
| 417 |
+
total_critic_loss += epoch_critic_loss / num_batches
|
| 418 |
+
total_entropy_loss += epoch_entropy_loss / num_batches
|
| 419 |
+
|
| 420 |
+
# Average losses over all epochs
|
| 421 |
+
avg_total_loss = total_loss / num_epochs
|
| 422 |
+
avg_actor_loss = total_actor_loss / num_epochs
|
| 423 |
+
avg_critic_loss = total_critic_loss / num_epochs
|
| 424 |
+
avg_entropy_loss = total_entropy_loss / num_epochs
|
| 425 |
+
|
| 426 |
+
# Log metrics
|
| 427 |
+
metrics = {
|
| 428 |
+
"train/total_loss": avg_total_loss,
|
| 429 |
+
"train/actor_loss": avg_actor_loss,
|
| 430 |
+
"train/critic_loss": avg_critic_loss,
|
| 431 |
+
"train/entropy_loss": avg_entropy_loss,
|
| 432 |
+
"train/collected_frames": collected_frames,
|
| 433 |
+
}
|
| 434 |
+
|
| 435 |
+
# Log reward if available in batch
|
| 436 |
+
if "reward" in batch.keys():
|
| 437 |
+
avg_reward = batch["reward"].mean().item()
|
| 438 |
+
metrics["train/avg_reward"] = avg_reward
|
| 439 |
+
print(f"Average Reward: {avg_reward:.3f}")
|
| 440 |
+
|
| 441 |
+
for key, value in metrics.items():
|
| 442 |
+
logger.log_scalar(key, value, step=iteration)
|
| 443 |
+
|
| 444 |
+
print(f"Total Loss: {avg_total_loss:.4f}")
|
| 445 |
+
print(f"Actor Loss: {avg_actor_loss:.4f}")
|
| 446 |
+
print(f"Critic Loss: {avg_critic_loss:.4f}")
|
| 447 |
+
print(f"Entropy Loss: {avg_entropy_loss:.4f}")
|
| 448 |
+
print(f"Collected Frames: {collected_frames}")
|
| 449 |
+
|
| 450 |
+
# Clear replay buffer for next iteration
|
| 451 |
+
replay_buffer.empty()
|
| 452 |
+
|
| 453 |
+
print("\nTraining completed!")
|
| 454 |
+
return actor_module, value_module, loss_module
|
| 455 |
+
|
| 456 |
+
train_ppo_chess(env)
|