赞
踩
上周末利用python简单实现了一个卷积神经网络,只包含一个卷积层和一个maxpooling层,pooling层后面的多层神经网络采用了softmax形式的输出。实验输入仍然采用MNIST图像使用10个feature map时,卷积和pooling的结果分别如下所示。
部分源码如下:
[python] view plain copy
#coding=utf-8
'''''
Created on 2014年11月30日
@author: Wangliaofan
'''
import numpy
import struct
import matplotlib.pyplot as plt
import math
import random
import copy
#test
from BasicMultilayerNeuralNetwork import BMNN2
def sigmoid(inX):
if 1.0+numpy.exp(-inX)== 0.0:
return 999999999.999999999
return 1.0/(1.0+numpy.exp(-inX))
def difsigmoid(inX):
return sigmoid(inX)*(1.0-sigmoid(inX))
def tangenth(inX):
return (1.0*math.exp(inX)-1.0*math.exp(-inX))/(1.0*math.exp(inX)+1.0*math.exp(-inX))
def cnn_conv(in_image, filter_map,B,type_func='sigmoid'):
#in_image[num,feature map,row,col]=>in_image[Irow,Icol]
#features map[k filter,row,col]
#type_func['sigmoid','tangenth']
#out_feature[k filter,Irow-row+1,Icol-col+1]
shape_image=numpy.shape(in_image)#[row,col]
#print "shape_image",shape_image
shape_filter=numpy.shape(filter_map)#[k filter,row,col]
if shape_filter[1]>shape_image[0] or shape_filter[2]>shape_image[1]:
raise Exception
shape_out=(shape_filter[0],shape_image[0]-shape_filter[1]+1,shape_image[1]-shape_filter[2]+1)
out_feature=numpy.zeros(shape_out)
k,m,n=numpy.shape(out_feature)
for k_idx in range(0,k):
#rotate 180 to calculate conv
c_filter=numpy.rot90(filter_map[k_idx,:,:], 2)
for r_idx in range(0,m):
for c_idx in range(0,n):
#conv_temp=numpy.zeros((shape_filter[1],shape_filter[2]))
conv_temp=numpy.dot(in_image[r_idx:r_idx+shape_filter[1],c_idx:c_idx+shape_filter[2]],c_filter)
sum_temp=numpy.sum(conv_temp)
if type_func=='sigmoid':
out_feature[k_idx,r_idx,c_idx]=sigmoid(sum_temp+B[k_idx])
elif type_func=='tangenth':
out_feature[k_idx,r_idx,c_idx]=tangenth(sum_temp+B[k_idx])
else:
raise Exception
return out_feature
def cnn_maxpooling(out_feature,pooling_size=2,type_pooling="max"):
k,row,col=numpy.shape(out_feature)
max_index_Matirx=numpy.zeros((k,row,col))
out_row=int(numpy.floor(row/pooling_size))
out_col=int(numpy.floor(col/pooling_size))
out_pooling=numpy.zeros((k,out_row,out_col))
for k_idx in range(0,k):
for r_idx in range(0,out_row):
for c_idx in range(0,out_col):
temp_matrix=out_feature[k_idx,pooling_size*r_idx:pooling_size*r_idx+pooling_size,pooling_size*c_idx:pooling_size*c_idx+pooling_size]
out_pooling[k_idx,r_i
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。