-
Notifications
You must be signed in to change notification settings - Fork 1
/
differentiability.py
145 lines (114 loc) · 3.9 KB
/
differentiability.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# %%
import matplotlib.pyplot as plt
import torch as th
from ds.fol import FOL, PredicateBase
# ===============================================================================
# Predicate Examples
# ===============================================================================
class InFirstQuadrant(PredicateBase):
def __init__(self):
super().__init__(name="InFirstQuadrant")
def eval(self, xs: th.Tensor) -> th.Tensor:
"""
Return positive values if the point is in the first quadrant.
Return negative values if the point is not in the first quadrant.
"""
return th.min(xs, dim=1).values
class XGreatThan(PredicateBase):
def __init__(self, x):
super().__init__(name=f"XGreatThan({x})")
self.x = x
def eval(self, xs: th.Tensor) -> th.Tensor:
"""
Return positive values if the first coordinate is greater than x.
Return negative values if the first coordinate is not greater than x.
"""
return xs[..., 0] - self.x
class XLessThan(PredicateBase):
def __init__(self, x):
super().__init__(name=f"XLessThan({x})")
self.x = x
def eval(self, xs: th.Tensor) -> th.Tensor:
"""
Return positive values if the first coordinate is less than x.
Return negative values if the first coordinate is not less than x.
"""
return -xs[..., 0] + self.x
class YGreatThan(PredicateBase):
def __init__(self, y):
super().__init__(name=f"YGreatThan({y})")
self.y = y
def eval(self, xs: th.Tensor) -> th.Tensor:
"""
Return positive values if the second coordinate is greater than y.
Return negative values if the second coordinate is not greater than y.
"""
return xs[..., 1] - self.y
class YLessThan(PredicateBase):
def __init__(self, y):
super().__init__(name=f"YLessThan({y})")
self.y = y
def eval(self, xs: th.Tensor) -> th.Tensor:
"""
Return positive values if the second coordinate is less than y.
Return negative values if the second coordinate is not less than y.
"""
return self.y - xs[..., 1]
# ===============================================================================
# Differentiability
# ===============================================================================
def backward():
# Define a predicate
p1 = InFirstQuadrant()
p2 = XGreatThan(2.0)
p3 = XLessThan(3.0)
p4 = YLessThan(3.0)
# Define a formula
f = FOL(p1).forall() & FOL(p2).exists() & FOL(p3).forall() & FOL(p4).forall()
print(f)
# create plot
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
# set x and y limits
ax[0].set_xlim(-3, 3)
ax[0].set_ylim(-3, 3)
ax[1].set_xlim(-3, 3)
ax[1].set_ylim(-3, 3)
# Define two worlds, we support batch evaluation,
# but the points in each world should be the same / padding to same.
x = th.tensor(
[
[[1.0, 1.0], [-1.0, 1.0], [1.0, -1.0], [-1.0, -1.0]],
[[0.5, 0.5], [-0.5, 0.5], [0.5, -0.5], [-0.5, -0.5]],
],
requires_grad=True,
)
ax[0].scatter(
x.numpy(force=True)[0, :, 0], x.numpy(force=True)[0, :, 1], label="world 1"
)
ax[0].scatter(
x.numpy(force=True)[1, :, 0], x.numpy(force=True)[1, :, 1], label="world 2"
)
# define optimizer
optimizer = th.optim.Adam([x], lr=0.1)
# optimize
loss = None
for i in range(10000):
optimizer.zero_grad()
loss = -f(x).mean()
loss.backward()
optimizer.step()
print(loss)
print(x)
# plot
ax[1].scatter(
x.numpy(force=True)[0, :, 0], x.numpy(force=True)[0, :, 1], label="world 1"
)
ax[1].scatter(
x.numpy(force=True)[1, :, 0], x.numpy(force=True)[1, :, 1], label="world 2"
)
ax[0].legend()
ax[1].legend()
plt.show()
if __name__ == "__main__":
backward()
# %%