Skip to content

Commit b691fcc

Browse files
minor updates and ensuring bias free models, working with pretty mnist and gene_exp scripts
1 parent 8860e87 commit b691fcc

4 files changed

Lines changed: 49 additions & 20 deletions

File tree

scripts/visualize_mnist_embeddings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
plt.savefig("umap_original.png")
3131
plt.close()
3232

33-
for epoch in [1, 5, 10, 50, 100, 200, 500]:
33+
for epoch in [4]:#1, 5, 10, 50, 100, 200, 500]:
3434
reducer = ParametricUMAP(
3535
epochs=epoch,
3636
lr=1e-3,
@@ -52,5 +52,5 @@
5252
output = reducer.transform(X)
5353

5454
plt.scatter(output[:, 0], output[:, 1], c=colors, s=5)
55-
plt.savefig(f"umap_glassbox_{epoch}.png")
55+
plt.savefig(f"feb_26_pr_umap_glassbox_{epoch}.png")
5656
plt.close()

src/glass_box_umap/parametric_umap/core.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,37 @@ def fit(self, X: NDArray[np.floating] | Tensor) -> Self:
168168

169169
if isinstance(X, torch.Tensor):
170170
X = X.detach().cpu().squeeze()
171-
172-
graph = get_umap_graph(
173-
X,
174-
n_neighbors=self.n_neighbors,
175-
metric=self.metric,
176-
random_state=self.random_state,
177-
)
171+
172+
if len(X.shape)>2:
173+
conv_flag=True
174+
# X = X.reshape([-1,784])
175+
graph = get_umap_graph(
176+
X.reshape([-1,784]),
177+
n_neighbors=self.n_neighbors,
178+
metric=self.metric,
179+
random_state=self.random_state,
180+
)
181+
else:
182+
conv_flag=False
183+
graph = get_umap_graph(
184+
X,
185+
n_neighbors=self.n_neighbors,
186+
metric=self.metric,
187+
random_state=self.random_state,
188+
)
189+
190+
# graph = get_umap_graph(
191+
# X,
192+
# n_neighbors=self.n_neighbors,
193+
# metric=self.metric,
194+
# random_state=self.random_state,
195+
# )
178196

179197
if isinstance(X, torch.Tensor):
180198
X = X.detach().cpu().numpy()
199+
if conv_flag:
200+
X = torch.tensor(X.squeeze()).unsqueeze(1).detach().cpu().numpy() #reshape([-1,28,28]).unsqueeze(1)
201+
print("Shape: ", X.shape)
181202
datamodule = UMAPDataModule(
182203
# UMAPDataset(X.detach().cpu().numpy(), graph),
183204
UMAPDataset(X, graph),
@@ -226,7 +247,7 @@ def transform(
226247

227248
return torch.cat(results).numpy()
228249

229-
def fit_transform(self, X: NDArray[np.floating] | Tensor) -> NDArray[np.floating]:
250+
def fit_transform(self, X: Tensor) -> NDArray[np.floating]:
230251
self.fit(X)
231252
return self.transform(X)
232253

src/glass_box_umap/parametric_umap/graph.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ def get_graph_elements(graph: csr_matrix, n_epochs: int) -> GraphElements:
9797
graph_coo.data[graph_coo.data < (graph_coo.data.max() / float(n_epochs))] = 0.0
9898
graph_coo.eliminate_zeros()
9999

100+
# print("Graph len 0: ", len(graph_coo.data))
101+
# graph_cutoff_sorted = np.percentile(graph_coo.data, 90)
102+
# graph_coo.data[graph_coo.data < graph_cutoff_sorted]=0
103+
# graph_coo.eliminate_zeros()
104+
# print("Graph len 1: ", len(graph_coo.data))
105+
100106
epochs_per_sample = (n_epochs * graph_coo.data).astype(np.float32)
101107
head = graph_coo.row
102108
tail = graph_coo.col

src/glass_box_umap/parametric_umap/models.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ def __init__(
4444
hidden_dims = [512, 512]
4545

4646
self.conv_layers = nn.Sequential(
47-
nn.Conv2d(in_channels=in_channels, out_channels=64, kernel_size=3, stride=2, padding=1),
48-
nn.ReLU(),
49-
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=2, padding=1),
50-
nn.ReLU(),
47+
nn.Conv2d(in_channels=in_channels, out_channels=64, kernel_size=3, stride=2, padding=1,bias=False),
48+
nn.PReLU(),
49+
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=2, padding=1,bias=False),
50+
nn.PReLU(),
5151
nn.Flatten(),
5252
)
5353

@@ -56,9 +56,9 @@ def __init__(
5656
mlp_layers: list[nn.Module] = []
5757
prev_dim = flattened_size
5858
for dim in hidden_dims:
59-
mlp_layers.extend([nn.Linear(prev_dim, dim), nn.ReLU()])
59+
mlp_layers.extend([nn.Linear(prev_dim, dim, bias=False), nn.PReLU()])
6060
prev_dim = dim
61-
mlp_layers.append(nn.Linear(prev_dim, n_components))
61+
mlp_layers.append(nn.Linear(prev_dim, n_components, bias=False))
6262

6363
self.mlp = nn.Sequential(*mlp_layers)
6464

@@ -160,17 +160,19 @@ def __init__(
160160
self.norm2 = LayerNormDetached(dim) if use_norm else nn.Identity()
161161

162162
if activation == "leaky_relu":
163-
self.act = nn.LeakyReLU(negative_slope=negative_slope)
164-
a = negative_slope
163+
# self.act = nn.LeakyReLU(negative_slope=negative_slope)
164+
# a = negative_slope
165+
166+
self.act = nn.PReLU()#negative_slope=negative_slope)
165167
nonlin = "leaky_relu"
166168
else:
167169
self.act = nn.ReLU()
168170
a = 0.0
169171
nonlin = "relu"
170172

171173
# He/Kaiming init for ReLU-family activations
172-
init.kaiming_uniform_(self.fc1.weight, a=a, nonlinearity=nonlin)
173-
init.kaiming_uniform_(self.fc2.weight, a=a, nonlinearity=nonlin)
174+
init.kaiming_uniform_(self.fc1.weight, nonlinearity=nonlin)
175+
init.kaiming_uniform_(self.fc2.weight, nonlinearity=nonlin)
174176

175177
def forward(self, x: Tensor) -> Tensor:
176178
h = self.fc1(x)

0 commit comments

Comments
 (0)