Example #1
0
class NNtools(object):
    """ Abstract class providing basic functionality to make neural network training more comfortable """

    def __init__(self, DS, **kwargs):
        """ Initialize with the training data set DS. All keywords given are set as member variables.
        The following are particularly important:

        :key hidden: number of hidden units
        :key TDS: test data set for checking convergence
        :key VDS: validation data set for final performance evaluation
        :key epoinc: number of epochs to train for, before checking convergence (default: 5)
        """
        self.DS = DS
        self.hidden = 10
        self.maxepochs = 1000
        self.Graph = None
        self.TDS = None
        self.VDS = None
        self.epoinc = 5
        setAllArgs(self, kwargs)
        self.trainCurve = None


    def initGraphics(self, ymax=10, xmax= -1):
        """ initialize the interactive graphics output window, and return a handle to the plot """
        if xmax < 0:
            xmax = self.maxepochs
        figure(figsize=[12, 8])
        ion()
        draw()
        #self.Graph = MultilinePlotter(autoscale=1.2 ) #xlim=[0, self.maxepochs], ylim=[0, ymax])
        self.Graph = MultilinePlotter(xlim=[0, xmax], ylim=[0, ymax])
        self.Graph.setLineStyle([0, 1], linewidth=2)
        return self.Graph


    def set(self, **kwargs):
        """ convenience method to set several member variables at once """
        setAllArgs(self, kwargs)


    def saveTrainingCurve(self, learnfname):
        """ save the training curves into a file with the given name (CSV format) """
        logging.info('Saving training curves into ' + learnfname)
        if self.trainCurve is None:
            logging.error('No training curve available for saving!')
        learnf = open(learnfname, "wb")
        writer = csv.writer(learnf, dialect='excel')
        nDataSets = len(self.trainCurve)
        for i in range(1, len(self.trainCurve[0]) - 1):
            writer.writerow([self.trainCurve[k][i] for k in range(nDataSets)])
        learnf.close()

    def saveNetwork(self, fname):
        """ save the trained network to a file """
        NetworkWriter.writeToFile(self.Trainer.module, fname)
        logging.info("Network saved to: " + fname)
Example #2
0
class NNtools(object):
    """ Abstract class providing basic functionality to make neural network training more comfortable """

    def __init__(self, DS, **kwargs):
        """ Initialize with the training data set DS. All keywords given are set as member variables. 
        The following are particularly important:
        @param hidden: number of hidden units
        @param TDS: test data set for checking convergence
        @param VDS: validation data set for final performance evaluation
        @param epoinc: number of epochs to train for, before checking convergence (default: 5)
        """
        self.DS = DS
        self.hidden=10 
        self.maxepochs=1000
        self.Graph=None
        self.TDS=None
        self.VDS=None
        self.epoinc = 5  
        setAllArgs( self, kwargs)
        self.trainCurve = None
        
        
    def initGraphics(self, ymax=10, xmax=-1):
        """ initialize the interactive graphics output window, and return a handle to the plot """
        if xmax<0:
            xmax = self.maxepochs
        figure(figsize=[12,8])
        ion()
        draw()
        #self.Graph = MultilinePlotter(autoscale=1.2 ) #xlim=[0, self.maxepochs], ylim=[0, ymax])
        self.Graph = MultilinePlotter(xlim=[0, xmax], ylim=[0, ymax])
        self.Graph.setLineStyle([0,1],linewidth=2)
        return self.Graph
    

    def set(self, **kwargs):
        """ convenience method to set several member variables at once """
        setAllArgs(self, kwargs)


    def saveTrainingCurve(self,learnfname):
        """ save the training curves into a file with the given name (CSV format) """
        logging.info('Saving training curves into '+learnfname)
        if self.trainCurve is None:
            logging.error('No training curve available for saving!')
        learnf = open(learnfname, "wb")
        writer = csv.writer(learnf, dialect='excel')
        nDataSets = len(self.trainCurve)
        for i in range(1,len(self.trainCurve[0])-1):        
            writer.writerow([self.trainCurve[k][i] for k in range(nDataSets)])
        learnf.close()

    def saveNetwork(self, fname):
        """ save the trained network to a file """
        NetworkWriter.writeToFile(self.Trainer.module, fname) 
        logging.info("Network saved to: "+fname)
