40 行 Python 代码,实现卷积特征可视化_torch 特征可视化 激活最大化(1)

特征可视化

神经网络学习将输入数据(如图像)转换为越来越有意义但表征越来越复杂的连续层。

你可以将深度网络看做一个多阶段信息蒸馏操作,其中信息通过连续的滤波器并不断被「提纯」。(François Chollet, Deep Learning with Python (Shelter Island, NY: Manning Publications, 2018), p. 9)

阅读完他的文章后,你将了解如何生成模式,以最大化这些层次表征的某个层中所选特征图的平均激活,如何解释其中一些可视化,以及最终如何测试所选滤波器可能响应的模式或纹理的假设。你可以在下面找到 VGG-16 网络多个层中滤波器的特征可视化。在查看它们时,希望你能观察到生成模式的复杂性如何随着进入网络的深度而增加。

Layer 7: Conv2d(64, 128)

img

滤波器 12, 16, 86, 110(左上到右下,逐行)

Layer 14: Conv2d(128, 256)

img

滤波器 1, 6, 31, 32, 54, 77, 83, 97, 125, 158, 162, 190(左上到右下,逐行)

Layer 20: Conv2d(256, 256)

img

滤波器 3, 34, 39, 55, 62, 105, 115, 181, 231(左上到右下,逐行)

Layer 30: Conv2d(512, 512)

img

img

滤波器 54, 62, 67, 92, 123, 141, 150, 172, 180, 213, 233, 266, 277, 293, 331, 350, 421, 427(左上到右下,逐行)

Layer 40: Conv2d(512, 512)—top of the network

img

img

滤波器 4, 9, 34, 35, 75, 123, 150, 15

  • 17
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用Python和常见的深度学习库(如TensorFlow或PyTorch)进二维卷积和t-SNE可视化的示例代码: ```python import numpy as np import matplotlib.pyplot as plt from sklearn.manifold import TSNE import tensorflow as tf from tensorflow.keras.datasets import mnist # 加载MNIST数据集 (train_images, _), (_, _) = mnist.load_data() # 数据预处理 train_images = train_images.reshape(train_images.shape[0], 28, 28, 1) train_images = train_images.astype('float32') / 255 # 创建卷积神经网络模型(这里以TensorFlow为例) model = tf.keras.models.Sequential([ tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)), tf.keras.layers.MaxPooling2D((2, 2)), tf.keras.layers.Flatten(), tf.keras.layers.Dense(64, activation='relu'), tf.keras.layers.Dense(10, activation='softmax') ]) # 编译模型 model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) # 训练模型 model.fit(train_images, np.zeros(len(train_images)), epochs=1, batch_size=64) # 获取卷积层的输出特征 conv_layer = model.layers[0] conv_output = conv_layer.output # 创建新的模型,输出为卷积层的特征 feature_model = tf.keras.models.Model(inputs=model.input, outputs=conv_output) # 提取卷积特征 conv_features = feature_model.predict(train_images) # 使用t-SNE进降维 tsne = TSNE(n_components=2) tsne_features = tsne.fit_transform(conv_features.reshape(conv_features.shape[0], -1)) # 可视化 plt.scatter(tsne_features[:, 0], tsne_features[:, 1], c='b', marker='o') plt.title('t-SNE Visualization of Convolutional Features') plt.show() ``` 这段代码使用MNIST数据集作为示例数据,创建了一个简单的卷积神经网络模型,并提取了卷积层的输出特征。然后使用t-SNE对提取的特征降维,并将降维后的特征用散点图进可视化展示。 请注意,这只是一个示例代码,实际应用中可能需要根据具体任务和数据集进适当的调整和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值