-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnv.py
More file actions
304 lines (271 loc) · 10.7 KB
/
Env.py
File metadata and controls
304 lines (271 loc) · 10.7 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
298
299
300
301
302
303
304
# Creating environments for stochastic clustering
import numpy as np
import torch
from torch import nn
from scipy.spatial.distance import cdist
class ClusteringEnvNumpy:
"""
A NumPy implementation of a clustering environment for reinforcement learning.
This environment models a Markov Decision Process where:
- States are data points (i) that need to be assigned to clusters
- Actions are cluster assignments (j)
- Transitions move to new clusters (k) based on transition probabilities
The transition probabilities p(k|j,i) can be either:
1. Parametrized based on distances between data points and cluster centers
2. Fixed to predefined values
Parameters
----------
n_data : int
Number of data points to cluster
n_clusters : int
Number of clusters
n_features : int
Dimensionality of the data points
parametrized : bool
If True, transition probabilities are computed from data and cluster distances
If False, fixed transition probabilities are used
T_p : ndarray, optional, shape (n_clusters, n_clusters, n_data)
Fixed transition probabilities when parametrized=False
kappa : float, default=0.3
Exploration probability (kappa) - probability of transitioning to a different cluster
gamma : float, default=0.0
Weight for data-cluster distances d(i,k) in utility function
zeta : float, default=1.0
Weight for cluster-cluster distances d(j,k) in utility function
T : float, default=1.0
Softmax temperature parameter - controls randomness in transitions
seed : int, default=0
Random seed for reproducibility
Methods
-------
return_probabilities(X, Y)
Computes transition probabilities p(k|j,i) based on data points X and cluster centers Y
step(i, j, X=None, Y=None)
Performs one transition step from data point i and cluster j, returning new cluster k
"""
def __init__(
self,
n_data,
n_clusters,
n_features,
parametrized,
T_p=None,
kappa=0.3,
gamma=0.0,
zeta=1.0,
T=1.0,
seed=0,
):
np.random.seed(seed)
self.n_data = n_data
self.n_clusters = n_clusters
self.n_features = n_features
self.parametrized = parametrized
self.kappa = kappa # exploration probability (kappa)
self.gamma = gamma # weight for d(i,k)
self.zeta = zeta # weight for d(j,k)
self.T = T # softmax temperature
self.T_p = T_p # optional fixed transition probabilities
self.prob = None
_ = self.return_probabilities(None, None)
def return_probabilities(self, X, Y):
"""
Compute p(k|j,i).
X : (n_data, n_features) data points
Y : (n_clusters, n_features) cluster centers
"""
if self.parametrized and X is not None and Y is not None:
# Pairwise squared distances
d_ik = np.sum(
(X[:, None, :] - Y[None, :, :]) ** 2, axis=2
) # (n_data, n_clusters)
d_jk = np.sum(
(Y[:, None, :] - Y[None, :, :]) ** 2, axis=2
) # (n_clusters, n_clusters)
# utilities u_k(j,i) = zeta*d_jk + gamma*d_ik
# Result shape: (k, j, i)
u = (
self.zeta * d_jk.T[:, :, None] # (k,j,1)
+ self.gamma * d_ik.T[:, None, :] # (k,1,i)
)
# mask True where k != j
mask = ~np.eye(self.n_clusters, dtype=bool)[:, :, None] # (k,j,1)
# exponentiate only off-diagonal terms
u_masked = np.where(mask, u, np.inf) # (k,j,i) with inf where k == j
u_masked_mins = np.min(u_masked, axis=0, keepdims=True) # (1,j,i)
# subtract mins for numerical stability
exp_u = np.exp(-(u_masked - u_masked_mins) / self.T)
# softmax over k dimension, but only for k ≠ j
denom = exp_u.sum(axis=0, keepdims=True) # (1, j, i)
# kappa * normalized exp(-u/T) for k ≠ j
prob = np.where(mask, self.kappa * exp_u / denom, 0.0)
# diagonal entries k == j get 1 - kappa
diag = np.arange(self.n_clusters)
prob[diag, diag, :] = 1.0 - self.kappa
else:
if self.T_p is not None:
prob = self.T_p
else:
prob = np.full(
(self.n_clusters, self.n_clusters, self.n_data),
0.0 / (self.n_clusters - 1),
)
for i in range(self.n_data):
for j in range(self.n_clusters):
prob[j, j, i] = 1.0
self.prob = prob
return prob
def step(self, i, j, X=None, Y=None):
if self.parametrized:
self.return_probabilities(X, Y)
k = np.random.choice(self.n_clusters, p=self.prob[:, j, i])
return k
class ClusteringEnvTorch:
"""
PyTorch implementation of the clustering environment.
Parameters
----------
n_data : int
n_clusters : int
n_features : int
parametrized : bool
If True, transition probabilities are computed from data and cluster distances.
If False, fixed transition probabilities are used.
T_p : torch.Tensor, optional, shape (n_clusters, n_clusters, n_data)
Fixed transition probabilities when parametrized=False.
kappa : float, default=0.3
Exploration probability.
gamma : float, default=0.0
Weight for data-cluster distances d(i,k).
zeta : float, default=1.0
Weight for cluster-cluster distances d(j,k).
T : float, default=1.0
Softmax temperature.
seed : int, default=0
Random seed.
device : torch.device or str, default="cpu"
"""
def __init__(
self,
n_data,
n_clusters,
n_features,
parametrized,
T_p=None,
kappa=0.3,
gamma=0.0,
zeta=1.0,
T=1.0,
seed=0,
device="cpu",
):
torch.manual_seed(seed)
self.n_data = n_data
self.n_clusters = n_clusters
self.n_features = n_features
self.parametrized = parametrized
self.kappa = kappa
self.gamma = gamma
self.zeta = zeta
self.T = T
self.device = torch.device(device)
self.T_p = T_p.to(self.device) if T_p is not None else None
self.prob = None
_ = self.return_probabilities(None, None) # initialize prob
@torch.no_grad() # no need to track gradients here
def return_probabilities(self, X, Y):
"""
Compute p(k|j,i).
X : (n_data, n_features)
Y : (n_clusters, n_features)
"""
if self.parametrized and X is not None and Y is not None:
X = X.to(self.device)
Y = Y.to(self.device)
# pairwise squared distances
d_ik = torch.sum(
(X[:, None, :] - Y[None, :, :]) ** 2, dim=2
) # (n_data, n_clusters)
d_jk = torch.sum(
(Y[:, None, :] - Y[None, :, :]) ** 2, dim=2
) # (n_clusters, n_clusters)
# utilities u_k(j,i) = zeta*d_jk + gamma*d_ik -> (k, j, i)
u = self.zeta * d_jk.T[:, :, None] + self.gamma * d_ik.T[:, None, :]
mask = ~torch.eye(self.n_clusters, dtype=torch.bool, device=self.device)[
:, :, None
]
u_masked = torch.where(
mask, u, torch.tensor(float("inf"), device=self.device)
)
u_masked_mins = torch.min(u_masked, dim=0, keepdim=True).values
exp_u = torch.exp(-(u_masked - u_masked_mins) / self.T)
denom = exp_u.sum(dim=0, keepdim=True) # (1, j, i)
prob = torch.where(mask, self.kappa * exp_u / denom, torch.zeros_like(u))
diag = torch.arange(self.n_clusters, device=self.device)
prob[diag, diag, :] = 1.0 - self.kappa
else:
if self.T_p is not None:
prob = self.T_p
else:
prob = torch.full(
(self.n_clusters, self.n_clusters, self.n_data),
0.1 / (self.n_clusters - 1),
device=self.device,
)
for i in range(self.n_data):
for j in range(self.n_clusters):
prob[j, j, i] = 0.9
self.prob = prob
return prob
@torch.no_grad() # no need to track gradients here
def step(self, batch_indices_all, idx, B, S, mc_samples, X=None, Y=None):
"""
Batched Monte-Carlo sampling of next clusters.
Parameters
----------
batch_indices_all : (B, S) LongTensor
Indices of data points i.
idx : (B, S) LongTensor
Current cluster assignments j.
B : int
Batch size.
S : int
Number of samples in each batch.
mc_samples : int
Number of Monte-Carlo samples per (B,S).
Returns
-------
realized_clusters : (B, S, mc_samples) LongTensor
"""
M = self.n_clusters
if self.parametrized:
self.return_probabilities(X, Y) # update self.prob if needed
probs = self.prob.to(self.device)
m_idx = torch.arange(M, device=self.device).view(1, 1, M).expand(B, S, M)
i_idx = batch_indices_all.unsqueeze(-1).expand(B, S, M)
j_idx = idx.unsqueeze(-1).expand(B, S, M)
prob_matrix = probs[m_idx, j_idx, i_idx] # (B, S, M)
flat_probs = prob_matrix.reshape(-1, M) # (B*S, M)
assert torch.all(torch.isfinite(flat_probs)), "NaN/Inf in probs"
assert torch.all(flat_probs >= 0), "Negative probs"
row_sums = flat_probs.sum(dim=1)
assert torch.all(row_sums > 0), "Row with zero total probability"
realized_clusters = torch.multinomial(flat_probs, mc_samples, replacement=True)
realized_clusters = realized_clusters.view(B, S, mc_samples)
return realized_clusters
if __name__ == "__main__":
# test the torch environment
N = 100 # number of data points
M = 5 # number of clusters
d = 2 # number of features
env = ClusteringEnvTorch(
n_data=N, n_clusters=M, n_features=d, parametrized=True, device="cuda"
)
X = torch.randn(N, d)
Y = torch.randn(M, d)
prob = env.return_probabilities(X, Y)
print("Transition probabilities shape:", prob.shape) # should be (M, M, N)
bathch_indices_all = torch.randint(0, N, (10, 4)).long().to("cuda")
idx = torch.randint(0, M, (10, 4)).long().to("cuda")
next_clusters = env.step(bathch_indices_all, idx, 10, 4, 3)
print("\033[93mNext clusters shape:", next_clusters.shape, "\033[0m") # should be (10, 4, 3)