由于你有一个“形状转换:(14,14,512)”输出并且你标记了你的问题“tensorflow”,我会假设你没有使用Theano后端并且 “image_data_format”是“channels_last” 。我自己没有使用Theano,但根据我的搜索,Thenao后端默认情况下可能有“channels_first”。因此,在循环输出图层时:
for i in range(len(convolutions)): ax = fig.add_subplot(n,n,i+1) ax.imshow(convolutions[i], cmap='viridis')
实际上你正在绘制14个图像,每个图像为14x512,而不是512个图像,每个图像为14x14(我认为这是你想要的)。
一个简单的(意思是你可以使用你已编码的功能)修复是通过添加设置“image_data_format”“channels_first” K.set_image_data_format('channels_first') 到你的笔记本/脚本的顶部。但是,此修复程序可能与您的其他代码冲突。在这种情况下,您可以重写图层可视化功能。这是一个适用的例子 https://github.com/keras-team/keras/blob/master/examples/mnist_cnn.py
K.set_image_data_format('channels_first')
import matplotlib.pyplot as plt import numpy as np def visualize_layer(model, layer, input, train_mode=False): get_layer_output = K.function([model.input, K.learning_phase()], [layer.output]) layer_output = get_layer_output([input, int(train_mode)])[0] print('Shape of {} layer output: {}'.format(layer, layer_output.shape)) for i, sample in enumerate(layer_output): n_img = sample.shape[-1] img_row = int(np.ceil(np.sqrt(n_img))) fig = plt.figure() for j in range(n_img): ax = fig.add_subplot(img_row, img_row, j+1) ax.imshow(sample[:, :, j], cmap='gray') fig.savefig('sample_{}.png'.format(i)) visualize_layer(model, model.layers[1], [x_train[0]])