def show_images(images, labels, preds): plt.figure(figsize=(8, 4)) for i, image in enumerate(images): plt.subplot(1, 6, i + 1, xticks=[], yticks=[]) image = image.numpy().transpose((1, 2, 0)) mean = np.array([0.485, 0.456, 0.406]) std = np.array([0.229, 0.224, 0.225]) image = image * std + mean image = np.clip(image, 0., 1.) plt.imshow(image) col = 'green' if preds[i] != labels[i]: col = 'red' plt.xlabel(f'{class_names[int(labels[i].numpy())]}') plt.ylabel(f'{class_names[int(preds[i].numpy())]}', color=col) plt.tight_layout() plt.show()
#------------------------------------------------------训练------------------------------------------------- epochs = 15 batch_size = 128 history = model.fit(x_train, x_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, x_test)) #-----------------------------------------------------查看解码效果-------------------------------------------- decoded_imgs = model.predict(x_test) n = 10 plt.figure(figsize=(20, 6)) for i in range(n): # 原图 ax = plt.subplot(3, n, i+1) plt.imshow(x_test[i].reshape(28, 28)) plt.gray() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) # 解码效果图 ax = plt.subplot(3, n, i+n+1) plt.imshow(decoded_imgs[i].reshape(28, 28)) plt.gray() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.show() #----------------------------------------------------训练过程可视化---------------------------------------------