Skip to content

Commit

Permalink
Merge pull request #2 from aria1th/patch-6
Browse files Browse the repository at this point in the history
generalized some functions and option for ignoring first layer
  • Loading branch information
discus0434 committed Oct 22, 2022
2 parents f8733ad + f89829e commit 6a02841
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions modules/hypernetworks/hypernetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,27 @@

class HypernetworkModule(torch.nn.Module):
multiplier = 1.0

activation_dict = {"relu": torch.nn.ReLU, "leakyrelu": torch.nn.LeakyReLU, "elu": torch.nn.ELU,
"swish": torch.nn.Hardswish}

def __init__(self, dim, state_dict=None, layer_structure=None, add_layer_norm=False, activation_func=None):
super().__init__()

assert layer_structure is not None, "layer_structure must not be None"
assert layer_structure[0] == 1, "Multiplier Sequence should start with size 1!"
assert layer_structure[-1] == 1, "Multiplier Sequence should end with size 1!"

linears = []
for i in range(len(layer_structure) - 1):
linears.append(torch.nn.Linear(int(dim * layer_structure[i]), int(dim * layer_structure[i+1])))
if activation_func == "relu":
linears.append(torch.nn.ReLU())
if activation_func == "leakyrelu":
linears.append(torch.nn.LeakyReLU())
# if skip_first_layer because first parameters potentially contain negative values
# if i < 1: continue
if activation_func in HypernetworkModule.activation_dict:
linears.append(HypernetworkModule.activation_dict[activation_func]())
else:
print("Invalid key {} encountered as activation function!".format(activation_func))
# if use_dropout:
# linears.append(torch.nn.Dropout(p=0.3))
if add_layer_norm:
linears.append(torch.nn.LayerNorm(int(dim * layer_structure[i+1])))

Expand All @@ -46,7 +52,7 @@ def __init__(self, dim, state_dict=None, layer_structure=None, add_layer_norm=Fa
self.load_state_dict(state_dict)
else:
for layer in self.linear:
if not "ReLU" in layer.__str__():
if isinstance(layer, torch.nn.Linear):
layer.weight.data.normal_(mean=0.0, std=0.01)
layer.bias.data.zero_()

Expand Down Expand Up @@ -74,7 +80,7 @@ def forward(self, x):
def trainables(self):
layer_structure = []
for layer in self.linear:
if not "ReLU" in layer.__str__():
if isinstance(layer, torch.nn.Linear):
layer_structure += [layer.weight, layer.bias]
return layer_structure

Expand Down Expand Up @@ -298,6 +304,7 @@ def train_hypernetwork(hypernetwork_name, learn_rate, batch_size, data_root, log
return hypernetwork, filename

scheduler = LearnRateScheduler(learn_rate, steps, ititial_step)
# if optimizer == "AdamW": or else Adam / AdamW / SGD, etc...
optimizer = torch.optim.AdamW(weights, lr=scheduler.learn_rate)

pbar = tqdm.tqdm(enumerate(ds), total=steps - ititial_step)
Expand Down

0 comments on commit 6a02841

Please sign in to comment.