Recursively instantiate SimpleDenseNet using Hydra (#209)
* Add recursive instantiation of `net` object which is passed to `MNISTLitModule` on init
This commit is contained in:
1 parent
5b7fde8e3a
commit
d0803d4e63
6 files changed
+48
-38
No files matched your search
@@ -2,20 +2,27 @@ from torch import nn
|
||||
|
||||
|
||||
class SimpleDenseNet(nn.Module):
|
||||
def __init__(self, hparams: dict):
|
||||
def __init__(
|
||||
self,
|
||||
input_size: int = 784,
|
||||
lin1_size: int = 256,
|
||||
lin2_size: int = 256,
|
||||
lin3_size: int = 256,
|
||||
output_size: int = 10,
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
self.model = nn.Sequential(
|
||||
nn.Linear(hparams["input_size"], hparams["lin1_size"]),
|
||||
nn.BatchNorm1d(hparams["lin1_size"]),
|
||||
nn.Linear(input_size, lin1_size),
|
||||
nn.BatchNorm1d(lin1_size),
|
||||
nn.ReLU(),
|
||||
nn.Linear(hparams["lin1_size"], hparams["lin2_size"]),
|
||||
nn.BatchNorm1d(hparams["lin2_size"]),
|
||||
nn.Linear(lin1_size, lin2_size),
|
||||
nn.BatchNorm1d(lin2_size),
|
||||
nn.ReLU(),
|
||||
nn.Linear(hparams["lin2_size"], hparams["lin3_size"]),
|
||||
nn.BatchNorm1d(hparams["lin3_size"]),
|
||||
nn.Linear(lin2_size, lin3_size),
|
||||
nn.BatchNorm1d(lin3_size),
|
||||
nn.ReLU(),
|
||||
nn.Linear(hparams["lin3_size"], hparams["output_size"]),
|
||||
nn.Linear(lin3_size, output_size),
|
||||
)
|
||||
|
||||
def forward(self, x):
|
||||
|
||||
@@ -25,11 +25,7 @@ class MNISTLitModule(LightningModule):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
input_size: int = 784,
|
||||
lin1_size: int = 256,
|
||||
lin2_size: int = 256,
|
||||
lin3_size: int = 256,
|
||||
output_size: int = 10,
|
||||
net: torch.nn.Module,
|
||||
lr: float = 0.001,
|
||||
weight_decay: float = 0.0005,
|
||||
):
|
||||
@@ -39,7 +35,7 @@ class MNISTLitModule(LightningModule):
|
||||
# it also ensures init params will be stored in ckpt
|
||||
self.save_hyperparameters(logger=False)
|
||||
|
||||
self.model = SimpleDenseNet(hparams=self.hparams)
|
||||
self.net = net
|
||||
|
||||
# loss function
|
||||
self.criterion = torch.nn.CrossEntropyLoss()
|
||||
@@ -54,7 +50,7 @@ class MNISTLitModule(LightningModule):
|
||||
self.val_acc_best = MaxMetric()
|
||||
|
||||
def forward(self, x: torch.Tensor):
|
||||
return self.model(x)
|
||||
return self.net(x)
|
||||
|
||||
def step(self, batch: Any):
|
||||
x, y = batch
|
||||
|
||||
Reference in new issue
Block a user