forked from ellisk42/ec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dreaming.py
90 lines (81 loc) · 3.93 KB
/
dreaming.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import json
import os
import subprocess
from dreamcoder.domains.arithmetic.arithmeticPrimitives import k1, k0, addition, subtraction, multiplication
from dreamcoder.frontier import Frontier, FrontierEntry
from dreamcoder.grammar import Grammar
from dreamcoder.program import Program
from dreamcoder.task import Task
from dreamcoder.type import arrow, tint
from dreamcoder.utilities import tuplify, timing, eprint, get_root_dir, mean
def helmholtzEnumeration(g, request, inputs, timeout, _=None,
special=None, evaluationTimeout=None):
"""Returns json (as text)"""
message = {"request": request.json(),
"timeout": timeout,
"DSL": g.json(),
"extras": inputs}
if evaluationTimeout: message["evaluationTimeout"] = evaluationTimeout
if special: message["special"] = special
message = json.dumps(message)
with open('/tmp/hm', 'w') as handle:
handle.write(message)
try:
binary = os.path.join(get_root_dir(), 'helmholtz')
process = subprocess.Popen(binary,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
response, error = process.communicate(bytes(message, encoding="utf-8"))
except OSError as exc:
raise exc
return response
def backgroundHelmholtzEnumeration(tasks, g, timeout, _=None,
special=None, evaluationTimeout=None):
from pathos.multiprocessing import Pool
requests = list({t.request for t in tasks})
inputs = {r: list({tuplify(xs)
for t in tasks if t.request == r
for xs, y in t.examples})
for r in requests}
workers = Pool(len(requests))
promises = [workers.apply_async(helmholtzEnumeration,
args=(g, r, inputs[r], float(timeout)),
kwds={'special': special,
'evaluationTimeout': evaluationTimeout})
for r in requests]
def get():
results = [p.get() for p in promises]
frontiers = []
with timing("(Helmholtz enumeration) Decoded json into frontiers"):
for request, result in zip(requests, results):
response = json.loads(result.decode("utf-8"))
for b, entry in enumerate(response):
frontiers.append(Frontier([FrontierEntry(program=Program.parse(p),
logPrior=entry["ll"],
logLikelihood=0.)
for p in entry["programs"]],
task=Task(str(b),
request,
[])))
eprint("Total number of Helmholtz frontiers:", len(frontiers))
return frontiers
return get
if __name__ == "__main__":
from dreamcoder.recognition import RecognitionModel, DummyFeatureExtractor
g = Grammar.uniform([k1, k0, addition, subtraction, multiplication])
frontiers = helmholtzEnumeration(g,
arrow(tint, tint),
[[0], [1], [2]],
10.)
eprint("average frontier size", mean(len(f.entries) for f in frontiers))
f = DummyFeatureExtractor([])
r = RecognitionModel(f, g, hidden=[], contextual=True)
r.trainBiasOptimal(frontiers, frontiers, steps=70)
g = r.grammarOfTask(frontiers[0].task).untorch()
frontiers = helmholtzEnumeration(g,
arrow(tint, tint),
[[0], [1], [2]],
10.)
for f in frontiers:
eprint(f.summarizeFull())
eprint("average frontier size", mean(len(f.entries) for f in frontiers))