-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtwo_relations.py
225 lines (201 loc) · 7.25 KB
/
two_relations.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# Copyright 2021 MIT Probabilistic Computing Project
# Apache License, Version 2.0, refer to LICENSE.txt
import os
import random
from pprint import pprint
import matplotlib.pyplot as plt
import numpy as np
from hirm import IRM
from hirm.util_plot import plot_binary_relation
prng = random.Random(1)
# ===== Synthetic data generation.
items_D1_R1 = [
list(range(0, 10)) + list(range(20,30)),
list(range(10, 20)),
]
items_D2_R1 = [
list(range(0, 20)),
list(range(20, 40)),
]
data_r1_d10_d20 = [((i, j), 0) for i in items_D1_R1[0] for j in items_D2_R1[0]]
data_r1_d10_d21 = [((i, j), 1) for i in items_D1_R1[0] for j in items_D2_R1[1]]
data_r1_d11_d20 = [((i, j), 1) for i in items_D1_R1[1] for j in items_D2_R1[0]]
data_r1_d11_d21 = [((i, j), 0) for i in items_D1_R1[1] for j in items_D2_R1[1]]
data_r1 = data_r1_d10_d20 + data_r1_d10_d21 + data_r1_d11_d20 + data_r1_d11_d21
items_D1_R2 = [
list(range(0, 30))[::2],
list(range(0, 30))[1::2]
]
items_D2_R2 = [
list(range(0, 20))[::2],
list(range(0, 20))[1::2],
list(range(20, 40))[::2],
list(range(20, 40))[1::2]
]
data_r2_d10_d20 = [((i, j), 0) for i in items_D1_R2[0] for j in items_D2_R2[0]]
data_r2_d10_d21 = [((i, j), 1) for i in items_D1_R2[0] for j in items_D2_R2[1]]
data_r2_d10_d22 = [((i, j), 0) for i in items_D1_R2[0] for j in items_D2_R2[2]]
data_r2_d10_d23 = [((i, j), 1) for i in items_D1_R2[0] for j in items_D2_R2[3]]
data_r2_d11_d20 = [((i, j), 1) for i in items_D1_R2[1] for j in items_D2_R2[0]]
data_r2_d11_d21 = [((i, j), 0) for i in items_D1_R2[1] for j in items_D2_R2[1]]
data_r2_d11_d22 = [((i, j), 1) for i in items_D1_R2[1] for j in items_D2_R2[3]]
data_r2_d11_d23 = [((i, j), 0) for i in items_D1_R2[1] for j in items_D2_R2[2]]
data_r2 \
= data_r2_d10_d20 + data_r2_d10_d21 + data_r2_d10_d22 + data_r2_d10_d23 \
+ data_r2_d11_d20 + data_r2_d11_d21 + data_r2_d11_d22 + data_r2_d11_d23
xlabels = {'R1': 'D2', 'R2': 'D2'}
ylabels = {'R1': 'D1', 'R2': ''}
# Write schema to disk.
path_schema = os.path.join('assets', 'two_relations.schema')
with open(path_schema, 'w') as f:
f.write('bernoulli R1 D1 D2\n')
f.write('bernoulli R2 D1 D2\n')
print(path_schema)
# Write observations to disk.
path_obs = os.path.join('assets', 'two_relations.obs')
with open(path_obs, 'w') as f:
for ((i, j), value) in data_r1:
f.write('%d R1 %d %d\n' % (value, i, j))
for ((i, j), value) in data_r2:
f.write('%d R2 %d %d\n' % (value, i, j))
print(path_obs)
# Plot the synthetic data.
fig, axes = plt.subplots(ncols=2)
for relation, data, ax in [('R1', data_r1, axes[0]), ('R2', data_r2, axes[1])]:
X = np.zeros((30, 40))
for (i, j), v in data:
X[i,j] = v
nr = 30
nc = 40
pir = prng.sample(list(range(nr)), k=nr)
pic = prng.sample(list(range(nc)), k=nc)
X = np.asarray([
[X[pir[r], pic[c]] for c in range(nc)]
for r in range(nr)
])
ax.imshow(X, cmap='Greys')
ax.xaxis.tick_top()
ax.xaxis.set_label_position('top')
ax.set_xlabel(xlabels[relation])
ax.set_ylabel(ylabels[relation])
ax.set_xticks([])
ax.set_yticks([])
ax.text(.05, .95, relation,
ha='left', va='top',
transform=ax.transAxes,
bbox={'facecolor': 'red', 'alpha': 1, 'edgecolor':'k'})
figname = os.path.join('assets', 'two_relations.data.png')
fig.set_size_inches((4,2))
fig.set_tight_layout(True)
fig.savefig(figname)
print(figname)
# ===== Make an IRM for both relations (using seed that underfits).
schema = {'R1': ('D1', 'D2'), 'R2': ('D1', 'D2')}
irm = IRM(schema, prng=random.Random(1))
for relation, data in [
('R1', data_r1),
('R2', data_r2)
]:
for (i, j), v in data:
irm.incorporate(relation, (i, j), v)
# Run inference.
for i in range(100):
irm.transition_cluster_assignments()
pprint(irm.domains['D1'].crp.tables)
pprint(irm.domains['D2'].crp.tables)
pprint(irm.logp_score())
# Plot the posterior.
fig, axes = plt.subplots(ncols=2)
for relation, data, ax in [('R1', data_r1, axes[0]), ('R2', data_r2, axes[1])]:
plot_binary_relation(irm.relations[relation], ax=ax)
score = irm.relations[relation].logp_score()
ax.xaxis.set_label_position('top')
ax.set_xlabel(xlabels[relation])
ax.set_ylabel(ylabels[relation])
ax.set_xticks([])
ax.set_yticks([])
ax.text(.5, -.1, 'log score = %1.2f' % (score,),
ha='center', va='top', color='k',
transform=ax.transAxes)
figname = os.path.join('assets', 'two_relations.underfit.png')
fig.set_size_inches((4,2))
fig.set_tight_layout(True)
fig.savefig(figname)
print(figname)
# ===== Make an IRM for both relations (using seed that overfits).
schema = {'R1': ('D1', 'D2'), 'R2': ('D1', 'D2')}
irm = IRM(schema, prng=random.Random(10))
for relation, data in [
('R1', data_r1),
('R2', data_r2)
]:
for (i, j), v in data:
irm.incorporate(relation, (i, j), v)
# Run inference.
for i in range(200):
irm.transition_cluster_assignments()
pprint(irm.domains['D1'].crp.tables)
pprint(irm.domains['D2'].crp.tables)
pprint(irm.logp_score())
# Plot the posterior.
fig, axes = plt.subplots(ncols=2)
for relation, data, ax in [('R1', data_r1, axes[0]), ('R2', data_r2, axes[1])]:
plot_binary_relation(irm.relations[relation], ax=ax)
score = irm.relations[relation].logp_score()
ax.xaxis.set_label_position('top')
ax.set_xlabel(xlabels[relation])
ax.set_ylabel(ylabels[relation])
ax.set_xticks([])
ax.set_yticks([])
ax.text(.5, -.1, 'log score = %1.2f' % (score,),
ha='center', va='top', color='k',
transform=ax.transAxes)
figname = os.path.join('assets', 'two_relations.overfit.png')
fig.set_size_inches((4,2))
fig.set_tight_layout(True)
fig.savefig(figname)
print(figname)
# ===== Make IRM for each relation separately.
irm1 = IRM({'R1': ('D1', 'D2')}, prng=random.Random(1))
irm2 = IRM({'R2': ('D1', 'D2')}, prng=random.Random(10))
for (i, j), v in data_r1:
irm1.incorporate('R1', (i, j), v)
for (i, j), v in data_r2:
irm2.incorporate('R2', (i, j), v)
# Run inference.
for i in range(100):
irm1.transition_cluster_assignments()
irm2.transition_cluster_assignments()
pprint(irm1.domains['D1'].crp.tables)
pprint(irm2.domains['D2'].crp.tables)
pprint(irm1.logp_score())
pprint(irm2.logp_score())
# Plot the posterior.
fig, axes = plt.subplots(ncols=2)
plot_binary_relation(irm1.relations['R1'], ax=axes[0])
score1 = irm1.relations['R1'].logp_score()
axes[0].xaxis.set_label_position('top')
axes[0].set_xlabel(xlabels['R1'])
axes[0].set_ylabel(ylabels['R1'])
axes[0].set_xticks([])
axes[0].set_yticks([])
axes[0].text(.5, -.1, 'log score = %1.2f' % (score1,),
ha='center', va='top', color='k', transform=axes[0].transAxes)
plot_binary_relation(irm2.relations['R2'], ax=axes[1])
score2 = irm2.relations['R2'].logp_score()
axes[1].xaxis.set_label_position('top')
axes[1].set_xlabel(xlabels['R2'])
axes[1].set_ylabel(ylabels['R2'])
axes[1].set_xticks([])
axes[1].set_yticks([])
axes[1].text(.5, -.1, 'log score = %1.2f' % (score2,),
ha='center', va='top', color='k', transform=axes[1].transAxes)
figname = os.path.join('assets', 'two_relations.separate.png')
fig.set_size_inches((4,2))
fig.set_tight_layout(True)
fig.savefig(figname)
print(figname)
plt.show()
# from hirm.util_math import log_normalize
# p1, p2 = np.exp(log_normalize([score, score1 + score2]))
# print((p1, p2))