Example #3
0
 def initGraphics(self, ymax=10, xmax=-1):
     """ initialize the interactive graphics output window, and return a handle to the plot """
     if xmax<0:
         xmax = self.maxepochs
     figure(figsize=[12,8])
     ion()
     draw()
     #self.Graph = MultilinePlotter(autoscale=1.2 ) #xlim=[0, self.maxepochs], ylim=[0, ymax])
     self.Graph = MultilinePlotter(xlim=[0, xmax], ylim=[0, ymax])
     self.Graph.setLineStyle([0,1],linewidth=2)
     return self.Graph
Example #4
0
 def initGraphics(self, ymax=10, xmax=-1):
     """ initialize the interactive graphics output window, and return a handle to the plot """
     if xmax<0:
         xmax = self.maxepochs
     figure(figsize=[12,8])
     ion()
     draw()
     #self.Graph = MultilinePlotter(autoscale=1.2 ) #xlim=[0, self.maxepochs], ylim=[0, ymax])
     self.Graph = MultilinePlotter(xlim=[0, xmax], ylim=[0, ymax])
     self.Graph.setLineStyle([0,1],linewidth=2)
     return self.Graph
Example #5
0
learner.explorer.sigma = sigma
#learner.explorer.epsilon = 0.01 # default: 0.3
#learner.learningRate = 0.01 # (0.1-0.001, down to 1e-7 for RNNs)

# Alternatively, use blackbox optimisation.
#learner = HillClimber(storeAllEvaluations=True)
##learner = CMAES(storeAllEvaluations=True)
##learner = FEM(storeAllEvaluations=True)
##learner = ExactNES(storeAllEvaluations=True)
##learner = PGPE(storeAllEvaluations=True)
#agent = OptimizationAgent(net, learner)

# Prepare for plotting.
pylab.figure()  #figsize=(16,8))
pylab.ion()
plot = MultilinePlotter(autoscale=1.1, xlim=[0, nf], ylim=[0, 1])

# Read ideal system cost and set-point values determined using OPF.
f_dc = scipy.io.mmread("../data/fDC.mtx").flatten()
f_ac = scipy.io.mmread("../data/fAC.mtx").flatten()
Pg_dc = scipy.io.mmread("../data/PgDC.mtx")
Pg_ac = scipy.io.mmread("../data/PgAC.mtx")
Qg_ac = scipy.io.mmread("../data/QgAC.mtx")

rday = range(nf)
for i in range(len(case.online_generators)):
    plot.setData(i, rday, numpy.zeros(nf))
plot.setData(3, rday, f_dc[:nf])
plot.setData(4, rday, f_ac[:nf])
plot.setData(5, rday, numpy.zeros(nf))  # reward
#plot.setData(6, rday, Pg_ac[:nf] * 10)
Example #6
0
    filepointer = file(filename)
    agent.learner.original = load(filepointer)
    filepointer.close()
    agent.learner.gd.init(agent.learner.original)

# Method for saving the weight matrix    
def saveWeights(filename, w):
    filepointer = file(filename, 'w+')
    dump(w, filepointer)
    filepointer.close()

useGraphics = False
if useGraphics:
    figure()
    ion()
    pl = MultilinePlotter(autoscale=1.2, xlim=[0, 50], ylim=[0, 1])
    pl.setLineStyle(linewidth=2)

numbExp=25 #number of experiments
for runs in range(numbExp):
    # create environment
    #Options: Bool(OpenGL), Bool(Realtime simu. while client is connected), ServerIP(default:localhost), Port(default:21560)
    env = ShipSteeringEnvironment(False)
    # create task
    task = GoNorthwardTask(env,maxsteps = 500)
    # create controller network
    net = buildNetwork(task.outdim, task.indim, outclass=TanhLayer)
    # create agent with controller and learner
    agent = FiniteDifferenceAgent(net, SPLA())
    # learning options
    agent.learner.gd.alpha = 0.3 #step size of \mu adaption
Example #7
0
learner.gd.momentum = 0.9

