Error when using EWC

Chihoub_Chiheb_Eddin: <p>I am trying to train my model using Elastic Weight Consolidation technique, However using the code below, Miss match error<br>
def train_one_epoch(net, train_dataloader):<br>
crit = nn.MSELoss()<br>
device = “cuda” if torch.cuda.is_available() else “cpu”<br>
net.to(device)<br>
ewc = ElasticWeightConsolidation(net, crit=crit, lr=1e-4)<br>
net.train()<br>
for input in enumerate(train_dataloader):<br>
reconstructed = net(input)<br>
ewc.forward_backward_update(reconstructed,input)</p>
<p>train_transforms = transforms.Compose(<br>
[transforms.RandomCrop(args.patch_size), transforms.ToTensor()]<br>
)</p>
<pre><code>test_transforms = transforms.Compose(
    [transforms.CenterCrop(args.patch_size), transforms.ToTensor()]
)

train_dataset = ImageFolder(args.dataset, split="train", transform=train_transforms) #args.dataset
test_dataset = ImageFolder(args.dataset, split="test", transform=test_transforms)

device = "cuda" if torch.cuda.is_available() else "cpu"

train_dataloader = DataLoader(
    train_dataset,
    batch_size=args.batch_size,
    num_workers=args.num_workers,
    shuffle=True,
    pin_memory=(device == "cuda"),
)

test_dataloader = DataLoader(
    test_dataset,
    batch_size=args.test_batch_size,
    num_workers=args.num_workers,
    shuffle=False,
    pin_memory=(device == "cuda"),
)

net = bmshj2018_hyperprior(quality=2, pretrained= True)
#net = image_models["bmshj2018-hyperprior"](quality=3)
net = net.to(device)

if args.cuda and torch.cuda.device_count() &gt; 1:
    net = CustomDataParallel(net)
optimizer, aux_optimizer = configure_optimizers(net, args)
lr_scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, "min")
criterion = RateDistortionLoss(lmbda=0.0035)

last_epoch = 0
if args.checkpoint:  # load from previous checkpoint
    print("Loading", args.checkpoint)
    checkpoint = torch.load(args.checkpoint, map_location=device)
    last_epoch = checkpoint["epoch"] + 1
    net.load_state_dict(checkpoint["state_dict"])
    optimizer.load_state_dict(checkpoint["optimizer"])
    aux_optimizer.load_state_dict(checkpoint["aux_optimizer"])
    lr_scheduler.load_state_dict(checkpoint["lr_scheduler"])

best_loss = float("inf")
for epoch in range(last_epoch, args.epochs):
    print(f"Learning rate: {optimizer.param_groups[0]['lr']}")
    train_one_epoch(
        net,
        train_dataloader
    )
    loss = test_epoch(epoch, test_dataloader, net, criterion)
    lr_scheduler.step(loss)

    is_best = loss &lt; best_loss
    best_loss = min(loss, best_loss)

    if args.save:
        save_checkpoint(
            {
                "epoch": epoch,
                "state_dict": net.state_dict(),
                "loss": loss,
                "optimizer": optimizer.state_dict(),
                "aux_optimizer": aux_optimizer.state_dict(),
                "lr_scheduler": lr_scheduler.state_dict(),
            },
            is_best,
        )
