-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata.py
134 lines (96 loc) · 3.15 KB
/
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
128
129
130
131
132
133
import numpy as np
import torch
from scipy.stats import special_ortho_group as rot
from stats_utils import diagonal_matrix
def random_matrix_sv (singular_values, d_input=10, d_output=2, return_svd=False):
'''
Generates a random matrix with given singular values
'''
# generate random rotation matrices
# to use as left and right singular vector basis
U = rot.rvs(d_output) if d_output > 1 else np.array([[1.]])
Vh = rot.rvs(d_input).T if d_input > 1 else np.array([[1.]])
_S = diagonal_matrix(singular_values, d_output, d_input)
_mat = np.matmul( U, _S )
_mat = np.matmul( _mat, Vh )
if return_svd:
return _mat, (U, S, Vh)
else:
return _mat
class LinearRegressionDataset(torch.utils.data.Dataset):
'''
data to be used through a data loader
- N: dimensionality of the input
- n_samples: number of samples in the dataset
'''
def __init__ (self, w_star, n_samples, cov=None):
self.w = np.atleast_2d(w_star)
self.d, self.N = self.w.shape
if cov is None:
cov = np.eye(self.N)
assert len(cov) == self.N, "covariance matrix must have the same dimensions as the input vector"
X = np.random.multivariate_normal(np.zeros(self.N), cov, size=n_samples)
y = np.matmul(X, self.w.T)
self.X = torch.from_numpy(X)
self.y = torch.from_numpy(y)
@property
def data(self):
return self.X
@property
def targets(self):
return self.y
def __len__ (self):
return len(self.X)
def __getitem__ (self, i):
return self.X[i], self.y[i]
class SemanticsDataset (torch.utils.data.Dataset):
# 0 - grow
# 1 - move
# 2 - roots
# 3 - fly
# 4 - swims
# 5 - leaves
# 6 - petals
N = 7
def __init__ (self, n_samples, cov=None):
# input-output covariance matrix
IO_cov = np.array([
[1,1,0,1,0,0,0],
[1,1,0,0,1,0,0],
[1,0,1,0,0,1,0],
[1,0,1,0,0,0,1]
]).astype(float).T
# input-input covariance matrix
if cov is None:
cov = np.eye(self.N)
# target input-output map
self.w = np.dot( np.linalg.inv(cov), IO_cov ).T
X = np.random.multivariate_normal(np.zeros(self.N), cov, size=n_samples)
y = np.matmul(X, self.w.T)
self.X = torch.from_numpy(X)
self.y = torch.from_numpy(y)
@property
def data(self):
return self.X
@property
def targets(self):
return self.y
def __len__ (self):
return len(self.X)
def __getitem__ (self, i):
return self.X[i], self.y[i]
if __name__ == "__main__":
d_input = 3
d_output = 2
S = np.array([2., 1.])
w_star, (_U, _, _Vh) = random_matrix_sv(S, d_input=d_input, d_output=d_output, return_svd=True)
print("w_star")
print(w_star.shape)
print(w_star)
U, S, Vh = np.linalg.svd(w_star)
print("S")
print(S)
print(np.matmul(U.T, _U))
print(np.allclose(np.eye(d_output), np.matmul(U.T, _U)))
print(np.matmul(_Vh, _Vh.T))
print(np.allclose(np.eye(d_input), np.matmul(_Vh, _Vh.T)))