agent = LearningAgent(net, learner)
agent.actaspg = False

# create experiment
experiment = EpisodicExperiment(task, agent)

# print weights at beginning
print(agent.module.params)

rewards = []
if useGraphics:
    figure()
    ion()
    pl = MultilinePlotter(autoscale=1.2, xlim=[0, 50], ylim=[0, 1])
    pl.setLineStyle(linewidth=2)

# queued version
# experiment._fillQueue(30)
# while True:
#     experiment._stepQueueLoop()
#     # rewards.append(mean(agent.history.getSumOverSequences('reward')))
#     print agent.module.getParameters(),
#     print mean(agent.history.getSumOverSequences('reward'))
#     clf()
#     plot(rewards)

# episodic version
x = 0
batch = 30  #number of samples per gradient estimate (was: 20; more here due to stochastic setting)
Example #8
0
learner.explorer.sigma = sigma
#learner.explorer.epsilon = 0.01 # default: 0.3
#learner.learningRate = 0.01 # (0.1-0.001, down to 1e-7 for RNNs)

# Alternatively, use blackbox optimisation.
#learner = HillClimber(storeAllEvaluations=True)
##learner = CMAES(storeAllEvaluations=True)
##learner = FEM(storeAllEvaluations=True)
##learner = ExactNES(storeAllEvaluations=True)
##learner = PGPE(storeAllEvaluations=True)
#agent = OptimizationAgent(net, learner)

# Prepare for plotting.
pylab.figure()#figsize=(16,8))
pylab.ion()
plot = MultilinePlotter(autoscale=1.1, xlim=[0, nf], ylim=[0, 1])

# Read ideal system cost and set-point values determined using OPF.
f_dc = scipy.io.mmread("../data/fDC.mtx").flatten()
f_ac = scipy.io.mmread("../data/fAC.mtx").flatten()
Pg_dc = scipy.io.mmread("../data/PgDC.mtx")
Pg_ac = scipy.io.mmread("../data/PgAC.mtx")
Qg_ac = scipy.io.mmread("../data/QgAC.mtx")

rday = range(nf)
for i in range(len(case.online_generators)):
    plot.setData(i, rday, numpy.zeros(nf))
plot.setData(3, rday, f_dc[:nf])
plot.setData(4, rday, f_ac[:nf])
plot.setData(5, rday, numpy.zeros(nf)) # reward
#plot.setData(6, rday, Pg_ac[:nf] * 10)
Example #9
0
    module.initialize(1.0)
    #    learner = SARSA(gamma=0.9)
    learner = Q()
    #    learner = QLambda()
    #    learner.explorer = BoltzmannExplorer() # default is e-greedy.
    agent = LearningAgent(module, learner)

    agent.name = g.name
    experiment.tasks.append(task)
    experiment.agents.append(agent)

# Prepare for plotting.
pylab.figure(1)  #figsize=(16,8))
pylab.ion()
pl = MultilinePlotter(autoscale=1.1,
                      xlim=[0, 24],
                      ylim=[0, 1],
                      maxLines=len(experiment.agents))
pl.setLineStyle(linewidth=2)
pl.setLegend([a.name for a in experiment.agents], loc='upper left')

pylab.figure(2)
pylab.ion()
pl2 = MultilinePlotter(autoscale=1.1,
                       xlim=[0, 24],
                       ylim=[0, 1],
                       maxLines=len(experiment.agents))
pl2.setLineStyle(linewidth=2)

