Google Colab中的实时标准化区域图

发布于 2025-01-21 13:44:01 字数 367 浏览 1 评论 0原文

我正在培训Google Colab上的二进制分类的神经网络。每个时期,我都会在验证数据集中对其进行评估,并计算真实阳性,误报,真实负面因素和虚假负面的百分比。我想看到一个实时的归一化堆叠区域图(请参见 https:// altair-viz。 github.io/gallery/normalized_stacked_area_chart.html 对于这四个数字的解释)。随着培训过程的进行,每个时期都应更新。我该如何实现?我准备使用任何第三方库。

I am training a neural network for binary classification on Google Colab. Each epoch, I evaluate it on the validation dataset and calculate the percentages of true positives, false positives, true negatives, and false negatives. I want to see a live normalized stacked area chart (see https://altair-viz.github.io/gallery/normalized_stacked_area_chart.html for an explanation of what that is) of these four numbers. It should get updated with each epoch as the training process goes on. How do I achieve this? I am ready to use any 3rd party library.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦魇绽荼蘼 2025-01-28 13:44:01

解决方案

我认为您每个时期都保存目标度量。

  • tf:true正面
  • tn
  • true nater Code> fp :false strusy
  • fn:false Nater

注意:理想情况下,您将创建一个字典列表,其中每个字典由您想要的指标和相应的时期编号组成,如下所示:

 #调用此列表结果
结果= [ 
  ...
  {“ Epoch”:0,“ TP”:200,“ TN”:80,“ FP”:18,“ FN”:5}
  ... 
这是给出的
 

使用此列表创建PANDAS DataFrame,然后使用自定义绘图函数如下所示。

  df = pf.dataframe(结果).t
 

下面的解决方案使用 plotly 创建所需的库图表。这是Jupyter笔记本(带有Google Colab链接),可以快速检查提出的解决方案。

使交互式堆叠的归一化区域图

figure_title="Confusion Matrix Evolution over Model Training Epochs"
columns = ["tp", "tn", "fp", "fn"]
colors = ['#d7191c','#fdae61','#abdda4','#2b83ba']
palette = dict((column, color) for column, color in zip(columns, colors))

# create interactive chart with user-defined function
make_stacked_normalized_chart(
    df, 
    x="epoch", 
    columns=columns, 
    palette=palette, 
    figure_title=figure_title,
)

​区域形式。

import plotly.graph_objects as go
from typing import List, Dict

def make_stacked_normalized_chart(df: pd.DataFrame, x: str, 
                                  columns: List[str], 
                                  palette: Dict[str, str], 
                                  figure_title: str="Figure Title"):
    """Create a stacked normalized interactive chart with Plotly library."""
    x_label = x
    x = df[x_label]
    fig = go.Figure()

    def add_trace(column: str):
        fig.add_trace(go.Scatter(
            x=x, y=df[column],
            text=column, # set the name shown while hovering over
            name=column, # set the name in the legend
            # fill='toself',
            hoveron = 'points+fills', # select where hover is active
            hoverinfo='text+x+y',
            mode='lines',        
            line=dict(width=0.5, color=palette.get(column)),
            stackgroup='one', # define stack group
            groupnorm='percent', # sets the normalization for the sum of the stackgroup
        ))

    for column in columns:
        add_trace(column)

    fig.update_layout(
        title_text=figure_title,
        showlegend=True,
        xaxis=dict(
            type="category",
            title=x_label,
        ),
        yaxis=dict(
            type='linear',
            range=[1, 100],
            ticksuffix='%',
        ),
    )
    fig.show()

虚拟数据

我们将使用以下数据来演示交互式堆叠式构型-AREA-CHART

import numpy as np
import pandas as pd

np.random.seed(42)

nrows = 100
x = np.arange(nrows)
tp = 60 + np.arange(nrows) + np.random.randint(0, 20, nrows)
tn = 25 + np.arange(nrows) + np.random.randint(0, 20, nrows)
fp = np.random.randint(2, 6, nrows) + np.random.randint(0, 8, nrows)
fn = np.random.randint(4, 7, nrows) + np.random.randint(3, 6, nrows)

df = pd.DataFrame({"epoch": x, "tp": tp, "tn": tn, "fp": fp, "fn": fn})

Solution

I assume that you are saving your target metric after every epoch.

  • tf: True Positive
  • tn: True Negative
  • fp: False Positive
  • fn: False Negative

Note: Ideally, you would create a list of dictionaries, where each dictionary consists of the metrics you want and the corresponding epoch number as follows:

# call this list results
results = [ 
  ...
  {"epoch": 0, "tp": 200, "tn": 80, "fp": 18, "fn": 5}
  ... 
]

Use this list to create a pandas dataframe and then use the custom plotting function as shown below.

df = pf.DataFrame(results).T

The solution below uses Plotly library to create the desired chart. Here is a jupyter notebook (with a google colab link) to quickly check-out the proposed solution.

Make Interactive Stacked Normalized Area Chart

figure_title="Confusion Matrix Evolution over Model Training Epochs"
columns = ["tp", "tn", "fp", "fn"]
colors = ['#d7191c','#fdae61','#abdda4','#2b83ba']
palette = dict((column, color) for column, color in zip(columns, colors))

# create interactive chart with user-defined function
make_stacked_normalized_chart(
    df, 
    x="epoch", 
    columns=columns, 
    palette=palette, 
    figure_title=figure_title,
)

enter image description here

Define Custom Plotting Function

Here we define a custom function (make_stacked_normalized_chart()) to create an interactive-stacked-normalized-area-chart.

import plotly.graph_objects as go
from typing import List, Dict

def make_stacked_normalized_chart(df: pd.DataFrame, x: str, 
                                  columns: List[str], 
                                  palette: Dict[str, str], 
                                  figure_title: str="Figure Title"):
    """Create a stacked normalized interactive chart with Plotly library."""
    x_label = x
    x = df[x_label]
    fig = go.Figure()

    def add_trace(column: str):
        fig.add_trace(go.Scatter(
            x=x, y=df[column],
            text=column, # set the name shown while hovering over
            name=column, # set the name in the legend
            # fill='toself',
            hoveron = 'points+fills', # select where hover is active
            hoverinfo='text+x+y',
            mode='lines',        
            line=dict(width=0.5, color=palette.get(column)),
            stackgroup='one', # define stack group
            groupnorm='percent', # sets the normalization for the sum of the stackgroup
        ))

    for column in columns:
        add_trace(column)

    fig.update_layout(
        title_text=figure_title,
        showlegend=True,
        xaxis=dict(
            type="category",
            title=x_label,
        ),
        yaxis=dict(
            type='linear',
            range=[1, 100],
            ticksuffix='%',
        ),
    )
    fig.show()

Dummy Data

We will use the following data to demonstrate the interactive-stacked-normalized-area-chart.

import numpy as np
import pandas as pd

np.random.seed(42)

nrows = 100
x = np.arange(nrows)
tp = 60 + np.arange(nrows) + np.random.randint(0, 20, nrows)
tn = 25 + np.arange(nrows) + np.random.randint(0, 20, nrows)
fp = np.random.randint(2, 6, nrows) + np.random.randint(0, 8, nrows)
fn = np.random.randint(4, 7, nrows) + np.random.randint(3, 6, nrows)

df = pd.DataFrame({"epoch": x, "tp": tp, "tn": tn, "fp": fp, "fn": fn})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文