[loss画图记录]

本文档展示了如何使用Python绘制GRLPA和DDPG模型在早晨、中午和晚上三个时间段的loss曲线,通过morning_flow、morning_unweighted和unweighted三种类型的loss数据,对比不同模型在流量和未加权情况下的性能。
摘要由CSDN通过智能技术生成

一、导入库

import numpy as np
import pandas as pd 
#import networkx as nx
import matplotlib.pyplot as plt
import random
from collections import *
import csv  
import pickle  
import os

二、修改当前文件工作路径

os.getcwd()  # 获得当前工作路径

cd "C:\\Users\\Administrator\\Desktop\\潘\\GDDPG\\model_plot\\L" # 修改工作路径

### 获得当前文件路径下的文件
lpath="C:\\Users\\Administrator\\Desktop\\潘\\GDDPG\\model_plot\\L"
for root, dirs, files in os.walk(lpath):
    print(files)

三、画loss的两个model的morning段的图

## 单独画GRLPA_flow_morning_loss图
# 先改工作位置
fig1, ax1 = plt.subplots(figsize=(8, 5), dpi=80)
l_f='loss1000GRLPA29morningflow_weekday.pkl'
#l_u='loss1000GRLPA29morningunweighted_weekday.pkl'
loss = pickle.load(open(l_f,"rb"))
loss = np.asarray(loss)
loss = loss[::2][:100]
x = range(len(loss))
label = "morning"
ax1.plot(x,loss,color="orange",label=label)
ax1.legend()
#plt.title("GRLPA model under flow-matrix in the morning") 
plt.savefig("GRLPA_flow_morning.png")

第二个

## 单独画GRLPA_unweighted_morning_loss图
# 先改工作位置
fig1, ax1 = plt.subplots(figsize=(8, 5), dpi=80)
#l_f='loss1000GRLPA29morningflow_weekday.pkl'
l_u='loss1000GRLPA29morningunweighted_weekday.pkl'
loss = pickle.load(open(l_u,"rb"))
loss = np.asarray(loss)
loss = loss[::2][:100]
x = range(len(loss))
label = "morning"
ax1.plot(x,loss,color="orange",label=label)
ax1.legend()
#plt.title("GRLPA model under flow-matrix in the morning") 
plt.savefig("GRLPA_unweighted_morning.png")

四、画loss的3个model的三个时间段的图

fig1, ax1 = plt.subplots(figsize=(8, 5), dpi=80)
for f in files:
    if "GRLPA" in f:
        if "flow" in f:
            loss = pickle.load(open(f,"rb"))
            loss = np.asarray(loss)
            loss = loss[::2]
            x = range(len(loss))
            label = Judge_Label(f)
            if label is not None:
                ax1.plot(x,loss,label=label)
                ax1.legend()
plt.title("GRLPA model under flow-matrix") 
plt.savefig("GRLPA_flow.png")
fig2, ax2 = plt.subplots(figsize=(8, 5), dpi=80)
for f in files:
    if "GRLPA" in f:
        if "unweighted" in f:
            loss = pickle.load(open(f,"rb"))
            loss = np.asarray(loss)
            loss = loss[::2]
            x = range(len(loss))
            label = Judge_Label(f)   
            if label is not None:
                ax2.plot(x,loss,label=label)
                ax2.legend()                
plt.title("GRLPA model under unweighted-matrix") 
plt.savefig("GRLPA_unweighted.png")
fig3, ax3 = plt.subplots(figsize=(8, 5), dpi=80)
for f in files:
    if "DDPG" in f:
        loss = pickle.load(open(f,"rb"))
        loss = np.asarray(loss)
        loss = loss[::2]
        x = range(len(loss))
        label = Judge_Label(f)
        if label is not None:
            ax3.plot(x,loss,label=label)
            ax3.legend()                
plt.title("DDPG model under unweighted-matrix") 
plt.savefig("DDPG_loss.png")

附录:调用函数

## 获取L文件夹下的所有为.pkl的文件
def juduge_labdel(f):
    if "DDPG" in f:
        if "morning" in f:
            label="DDPG_morning"
        elif "noon" in f :
            label="DDPG_noon"
        elif "evening" in f or "eveining" in f:
            label="DDPG_evening"
        else :
            label= None
    if "GRLPA" in f:
        if "flow" in f:
            if "morning" in f:
                label="GRLPA_morning_flow"
            elif "noon" in f :
                label="GRLPA_noon_flow"
            elif "evening" in f or "eveining" in f:
                label="GRLPA_evening_flow"
            else:
                label= None
        else:
            if "morning" in f:
                label="GRLPA_morning_unweighted"
            elif "noon" in f :
                label="GRLPA_noon_unweighted"
            elif "evening" in f or "eveining" in f:
                label="GRLPA_evening_unweighted"
            else:
                label= None
    return label

def Judge_Label(f):
    if "morning" in f:
        label="morning"
    elif "noon" in f :
        label="noon"
    elif "evening" in f or "eveining" in f:
        label="evening"
    else:
        label= None
    return label
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值