training process Parameters ---------- summary_writer: Any logging for tensorboard model_path: str model path for resuming default: train from scratch
(config, summary_writer, model_path=None)
| 424 | |
| 425 | |
| 426 | def train(config, summary_writer, model_path=None): |
| 427 | """training process |
| 428 | Parameters |
| 429 | ---------- |
| 430 | summary_writer: Any |
| 431 | logging for tensorboard |
| 432 | model_path: str |
| 433 | model path for resuming |
| 434 | default: train from scratch |
| 435 | """ |
| 436 | model = config.get_uniform_network() |
| 437 | target_model = config.get_uniform_network() |
| 438 | if model_path: |
| 439 | print('resume model from path: ', model_path) |
| 440 | weights = torch.load(model_path) |
| 441 | |
| 442 | model.load_state_dict(weights) |
| 443 | target_model.load_state_dict(weights) |
| 444 | |
| 445 | storage = SharedStorage.remote(model, target_model) |
| 446 | |
| 447 | # prepare the batch and mctc context storage |
| 448 | batch_storage = QueueStorage(15, 20) |
| 449 | mcts_storage = QueueStorage(18, 25) |
| 450 | replay_buffer = ReplayBuffer.remote(config=config) |
| 451 | |
| 452 | # other workers |
| 453 | workers = [] |
| 454 | |
| 455 | # reanalyze workers |
| 456 | cpu_workers = [BatchWorker_CPU.remote(idx, replay_buffer, storage, batch_storage, mcts_storage, config) for idx in range(config.cpu_actor)] |
| 457 | workers += [cpu_worker.run.remote() for cpu_worker in cpu_workers] |
| 458 | gpu_workers = [BatchWorker_GPU.remote(idx, replay_buffer, storage, batch_storage, mcts_storage, config) for idx in range(config.gpu_actor)] |
| 459 | workers += [gpu_worker.run.remote() for gpu_worker in gpu_workers] |
| 460 | |
| 461 | # self-play workers |
| 462 | data_workers = [DataWorker.remote(rank, replay_buffer, storage, config) for rank in range(0, config.num_actors)] |
| 463 | workers += [worker.run.remote() for worker in data_workers] |
| 464 | # test workers |
| 465 | workers += [_test.remote(config, storage)] |
| 466 | |
| 467 | # training loop |
| 468 | final_weights = _train(model, target_model, replay_buffer, storage, batch_storage, config, summary_writer) |
| 469 | |
| 470 | ray.wait(workers) |
| 471 | print('Training over...') |
| 472 | |
| 473 | return model, final_weights |
no test coverage detected