def onHostTest(): from PuzzleLib.Containers import Sequential from PuzzleLib.Modules import Conv2D, MaxPool2D, Activation, relu, Flatten, Linear from PuzzleLib.Cost.CrossEntropy import CrossEntropy data = np.random.randn(50000, 3, 28, 28).astype(np.float32) dataTarget = np.random.randint(low=0, high=10, size=(50000, )).astype(np.int32) seq = Sequential() seq.append(Conv2D(3, 16, 9)) seq.append(MaxPool2D()) seq.append(Activation(relu)) seq.append(Conv2D(16, 32, 5)) seq.append(MaxPool2D()) seq.append(Activation(relu)) seq.append(Flatten()) seq.append(Linear(3 * 3 * 32, 10)) entr = CrossEntropy() val = Validator(seq, entr) val.validateFromHost(data, dataTarget) print("Validation error on big data: %s" % val.error)
def buildNet(): from PuzzleLib.Containers import Sequential from PuzzleLib.Modules import Conv2D, MaxPool2D, Activation, relu, Flatten, Linear seq = Sequential() seq.append(Conv2D(3, 32, 5, pad=2, wscale=0.0001, initscheme="gaussian")) seq.append(MaxPool2D(3, 2)) seq.append(Activation(relu)) seq.append(Conv2D(32, 32, 5, pad=2, wscale=0.01, initscheme="gaussian")) seq.append(MaxPool2D(3, 2)) seq.append(Activation(relu)) seq.append(Conv2D(32, 64, 5, pad=2, wscale=0.01, initscheme="gaussian")) seq.append(MaxPool2D(3, 2)) seq.append(Activation(relu)) seq.append(Flatten()) seq.append( Linear(seq.dataShapeFrom((1, 3, 32, 32))[1], 64, wscale=0.1, initscheme="gaussian")) seq.append(Activation(relu)) seq.append(Linear(64, 10, wscale=0.1, initscheme="gaussian")) return seq
def onDeviceTest(): from PuzzleLib.Containers import Sequential from PuzzleLib.Modules import Conv2D, MaxPool2D, Activation, relu, Flatten, Linear from PuzzleLib.Cost.CrossEntropy import CrossEntropy from PuzzleLib.Optimizers.NesterovSGD import NesterovSGD data = gpuarray.to_gpu( np.random.randn(10000, 3, 28, 28).astype(np.float32)) dataTarget = gpuarray.to_gpu( np.random.randint(low=0, high=10, size=(10000, )).astype(np.int32)) seq = Sequential() seq.append(Conv2D(3, 16, 9)) seq.append(MaxPool2D()) seq.append(Activation(relu)) seq.append(Conv2D(16, 32, 5)) seq.append(MaxPool2D()) seq.append(Activation(relu)) seq.append(Flatten()) seq.append(Linear(3 * 3 * 32, 10)) entr = CrossEntropy() opt = NesterovSGD() opt.setupOn(seq) def onBatchFinish(train): print("Finished batch #%d, error=%s" % (train.currBatch, train.cost.getError())) trainer = Trainer(seq, entr, opt, onBatchFinish=onBatchFinish) trainer.train(data, dataTarget)
def buildNet(): from PuzzleLib.Containers import Sequential from PuzzleLib.Modules import Conv2D, MaxPool2D, Activation, relu, Flatten, Linear seq = Sequential(name="lenet-5-like") seq.append(Conv2D(1, 16, 3)) seq.append(MaxPool2D()) seq.append(Activation(relu)) seq.append(Conv2D(16, 32, 4)) seq.append(MaxPool2D()) seq.append(Activation(relu)) seq.append(Flatten()) seq.append(Linear(32 * 5 * 5, 1024)) seq.append(Activation(relu)) seq.append(Linear(1024, 10)) return seq
def onDeviceTest(): from PuzzleLib.Containers import Sequential from PuzzleLib.Modules import Conv2D, MaxPool2D, Activation, relu, Flatten, Linear data = gpuarray.to_gpu( np.random.randn(10000, 3, 28, 28).astype(np.float32)) seq = Sequential() seq.append(Conv2D(3, 16, 9)) seq.append(MaxPool2D()) seq.append(Activation(relu)) seq.append(Conv2D(16, 32, 5)) seq.append(MaxPool2D()) seq.append(Activation(relu)) seq.append(Flatten()) seq.append(Linear(3 * 3 * 32, 10)) calc = Calculator(seq) calc.onBatchFinish = lambda calculator: print("Finished batch #%d" % calculator.currBatch) calc.calc(data)
def complexNetTest(): from PuzzleLib.Modules import Conv2D, MaxPool2D, Activation, relu, Flatten data = gpuarray.to_gpu( np.random.randn(128, 3, 150, 150).astype(np.float32)) seq = Sequential() seq.append(Conv2D(3, 16, 11)) seq.append(MaxPool2D()) seq.append(Activation(relu)) seq.append(Conv2D(16, 16, 5)) seq.append(MaxPool2D()) seq.append(Activation(relu)) seq.append(Flatten()) seq(data) grad = gpuarray.to_gpu(np.random.randn(*seq.data.shape).astype(np.float32)) seq.backward(grad) seq.updateParams(1e-4)