网络智能总能超乎你的想象:胶囊网络 一种全新的富有吸引力的AI架构
网络智能总能超乎你的想象:胶囊网络 一种全新的富有吸引力的AI架构那么我们先创建一个用于对MNIST数据集进行分类的分步胶囊体系结构:就像深层CNN的不同层数学习图像的不同语义属性(内容、纹理、样式等)一样,胶囊也可以被组织成不同的层次。同一层的胶囊进行预测,学习对象的形状,并将其传递给学习方向的更高层次的胶囊。当多个预测一致时,更高级别的胶囊变得活跃。这个过程被描述为动态路由,现在我将更详细地讨论它。这些挤压函数的输出告诉我们如何通过训练成学习不同思维的各种胶囊来路由数据。图像中每个对象的属性都用路由它们的向量来表示。例如,面部的激活可以将图像的不同部分路由到能够理解眼睛、鼻子、嘴和耳朵的胶囊。胶囊网络的角度估计下一步至关重要:
挤压函数
在深度神经网络中,激活函数是应用于层输出的简单数学运算。它们用于近似数据中存在的非线性关系。激活层通常对标量值起作用,例如,对向量中的每个元素进行规范化,使其介于0和1之间。
在胶囊网络中,一种称为挤压函数的特殊类型的激活函数被用来归一化矢量的大小,而不是标量元素本身。
协议路由算法
这些挤压函数的输出告诉我们如何通过训练成学习不同思维的各种胶囊来路由数据。图像中每个对象的属性都用路由它们的向量来表示。例如,面部的激活可以将图像的不同部分路由到能够理解眼睛、鼻子、嘴和耳朵的胶囊。
针对MNIST数据集的胶囊网络应用
胶囊网络的角度估计
下一步至关重要:
就像深层CNN的不同层数学习图像的不同语义属性(内容、纹理、样式等)一样,胶囊也可以被组织成不同的层次。同一层的胶囊进行预测,学习对象的形状,并将其传递给学习方向的更高层次的胶囊。当多个预测一致时,更高级别的胶囊变得活跃。这个过程被描述为动态路由,现在我将更详细地讨论它。
那么我们先创建一个用于对MNIST数据集进行分类的分步胶囊体系结构:
第一层为典型的卷积层。在第二层中,在称为初级胶囊 的层中执行卷积过程,其中应用了挤压函数。每个主胶囊接收图像的一个小区域作为输入(称为感受野),它用来检测特定图案(如圆圈)的存在和姿态。
较高层的胶囊(称为路由胶囊)检测更大和更复杂的物体,如由两个圆组成都的数字8。然后他们使用一个新的挤压函数来保证这些向量的长度在0-1之间。
在初级胶囊层之前应用标准卷积层,并且得到9x9x256的输出。在初级胶囊层中采用32通道、步长为2的卷积核。然而,这个区别于其他卷积核的特征就是挤压函数。最后就得到了初级胶囊的输出。
初级胶囊的卷积过程
此时可以得到6x6的输出。因为在应用了动态路由算法的胶囊层中,其具有第三层动态路由的结果(根据协议路由算法),即获得这些8-长度输出DigitCaps矢量的32个输出。协议路由算法包括一些协议(检测和路由)更新的迭代。
胶囊层
def CapsNet(input_shape n_class num_routing):
"""
MNIST Dataset for Capsule Networks.
: "input_shape" parameter: vdata shape 3d [w h c]
: "n_class" parameter: number of classes
: "num_routing" parameter: dynamic routing number of iteration
: Function output: two Keras model first for training second for evalaution.
`eval_model` used for traning at the same time.
"""
x = layers.Input(shape=input_shape )
# LAYER 1: Convolution Layer (Conv2D)
conv1 = layers.Conv2D(filters=256 kernel_size=9 strides=1 padding='valid' activation='relu' name='conv1')(x)
# LAYER 2: Conv2D squash activation [None num_capsule dim_capsule] fro reshaping.
primarycaps = PrimaryCap(conv1 dim_capsule=8 n_channels=32 kernel_size=9 strides=2 padding='valid')
# LAYER 3: Capsule Layers. Run: Dynamic routing algorithm.
digitcaps = CapsuleLayer(num_capsule=n_class dim_capsule=16 num_routing=num_routing
name='digitcaps')(primarycaps)
# LAYER 4:
# If you use Tensorflow you can skip this session :)
out_caps = Length(name='capsnet')(digitcaps)
capsulelayers.py中的动态路由类CapsuleLayer (layers.Layer)函数定义。由于这个计算步骤,在图像中不存在对象的区域中向量值很小,而检测区域中向量的维度根据属性而变化。
class CapsuleLayer(layers.Layer):
"""
The capsule layer. It is similar to Dense layer. Dense layer has `in_num` inputs each is a scalar the output of the
neuron from the former layer and it has `out_num` output neurons. CapsuleLayer just expand the output of the neuron
from scalar to vector. So its input shape = [None input_num_capsule input_dim_capsule] and output shape = \
[None num_capsule dim_capsule]. For Dense Layer input_dim_capsule = dim_capsule = 1.
:param num_capsule: number of capsules in this layer
:param dim_capsule: dimension of the output vectors of the capsules in this layer
:param routings: number of iterations for the routing algorithm
"""
def __init__(self num_capsule dim_capsule routings=3
kernel_initializer='glorot_uniform'
**kwargs):
super(CapsuleLayer self).__init__(**kwargs)
self.num_capsule = num_capsule
self.dim_capsule = dim_capsule
self.routings = routings
self.kernel_initializer = initializers.get(kernel_initializer)
def build(self input_shape):
assert len(input_shape) >= 3 "The input Tensor should have shape=[None input_num_capsule input_dim_capsule]"
self.input_num_capsule = input_shape[1]
self.input_dim_capsule = input_shape[2]
# Transform matrix
self.W = self.add_weight(shape=[self.num_capsule self.input_num_capsule
self.dim_capsule self.input_dim_capsule]
initializer=self.kernel_initializer
name='W')
你也可在此查看全部代码。
性能测试 ?
在对10000幅图像的测试数据集进行测试时,我们对MNIST数据集的准确率达到99.61%,对FASHION MNIST数据集的准确率达到92.22%。棒吧!!?
对于80%重叠手写数字的MultiMNIST数据集,当数据重叠时胶囊网络的性能显得非常好,特别是与CNN模型相比。