加入收藏 | 设为首页 | 会员中心 | 我要投稿 甘孜站长网 (https://www.0836zz.com.cn/)- 运维、物联设备、数据计算、智能推荐、云管理!
当前位置: 首页 > 运营中心 > 建站资源 > 经验 > 正文

看懂这篇指南,包你掌握神经网络的“黑匣子”

发布时间:2019-06-04 23:54:13 所属栏目:经验 来源:读芯术
导读:人类的神经网络是如何运行的?这个问题让很多数据科学家感到困惑。解释某个简单神经网络的工作机制非常容易,但是当某个计算机视觉项目中的层数增加1000倍时,该怎么办呢? 终端用户想要了解模型是如何得到最终结果时,仅用纸和笔来解释深度神经网络的工作机

上述代码的输出结果如下所示,包含了block5_conv1层的不同参数:

  1. {'name': 'block5_conv1', 
  2.  'trainable': True, 
  3.  'filters': 512, 
  4.  'kernel_size': (3, 3), 
  5.  'strides': (1, 1), 
  6.  'padding': 'same', 
  7.  'data_format': 'channels_last', 
  8.  'dilation_rate': (1, 1), 
  9.  'activation': 'relu', 
  10.  'use_bias': True, 
  11.  'kernel_initializer': {'class_name': 'VarianceScaling', 
  12.   'config': {'scale': 1.0, 
  13.    'mode': 'fan_avg', 
  14.    'distribution': 'uniform', 
  15.    'seed': None}}, 
  16.  'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 
  17.  'kernel_regularizer': None, 
  18.  'bias_regularizer': None, 
  19.  'activity_regularizer': None, 
  20.  'kernel_constraint': None, 
  21.  'bias_constraint': None} 

’block5_conv1’层的可训练参数值是真实的,这意味着之后可以通过进一步模型训练来更新权重。

过滤器——卷积神经网络构件的可视化

过滤器是卷积神经网络的基本组成部分。如下图所示,不同的过滤器会从图像中提取不同类型的特征:

看懂这篇指南,包你掌握神经网络的“黑匣子”

如图所示,每个卷积层都由多个过滤器组成。回顾上一节中提到的‘block5_conv1’层的参数概要显示了该层含有512个过滤器,确实是这个道理。

通过下列编码,可以绘制每VGG16模块的第一个卷积层的首个过滤器:

  1. layers = model.layers 
  2. layer_ids = [1,4,7,11,15] 
  3. #plot the filters 
  4. fig,ax = plt.subplots(nrows=1,ncols=5) 
  5. for i in range(5): 
  6.     ax[i].imshow(layers[layer_ids[i]].get_weights()[0][:,:,:,0][:,:,0],cmap='gray') 
  7.     ax[i].set_title('block'+str(i+1)) 
  8.     ax[i].set_xticks([]) 
  9.     ax[i].set_yticks([]) 

看懂这篇指南,包你掌握神经网络的“黑匣子”

以上输出结果即为不同层的过滤器。由于VGG16只使用3×3过滤器,因此所有过滤器形状大小都相同。

激活最大化——将模型所期望的进行可视化

通过下面的图片来理解最大激活的概念:

看懂这篇指南,包你掌握神经网络的“黑匣子”

在识别大象的过程中,哪些特征比较重要?

下面是一些较容易想到的特征。

  • 獠牙
  • 象鼻
  • 耳朵

这就是人类凭直觉判别大象的方式。但是,使用卷积神经网络优化随机图像,并尝试将其归类为大象时,会得到什么结果呢?

卷积神经网络中,每个卷积层都在前一层的输出中寻找相似的模式。当输入包含其正在寻找的模式时,就能实现最大激活。

在激活最大化技术中,更新每一层的输入,使该过程中的损失达到最小值。

应该怎么做呢?首先需要计算激活损失相对于输入的梯度,并据此更新输入。

看懂这篇指南,包你掌握神经网络的“黑匣子”

以下为所述方法的代码:

  1. #importing the required modules 
  2. from vis.visualization import visualize_activation 
  3. from vis.utils import utils 
  4. from keras import activations 
  5. from keras import applications 
  6. import matplotlib.pyplot as plt 
  7. %matplotlib inline 
  8. plt.rcParams['figure.figsize'] = (18,6) 
  9. #creating a VGG16 model using fully connected layers also because then we can  
  10. #visualize the patterns for individual category 
  11. from keras.applications import VGG16 
  12. model = VGG16(weights='imagenet',include_top=True) 
  13.  
  14. #finding out the layer index using layer name 
  15. #the find_layer_idx function accepts the model and name of layer as parameters and return the index of respective layer 
  16. layer_idx = utils.find_layer_idx(model,'predictions') 
  17. #changing the activation of the layer to linear 
  18. model.layers[layer_idx].activation = activations.linear 
  19. #applying modifications to the model 
  20. model = utils.apply_modifications(model) 
  21. #Indian elephant 
  22. img3 = visualize_activation(model,layer_idx,filter_indices=385,max_iter=5000,verbose=True) 
  23. plt.imshow(img3) 

(编辑:甘孜站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读