我想对神经网络进行可视化,节点之间的线条的粗细和颜色需要与权重的值相对应(正的->红,负的->蓝,线条的粗细应该根据绝对值而变化),这一点与以下内容非常相似:
NN-SVG
下图所示。

我已经能够使用创建神经网络并显示权重值,如下面的代码和结果图所示。如何才能将代码更改为如上所示的绘图。
## Gist originally developed by @craffel and improved by @ljhuang2017
import matplotlib.pyplot as plt
import numpy as np
coefs_ = w
def draw_neural_net(ax, left, right, bottom, top, layer_sizes, coefs_):
'''
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)
# Input-Arrows
layer_top_0 = v_spacing*(layer_sizes[0] - 1)/2. + (top + bottom)/2.
for m in range(layer_sizes[0]):
plt.arrow(left-0.15, layer_top_0 - m*v_spacing, 0.12, 0, lw =0.05, head_width=0.01, head_length=0.02)
# 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):
circle = plt.Circle((n*h_spacing + left, layer_top - m*v_spacing), v_spacing/8.,
color='w', ec='k', zorder=4)
if n == 0:
plt.text(left-0.125, layer_top - m*v_spacing, r'$X_{'+str(m+1)+'}$', fontsize=15)
elif (n_layers == 3) & (n == 1):
plt.text(n*h_spacing + left+0.00, layer_top - m*v_spacing+ (v_spacing/8.+0.01*v_spacing), r'$H_{'+str(m+1)+'}$', fontsize=15)
elif n == n_layers -1:
plt.text(n*h_spacing + left+0.10, layer_top - m*v_spacing, r'$y_{'+str(m+1)+'}$', fontsize=15)
ax.add_artist(circle)
# Edges between nodes
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 range(layer_size_a):
for o in range(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)
xm = (n*h_spacing + left)
xo = ((n + 1)*h_spacing + left)
ym = (layer_top_a - m*v_spacing)
yo = (layer_top_b - o*v_spacing)
rot_mo_rad = np.arctan((yo-ym)/(xo-xm))
rot_mo_deg = rot_mo_rad*180./np.pi
xm1 = xm + (v_spacing/8.+0.05)*np.cos(rot_mo_rad)
if n == 0:
if yo > ym:
ym1 = ym + (v_spacing/8.+0.12)*np.sin(rot_mo_rad)
else:
ym1 = ym + (v_spacing/8.+0.05)*np.sin(rot_mo_rad)
else:
if yo > ym:
ym1 = ym + (v_spacing/8.+0.12)*np.sin(rot_mo_rad)
else:
ym1 = ym + (v_spacing/8.+0.04)*np.sin(rot_mo_rad)
plt.text( xm1, ym1,\
str(round(coefs_[n][m, o],4)),\
rotation = rot_mo_deg, \
fontsize = 10)
# Output-Arrows
layer_top_0 = v_spacing*(layer_sizes[-1] - 1)/2. + (top + bottom)/2.
for m in range(layer_sizes[-1]):
plt.arrow(right+0.011, layer_top_0 - m*v_spacing, 0.16*h_spacing, 0, lw =0.05, head_width=0.01, head_length=0.02)
fig = plt.figure(figsize=(18, 18))
ax = fig.gca()
ax.axis('off')
# layer_sizes = [7] + [10] + [7]
layer_sizes = nodes
draw_neural_net(ax, .1, .9, .1, .9, layer_sizes, coefs_)
fig.savefig('nn_digaram.png')

coefs的价值
_
有
array([matrix([[-0.94539221, 4.71558222, 1.18114055, -0.0247317 , 0.65897569,
-4.56814996, -3.35490889, 0.1859133 , 0.07424753, 6.90725199],
[-2.48287521, 3.14839658, 0.44249679, -0.32001095, -1.14217304,
-1.9226931 , -2.4374025 , 2.35347059, -0.54031149, 3.9080797 ],
[ 0.15713579, -0.10550088, 1.88657889, 1.69982016, -0.57003659,
0.23898249, 0.53788204, 0.58590049, 3.56283575, -1.52514785],
[ 2.78640583, 2.69520817, 2.91659772, 0.83917307, 2.63572781,
2.38591315, 1.16198163, 1.24653089, 1.43679067, 0.52219298],
[ 0.56640031, 1.56162596, 3.53929899, -4.92257699, 3.10732374,
-1.71019921, -1.45060365, -1.9656212 , -2.54415882, 0.57261768],
[-1.32074064, 0.94831387, 0.47141262, -3.64962347, 3.39212857,
0.77377745, 1.04295819, 7.11011713, 1.18975941, 1.59917691],
[ 0.977984 , 1.07923358, -2.81972891, 4.65242696, -3.10353591,
-0.68438475, -1.39959398, -1.22330595, 0.45223289, 0.65884348]]),
matrix([[-0.49489946, 0.17994493, -0.03041135, -1.2021098 , 2.00356709,
2.19315657, -2.39778753],
[ 0.00898019, -0.01341118, -1.42407278, -0.98645785, -1.23737296,
-1.31009782, 0.32579397],
[ 1.01009537, -0.08545276, 0.28988038, 3.46345191, -0.09588307,
0.90830877, -2.08828012],
[-4.97097194, 0.01332595, 0.58301252, 2.10843412, 0.37481177,
-1.83738501, 2.92358809],
[ 3.93275315, -0.02436187, 0.51174384, -2.15054043, -0.9096572 ,
-3.14182279, 2.7819789 ],
[ 0.04236115, 0.83878036, 0.38800743, 1.83881625, 0.95167318,
0.46700375, 1.17837088],
[ 0.86639556, 1.11735707, 0.76364426, 1.05078341, 0.7146457 ,
1.16666294, 0.75577211],
[ 2.31029687, 5.89363035, -6.65521314, 0.60303478, -0.87160711,
-1.47199575, -2.88012028],
[-0.12512919, 2.20846339, 6.21649468, -3.41157653, -1.29878851,
0.06757862, -2.03271134],
[ 0.59767784, -0.00900227, -1.79903724, -1.84302839, -1.68108604,
-1.36057165, -0.46816008]])], dtype=object)发布于 2021-03-01 18:13:32
答案取决于你想如何标准化你的权重。例如,如果您希望对网络的每一层进行标准化,则可以执行类似于以下内容的操作:
normed_coef = np.array([(c / np.max(np.abs(c)) + 1) /2 for c in coefs_])通过这种方式,您可以使用matplotlib颜色映射,如下所示:
import matplotlib
cmap = matplotlib.cm.get_cmap('RdBu_r')
c=cmap(normed_coef[n][m, o])把所有这些放在一起:
## Gist originally developed by @craffel and improved by @ljhuang2017
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
cmap = matplotlib.cm.get_cmap('RdBu_r')
coefs_ = np.array([np.matrix([[-0.94539221, 4.71558222, 1.18114055, -0.0247317 , 0.65897569,
-4.56814996, -3.35490889, 0.1859133 , 0.07424753, 6.90725199],
[-2.48287521, 3.14839658, 0.44249679, -0.32001095, -1.14217304,
-1.9226931 , -2.4374025 , 2.35347059, -0.54031149, 3.9080797 ],
[ 0.15713579, -0.10550088, 1.88657889, 1.69982016, -0.57003659,
0.23898249, 0.53788204, 0.58590049, 3.56283575, -1.52514785],
[ 2.78640583, 2.69520817, 2.91659772, 0.83917307, 2.63572781,
2.38591315, 1.16198163, 1.24653089, 1.43679067, 0.52219298],
[ 0.56640031, 1.56162596, 3.53929899, -4.92257699, 3.10732374,
-1.71019921, -1.45060365, -1.9656212 , -2.54415882, 0.57261768],
[-1.32074064, 0.94831387, 0.47141262, -3.64962347, 3.39212857,
0.77377745, 1.04295819, 7.11011713, 1.18975941, 1.59917691],
[ 0.977984 , 1.07923358, -2.81972891, 4.65242696, -3.10353591,
-0.68438475, -1.39959398, -1.22330595, 0.45223289, 0.65884348]]),
np.matrix([[-0.49489946, 0.17994493, -0.03041135, -1.2021098 , 2.00356709,
2.19315657, -2.39778753],
[ 0.00898019, -0.01341118, -1.42407278, -0.98645785, -1.23737296,
-1.31009782, 0.32579397],
[ 1.01009537, -0.08545276, 0.28988038, 3.46345191, -0.09588307,
0.90830877, -2.08828012],
[-4.97097194, 0.01332595, 0.58301252, 2.10843412, 0.37481177,
-1.83738501, 2.92358809],
[ 3.93275315, -0.02436187, 0.51174384, -2.15054043, -0.9096572 ,
-3.14182279, 2.7819789 ],
[ 0.04236115, 0.83878036, 0.38800743, 1.83881625, 0.95167318,
0.46700375, 1.17837088],
[ 0.86639556, 1.11735707, 0.76364426, 1.05078341, 0.7146457 ,
1.16666294, 0.75577211],
[ 2.31029687, 5.89363035, -6.65521314, 0.60303478, -0.87160711,
-1.47199575, -2.88012028],
[-0.12512919, 2.20846339, 6.21649468, -3.41157653, -1.29878851,
0.06757862, -2.03271134],
[ 0.59767784, -0.00900227, -1.79903724, -1.84302839, -1.68108604,
-1.36057165, -0.46816008]])], dtype=object)
def draw_neural_net(ax, left, right, bottom, top, layer_sizes, coefs_):
'''
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)
normed_coef = np.array([(c / np.max(np.abs(c)) + 1) /2 for c in coefs_])
# Input-Arrows
layer_top_0 = v_spacing*(layer_sizes[0] - 1)/2. + (top + bottom)/2.
for m in range(layer_sizes[0]):
plt.arrow(left-0.15, layer_top_0 - m*v_spacing, 0.12, 0, lw =0.05, head_width=0.01, head_length=0.02)
# 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):
circle = plt.Circle((n*h_spacing + left, layer_top - m*v_spacing), v_spacing/8.,
color='w', ec='k', zorder=4)
if n == 0:
plt.text(left-0.125, layer_top - m*v_spacing, r'$X_{'+str(m+1)+'}$', fontsize=15)
elif (n_layers == 3) & (n == 1):
plt.text(n*h_spacing + left+0.00, layer_top - m*v_spacing+ (v_spacing/8.+0.01*v_spacing), r'$H_{'+str(m+1)+'}$', fontsize=15)
elif n == n_layers -1:
plt.text(n*h_spacing + left+0.10, layer_top - m*v_spacing, r'$y_{'+str(m+1)+'}$', fontsize=15)
ax.add_artist(circle)
# Edges between nodes
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 range(layer_size_a):
for o in range(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=cmap(normed_coef[n][m, o]))
ax.add_artist(line)
xm = (n*h_spacing + left)
xo = ((n + 1)*h_spacing + left)
ym = (layer_top_a - m*v_spacing)
yo = (layer_top_b - o*v_spacing)
rot_mo_rad = np.arctan((yo-ym)/(xo-xm))
rot_mo_deg = rot_mo_rad*180./np.pi
xm1 = xm + (v_spacing/8.+0.05)*np.cos(rot_mo_rad)
if n == 0:
if yo > ym:
ym1 = ym + (v_spacing/8.+0.12)*np.sin(rot_mo_rad)
else:
ym1 = ym + (v_spacing/8.+0.05)*np.sin(rot_mo_rad)
else:
if yo > ym:
ym1 = ym + (v_spacing/8.+0.12)*np.sin(rot_mo_rad)
else:
ym1 = ym + (v_spacing/8.+0.04)*np.sin(rot_mo_rad)
plt.text( xm1, ym1,\
str(round(coefs_[n][m, o],4)),\
rotation = rot_mo_deg, \
fontsize = 10)
# Output-Arrows
layer_top_0 = v_spacing*(layer_sizes[-1] - 1)/2. + (top + bottom)/2.
for m in range(layer_sizes[-1]):
plt.arrow(right+0.011, layer_top_0 - m*v_spacing, 0.16*h_spacing, 0, lw =0.05, head_width=0.01, head_length=0.02)
fig = plt.figure(figsize=(18, 18))
ax = fig.gca()
ax.axis('off')
layer_sizes = [7] + [10] + [7]
# layer_sizes = nodes
draw_neural_net(ax, .1, .9, .1, .9, layer_sizes, coefs_)
# fig.savefig('nn_digaram.png')

https://stackoverflow.com/questions/66419133
复制相似问题