Skip to content

Commit

Permalink
miscellaneous improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ellisk42 committed Mar 8, 2019
1 parent ab2e93b commit e42f4ce
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 15 deletions.
14 changes: 14 additions & 0 deletions ec.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ def checkpointPath(iteration, extra=""):

if addFullTaskMetrics:
assert resume is not None, "--addFullTaskMetrics requires --resume"

def reportMemory():
eprint(f"Currently using this much memory: {getThisMemoryUsage()}")

# Restore checkpoint
if resume is not None:
Expand Down Expand Up @@ -368,6 +371,8 @@ def checkpointPath(iteration, extra=""):
eprint("Resetting task metrics for next iteration.")
result.recognitionTaskMetrics = {}

reportMemory()

# Evaluate on held out tasks if we have them
if testingTimeout > 0 and ((j % testEvery == 0) or (j == iterations - 1)):
eprint("Evaluating on held out testing tasks for iteration: %d" % (j))
Expand All @@ -388,6 +393,8 @@ def checkpointPath(iteration, extra=""):
else:
helmholtzFrontiers = lambda: []

reportMemory()

# Get waking task batch.
wakingTaskBatch = taskBatcher.getTaskBatch(result, tasks, taskBatchSize, j)
eprint("Using a waking task batch of size: " + str(len(wakingTaskBatch)))
Expand All @@ -407,6 +414,8 @@ def checkpointPath(iteration, extra=""):
tasksHitTopDown = {f.task for f in topDownFrontiers if not f.empty}
result.hitsAtEachWake.append(len(tasksHitTopDown))

reportMemory()

# Combine topDownFrontiers from this task batch with all frontiers.
for f in topDownFrontiers:
if f.task not in result.allFrontiers: continue # backward compatibility with old checkpoints
Expand Down Expand Up @@ -452,9 +461,11 @@ def checkpointPath(iteration, extra=""):

