Python-ValueError:太多值无法解包(预期2)2D数组

时间:2018-10-31 11:33:01

标签: python numpy matplotlib

我正在尝试从CNN输出的权重中输出特征图。 Full code can be found here.

当我尝试运行以下功能时。

import matplotlib.pyplot as plt
import matplotlib.colors as cm

def plot_filters(layer, x, y):

filters = layer.get_weights()
fig = plt.figure()

for j in range(len(filters)):
    ax = fig.add_subplot(y, x, j+1)
    ax.matshow(filters[j][0], cmap = cm.binary)
    plt.xticks(np.array([]))
    plt.yticks(np.array([]))

plt.tight_layout()

我得到以下TraceBack:

Traceback (most recent call last):
File "PATH\Code\CNN.py", line 85, in <module>
plot_filters(model.layers[0], 8, 4)
File "PATH\Code\CNN.py", line 21, in plot_filters
ax.matshow(filters[j][0], cmap = plt.cm.binary)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64     \lib\site-packages\matplotlib\axes\_axes.py", line 7841, in matshow
nr, nc = Z.shape
ValueError: too many values to unpack (expected 2)

我运行了print(filters[j][0].shape),它输出(3, 1, 32) 我的数据集中的图片是20x20x1,下面是我的模型摘要的摘要。

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 9, 9, 32)          320       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 9, 9, 32)          0         
_________________________________________________________________

任何有关如何显示要素地图的帮助将不胜感激!

非常感谢您!

0 个答案:

没有答案