Skip to content

Commit 0387dc2

Browse files
committed
much things
1 parent 7da7ebe commit 0387dc2

10 files changed

+47
-26
lines changed

Mariana/candies.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def fatal(msgSubject, msg, toRaise = ValueError, flush = True) :
3737

3838
subject = msgSubject
3939

40-
s = """\n%s:\n%s\n%s\n\n Sorry,\n\n Mariana\n""" %(subject, "-"*(len(subject) + 1), m)
40+
s = """\n%s:\n%s\n%s\n\n %s\nSorry,\n\n Mariana\n""" %(subject, "-"*(len(subject) + 1), m, toRaise.message)
4141

4242
if MSET.SAVE_MESSAGE_LOG :
4343
if not MESSAGE_LOG_FILE :
@@ -46,4 +46,4 @@ def fatal(msgSubject, msg, toRaise = ValueError, flush = True) :
4646
if flush :
4747
MESSAGE_LOG_FILE.flush()
4848

49-
raise toRaise(s)
49+
raise toRaise

Mariana/doc/source/conf.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@
6464
# built documents.
6565
#
6666
# The short X.Y version.
67-
version = '1.0.1'
67+
version = '1.0.x'
6868
# The full version, including alpha/beta/rc tags.
69-
release = '1.0.1rc'
69+
release = '1.0.xrc'
7070

7171
# The language for content autogenerated by Sphinx. Refer to documentation
7272
# for a list of supported languages.

