-
Notifications
You must be signed in to change notification settings - Fork 8
/
generate_synthetic_data.py
128 lines (112 loc) · 4.68 KB
/
generate_synthetic_data.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
"""Generate synthetic data for experiments."""
import argparse
import os
from e2cnn import gspaces
from e2cnn import nn as gnn
from scipy.special import softmax
import numpy as np
import torch
from torch import nn
from layers import LocallyConnected1d
def generate_1d(out_path):
lc_layer = LocallyConnected1d(1, 1, 68, bias=False)
xs, ys, ws = [], [], []
for task_idx in range(10000):
filt = np.random.randn(1, 1, 1, 1, 3).astype(np.float32)
filt = np.repeat(filt, 68, axis=3)
ws.append(filt)
lc_layer.weight = nn.Parameter(torch.from_numpy(filt))
task_xs, task_ys = [], []
inp = np.random.randn(20, 1, 70).astype(np.float32)
result = lc_layer(torch.from_numpy(inp)) # (20, 1, 68)
result = result.cpu().detach().numpy()
xs.append(inp)
ys.append(result)
if task_idx % 100 == 0:
print(f"Finished generating task {task_idx}")
xs, ys, ws = np.stack(xs), np.stack(ys), np.stack(ws)
np.savez(out_path, x=xs, y=ys, w=ws)
def generate_1d_low_rank(out_path, rank=2):
lc_layer = LocallyConnected1d(1, 1, 68, bias=False)
xs, ys, ws = [], [], []
connectivity = softmax(np.random.randn(68, rank), axis=1) # shape == (68, rank)
for task_idx in range(10000):
basis = np.random.randn(rank, 3)
filt = np.dot(connectivity, basis) # shape == (68, 3)
filt = np.reshape(filt, (1, 1, 1, 68, 3)).astype(np.float32)
ws.append(filt)
lc_layer.weight = nn.Parameter(torch.from_numpy(filt))
task_xs, task_ys = [], []
inp = np.random.randn(20, 1, 70).astype(np.float32)
result = lc_layer(torch.from_numpy(inp)) # (20, 1, 68)
result = result.cpu().detach().numpy()
xs.append(inp)
ys.append(result)
if task_idx % 100 == 0:
print(f"Finished generating task {task_idx}")
xs, ys, ws = np.stack(xs), np.stack(ys), np.stack(ws)
np.savez(out_path, x=xs, y=ys, w=ws)
def generate_2d_rot8(out_path):
r2_act = gspaces.Rot2dOnR2(N=8)
feat_type_in = gnn.FieldType(r2_act, [r2_act.trivial_repr])
feat_type_out = gnn.FieldType(r2_act, 3 * [r2_act.regular_repr])
conv = gnn.R2Conv(feat_type_in, feat_type_out, kernel_size=3, bias=False)
xs, ys, ws = [], [], []
for task_idx in range(10000):
gnn.init.generalized_he_init(conv.weights, conv.basisexpansion)
inp = gnn.GeometricTensor(torch.randn(20, 1, 32, 32), feat_type_in)
result = conv(inp).tensor.detach().cpu().numpy()
xs.append(inp.tensor.detach().cpu().numpy())
ys.append(result)
ws.append(conv.weights.detach().cpu().numpy())
if task_idx % 100 == 0:
print(f"Finished generating task {task_idx}")
xs, ys, ws = np.stack(xs), np.stack(ys), np.stack(ws)
np.savez(out_path, x=xs, y=ys, w=ws)
def generate_2d_rot8_flip(out_path):
r2_act = gspaces.FlipRot2dOnR2(N=8)
feat_type_in = gnn.FieldType(r2_act, [r2_act.trivial_repr])
feat_type_out = gnn.FieldType(r2_act, 3 * [r2_act.regular_repr])
xs, ys, ws = [], [], []
device = torch.device("cuda")
conv = gnn.R2Conv(feat_type_in, feat_type_out, kernel_size=3, bias=False).to(device)
for task_idx in range(2000):
gnn.init.generalized_he_init(conv.weights, conv.basisexpansion)
inp = gnn.GeometricTensor(torch.randn(20, 1, 32, 32).to(device), feat_type_in)
result = conv(inp).tensor.detach().cpu().numpy()
xs.append(inp.tensor.detach().cpu().numpy())
ys.append(result)
ws.append(conv.weights.detach().cpu().numpy())
del inp, result
if task_idx % 100 == 0:
print(f"Finished generating task {task_idx}")
xs, ys, ws = np.stack(xs), np.stack(ys), np.stack(ws)
np.savez(out_path, x=xs, y=ys, w=ws)
TYPE_2_PATH = {
"rank1": "./data/rank1.npz",
"rank2": "./data/rank2.npz",
"rank5": "./data/rank5.npz",
"2d_rot8": "./data/2d_rot8.npz",
"2d_rot8_flip": "./data/2d_rot8_flip.npz",
}
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--problem", type=str, default="rank1")
args = parser.parse_args()
out_path = TYPE_2_PATH[args.problem]
if os.path.exists(out_path):
raise ValueError(f"File exists at {out_path}.")
if args.problem == "rank1":
generate_1d(out_path)
elif args.problem == "rank2":
generate_1d_low_rank(out_path, rank=2)
elif args.problem == "rank5":
generate_1d_low_rank(out_path, rank=5)
elif args.problem == "2d_rot8":
generate_2d_rot8(out_path)
elif args.problem == "2d_rot8_flip":
generate_2d_rot8_flip(out_path)
else:
raise ValueError(f"Unrecognized problem {args.problem}")
if __name__ == "__main__":
main()