-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_rl.py
More file actions
80 lines (68 loc) · 2.05 KB
/
train_rl.py
File metadata and controls
80 lines (68 loc) · 2.05 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
76
77
78
79
80
import os
import yaml
import numpy as np
import matplotlib.pyplot as plt
from using_Reinforcement_learing.approach_1.env import HangmanEnv
from using_Reinforcement_learing.approach_1.hangman_agent import HangmanPlayer
import logging
from datetime import datetime
# 设置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('training.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
def plot_training_curves(rewards, win_rates, save_dir='training_curves'):
"""绘制训练曲线"""
os.makedirs(save_dir, exist_ok=True)
# 绘制reward曲线
plt.figure(figsize=(10, 5))
plt.plot(rewards)
plt.title('Training Reward Curve')
plt.xlabel('Episode')
plt.ylabel('Total Reward')
plt.grid(True)
plt.savefig(os.path.join(save_dir, 'reward_curve.png'))
plt.close()
# 绘制胜率曲线
plt.figure(figsize=(10, 5))
plt.plot(win_rates)
plt.title('Training Win Rate Curve')
plt.xlabel('Episode')
plt.ylabel('Win Rate')
plt.grid(True)
plt.savefig(os.path.join(save_dir, 'winrate_curve.png'))
plt.close()
def main():
# 加载配置
with open("config.yaml", 'r') as f:
config = yaml.safe_load(f)
# 创建环境
env = HangmanEnv()
# 创建智能体
agent = HangmanPlayer(env, config)
# 创建模型保存目录
os.makedirs("models", exist_ok=True)
# 训练记录
rewards = []
win_rates = []
eval_interval = 10 # 每10轮评估一次
eval_episodes = 20 # 每次评估20轮
# 开始训练
logger.info("开始训练...")
try:
agent.fit()
except Exception as e:
logger.error(f"训练过程中出现错误: {str(e)}", exc_info=True)
finally:
# 保存最终模型
agent.save()
logger.info("训练完成,模型已保存")
# 保存最终训练曲线
plot_training_curves(rewards, win_rates)
if __name__ == "__main__":
main()