# Sleep-G
if useDSL:
eprint(f"Currently using this much memory: {getThisMemoryUsage()}")
grammar = consolidate(result, grammar, topK=topK, pseudoCounts=pseudoCounts, arity=arity, aic=aic,
structurePenalty=structurePenalty, compressor=compressor, CPUs=CPUs,
iteration=j)
eprint(f"Currently using this much memory: {getThisMemoryUsage()}")
else:
eprint("Skipping consolidation.")
result.grammars.append(grammar)
Expand Down Expand Up @@ -561,6 +572,7 @@ def sleep_recognition(result, grammar, taskBatch, tasks, testingTasks, allFronti
contextual=contextual,
previousRecognitionModel=previousRecognitionModel,
id=i) for i in range(ensembleSize)]
eprint(f"Currently using this much memory: {getThisMemoryUsage()}")
trainedRecognizers = parallelMap(min(CPUs,len(recognizers)),
lambda recognizer: recognizer.train(allFrontiers,
biasOptimal=biasOptimal,
Expand All @@ -574,6 +586,7 @@ def sleep_recognition(result, grammar, taskBatch, tasks, testingTasks, allFronti
vectorized=True),
recognizers,
seedRandom=True)
eprint(f"Currently using this much memory: {getThisMemoryUsage()}")
# Enumerate frontiers for each of the recognizers.
eprint("Trained an ensemble of %d recognition models, now enumerating." % len(trainedRecognizers))
ensembleFrontiers, ensembleTimes, ensembleRecognitionTimes = [], [], []
Expand Down Expand Up @@ -614,6 +627,7 @@ def sleep_recognition(result, grammar, taskBatch, tasks, testingTasks, allFronti
'startProductions')

result.hitsAtEachWake.append(len(totalTasksHitBottomUp))
eprint(f"Currently using this much memory: {getThisMemoryUsage()}")

""" Rescore and combine the frontiers across the ensemble of recognition models."""
eprint("Recognition model enumeration results for the best recognizer.")
Expand Down
2 changes: 1 addition & 1 deletion examineFrontier.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ def examineProgram(entry):

print(f"Best posteriorc hits task {posteriorHits}/{totalTasks} = {posteriorHits/totalTasks}")
print(f"Best likelihood hits task {likelihoodHits}/{totalTasks} = {likelihoodHits/totalTasks}")
>>>>>>> 56832f1fe9d3731ce0d8dfc524025c95f1b428d7

37 changes: 31 additions & 6 deletions logo.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@
global prefix_dreams

def dreamFromGrammar(g, directory, N=100):
programs = [ p
for _ in range(N)
for p in [g.sample(arrow(turtle,turtle),
maximumDepth=20)]
if p is not None]
if isinstance(g,Grammar):
programs = [ p
for _ in range(N)
for p in [g.sample(arrow(turtle,turtle),
maximumDepth=20)]
if p is not None]
else:
programs = g
drawLogo(*programs,
pretty=False, smoothPretty=False,
resolution=512,
Expand Down Expand Up @@ -178,6 +181,27 @@ def outputDreams(checkpoint, directory):
os.system("mkdir -p %s"%directory)
dreamFromGrammar(g, directory)

def enumerateDreams(checkpoint, directory):
from recognition import backgroundHelmholtzEnumeration
from utilities import loadPickle,standardDeviation,mean
result = loadPickle(checkpoint)
eprint(" [+] Loaded checkpoint",checkpoint)
g = result.grammars[-1]
if directory is None: assert False, "please specify a directory"
eprint(" Dreaming into",directory)
os.system("mkdir -p %s"%directory)
frontiers = backgroundHelmholtzEnumeration(makeTasks(None,None), g, 500,
evaluationTimeout=0.01,
special=LogoFeatureCNN.special)()
random.shuffle(frontiers)
frontiers = frontiers[:500]
md = [list(f.entries)[0].logPrior for f in frontiers]
eprint("MDLs",md)
eprint(f"average MDL {mean(md)} +/- {standardDeviation(md)}")

dreamFromGrammar([list(f.entries)[0].program for f in frontiers],
directory)

def visualizePrimitives(primitives, export='/tmp/logo_primitives.png'):
from itertools import product
from pylab import imshow,show
Expand Down Expand Up @@ -300,7 +324,8 @@ def argumentChoices(t):
proto = args.pop("proto")

if dreamCheckpoint is not None:
outputDreams(dreamCheckpoint, dreamDirectory)
#outputDreams(dreamCheckpoint, dreamDirectory)
enumerateDreams(dreamCheckpoint, dreamDirectory)
sys.exit(0)

target = args.pop("target")
Expand Down
1 change: 1 addition & 0 deletions official_experiments
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Scientific laws:
python launch.py -k -c -z n1-highmem-64 scientific_1h "python scientificLaws.py -t 3600 --topK 5 --arity 3 --maximumFrontier 5 -i 10 -R 3600 -RS 5000 --biasOptimal --contextual --mask -r 0."
python launch.py -k -c -z n1-highmem-64 scientific_12m_10b "python scientificLaws.py -t 720 --taskReranker randomShuffle --taskBatchSize 10 --topK 5 --arity 3 --maximumFrontier 5 -i 10 -R 3600 -RS 5000 --biasOptimal --contextual --mask -r 0."
python launch.py -k -c -z n1-highmem-64 scientific_60m_10b "python scientificLaws.py -t 3600 --taskReranker randomShuffle --taskBatchSize 10 --topK 5 --arity 3 --maximumFrontier 5 -i 10 -R 3600 -RS 5000 --biasOptimal --contextual --mask -r 0."
python launch.py -k -c -z n1-highmem-64 scientific_30m_40b "python scientificLaws.py -t 3600 --taskReranker randomShuffle --taskBatchSize 40 --topK 5 --arity 3 --maximumFrontier 5 -i 10 -R 3600 -RS 5000 --biasOptimal --contextual --mask -r 0."
python launch.py -k -c -z n1-highmem-64 scientific_4h "python scientificLaws.py -t 3600 --topK 5 --arity 3 --maximumFrontier 5 -i 10 -R 3600 -RS 5000 --biasOptimal --contextual --mask -r 0."


Expand Down
16 changes: 8 additions & 8 deletions physics.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,14 @@ def physics_options(parser):

tasks = physicsTasks()

generator = ecIterator(g, tasks,
outputPrefix="experimentOutputs/physics",
evaluationTimeout=0.01,
**arguments)
for result in generator:
pass
# generator = ecIterator(g, tasks,
# outputPrefix="experimentOutputs/physics",
# evaluationTimeout=0.01,
# **arguments)
# for result in generator:
# pass

assert False
# assert False


def showLikelihood(e):
Expand Down Expand Up @@ -286,7 +286,7 @@ def showLikelihood(e):
dt = "$%d"%(Program.parse(dv).numberOfFreeVariables)
dv = "(*v %s %s)"%(dt,dv)
showLikelihood(dv)
dv = "(+v (velocity $0) %s)"%dv
dv = "(+v (get-velocity $0) %s)"%dv
showLikelihood(dv)

eprint()
Expand Down
14 changes: 14 additions & 0 deletions solvers/logoLib/logoInterpreter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ let eval_turtle ?sequence (t2t : turtle -> turtle) =
List.iter eval_instruction p ;
!c

let animate_turtle ?sequence (t2t : turtle -> turtle) =
let p,_ = (t2t logo_NOP) (init_state ()) in
let p = center_logo_list p in
let c = ref (new_canvas ()) in
let lineto x y = (c := (lineto !c x y))
and moveto x y = (c := (moveto !c x y)) in
let t = init_state () in
moveto t.x t.y ;
let rec eval_instruction i = match i with
| SEGMENT(x1,y1,x2,y2) ->
(moveto x1 y1; lineto x2 y2) in
List.iter eval_instruction p ;
!c

let logo_PU : turtle =
fun s -> ([], {s with p = false})

Expand Down
1 change: 1 addition & 0 deletions tower.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ def visualizeSolutions(solutions, export, tasks=None):
dreamOfTowers(g0, "%s/random_0"%outputDirectory)

for result in generator:
continue
iteration = len(result.learningCurve)
newTowers = [tuple(centerTower(executeTower(frontier.sample().program)))
for frontier in result.taskSolutions.values() if not frontier.empty]
Expand Down

0 comments on commit e42f4ce

Please sign in to comment.