I would like to add an else statement to this comprehension but I am not having much luck placing it around different areas in the code below. I like the format of the below as I find it fairly readable given the length and activity.
This is not a duplicate of: if/else in a list comprehension as the guidance is seemingly not a solution for this form.
I am taking the guidance from this thread but still not finding that it works. Notice that I want an if
and an else
so this means that, following the thread:
['b' if isinstance(el, str) else el for el in X] # When using 'if' and 'else', put 'for' in the end
So I have an if
and and else
and so they come in order and for
is at the end.
import torch
import numpy as np
class mlp1(torch.nn.Module):
def __init__(self, num_features, num_hidden, num_classes):
super(mlp1, self).__init__()
self.num_classes = num_classes
self.input_layer = torch.nn.Linear(num_features, num_hidden)
self.out_layer = torch.nn.Linear(num_hidden, num_classes)
def forward(self, x):
x = self.input_layer(x)
x = torch.sigmoid(x)
logits = self.out_layer(x)
probas = torch.softmax(logits, dim=1)
return logits, probas
model = mlp1(num_features=28*28, num_hidden=100, num_classes=10)
freeze = ['input_layer']
# randomly select weights
locked_masks = {
name: torch.tensor(np.random.choice([False, True],
size=torch.numel(weight),
# freeze 90% of the weights
p=[0.0, 1.0]).reshape(weight.shape))
if any(weight in name for weight in freeze)
else
name: torch.tensor(np.random.choice([False, True],
size=torch.numel(weight),
# freeze 90% of the weights
p=[0.0, 1.0]).reshape(weight.shape))
for name, weight in model.named_parameters()
}