Mariana/initializations.py

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
__all__= [
88
"Initialization_ABC",
99
"HardSet",
10+
"Identity",
1011
"GlorotTanhInit",
1112
"Uniform",
1213
"UniformWeights",
@@ -44,6 +45,15 @@ def initialize(self, layer) :
4445
"""The function that all Initialization_ABCs must implement"""
4546
raise NotImplemented("This one should be implemented in child")
4647

48+
class Identity(Initialization_ABC) :
49+
"""Identity matrix for weights"""
50+
def __init__(self, *args, **kwargs) :
51+
Initialization_ABC.__init__(self, *args, **kwargs)
52+
53+
def initialize(self, layer) :
54+
v = numpy.identity(layer.nbOutputs, dtype = theano.config.floatX)
55+
setattr( layer, "W", theano.shared(value = v, name = "%s_%s" % (layer.name, "W") ) )
56+
4757
class HardSet(Initialization_ABC) :
4858
"""Sets the parameter to value (must have a correct shape)"""
4959
def __init__(self, parameter, value, *args, **kwargs) :

Mariana/layers.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def _setTheanoFunctions(self) :
168168
propagateTest returns the testOutputs, some decorators might not be applied.
169169
This is called after decorating"""
170170
self.propagate = MWRAP.TheanoFunction("propagate", self, [("outputs", self.outputs)], allow_input_downcast=True)
171-
self.propagateTest = MWRAP.TheanoFunction("propagateTest", self, [("testOutputs", self.testOutputs)], allow_input_downcast=True)
171+
self.propagateTest = MWRAP.TheanoFunction("propagateTest", self, [("outputs", self.testOutputs)], allow_input_downcast=True)
172172

173173
def setCustomTheanoFunctions(self) :
174174
"""This is where you should put the definitions of your custom theano functions. Theano functions
@@ -523,11 +523,11 @@ def setCustomTheanoFunctions(self) :
523523
clasAcc = tt.mean( tt.eq(self.targets, clas ) )
524524
predAcc = tt.mean( tt.eq(self.targets, pred ) )
525525

526-
self.classificationAccuracy = MWRAP.TheanoFunction("accuracy", self, [("accuracy", clasAcc)], { "targets" : self.targets }, allow_input_downcast=True)
527-
self.predictionAccuracy = MWRAP.TheanoFunction("accuracy", self, [("accuracy", predAcc)], { "targets" : self.targets }, allow_input_downcast=True)
526+
self.classificationAccuracy = MWRAP.TheanoFunction("classificationAccuracy", self, [("accuracy", clasAcc)], { "targets" : self.targets }, allow_input_downcast=True)
527+
self.predictionAccuracy = MWRAP.TheanoFunction("predictionAccuracy", self, [("accuracy", predAcc)], { "targets" : self.targets }, allow_input_downcast=True)
528528

529-
self.trainAndAccuracy = MWRAP.TheanoFunction("accuracy", self, [("score", self.cost), ("accuracy", clasAcc)], { "targets" : self.targets }, updates = self.updates, allow_input_downcast=True)
530-
self.testAndAccuracy = MWRAP.TheanoFunction("accuracy", self, [("score", self.testCost), ("accuracy", predAcc)], { "targets" : self.targets }, allow_input_downcast=True)
529+
self.trainAndAccuracy = MWRAP.TheanoFunction("trainAndAccuracy", self, [("score", self.cost), ("accuracy", clasAcc)], { "targets" : self.targets }, updates = self.updates, allow_input_downcast=True)
530+
self.testAndAccuracy = MWRAP.TheanoFunction("testAndAccuracy", self, [("score", self.testCost), ("accuracy", predAcc)], { "targets" : self.targets }, allow_input_downcast=True)
531531

532532
class Regression(Output_ABC) :
533533
"""For regressions, works great with a mean squared error cost"""

Mariana/network.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ def __repr__(self) :
5252
os.append(o.name)
5353

5454
os = ', '.join(os)
55-
return "<theano fct '%s' for output layer: '%s'>" % (self.name, os)
55+
return "<theano fct '%s' for layers: '%s'>" % (self.name, os)
5656

5757
class Network(object) :
58-
"""All **theano_x** functions of the outputs are accessible through the network interface **network.x(...)**."""
58+
"""All theano functions of all layers are accessible through the network interface **network.x(...)**."""
5959
def __init__(self) :
6060
self.inputs = OrderedDict()
6161
self.layers = OrderedDict()
@@ -227,7 +227,7 @@ def help(self) :
227227
os.append(repr(o))
228228
os = '\n\t'.join(os)
229229

230-
print "Available model functions:\n" % os
230+
print "Available model functions:\n%s\n" % os
231231

232232
def save(self, filename) :
233233
import cPickle, pickle

Mariana/regularizations.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def apply(self, layer) :
1313

1414
message = "%s uses %s regularization" % (layer.name, self.__class__.__name__)
1515
layer.network.logLayerEvent(layer, message, hyps)
16-
return self.getFormula(layer, x)
16+
return self.getFormula(layer)
1717

1818
def getFormula(self, layer) :
1919
"""Returns the expression to be added to the cost"""

Mariana/training/recorders.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ def __init__(self, mapName, outputName, functionName) :
6767
self.outputName = outputName.name
6868

6969
self.functionName = functionName
70-
70+
self.recorder = None
71+
72+
def _shouldISave(self, recorder) :
73+
"""This is the function that is called by the recorder. If sets self.recorder to 'recorder' and then calls self.shouldISave()"""
74+
self.recorder = recorder
75+
return self.shouldISave(recorder)
76+
7177
def shouldISave(self, recorder) :
7278
"""The function that defines when a save should be performed"""
7379
raise NotImplemented("Should be implemented in child")
@@ -79,7 +85,7 @@ def getFilename(self, recorder) :
7985
def loadLast(self):
8086
"""load the last saved model"""
8187
import Mariana.network as MNET
82-
return MNET.loadModel(self.getFilename())
88+
return MNET.loadModel(self.getFilename(self.recorder))
8389

8490
def __repr__(self) :
8591
return "%s on %s" %(self.__class__.__name__, (self.mapName, self.outputName, self.functionName) )
@@ -198,7 +204,7 @@ def _fillLine(csvFile, score, minScore, maxScore, mapName, setLen, outputName, o
198204
self.printCurrentState()
199205

200206
for w in self.whenToSave :
201-
if w.shouldISave(self) :
207+
if w._shouldISave(self) :
202208
model.save(w.getFilename(self))
203209

204210
def printCurrentState(self) :

Mariana/training/stopcriteria.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(self, maxEpochs) :
2727
self.maxEpochs = maxEpochs
2828

2929
def stop(self, trainer) :
30-
if trainer.store["runInfos"]["epoch"] >= self.maxEpochs :
30+
if trainer.store["runInfos"]["epoch"]+1 >= self.maxEpochs :
3131
return True
3232
return False
3333

Mariana/training/trainers.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ def _trainTest(aMap, modelFct, trainingOrder, miniBatchSize, inputLayers, output
276276
layerList.append(output)
277277
batchData = aMap.getBatch(i, miniBatchSize, layerList = layerList)
278278
res = modelFct(output, **batchData)
279-
280279
if output.name not in scores :
281280
scores[output.name] = {}
282281

@@ -287,7 +286,6 @@ def _trainTest(aMap, modelFct, trainingOrder, miniBatchSize, inputLayers, output
287286
scores[output.name][k] = [v]
288287

289288
layerList.pop(-1)
290-
291289
elif trainingOrder == DefaultTrainer.SIMULTANEOUS_TRAINING :
292290
for i in xrange(0, len(aMap), miniBatchSize) :
293291
batchData = aMap.getBatch(i, miniBatchSize, layerList = layerList)
@@ -383,7 +381,7 @@ def _trainTest(aMap, modelFct, trainingOrder, miniBatchSize, inputLayers, output
383381
self.store["setSizes"][mapName] = self.maps[mapName].getMinFullLength()
384382
self.store["scores"][mapName] = {}
385383
for o in outputLayers :
386-
self.store["scores"][mapName][o.name] = "NA"
384+
self.store["scores"][mapName][o.name] = {}
387385

388386
while True :
389387
for mapName in ["train", "test", "validation"] :

Mariana/wrappers.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,22 @@ def dumpToHTML(self, filename) :
9696
d3v.d3viz(self.theano_fct, filename)
9797

9898
def run(self, **kwargs) :
99-
"""Run the thano function with the kwargs. Will return an OrderedDict of the outputs"""
99+
"""Run the theano function with the kwargs. Will return an OrderedDict of the outputs"""
100100
def _die(fctName, outputLayer, kwargs, exc) :
101-
sys.stderr.write("!!=> Error in function '%s' for layer '%s':\n" % (fctName, outputLayer.name))
102-
sys.stderr.write("\t!!=> the arguments were:\n %s\n" % (kwargs))
103-
raise exc
101+
localStr = "!!=> Error in function '%s' for layer '%s':\n%s\n" % (fctName, outputLayer.name, exc.message)
102+
sys.stderr.write(localStr)
103+
sys.stderr.write("Have a look at the log file: %s for details about the arguments" % MSET.SAVE_MESSAGE_LOG_FILE)
104+
strArgs = []
105+
for k, v in kwargs.iteritems() :
106+
strArgs.append("%s, shape: %s \n----\n%s" % (k, numpy.asarray(v).shape, v))
107+
MCAN.fatal(localStr, "!!=> the arguments were:\n %s\n" % ('\n'.join(strArgs)), toRaise = exc)
104108

105109
self.fctInputs.update(kwargs)
106-
fres = iter(self.theano_fct(*self.fctInputs.values()))
107-
110+
try :
111+
fres = iter(self.theano_fct(*self.fctInputs.values()))
112+
except Exception as e:
113+
_die(self.name, self.outputLayer, kwargs, e)
114+
108115
for k in self.output_expressions.iterkeys() :
109116
self.results[k] = fres.next()
110117

0 commit comments

Comments
 (0)