def testPlot1(): dim = 15 from scipy import rand, dot from pybrain.datasets import SupervisedDataSet from pybrain import LinearLayer, FullConnection, FeedForwardNetwork from pybrain.utilities import dense_orth net = FeedForwardNetwork() net.addInputModule(LinearLayer(dim, name='in')) net.addOutputModule(LinearLayer(1, name='out')) net.addConnection(FullConnection(net['in'], net['out'])) net.sortModules() ds = SupervisedDataSet(dim, 1) ds2 = SupervisedDataSet(dim, 1) R = dense_orth(dim) for _ in range(1000): tmp = rand(dim) > 0.5 tmp2 = dot(tmp, R) ds.addSample(tmp, [tmp[-1]]) ds2.addSample(tmp2, [tmp[-1]]) f = ModuleWrapper(ds, net) f2 = ModuleWrapper(ds2, net) # tracking progress by callback ltrace = [] def storer(a): ltrace.append(a.provider.currentLosses(a.bestParameters)) x = net.params x *= 0.001 algo = SGD(f, net.params.copy(), callback=storer, learning_rate=0.2) algo.run(1000) pylab.plot(ltrace, 'r-') del ltrace[:] algo = SGD(f2, net.params.copy(), callback=storer, learning_rate=0.2) algo.run(1000) pylab.plot(ltrace, 'g-') pylab.semilogy() pylab.show()
def __init__( self, basef, translate=True, rotate=False, conditioning=None, asymmetry=None, oscillate=False, penalized=0, desiredValue=1e-8, gnoise=None, unoise=None, cnoise=None, sparse=True, ): FunctionEnvironment.__init__(self, basef.xdim, basef.xopt) self._name = basef.__class__.__name__ self.desiredValue = desiredValue self.toBeMinimized = basef.toBeMinimized if self.xdim < 500: sparse = False if sparse: try: from scipy.sparse import csc_matrix except: sparse = False if translate: self.xopt = (rand(self.xdim) - 0.5) * 9.8 if conditioning: prefix = generateDiags(conditioning, self.xdim) if sparse: prefix = csc_matrix(prefix) if rotate: prefix = prefix * sparse_orth(self.xdim) if oscillate or not asymmetry: prefix = sparse_orth(self.xdim) * prefix else: if rotate: prefix = dot(prefix, dense_orth(self.xdim)) if oscillate or not asymmetry: prefix = dot(dense_orth(self.xdim), prefix) elif rotate and asymmetry and not oscillate: if sparse: prefix = sparse_orth(self.xdim) else: prefix = dense_orth(self.xdim) elif sparse: prefix = None else: prefix = eye(self.xdim) if penalized != 0: if self.penalized: penalized = 0 else: self.penalized = True # combine transformations if rotate: if sparse: r = sparse_orth(self.xdim) tmp1 = lambda x: ravel(x * r) else: r = dense_orth(self.xdim) tmp1 = lambda x: dot(x, r) else: tmp1 = lambda x: x if oscillate: tmp2 = lambda x: BBOBTransformationFunction.oscillatify(tmp1(x)) else: tmp2 = tmp1 if asymmetry is not None: tmp3 = lambda x: BBOBTransformationFunction.asymmetrify( tmp2(x), asymmetry) else: tmp3 = tmp2 # noise ntmp = None if gnoise: ntmp = lambda f: f * exp(gnoise * gauss(0, 1)) elif unoise: alpha = 0.49 * (1. / self.xdim) * unoise ntmp = lambda f: f * power(random(), unoise) * max( 1, power(1e9 / (f + 1e-99), alpha * random())) elif cnoise: alpha, beta = cnoise ntmp = lambda f: f + alpha * max( 0, 1000 * (random() < beta) * gauss(0, 1) / (abs(gauss(0, 1)) + 1e-199)) def noisetrans(f): if ntmp is None or f < 1e-8: return f else: return ntmp(f) + 1.01e-8 if sparse: if prefix is None: tmp4 = lambda x: tmp3(x - self.xopt) else: tmp4 = lambda x: ravel(prefix * tmp3(x - self.xopt)) else: tmp4 = lambda x: dot(prefix, tmp3(x - self.xopt)) self.f = lambda x: (noisetrans(basef.f(tmp4(x))) + penalized * penalize(x))
def __init__(self, basef, translate=True, rotate=False, conditioning=None, asymmetry=None, oscillate=False, penalized=0, desiredValue=1e-8, gnoise=None, unoise=None, cnoise=None, sparse=True, ): FunctionEnvironment.__init__(self, basef.xdim, basef.xopt) self._name = basef.__class__.__name__ self.desiredValue = desiredValue self.toBeMinimized = basef.toBeMinimized if self.xdim < 500: sparse = False if sparse: try: from scipy.sparse import csc_matrix except: sparse = False if translate: self.xopt = (rand(self.xdim) - 0.5) * 9.8 if conditioning: prefix = generateDiags(conditioning, self.xdim) if sparse: prefix = csc_matrix(prefix) if rotate: prefix = prefix * sparse_orth(self.xdim) if oscillate or not asymmetry: prefix = sparse_orth(self.xdim) * prefix else: if rotate: prefix = dot(prefix, dense_orth(self.xdim)) if oscillate or not asymmetry: prefix = dot(dense_orth(self.xdim), prefix) elif rotate and asymmetry and not oscillate: if sparse: prefix = sparse_orth(self.xdim) else: prefix = dense_orth(self.xdim) elif sparse: prefix = None else: prefix = eye(self.xdim) if penalized != 0: if self.penalized: penalized = 0 else: self.penalized = True # combine transformations if rotate: if sparse: r = sparse_orth(self.xdim) tmp1 = lambda x: ravel(x * r) else: r = dense_orth(self.xdim) tmp1 = lambda x: dot(x, r) else: tmp1 = lambda x: x if oscillate: tmp2 = lambda x: BBOBTransformationFunction.oscillatify(tmp1(x)) else: tmp2 = tmp1 if asymmetry is not None: tmp3 = lambda x: BBOBTransformationFunction.asymmetrify(tmp2(x), asymmetry) else: tmp3 = tmp2 # noise ntmp = None if gnoise: ntmp = lambda f: f * exp(gnoise * gauss(0, 1)) elif unoise: alpha = 0.49 * (1. / self.xdim) * unoise ntmp = lambda f: f * power(random(), unoise) * max(1, power(1e9 / (f + 1e-99), alpha * random())) elif cnoise: alpha, beta = cnoise ntmp = lambda f: f + alpha * max(0, 1000 * (random() < beta) * gauss(0, 1) / (abs(gauss(0, 1)) + 1e-199)) def noisetrans(f): if ntmp is None or f < 1e-8: return f else: return ntmp(f) + 1.01e-8 if sparse: if prefix is None: tmp4 = lambda x: tmp3(x - self.xopt) else: tmp4 = lambda x: ravel(prefix * tmp3(x - self.xopt)) else: tmp4 = lambda x: dot(prefix, tmp3(x - self.xopt)) self.f = lambda x: (noisetrans(basef.f(tmp4(x))) + penalized * penalize(x))