forked from ellisk42/ec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geom.py
118 lines (101 loc) · 4.05 KB
/
geom.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from ec import explorationCompression, commandlineArguments
from grammar import Grammar
from utilities import eprint, testTrainSplit, numberOfCPUs
from makeGeomTasks import makeTasks
from geomPrimitives import primitives, tcanvas
from math import log
import torch
import png
import time
import subprocess
import torch.nn as nn
from recognition import variable
# : Task -> feature list
class GeomFeatureCNN(nn.Module):
def __init__(self, tasks, cuda=False, H=10):
super(GeomFeatureCNN, self).__init__()
self.net1 = nn.Sequential(
nn.Conv2d(1, 6, kernel_size=(5, 5)),
nn.ReLU(),
nn.Conv2d(6, 16, kernel_size=(5, 5)),
nn.MaxPool2d(kernel_size=(2, 2), stride=2),
nn.ReLU()
)
self.net2 = nn.Sequential(
nn.Linear(12544, 120), # Hardocde the first one I guess :/
nn.Linear(120, 84),
nn.Linear(84, H)
)
self.mean = []
self.outputDimensionality = H
def forward(self, v):
x, y = 64, 64 # Should not hardocode these.
floatOnlyTask = map(float, v)
reshaped = [floatOnlyTask[i:i+x]
for i in range(0, len(floatOnlyTask), y)]
variabled = variable(reshaped).float()
variabled = torch.unsqueeze(variabled, 0)
variabled = torch.unsqueeze(variabled, 0)
output = self.net1(variabled)
s1, s2, s3, s4 = output.size()
output = output.view(-1, s1*s2*s3*s4)
output = self.net2(output).clamp(min=0)
output = torch.squeeze(output)
return output
def featuresOfTask(self, t): # Take a task and returns [features]
return self(t.examples[0][1])
def featuresOfProgram(self, p, t): # Won't fix for geom
if t == tcanvas:
try:
output = subprocess.check_output(['./geomDrawLambdaString',
p.evaluate([])]).split("\n")
shape = map(float, output[0].split(','))
# bigShape = map(float, output[1].split(','))
bigShape = shape
except OSError as exc:
raise exc
else:
assert(False)
try:
self.mean += [bigShape]
return self(shape)
except ValueError:
return None
def finish(self):
if len(self.mean) > 0:
mean = [log(1+float(sum(col))/len(col)) for col in zip(*self.mean)]
mi = min(mean)
ma = max(mean)
mean = [(x - mi + (1/255)) / (ma - mi) for x in mean]
img = [(int(x*254), int(x*254), int(x*254)) for x in mean]
img = [img[i:i+64] for i in range(0, 64*64, 64)]
img = [tuple([e for t in x for e in t]) for x in img]
fname = 'dream_low_calc/dream-'+(str(int(time.time())))+'.png'
f = open(fname, 'wb')
w = png.Writer(64, 64)
w.write(f, img)
f.close()
self.mean = []
if __name__ == "__main__":
tasks = makeTasks()
eprint("Generated", len(tasks), "tasks")
test, train = testTrainSplit(tasks, 0.5)
eprint("Split tasks into %d/%d test/train" % (len(test), len(train)))
baseGrammar = Grammar.uniform(primitives)
explorationCompression(baseGrammar, train,
testingTasks=test,
outputPrefix="experimentOutputs/geom",
compressor="rust",
evaluationTimeout=0.01,
**commandlineArguments(
steps=200,
a=1,
iterations=100,
useRecognitionModel=True,
helmholtzRatio=0.5,
helmholtzBatch=200,
featureExtractor=GeomFeatureCNN,
topK=2,
maximumFrontier=1000,
CPUs=numberOfCPUs(),
pseudoCounts=10.0))