Skip to content

paramer group format error #2355

@wyli

Description

@wyli

Describe the bug

generate_param_groups doesn't return the correct format for match_type 'filter'

import torch
from monai.networks.nets import Unet
from monai.optimizers import generate_param_groups

net = Unet(dimensions=3, in_channels=1, out_channels=3, channels=[2, 2, 2], strides=[1, 1, 1])
params = generate_param_groups(
    network=net,
    layer_matches=[lambda x: "weight" in x[0]],
    match_types=["filter"],
    lr_values=[1e-3],
    include_others=False,
)

torch.optim.Adam(params, lr=1e-4)
print(params)

error:

Traceback (most recent call last):
  File "tests.py", line 14, in <module>
    torch.optim.Adam(params, lr=1e-4)
  File "/lib/python3.7/site-packages/torch/optim/adam.py", line 48, in __init__
    super(Adam, self).__init__(params, defaults)
  File "/lib/python3.7/site-packages/torch/optim/optimizer.py", line 55, in __init__
    self.add_param_group(param_group)
  File "/lib/python3.7/site-packages/torch/optim/optimizer.py", line 256, in add_param_group
    "but one of the params is " + torch.typename(param))
TypeError: optimizer can only optimize Tensors, but one of the params is tuple

Environment

================================
Printing MONAI config...
================================
MONAI version: 0.5.3+105.ge6aa268e
Numpy version: 1.20.3
Pytorch version: 1.8.1
MONAI flags: HAS_EXT = False, USE_COMPILED = False
MONAI rev id: e6aa268e093434688907324cfe5d885c4d6ad9b4

Optional dependencies:
Pytorch Ignite version: 0.4.4
Nibabel version: 3.2.1
scikit-image version: 0.18.1
Pillow version: 8.2.0
Tensorboard version: 2.5.0
gdown version: 3.13.0
TorchVision version: 0.9.1
ITK version: 5.1.2
tqdm version: 4.61.0
lmdb version: 1.2.1
psutil version: 5.8.0

For details about installing the optional dependencies, please visit:
    https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies


Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions