python - 绘制神经网络

标签 python python-3.x matplotlib

我正在尝试用 python 绘制神经网络图,到目前为止我已经能够使用这个脚本。

import matplotlib.pyplot as plt

def draw_neural_net(ax, left, right, bottom, top, layer_sizes):
    '''
    Draw a neural network cartoon using matplotilb.
    
    :usage:
        >>> fig = plt.figure(figsize=(12, 12))
        >>> draw_neural_net(fig.gca(), .1, .9, .1, .9, [4, 7, 2])
    
    :parameters:
        - ax : matplotlib.axes.AxesSubplot
            The axes on which to plot the cartoon (get e.g. by plt.gca())
        - left : float
            The center of the leftmost node(s) will be placed here
        - right : float
            The center of the rightmost node(s) will be placed here
        - bottom : float
            The center of the bottommost node(s) will be placed here
        - top : float
            The center of the topmost node(s) will be placed here
        - layer_sizes : list of int
            List of layer sizes, including input and output dimensionality
    '''
    n_layers = len(layer_sizes)
    v_spacing = (top - bottom)/float(max(layer_sizes))
    h_spacing = (right - left)/float(len(layer_sizes) - 1)
    # Nodes
    for n, layer_size in enumerate(layer_sizes):
        layer_top = v_spacing*(layer_size - 1)/2. + (top + bottom)/2.
        for m in xrange(layer_size):
            circle = plt.Circle((n*h_spacing + left, layer_top - m*v_spacing), v_spacing/4.,
                                color='w', ec='k', zorder=4)
            ax.add_artist(circle)
    # Edges
    for n, (layer_size_a, layer_size_b) in enumerate(zip(layer_sizes[:-1], layer_sizes[1:])):
        layer_top_a = v_spacing*(layer_size_a - 1)/2. + (top + bottom)/2.
        layer_top_b = v_spacing*(layer_size_b - 1)/2. + (top + bottom)/2.
        for m in xrange(layer_size_a):
            for o in xrange(layer_size_b):
                line = plt.Line2D([n*h_spacing + left, (n + 1)*h_spacing + left],
                                  [layer_top_a - m*v_spacing, layer_top_b - o*v_spacing], c='k')
                ax.add_artist(line)

fig = plt.figure(figsize=(12, 12))
ax = fig.gca()
ax.axis('off')
draw_neural_net(ax, .1, .9, .1, .9, [4, 7, 2])
fig.savefig('nn.png')

enter image description here
我想要做的是复制下图中的隐藏层,将隐藏层和输出层节点一分为二来表示节点的内部和激活状态。
有没有办法拆分 plt.circle或添加连接两个半圆以获得这样的表示?
enter image description here

最佳答案

于是四处测试了一下,解决的方法也比较简单。在 matplotlib 中有 Wedge补丁,它可以创建半圆。
对于解决方案,我只需要更新 Nodes部分,我使用了以下代码
旁注我使用的是python 3.9,因此我更改了xrangerange .

# Nodes
for n, layer_size in enumerate(layer_sizes):
    layer_top = v_spacing * (layer_size - 1) / 2. + (top + bottom) / 2.

    for m in range(layer_size):
        center = (n * h_spacing + left, layer_top - m * v_spacing)
        radius = v_spacing / 4.

        if n > 0:
            # Hidden Layers
            wedge_left = Wedge(center, r=radius, theta1=90, theta2=270, color='w', fc='g', ec='k', zorder=4)
            wedge_right = Wedge(center, r=radius, theta1=270, theta2=90, color='w', fc='r', ec='k', zorder=4)

            ax.add_artist(wedge_left)
            ax.add_artist(wedge_right)
        else:
            # None hidden layers
            circle = plt.Circle(center, radius, color='w', ec='k', zorder=4)
            ax.add_artist(circle)
这会生成两个单独的半圆(楔形),一个面向左,一个面向右。为了区分它们,我给了它们 facecolor分别是绿色和红色。运行您的原始代码,将产生以下图像:
enter image description here

关于python - 绘制神经网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67279657/

相关文章:

python - 了解 Python 3 中的 XML 和 XSD 解析

python - 如何在列表中查找重复项,但忽略第一次出现的项?

python - 如何在Python中有效地搜索和访问数据框中的某些单元格?

python - 试图导入模块 : undefined symbol: PyUnicodeUCS4_DecodeUTF8

导入 matplotlib 时出现 Python Unicode 解码错误

python - 如何从 LinearSegmentedColormap 获取所有颜色代码?

python - 保存 Matplotlib 动画

python - 将嵌套字典替换为空数据帧

python - 在枚举中显示可选成员

python-3.x - 如何使用云函数触发数据流? (Python SDK)