Deep Learning 初探(五)

来源:互联网 发布:网络打字工作 编辑:程序博客网 时间:2024/06/06 17:20
下面展开对整体CNN的讨论:
卷积神经网络的整体结构相当于在MLP(多层感知机)的前面
加上若干卷积层及MaxPooling层进行预处理,从而得到边界的相关信息。


下面对有关整体代码的一些细节进行讨论:
有关一些初值的界的问题不进行进一步讨论,把它看成规定。
一般在使用MaxPooling的时候选择ignore_border=True是基于将边界
无实际意义的像素过滤掉。


numpy.ndarray.flatten方法接受参数为flatten的方向 默认为C数组的方向,
即按行拉直,当使用"F"选项时使用Fortan拉直的方向,即按列拉直,当将
输入参数指定为任意数字,如1000时,效果是按列拉直。


首先要将前述LogisticRegression及 HiddenLayer函数放到logistic_sgd函数中。


完整代码:
#coding: utf-8#from __future__ import divisionimport numpyimport theanofrom theano.tensor.signal import convfrom theano.tensor.signal import downsamplefrom theano import tensor as Tfrom logistic_sgd import HiddenLayer, LogisticRegression, load_datafrom theano.tensor.nnet import conv2dimport timeitimport osclass LeNetConvPoolLayer(object):def __init__(self, rng, input, filter_shape, image_shape, poolsize = (2,2)):# The m - 1 layer shape must equal to shape of feature mapassert image_shape[1] == filter_shape[1]self.input = input# This equal to w_bound in the previous time fan_in = numpy.prod(filter_shape[1:])fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:])) / numpy.prod(poolsize)W_bound = numpy.sqrt(6. / (fan_in + fan_out))self.W = theano.shared(numpy.asarray(rng.uniform(low = - W_bound, high = W_bound, size = filter_shape),dtype = theano.config.floatX),borrow = True)b_values = numpy.zeros((filter_shape[0],), dtype = theano.config.floatX)self.b = theano.shared(value = b_values, borrow = True)conv_out = conv2d(input = input,filters = self.W,filter_shape = filter_shape,input_shape = image_shape) pooled_out = downsample.max_pool_2d(input = conv_out,ds = poolsize,ignore_border = True)self.output = T.tanh(pooled_out + self.b.dimshuffle('x', 0, 'x', 'x'))self.params = [self.W, self.b]self.input = input def test_mlp(learning_rate = 0.1, n_epochs = 10,dataset = 'mnist.pkl.gz', batch_size = 500, nkerns=[20, 50]):datasets = load_data(dataset)train_set_x, train_set_y = datasets[0]valid_set_x, valid_set_y = datasets[1]test_set_x, test_set_y = datasets[2]n_train_batches = train_set_x.get_value(borrow = True).shape[0] / batch_sizen_valid_batches = valid_set_x.get_value(borrow = True).shape[0] / batch_sizen_test_batches = test_set_x.get_value(borrow = True).shape[0] / batch_sizerng = numpy.random.RandomState(23455)index = T.iscalar()x = T.matrix('x')y = T.ivector('y')print "... building the model"layer0_input = x.reshape((batch_size, 1, 28, 28))layer0 = LeNetConvPoolLayer(rng,input = layer0_input,image_shape = (batch_size, 1, 28, 28),filter_shape = (nkerns[0], 1, 5, 5),poolsize = (2, 2))layer1 = LeNetConvPoolLayer(rng,input = layer0.output,image_shape = (batch_size, nkerns[0], 12, 12),filter_shape = (nkerns[1], nkerns[0], 5, 5),poolsize = (2,2))layer2_input = layer1.output.flatten(2)layer2 = HiddenLayer(rng,input = layer2_input,n_in = nkerns[1] * 4 * 4,n_out = 500,activation = T.tanh) layer3 = LogisticRegression(input = layer2.output, n_in = 500, n_out = 10)cost = layer3.negative_log_likelihood(y)test_model = theano.function([index],layer3.errors(y),givens = {x: test_set_x[index * batch_size: (index + 1) * batch_size],y: test_set_y[index * batch_size: (index + 1) * batch_size]})validate_model = theano.function([index],layer3.errors(y),givens = {x: valid_set_x[index * batch_size: (index + 1) * batch_size],y: valid_set_y[index * batch_size: (index + 1) * batch_size]})params = layer3.params + layer2.params + layer1.params + layer0.paramsgrads = T.grad(cost, params)updates = [(param_i, param_i - learning_rate * grad_i) for param_i, grad_i in zip(params, grads)]train_model = theano.function([index],cost,updates = updates,givens = {x: train_set_x[index * batch_size: (1 + index) * batch_size],y: train_set_y[index * batch_size: (1 + index) * batch_size]})print "... training"patience = 10000patience_increase = 2 improvement_threshold = 0.995 validation_frequency = min(n_train_batches, patience / 2)best_validation_loss = numpy.inf best_iter = 0test_score = 0. start_time = timeit.default_timer()epoch = 0done_looping = Falsewhile (epoch < n_epochs) and (not done_looping):epoch = epoch + 1for minibatch_index in xrange(n_train_batches):minibatch_avg_cost = train_model(minibatch_index)iter = (epoch - 1) * n_train_batches + minibatch_indexif (iter + 1) % validation_frequency == 0:validation_losses = [validate_model(i) for i in xrange(n_valid_batches)]this_validation_loss = numpy.mean(validation_losses)print ('epoch %i, minibatch %i/%i, validation error %f %%' %(epoch,minibatch_index + 1,n_train_batches,this_validation_loss * 100.))if this_validation_loss < best_validation_loss:if this_validation_loss < best_validation_loss * improvement_threshold:patience = max(patience, iter * patience_increase)best_validation_loss = this_validation_lossbest_iter = itertest_losses = [test_model(i) for i in xrange(n_test_batches)]test_score = numpy.mean(test_losses)print (('epoch %i, minibatch %i/%i, test error of'' best model %f %%' ) %(epoch, minibatch_index + 1,n_train_batches,test_score * 100.)) if patience <= iter:done_looping = Truebreakend_time = timeit.default_timer()print (('Optimization complete. Best validation score of %f %% ''obtained at iteration %i, with test performance %f %%')%(best_validation_loss * 100., best_iter + 1, test_score * 100., ))print "begin predict :"test_set_x = test_set_x.get_value()num = 500predict_model = theano.function(inputs = [layer0.input],outputs = layer3.y_pred)predicted_values = predict_model(test_set_x[:num].reshape((-1, 1, 28, 28))) #print ("The Ori values :")#print test_set_y.eval()[:num]#print ("Predicted values for the first %i examples in test set:" % num)#print predicted_valuesprint "equal :"print float(numpy.sum(predicted_values == test_set_y.eval()[:num])) /  predicted_values.shape[0]if __name__ == "__main__":test_mlp() 




可以看到进行10次卷积神经网络训练后,对500个样本的精确度可以达到0.984







 

0 0
原创粉丝点击