Graphviz模型可视化

  1. sudo apt-get install graphviz
  2. pip install graphviz
  3. coding
def make_dot(vars, params = None):
    """ Produces Graphviz representation of PyTorch autograd graph
    Blue nodes are the Variables that require grad, orange are Tensors
    saved for backward in torch.autograd.Function
    Args:
        var: output Variable
        params: dict of (name, Variable) to add names to node that
            require grad (TODO: make optional)
    """
    if params is not None:
        assert isinstance(params.values()[0], Variable)
        param_map = {id(v): k for k, v in params.items()}

    node_attr = dict(style='filled',
                     shape='box',
                     align='left',
                     fontsize='12',
                     ranksep='0.1',
                     height='0.2')
    dot = Digraph(node_attr=node_attr, graph_attr=dict(size="12,12"))
    seen = set()

    def size_to_str(size):
        return '('+(', ').join(['%d' % v for v in size])+')'

    def add_nodes(var):
        if var not in seen:
            if torch.is_tensor(var):
                dot.node(str(id(var)), size_to_str(var.size()), fillcolor='orange')
            elif hasattr(var, 'variable'):
                u = var.variable
                name = param_map[id(u)] if params is not None else ''
                node_name = '%s\n %s' % (name, size_to_str(u.size()))
                dot.node(str(id(var)), node_name, fillcolor='lightblue')
            else:
                dot.node(str(id(var)), str(type(var).__name__))
            seen.add(var)
            if hasattr(var, 'next_functions'):
                for u in var.next_functions:
                    if u[0] is not None:
                        dot.edge(str(id(u[0])), str(id(var)))
                        add_nodes(u[0])
            if hasattr(var, 'saved_tensors'):
                for t in var.saved_tensors:
                    dot.edge(str(id(t)), str(id(var)))
                    add_nodes(t)
    for var in vars:
        add_nodes(var.grad_fn)
    return dot


def main()
    model.cuda()
    model.eval()
    output=model.forward(data)
    g = make_dot(output[-1])
    g.view('Digraph.gv',directory="./")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要使用Graphviz可视化决策树,你可以按照以下步骤进行操作: 1. 首先,确保已经安装了Graphviz软件包。你可以从Graphviz官方网站(https://graphviz.org/)下载并安装适用于你的操作系统的版本。 2. 在Python中,你需要安装`graphviz`包,可以使用pip命令进行安装:`pip install graphviz`。 3. 导入必要的库和模块: ```python from sklearn import tree import graphviz ``` 4. 准备决策树的数据集,并进行训练: ```python # 假设你有一个特征矩阵X和一个目标向量y X = [[0, 0], [1, 1]] y = [0, 1] # 创建并训练决策树模型 clf = tree.DecisionTreeClassifier() clf = clf.fit(X, y) ``` 5. 生成决策树的dot文件: ```python # 使用export_graphviz函数生成dot文件 dot_data = tree.export_graphviz(clf, out_file=None) # 或者,你可以指定一些参数来自定义可视化效果,例如: dot_data = tree.export_graphviz(clf, out_file=None, feature_names=['feature1', 'feature2'], class_names=['class1', 'class2'], filled=True, rounded=True) ``` 6. 将dot文件转换为可视化图形: ```python # 使用graphviz库将dot文件转换为可视化图形 graph = graphviz.Source(dot_data) ``` 7. 显示决策树可视化图形: ```python # 在Jupyter Notebook或类似的环境中,可以直接显示图形 graph # 如果你使用的是其他IDE或脚本方式运行,可以将图形保存为文件并打开 graph.render("decision_tree") ``` 这样,你就可以使用Graphviz可视化决策树了。记得替换掉步骤4中的数据集和步骤5中的特征名和类别名,以适应你的实际情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

syvge

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值