假设y=XW+b用于图片的分类,最后训练出的W如下(随机取一个)
import matplotlib.pyplot as plt
import numpy as np
W = np.random.randn(32*32*3,10)
现在将W展示为10个类别的模板图片:
W = W.reshape(32,32,3,-1)
W_min, W_max = np.min(W), np.max(W)
for i in range(10):
plt.subplot(2, 5, i + 1)
plt.rcParams['figure.figsize'] = (8.0, 4.0) # 设置figure_size尺寸
# Rescale the weights to be between 0 and 255
wimg = 255.0 * (W[:, :, :, i].squeeze() - W_min) / (W_max - W_min)
#np.squeeze()从数组的形状中删除单维条目,即把shape中为1的维度去掉
plt.imshow(wimg.astype('uint8'))
plt.axis('off')
plt.title('label%d'%i)
plt.show()
最后的展示结果:
如果是训练好的W,那么展示的图片模板与所对应的类别将有一定的相似性。