# 導入庫函數 import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns plt.style.use('ggplot') plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False
# 平滑處理,類似tensorboard的smoothing函數。 def smooth(read_path, save_path, file_name, x='timestep', y='reward', weight=0.75): data = pd.read_csv(read_path + file_name) scalar = data[y].values last = scalar[0] smoothed = [] for point in scalar: smoothed_val = last * weight + (1 - weight) * point smoothed.append(smoothed_val) last = smoothed_val save = pd.DataFrame({x: data[x].values, y: smoothed}) save.to_csv(save_path + 'smooth_'+ file_name)
# 平滑預處理原始reward數據 smooth(read_path='./BipedalWalker-v3/', save_path='./BipedalWalker-v3/', file_name='PPO_BipedalWalker-v3_log_210.csv') smooth(read_path='./BipedalWalker-v3/', save_path='./BipedalWalker-v3/', file_name='PPO_BipedalWalker-v3_log_310.csv') smooth(read_path='./BipedalWalker-v3/', save_path='./BipedalWalker-v3/', file_name='PPO_BipedalWalker-v3_log_410.csv')
# 讀取平滑后的數據 df1 = pd.read_csv('./BipedalWalker-v3/smooth_PPO_BipedalWalker-v3_log_210.csv') #[1100: 1200] df2 = pd.read_csv('./BipedalWalker-v3/smooth_PPO_BipedalWalker-v3_log_310.csv') #[1100: 1200] df3 = pd.read_csv('./BipedalWalker-v3/smooth_PPO_BipedalWalker-v3_log_410.csv') #[1100: 1200]
# 拼接到一起 df = df1.append(df2.append(df3))
# 重新排列索引 df.index = range(len(df)) print(df)
# 設置圖片大小 plt.figure(figsize=(15, 10))
# 畫圖 sns.lineplot(data=df, x="timestep", y="reward")