pylab.figure(3)
pylab.ion()
plc = MultilinePlotter(autoscale=1.1,
Example #10
0
learner.gd.momentum = 0.9

agent = LearningAgent(net, learner)
agent.actaspg = False

# create experiment
experiment = EpisodicExperiment(task, agent)

# print weights at beginning
print(agent.module.params)

rewards = []
if useGraphics:
    figure()
    ion()
    pl = MultilinePlotter(autoscale=1.2, xlim=[0, 50], ylim=[0, 1])
    pl.setLineStyle(linewidth=2)

# queued version
# experiment._fillQueue(30)
# while True:
#     experiment._stepQueueLoop()
#     # rewards.append(mean(agent.history.getSumOverSequences('reward')))
#     print agent.module.getParameters(),
#     print mean(agent.history.getSumOverSequences('reward'))
#     clf()
#     plot(rewards)

# episodic version
x = 0
batch = 30 #number of samples per gradient estimate (was: 20; more here due to stochastic setting)
Example #11
0
    # Add the task and agent to the experiment.
    experiment.tasks.append(task)
    experiment.agents.append(agent)

takers = case.generators[1:]
for g in takers:
    env = pyreto.continuous.MarketEnvironment([g], market, numOffbids)
    task = pyreto.continuous.ProfitTask(env, maxSteps=len(p1h))
    agent = pyreto.util.NegOneAgent(env.outdim, env.indim)
    experiment.tasks.append(task)
    experiment.agents.append(agent)

# Prepare for plotting.
pylab.figure()#figsize=(16,8))
pylab.ion()
plot = MultilinePlotter(autoscale=1.1, xlim=[0, len(p1h)], ylim=[0, 1])

# Solve an initial OPF.
OPF(case, market.locationalAdjustment=='dc', opt={"verbose": False}).solve()

weeks = 208 # number of roleouts
days = 7 # number of samples per learning step
for week in range(weeks):
    experiment.doEpisodes(days)

    if manual_sigma:
#        sigma = [sig - abs(sig * 0.05) - 0.1 for sig in sigma]
        sigma = [sig - 1.0 for sig in sigma]
        print "SIGMA:", sigma

    reward = experiment.agents[0].history["reward"]
Example #12
0
    module = ActionValueTable(dimState, dimAction)
    module.initialize(1.0)
#    learner = SARSA(gamma=0.9)
    learner = Q()
#    learner = QLambda()
#    learner.explorer = BoltzmannExplorer() # default is e-greedy.
    agent = LearningAgent(module, learner)

    agent.name = g.name
    experiment.tasks.append(task)
    experiment.agents.append(agent)

# Prepare for plotting.
pylab.figure(1)#figsize=(16,8))
pylab.ion()
pl = MultilinePlotter(autoscale=1.1, xlim=[0, 24], ylim=[0, 1],
                      maxLines=len(experiment.agents))
pl.setLineStyle(linewidth=2)
pl.setLegend([a.name for a in experiment.agents], loc='upper left')

pylab.figure(2)
pylab.ion()
pl2 = MultilinePlotter(autoscale=1.1, xlim=[0, 24], ylim=[0, 1],
                       maxLines=len(experiment.agents))
pl2.setLineStyle(linewidth=2)

pylab.figure(3)
pylab.ion()
plc = MultilinePlotter(autoscale=1.1, xlim=[0, 200], ylim=[0, 200],
                       maxLines=3 * len(experiment.agents))
#plc.graphColor = plc.graphColor[:len(experiment.agents)]
plc.setLineStyle(linewidth=2)
Example #13
0
def runNN():
    # data set CSV
    _trncsv = '../ml_pybrain/case1_send1/inc_attr_30_train.csv'
    _tstcsv = '../ml_pybrain/case1_send1/inc_attr_30_test.csv'
    #_attrnum = 30 # attrnum is imput dim, get later auto
    _classnum = 2
    # num of hidden layer
    _hidden = 12
    # max epochs
    _maxepochs = 100
    # learn val
    _learningrate = 0.013 # default 0.01
    _momentum = 0.03 # default 0.0, tutorial 0.1
    _lrdecay = 1.0 # default 1.0
    _weightdecay = 0.01 # default 0.0, tutorial 0.01
    # save training log path
    _logpath = 'att30class2_2.log'
    # graph
    _graphymax = 15

    '''#build 3 class
    means = [(-1,0),(2,4),(3,1)]
    cov = [diag([1,1]), diag([0.5,1.2]), diag([1.5,0.7])]
    alldata = ClassificationDataSet(2, 1, nb_classes=3)
    for n in xrange(400):
        for klass in range(3):
            input = multivariate_normal(means[klass],cov[klass])
            alldata.addSample(input, [klass])

    #split 25% test, 75% train
    tstdata, trndata = alldata.splitWithProportion(0.25)


    ''' #read csv
    with Timer() as readt:
        # read train data
        dictdata = csv.DictReader(open(_trncsv, 'r'))
        data  = [[row[f] for f in dictdata.fieldnames] for row in dictdata]
        # from 0th to last-1 col are training data set
        train = [[float(elm) for elm in row[0:-1]] for row in data]
        # last col is target data set, convert from ONE start to ZERO start
        target = [[int(row[-1])-1] for row in data]
        # get input dim
        _attrnum = len(train[0])
        # set DataSet
        trndata = ClassificationDataSet(_attrnum, 1, nb_classes=_classnum)
        trndata.setField('input', train)
        trndata.setField('target', target)

        # read test data
        dictdata = None
        dictdata = csv.DictReader(open(_tstcsv, 'r'))
        data  = [[row[f] for f in dictdata.fieldnames] for row in dictdata]
        # from 0th to last-1 col are training data set
        train = [[float(elm) for elm in row[0:-1]] for row in data]
        # last col is target data set, convert from ONE start to ZERO start
        target = [[int(row[-1])-1] for row in data]
        # set DataSet
        tstdata = ClassificationDataSet(_attrnum, 1, nb_classes=_classnum)
        tstdata.setField('input', train)
        tstdata.setField('target', target)

    #'''

    # 1-of-k representation
    trndata._convertToOneOfMany()
    tstdata._convertToOneOfMany()

    print "Number of training patterns: ", len(trndata)
    print "Input and output dimensions: ", trndata.indim, trndata.outdim
    print "First sample (input, target, class):"
    print trndata['input'][0], trndata['target'][0], trndata['class'][0]

    # build network and tariner
    fnn = buildNetwork( trndata.indim, _hidden, trndata.outdim, outclass=SoftmaxLayer)
    #trainer = BackpropTrainer(fnn, dataset=trndata, momentum=0.1, verbose=True, weightdecay=0.01)
    trainer = BackpropTrainer(fnn, dataset=trndata, verbose=True,
        learningrate = _learningrate,
        momentum = _momentum,
        lrdecay = _lrdecay,
        weightdecay = _weightdecay
        )

    # setup graph
    xmax = _maxepochs
    ymax = _graphymax
    figure(figsize=[12,8])
    ion()
    draw()
    graph = MultilinePlotter(xlim=[1, xmax], ylim=[0, ymax])
    graph.setLineStyle([0,1], linewidth=2)
    graph.setLabels(x='epoch', y='error %')
    graph.setLegend(['training', 'test'], loc='upper right')
    graph.update()
    draw()

    # setup storage training curve
    trainx = []
    trny = []
    tsty = []

    # start training
    with Timer() as traint:
        for i in range(_maxepochs):
            # train
            trainer.trainEpochs(1)
            # test by train/test
            trnresult = percentError(trainer.testOnClassData(), trndata['class'])
            tstresult = percentError(trainer.testOnClassData(dataset=tstdata), tstdata['class'])
            print "epoch: %4d" % trainer.totalepochs, \
                "  train error: %5.2f%%" % trnresult, \
                "  test error: %5.2f%%" % tstresult
            # store curve
            trainx.append(i+1)
            trny.append(trnresult)
            tsty.append(tstresult)

            # draw graph
            graph.addData(0, i+1, trnresult)
            graph.addData(1, i+1, tstresult)
            graph.update()
            draw()

    # save log
    f = csv.writer(open(_logpath, 'w'))
    # timer
    f.writerow(['read', readt.secs])
    f.writerow(['training and test(sec)', traint.secs])
    # data prop
    f.writerow(['train data num', len(trndata)])
    f.writerow(['test data num', len(tstdata)])
    f.writerow(['in / out dim', trndata.indim, trndata.outdim])
    # config
    f.writerow(['hidden', _hidden])
    f.writerow(['maxepochs', _maxepochs])
    f.writerow(['learningrate', _learningrate])
    f.writerow(['momentum', _momentum])
    f.writerow(['lrdecay', _lrdecay])
    f.writerow(['weightdecay', _weightdecay])
    # curve
    f.writerow(['epoch', 'train_err', 'test_err'])
    f.writerows([[trainx[r], trny[r], tsty[r]] for r in range(len(trainx))])