使用python建模人工神经网络及进行深度学习

来自集智百科 - 复杂系统|人工智能|复杂科学|复杂网络|自组织
跳到导航 跳到搜索

本篇主要由计算士整理

理论模型

本页面主要内容来自这里

有多层神经元

300px


高层神经元控制(接受)底层神经元的输入

300px


使用卷积来定义不同层神经元之间的作用,回忆一维卷积的效果

500px


可以将一维卷积推广到二维卷积

500px

卷积是很有意思的东西,是The common patterns in nature一文中的核心概念。可以简单解释如下(来自我以前写的文章《统计之道:墨子见鬼谷子》):

600px

600px

600px


图层之间的作用示例

500px


更具体完整的模型如下所示(lenet)

500px


准备一个图像数据

500px

我们将使用以下对图片进行处理


示例代码

   import theano
   import theano.tensor as T
   import pylab
   from PIL import Image
   import cPickle, gzip, numpy
   from theano.tensor.nnet import conv
   
   rng = numpy.random.RandomState(23455)
   input = T.tensor4(name='input')
   # initialize shared variable for weights.
   w_shp = (2, 3, 9, 9)
   w_bound = numpy.sqrt(3 * 9 * 9)
   W = theano.shared( numpy.asarray(
               rng.uniform(
                   low=-1.0 / w_bound,
                   high=1.0 / w_bound,
                   size=w_shp),
               dtype=input.dtype), name ='W')
   
   b_shp = (2,)
   b = theano.shared(numpy.asarray(
               rng.uniform(low=-.5, high=.5, size=b_shp),
               dtype=input.dtype), name ='b')
   
   conv_out = conv.conv2d(input, W)
   output = T.nnet.sigmoid(conv_out + b.dimshuffle('x', 0, 'x', 'x'))
   f = theano.function([input], output)
   # open random image of dimensions 639x516
   img = Image.open('E:/wulingfei/pkuwinter.jpg')
   (width, height) = img.size
   img = numpy.asarray(img, dtype='float64') / 256.
   # put image in 4D tensor of shape (1, 3, height, width)
   img_ = img.swapaxes(0, 2).swapaxes(1, 2).reshape(1, 3, height, width)
   filtered_img = f(img_)
   
   plt.figure(1,figsize=(7,15))
   # plot original image and first and second components of output
   pylab.subplot(3, 1, 1); pylab.axis('off'); pylab.imshow(img)
   pylab.gray();
   # recall that the convOp output (filtered image) is actually a "minibatch",
   # of size 1 here, so we take index 0 in the first dimension:
   pylab.subplot(3, 1, 2); pylab.axis('off'); pylab.imshow(filtered_img[0, 0, :, :])
   pylab.subplot(3, 1, 3); pylab.axis('off'); pylab.imshow(filtered_img[0, 1, :, :])

对处理结果的讨论

处理的结果如下图所示

CNN8.png

所谓的“深度学习”(deep learning)里的“深度”其实就是人工神经网络的层次。我们这里介绍的卷积神经网络是各类深度学习模型中的一种。因为机器的增强-特别是并行计算,和数据量的加大,使得构建多层的人工神经网络模型变得可行。经过多层的处理,高层的神经元可以以非线性的方式储藏底层神经元的信息,最终形成对原始图像的抽象特征的理解。例如在这个例子中,我们识别出了图象的“边缘”。这样的学习,比起单纯地用一个分线性的分类器,例如logistic或者SVM,来理解数据里的非线性结构,无疑是更深刻的,也更贴近人脑的工作方式:使用抽象概念描述世界。