</code></pre>
<p>I get this error:<br>
Cell In[129], <a>line 8</a> <a>6</a> net.train() <a>7</a> for input in enumerate(train_dataloader): ----&gt; <a>8</a> reconstructed = net(input) <a>9</a> ewc.forward_backward_update(reconstructed,input) File ~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1501, in Module._call_impl(self, *args, **kwargs) <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1496" rel="noopener nofollow ugc">1496</a> # If we don’t have any hooks, we want to skip the rest of the logic in <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1497" rel="noopener nofollow ugc">1497</a> # this function, and just call forward. <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1498" rel="noopener nofollow ugc">1498</a> if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1499" rel="noopener nofollow ugc">1499</a> or _global_backward_pre_hooks or _global_backward_hooks <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1500" rel="noopener nofollow ugc">1500</a> or _global_forward_hooks or _global_forward_pre_hooks): → <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1501" rel="noopener nofollow ugc">1501</a> return forward_call(*args, **kwargs) <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1502" rel="noopener nofollow ugc">1502</a> # Do not call functions when jit is used <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1503" rel="noopener nofollow ugc">1503</a> full_backward_hooks, non_full_backward_hooks = <span class="chcklst-box fa fa-square-o fa-fw"></span>, <span class="chcklst-box fa fa-square-o fa-fw"></span> File ~/.local/lib/python3.10/site-packages/compressai/models/google.py:293, in ScaleHyperprior.forward(self, x) <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/compressai/models/google.py:292" rel="noopener nofollow ugc">292</a> def forward(self, x): → <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/compressai/models/google.py:293" rel="noopener nofollow ugc">293</a> y = self.g_a(x) <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/compressai/models/google.py:294" rel="noopener nofollow ugc">294</a> z = self.h_a(torch.abs(y)) <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/compressai/models/google.py:295" rel="noopener nofollow ugc">295</a> z_hat, z_likelihoods = self.entropy_bottleneck(z) File ~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1501, in Module._call_impl(self, *args, **kwargs) <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1496" rel="noopener nofollow ugc">1496</a> # If we don’t have any hooks, we want to skip the rest of the logic in <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1497" rel="noopener nofollow ugc">1497</a> # this function, and just call forward. <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1498" rel="noopener nofollow ugc">1498</a> if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1499" rel="noopener nofollow ugc">1499</a> or _global_backward_pre_hooks or _global_backward_hooks <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1500" rel="noopener nofollow ugc">1500</a> or _global_forward_hooks or _global_forward_pre_hooks): → <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1501" rel="noopener nofollow ugc">1501</a> return forward_call(*args, **kwargs) <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1502" rel="noopener nofollow ugc">1502</a> # Do not call functions when jit is used <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1503" rel="noopener nofollow ugc">1503</a> full_backward_hooks, non_full_backward_hooks = <span class="chcklst-box fa fa-square-o fa-fw"></span>, <span class="chcklst-box fa fa-square-o fa-fw"></span> File ~/.local/lib/python3.10/site-packages/torch/nn/modules/container.py:217, in Sequential.forward(self, input) <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/container.py:215" rel="noopener nofollow ugc">215</a> def forward(self, input): <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/container.py:216" rel="noopener nofollow ugc">216</a> for module in self: → <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/container.py:217" rel="noopener nofollow ugc">217</a> input = module(input) <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/container.py:218" rel="noopener nofollow ugc">218</a> return input File ~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1501, in Module._call_impl(self, *args, **kwargs) <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1496" rel="noopener nofollow ugc">1496</a> # If we don’t have any hooks, we want to skip the rest of the logic in <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1497" rel="noopener nofollow ugc">1497</a> # this function, and just call forward. <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1498" rel="noopener nofollow ugc">1498</a> if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1499" rel="noopener nofollow ugc">1499</a> or _global_backward_pre_hooks or _global_backward_hooks <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1500" rel="noopener nofollow ugc">1500</a> or _global_forward_hooks or _global_forward_pre_hooks): → <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1501" rel="noopener nofollow ugc">1501</a> return forward_call(*args, **kwargs) <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1502" rel="noopener nofollow ugc">1502</a> # Do not call functions when jit is used <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1503" rel="noopener nofollow ugc">1503</a> full_backward_hooks, non_full_backward_hooks = <span class="chcklst-box fa fa-square-o fa-fw"></span>, <span class="chcklst-box fa fa-square-o fa-fw"></span> File ~/.local/lib/python3.10/site-packages/torch/nn/modules/conv.py:463, in Conv2d.forward(self, input) <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/conv.py:462" rel="noopener nofollow ugc">462</a> def forward(self, input: Tensor) → Tensor: → <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/conv.py:463" rel="noopener nofollow ugc">463</a> return self._conv_forward(input, self.weight, self.bias) File ~/.local/lib/python3.10/site-packages/torch/nn/modules/conv.py:459, in Conv2d._conv_forward(self, input, weight, bias) <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/conv.py:455" rel="noopener nofollow ugc">455</a> if self.padding_mode != ‘zeros’: <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/conv.py:456" rel="noopener nofollow ugc">456</a> return F.conv2d(F.pad(input, self._reversed_padding_repeated_twice, mode=self.padding_mode), <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/conv.py:457" rel="noopener nofollow ugc">457</a> weight, bias, self.stride, <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/conv.py:458" rel="noopener nofollow ugc">458</a> _pair(0), self.dilation, self.groups) → <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/conv.py:459" rel="noopener nofollow ugc">459</a> return F.conv2d(input, weight, bias, self.stride, <a href="https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/chiheb2/essai_hyperprior_2/~/.local/lib/python3.10/site-packages/torch/nn/modules/conv.py:460" rel="noopener nofollow ugc">460</a> self.padding, self.dilation, self.groups) TypeError: conv2d() received an invalid combination of arguments - got (tuple, Parameter, Parameter, tuple, tuple, tuple, int), but expected one of: * (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, tuple of ints padding, tuple of ints dilation, int groups) didn’t match because some of the arguments have invalid types: (!tuple of (int, Tensor)!, !Parameter!, !Parameter!, !tuple of (int, int)!, !tuple of (int, int)!, !tuple of (int, int)!, int) * (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, str padding, tuple of ints dilation, int groups) didn’t match because some of the arguments have invalid types: (!tuple of (int, Tensor)!, !Parameter!, !Parameter!, !tuple of (int, int)!, !tuple of (int, int)!, !tuple of (int, int)!, int)</p>

ptrblck: <p>Your code and error messages are not properly formatted, but it seems a <code>conv2d</code> call fails with an invalid input:</p>
<pre data-code-wrap="python"><code class="lang-python">
[quote="Chihoub_Chiheb_Eddin, post:1, topic:214928"]
TypeError: conv2d() received an invalid combination of arguments - got (tuple, Parameter, Parameter, tuple, tuple, tuple, int),
[/quote]
and it seems you are trying to pass a `tuple` to this layer while a tensor is expected.</code></pre>

Chihoub_Chiheb_Eddin: <p>Alright, I’ll reinspect the code, sorry for the mess</p>