-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathanalyze_example.py
More file actions
297 lines (261 loc) · 13.5 KB
/
analyze_example.py
File metadata and controls
297 lines (261 loc) · 13.5 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import os
import pickle
from tqdm import tqdm
import numpy as np
import torch
import matplotlib.pyplot as plt
import train
import preprocessing
import arc_compressor
import initializers
import multitensor_systems
import layers
import solution_selection
import visualization
"""
This file allows you to train one model on one task, and see plots of what
the process and end result looks like. You can input the training split and
the task code, and it will:
- Train a model for 1500 steps,
- Plot sampled solutions from the model at every 50 steps,
- Plot the KL and reconstruction error over time,
- Plot the contribution of each tensor shape to the KL over time,
- Show top principal components of each tensor that still contributes to
the KL at the end of training.
"""
# For some reason trying to set the seed doesn't actually fix results.
# Just run things over and over again until you see desired interesting behaviors.
np.random.seed(0)
torch.manual_seed(0)
torch.set_default_dtype(torch.float32)
torch.set_default_device('cuda')
if __name__ == "__main__":
# Some interesting tasks: 272f95fa, 6d75e8bb, 6cdd2623, 41e4d17e, 2bee17df
# 228f6490, 508bd3b6, 2281f1f4, ecdecbb3
split = input('Enter which split you want to find the task in (training, evaluation, test): ')
task_name = input('Enter which task you want to analyze (eg. 272f95fa): ')
folder = task_name + '/'
print('Performing a training run on task', task_name,
'and placing the results in', folder)
os.makedirs(folder, exist_ok=True)
# Preprocess the task, set up the training
task = preprocessing.preprocess_tasks(split, [task_name])[0]
model = arc_compressor.ARCCompressor(task)
optimizer = torch.optim.Adam(model.weights_list, lr=0.01, betas=(0.5, 0.9))
train_history_logger = solution_selection.Logger(task)
visualization.plot_problem(train_history_logger)
# Perform training for 1500 iterations
n_iterations = 1500
for train_step in tqdm(range(n_iterations)):
train.take_step(task, model, optimizer, train_step, train_history_logger)
# Plot solutions every 50 steps
if (train_step+1) % 50 == 0:
visualization.plot_solution(train_history_logger,
fname=folder + task_name + '_at_' + str(train_step+1) + ' steps.png')
visualization.plot_solution(train_history_logger,
fname=folder + task_name + '_at_' + str(train_step+1) + ' steps.pdf')
# Save the metrics, model weights, and learned representations.
np.savez(folder + task_name + '_KL_curves.npz',
KL_curves={key:np.array(val) for key, val in train_history_logger.KL_curves.items()},
reconstruction_error_curve=np.array(train_history_logger.reconstruction_error_curve),
multiposteriors=model.multiposteriors,
target_capacities=model.target_capacities,
decode_weights=model.decode_weights)
# Load the metrics, model weights, and learned representations.
stored_data = np.load(folder + task_name + '_KL_curves.npz', allow_pickle=True)
KL_curves = stored_data['KL_curves'][()]
reconstruction_error_curve = stored_data['reconstruction_error_curve']
multiposteriors = stored_data['multiposteriors'][()]
target_capacities = stored_data['target_capacities'][()]
decode_weights = stored_data['decode_weights'][()]
# Plot the KL curves over time.
# For specific tasks that we found interesting, we wrote some
# code to color interesting KL components differently.
special_curve_colors = {
'272f95fa': {
'dims': [(1,0,0,1,0), (1,0,0,0,1), (0,1,1,0,0), (0,1,0,0,0)],
'colors': [(1, 0, 0), (0, 1, 0), (0, 0.5, 1), (0.5, 0, 1)]
},
'6cdd2623': {
'dims': [(1,0,0,1,0), (1,0,0,0,1), (1,1,0,0,0), (1,0,0,1,1), (0,0,1,0,0)],
'colors': [(1, 0.6, 0), (0, 1, 0), (0, 0.5, 1), (0.5, 0, 1), (1, 0, 0.5)]
},
'41e4d17e': {
'dims': [(1,0,0,1,1), (0,1,0,0,0)],
'colors': [(1, 0, 0), (0, 0, 1)]
},
'6d75e8bb': {
'dims': [(1,0,0,1,0), (1,0,0,0,1), (1,0,0,1,1), (0,1,0,0,0)],
'colors': [(1, 0, 0), (0, 1, 0), (0, 0.5, 1), (0.5, 0, 1)]
}
}
fig, ax = plt.subplots()
for component_name, curve in KL_curves.items():
line_color = (0.5, 0.5, 0.5)
label = None
if task_name in special_curve_colors:
dims_list = special_curve_colors[task_name]['dims']
colors_list = special_curve_colors[task_name]['colors']
for dims, color in zip(dims_list, colors_list):
if tuple(eval(component_name)) == dims:
line_color = color
axis_names = ['example', 'color', 'direction', 'height', 'width']
axis_names = [axis_name
for axis_name, axis_exists in zip(axis_names, dims) if axis_exists]
label = '(' + ', '.join(axis_names) + ', channel)'
ax.plot(np.arange(curve.shape[0]), curve, color=line_color, label=label)
if task_name == '6cdd2623':
ax.set_ylim((0.3, 4e4))
ax.legend()
plt.yscale('log')
plt.xlabel('step')
plt.ylabel('KL contribution')
ax.grid(which='both', linestyle='-', linewidth='0.5', color='gray')
plt.savefig(folder + task_name + '_KL_components.png', bbox_inches='tight')
plt.close()
# Plot the KL vs reconstruction error
fig, ax = plt.subplots()
total_KL = 0
for component_name, curve in KL_curves.items():
total_KL = total_KL + curve
fig, ax = plt.subplots()
ax.plot(np.arange(total_KL.shape[0]), total_KL, label='KL from z', color='k')
ax.plot(np.arange(reconstruction_error_curve.shape[0]),
reconstruction_error_curve, label='reconstruction error', color='r')
ax.legend()
plt.yscale('log')
plt.xlabel('step')
plt.ylabel('total KL or reconstruction error')
ax.grid(which='both', linestyle='-', linewidth='0.5', color='gray')
plt.savefig(folder + task_name + '_KL_vs_reconstruction.png', bbox_inches='tight')
plt.close()
# Get the learned representation tensors
samples = []
for i in range(100):
sample, KL_amounts, KL_names = layers.decode_latents(target_capacities,
decode_weights, multiposteriors)
samples.append(sample)
def average_samples(dims, *items):
mean = torch.mean(torch.stack(items, dim=0), dim=0).detach().cpu().numpy()
all_but_last_dim = tuple(range(len(mean.shape) - 1))
mean = mean - np.mean(mean, axis=all_but_last_dim)
return mean
means = multitensor_systems.multify(average_samples)(*samples)
# Figure out which tensors contain significant information
dims_to_plot = []
for KL_amount, KL_name in zip(KL_amounts, KL_names):
dims = tuple(eval(KL_name))
if torch.sum(KL_amount).detach().cpu().numpy() > 1:
dims_to_plot.append(dims)
# Show the top principal components of the significant tensors.
color_names = ['black', 'blue', 'red', 'green', 'yellow', 'gray', 'magenta', 'orange', 'light blue', 'brown']
restricted_color_names = [color_names[i] for i in task.colors]
restricted_color_codes = [tuple((visualization.color_list[i]/255).tolist())
for i in task.colors]
for dims in dims_to_plot:
tensor = means[dims]
orig_shape = tensor.shape
if len(orig_shape) == 2:
tensor = tensor[None,:,:]
orig_shape = tensor.shape
if len(orig_shape) == 3:
tensor = np.reshape(tensor, (-1, orig_shape[-1]))
U, S, Vh = np.linalg.svd(tensor) # Get top 3 principal components
for component_num in range(3):
component = np.reshape(U[:,component_num], orig_shape[:-1])
component = component / np.max(np.abs(component))
strength = S[component_num] / tensor.shape[0] # Calculate component strength
# Show the component
fig, ax = plt.subplots()
ax.imshow(component, cmap='gray', vmin=-1, vmax=1)
# Pick the axis labels
axis_names = ['example', 'color', 'direction', 'height', 'width']
tensor_name = '_'.join([axis_name
for axis_name, axis_exists in zip(axis_names, dims) if axis_exists])
if sum(dims) == 2:
x_dim = [axis_names[i] for i, dim in enumerate(dims) if dim][0]
y_dim = [axis_names[i] for i, dim in enumerate(dims) if dim][1]
else:
x_dim = None
y_dim = [axis_names[i] for i, dim in enumerate(dims) if dim][0]
plt.ylabel(x_dim)
plt.xlabel(y_dim)
if x_dim is None:
ax.set_yticks([])
ax.set_xticks([], minor=True)
if y_dim is None:
ax.set_xticks([])
ax.set_xticks([], minor=True)
# Set the tick labels
# Tick labels for example axis
if x_dim == 'example':
ax.set_yticks(np.arange(task.n_examples))
if y_dim == 'example':
ax.set_xticks(np.arange(task.n_examples))
# Tick labels for color axis
if x_dim == 'color':
ax.set_yticks(np.arange(len(restricted_color_names[1:])))
ax.set_yticklabels(restricted_color_names[1:])
for ticklabel, tickcolor in zip(ax.get_yticklabels(), restricted_color_codes[1:]):
ticklabel.set_color(tickcolor)
ticklabel.set_fontweight("bold")
if y_dim == 'color':
ax.set_xticks(np.arange(len(restricted_color_names[1:])))
ax.set_xticklabels(restricted_color_names[1:])
for ticklabel, tickcolor in zip(ax.get_xticklabels(), restricted_color_codes[1:]):
ticklabel.set_color(tickcolor)
ticklabel.set_fontweight("bold")
# Tick labels for direction axis
direction_names = ["↓", "↘", "→", "↗", "↑", "↖", "←", "↙"]
if x_dim == 'direction':
ax.set_yticks(np.arange(8))
ax.set_yticklabels(direction_names)
ax.tick_params(axis='y', which='major', labelsize=22)
if y_dim == 'direction':
ax.set_xticks(np.arange(8))
ax.set_xticklabels(direction_names)
ax.tick_params(axis='x', which='major', labelsize=22)
# Standard tick labels for height and width axes
ax.set_title('component' + str(component_num) + ', strength = ' + str(float(strength)))
plt.savefig(folder + task_name + '_' + tensor_name + '_component_' + str(component_num) + '.png', bbox_inches='tight')
plt.close()
# Plot an ({example, color, direction}, x, y) tensor with subplots
elif len(orig_shape) == 4 and dims[3] == 1 and dims[4] == 1:
tensor = np.reshape(tensor, (-1, orig_shape[-1]))
U, S, Vh = np.linalg.svd(tensor) # Get the top 3 principal components
for component_num in range(3):
component = np.reshape(U[:,component_num], orig_shape[:-1])
component = component / np.max(np.abs(component))
strength = S[component_num] / tensor.shape[0]
n_plots = orig_shape[0]
# Make the subplots
fig, axs = plt.subplots(1, n_plots)
for plot_idx in range(n_plots):
ax = axs[plot_idx]
ax.imshow(component[plot_idx,:,:], cmap='gray', vmin=-1, vmax=1)
# Get the axis labels
axis_names = ['example', 'color', 'direction', 'height', 'width']
tensor_name = '_'.join([axis_name
for axis_name, axis_exists in zip(axis_names, dims) if axis_exists])
ax_dim = [axis_names[i] for i, dim in enumerate(dims) if dim][0]
x_dim = [axis_names[i] for i, dim in enumerate(dims) if dim][1]
y_dim = [axis_names[i] for i, dim in enumerate(dims) if dim][2]
ax.set_ylabel(x_dim)
ax.set_xlabel(y_dim)
# Standard tick labels for height and width axes
# Label the subplots
if ax_dim == 'example':
ax.set_title('example ' + str(plot_idx))
elif ax_dim == 'color':
ax.set_title(restricted_color_names[plot_idx],
color=restricted_color_codes[plot_idx],
fontweight="bold")
elif ax_dim == 'direction':
direction_names = ["↓", "↘", "→", "↗", "↑", "↖", "←", "↙"]
ax.set_title(direction_names[plot_idx], fontsize=22)
plt.subplots_adjust(wspace=1)
fig.suptitle('component ' + str(component_num) + ', strength = ' + str(float(strength)))
plt.subplots_adjust(top=1.4)
plt.savefig(folder + task_name + '_' + tensor_name + '_component_' + str(component_num) + '.png', bbox_inches='tight')
plt.close()
print('done')