tristandeleu / pytorch-meta

A collection of extensions and data-loaders for few-shot learning & meta-learning in PyTorch
https://tristandeleu.github.io/pytorch-meta/
MIT License
1.97k stars 256 forks source link

Failed on adaptation when step is 2 #91

Closed xliucs closed 3 years ago

xliucs commented 4 years ago

I got this error this error while i set --num-steps greater than 1, which means i can only update my one time during training.

File "/gradient_based.py", line 51, in gradient_update_parameters create_graph=not first_order) File "/gscratch/cse/xliu0/anaconda3/envs/torch-gpu/lib/python3.6/site-packages/torch/autograd/__init__.py", line 158, in grad inputs, allow_unused) RuntimeError: One of the differentiated Tensors appears to not have been used in the graph. Set allow_unused=True if this is the desired behavior.

    grads = torch.autograd.grad(loss,
                                params.values(),
                                create_graph=not first_order)

I am wondering if anyone has experienced this before? Thanks in advance!

tristandeleu commented 4 years ago

Can you give some more details about the script you are running? The examples for MAML and MAML + Higher in this repo only use a single step of adaptation, with no option to set a larger number with --num-steps (these examples are meant to be very simple). If you are referring to the implementation pytorch-maml repository, which does have an option for multiple steps of adaptation, then I suggest you to open an issue in that repository. Just as a reference, I did try that other repository with --num-steps=5, and I was not able to reproduce this issue unfortunately.

If you were not trying to run one of those scripts but you are running you own code, then can you provide a minimal example where this fails? Here is a small example that worked for me:

import torch
import torch.nn as nn

from torchmeta.modules import MetaSequential, MetaLinear
from torchmeta.utils import gradient_update_parameters

model = MetaSequential(
    MetaLinear(2, 3),
    nn.ReLU(),
    MetaLinear(3, 5)
)

train_inputs = torch.randn(7, 2)

train_outputs_1 = model(train_inputs)
inner_loss_1 = train_outputs_1.sum()  # Dummy loss
params = gradient_update_parameters(model, inner_loss_1)

train_outputs_2 = model(train_inputs, params=params)
inner_loss_2 = train_outputs_2.sum()  # Dummy loss
params = gradient_update_parameters(model, inner_loss_2, params=params)

test_inputs = torch.randn(7, 2)

test_outputs = model(test_inputs, params=params)
outer_loss = test_outputs.sum()

outer_loss.backward()
xliucs commented 4 years ago

@tristandeleu Hi Tristan, Thanks very much for getting back to me. Yeah, I was using pytorch-maml repo, and i got this problem. I had a few custom layers, so i am not sure if that is the reason causing the issue. I have switched to your maml example, and it seemed working fine over there. Thanks again for your code!!!

tristandeleu commented 3 years ago

I am closing this issue for now, but feel free to open a new issue in the pytorch-maml repo if you have any problem!