How to get rid of Variable API in PyTorch.autograd?
I am forwarding, and backpropping tensor data X through two simple nn.Module PyTorch models instances, model1 and model2. 

I can't get this process to work without usage of the depreciated Variable API.

So this works just fine:

    y1 = model1(X)
    v = Variable(y1.data, requires_grad=training)         # Its all about this line!
    y2 = model2(v)
    criterion = nn.NLLLoss()
    loss = criterion(y2, y)
    loss.backward()
    y1.backward(v.grad)
    self.step()


But this will throw an error:

    y1 = model1(X)
    y2 = model2(y1)
    criterion = nn.NLLLoss()
    loss = criterion(y2, y)
    loss.backward()
    y1.backward(y1.grad) # it breaks here
    self.step()
&gt;&gt;&gt; RuntimeError: grad can be implicitly created only for scalar outputs



I just can't seem to find a relevant difference between v in the first implementation, and y1 in the second. In both cases requires_grad is set to True. The only thing I could find was that y1.grad_fn=&lt;ThnnConv2DBackward&gt; and v.grad_fn=&lt;ThnnConv2DBackward&gt;

What am I missing here? What (tensor attributes?) do I not know about, and if Variable is depreciated, what other implementation would work?

After some investigation I came to the following two solutions.
The solution provided elsewhere in this thread retained the computation graph manually, without an option the free them, thus running fine initially, but causing OOM errors later on.

The first solution is to tie the models together using the built in torch.nn.Sequential as such:

model = torch.nn.Sequential(Model1(), Model2())


it's as easy as that. It looks clean and behaves exactly like an ordinary model would.

The alternative is to simply tie them together manually:

        model1 = Model1()
        model2 = Model2()
        y1 = model1(X)
        y2 = model2(y1)
        loss = criterion(y2, y)
        loss.backward()


My fear that this would only backpropagate model2 turned out to be unsubstantiated, since model1 is also stored in the computation graph that is back propagated over.
This implementation enabled inceased transparancy of the interface between the two models, compared to the previous implementation.
