BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / ml-dm / #16011同步于 2015/6/7
ML_DM机器人发帖

写了一个theano cnn, 希望懂theano的人帮我comment一下

jasonchi
2015/6/7镜像同步0 回复
学习theano中, 看了很多的代码了, 自己试图写了一个, 在看看存在的问题, 希望熟悉theano的伙伴可以comment一下, 让我知道下一步怎么继续熟悉theano... 估计这个代码离跑起来还有一段距离。 btw, python学的时间不长, 代码风格不好,也望点评一下, 谢谢 btw again, 有哪位能推荐theano的讨论群吗,qq群??? theano google group那个我知道, __author__ = 'XXXX' import theano import theano.tensor as T from theano.tensor.signal import downsample from theano.tensor.nnet import conv import cPickle import numpy as np from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams srng = RandomStreams() class ConvLayer(object): def __init__(self, inputs, maps=6, filtershape=(3,3), poolingYes = True, pooling=(2, 2)): weigthsnumberforeachnode = inputs.shape[1]*filtershape[0]*filtershape[1] self.W = theano.shared(np.random.uniform(-6.0/weigthsnumberforeachnode, 6.0/weigthsnumberforeachnode, (maps, inputs.shape[1], filtershape[0], filtershape[1])), dtype=theano.config.floatX) self.b = theano.shared(np.zeros((maps, )), dtype=theano.config.floatX) result = conv(input, self.W) + self.b result = T.max(result, 0) if poolingYes is True: result = downsample.max_pool_2d(result, (2, 2)) return result class Dropout(object): def __init__(self, X, p=0.0): if p>0: retain_prob = 1 - p X *= srng.binomial(X.shape, p=retain_prob, dtype=theano.config.floatX) X /= retain_prob return X class FCLayer(object): def __init__(self, input, nodes=100): self.W = theano.shared(np.random.uniform(-6.0/input.shape[1], 6.0/input.shape[1], (input.shape[1], nodes)), dtype=theano.config.floatX) self.b = theano.shared(np.random.uniform(-6.0/input.shape[1], 6.0/input.shape[1], (nodes, )), dtype=theano.config.floatX) result = T.dot(input, self.W) + self.b result = T.max(result, 0) return result; class ClassifyLayer(object): def __init__(self, input): self.W = theano.shared(np.random.uniform(-6.0/input.shape[1], 6.0/input.shape[1], (input.shape[1], 10)), dtype=theano.config.floatX) self.b = theano.shared(np.random.uniform(-6.0/input.shape[1], 6.0/input.shape[1], (10, )), dtype=theano.config.floatX) result = T.dot(input, self.W) + self.b result = T.nnet.softmax(result) return result def LoadData(datapath): train_set, valid_set, test_set = cPickle.load(file(datapath, 'r')) trX, trY = train_set teX, teY = test_set valX, valY = valid_set def shared_dataset(dataX, dataY): dX = theano.shared(np.asarray(dataX, dtype=theano.config.floatX)) dY = theano.shared(np.asarray(dataY, dtype=theano.config.floatX)) return dX, T.cast(dY, 'int32') (trainX, trainY) = shared_dataset(trX, trY) (validX, validY) = shared_dataset(valX, valY) (testX, testY) = shared_dataset(teX, teY) data = [(trainX, trainY), (validX, validY), (testX, testY)] return data class CNNStructure(object): def __init__(self, input): c1 = ConvLayer(input, 6, (3,3), True, (2, 2)) d1 = DropoutLayer(c1, p) c2 = ConvLayer(d1, 12, (3, 3), True, (2, 2)) d2 = DropoutLayer(c2, p) d2flatten = T.flatten(d2, 2) fc1 = FCLayer(d2flatten, 200) cf = ClassifyLayer(fc1) cost = -T.mean(T.log(cf)[T.arange(y.shape[0]), y]) predictLabel = T.argmax(cf, axis=1) params = [cf.params, fc1.params, c2.params, c1.params] paramsGrad = T.grad(cost=cost, params=params) updates = [] for i, j in zip(range(params.shape), params): updates[i] = (params[j], params[j] - 0.01*paramsGrad[j]) cnntrain = theano.function([input, y], [cost], updates=updates) cnnpredict = theano.function([input], predictLabel) if __name__ == '__main__': [(trainX, trainY), (validX, validY), (testX, testY)] = loadData('mnist.pkl') epoch = 1000 batchsize = 128 patchnum = trainX.shape[0]/batchsize earlystop = false for i range(epoch): if earlystop is false: for j in range(patchnum): cost = cnntrain(trainX[i*batchsize:(i+1)*batchsize], trainY[i*batchsize:(i+1)*batchsize]) predictlabels = cnnpredict(testX) acc = T.neg(predictlabels, y)
订阅后,新回复会通过你的通知中心匿名送达。
0 条回复
暂无回复 · 你可以订阅本帖等待新回复。