How to do only one forward propagation per epoch and multiple backward propagations on graph

username: user2
cooked: <p>How to do only one forward propagation per epoch and multiple back propagations？<br>
I want to  get the vector representation through several layers of graph neural networks first，<br>
then do the backward operation to optimize the loss according to the batch number.</p>
<p>what i  want to do is in code B.<br>
the main difference is the position of ’  user_embedding, item_embedding = model(G)’<br>
but an error will occur in code B<br>
: TypeError: cannot unpack non-iterable NoneType object</p>
<p>is there any way to do this?<br>
the same error:<a href="https://blog.csdn.net/weixin_37913042/article/details/102958608" rel="nofollow noopener">link</a></p>
<p>thank you!</p>
<p><span class="hashtag">#code</span> A</p>
<pre><code>model = Model(G, 8, 8, 8)  
opt = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4)

for epoch in range(2):
    for batch_data  in dataset:
        optimizer.zero_grad()
        user_embedding, item_embedding = model(G)
        loss=model.getloss(batch_data,user_embedding, item_embedding)
        loss.backward()
        optimizer.step()-----------
</code></pre>
<p><span class="hashtag">#code</span> B</p>
<pre><code>model = Model(G, 8, 8, 8)  
opt = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4)
user_embedding, item_embedding = model(G)

for epoch in range(2):
    for batch_data  in dataset:
        optimizer.zero_grad()
        loss=model.getloss(batch_data,user_embedding, item_embedding)
        loss.backward()
        optimizer.step()</code></pre>

username: JuanFMontesinos
cooked: <p>You just need to call <code>loss.backward(retain_graph=True)</code><br>
This will allow you to backprop several times.</p>

username: user2
cooked: <p>thank you for your reply<br>
the error still occurs.</p>
<p>Traceback (most recent call last):<br>
File “/Users/cc/Downloads/dgl-examples-pytorch/code1211/hgnn/hgnn.py”, line 778, in<br>
main()<br>
File “/Users/cc/Downloads/dgl-examples-pytorch/code1211/hgnn/hgnn.py”, line 699, in main<br>
loss.backward(retain_graph=True)<br>
File “/Users/cc/PycharmProjects/testfolder1127/venvpy3/lib/python3.7/site-packages/torch/tensor.py”, line 166, in backward<br>
torch.autograd.backward(self, gradient, retain_graph, create_graph)<br>
File “/Users/cc/PycharmProjects/testfolder1127/venvpy3/lib/python3.7/site-packages/torch/autograd/ <strong>init</strong> .py”, line 99, in backward<br>
allow_unreachable=True) # allow_unreachable flag<br>
File “/Users/cc/PycharmProjects/testfolder1127/venvpy3/lib/python3.7/site-packages/torch/autograd/function.py”, line 77, in apply<br>
return self._forward_cls.backward(self, *args)<br>
File “/Users/cc/PycharmProjects/testfolder1127/venvpy3/lib/python3.7/site-packages/dgl/backend/pytorch/tensor.py”, line 336, in backward<br>
= ctx.backward_cache<br>
TypeError: cannot unpack non-iterable NoneType object</p>

username: user2
cooked: <p>here is the code:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/9/595572d943e428d1027fdb782fda12ae77320330.png" data-download-href="https://discuss.pytorch.org/uploads/default/595572d943e428d1027fdb782fda12ae77320330" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/9/595572d943e428d1027fdb782fda12ae77320330_2_690x418.png" alt="image" data-base62-sha1="cKhzchj4BxVqA0Ax8QvLLInMGsg" width="690" height="418" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/9/595572d943e428d1027fdb782fda12ae77320330_2_690x418.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/9/595572d943e428d1027fdb782fda12ae77320330_2_1035x627.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/5/9/595572d943e428d1027fdb782fda12ae77320330_2_1380x836.png 2x" data-dominant-color="F6F3F2"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1670×1014 118 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: JuanFMontesinos
cooked: <p>Hi, I think you have any other issue.</p>
<pre><code class="lang-auto">import torch

model = torch.nn.Conv2d(1, 2, 3)
opt = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4)
inp = torch.rand(1, 1, 10, 10)
out = model(inp)
scalar = out.mean()

for _ in range(10):
    print(_)
    opt.zero_grad()
    # with torch.no_grad():
    gt = torch.rand_like(out)
    loss = (scalar * gt).mean() #just  a dummy loss
    loss.backward(retain_graph=True)
    print(list(model.parameters())[0].grad)
    opt.step()
</code></pre>
<p>This minimal example emulates what you want to do in code B. It’s runable and</p>
<pre><code class="lang-auto">0
tensor([[[[0.1069, 0.1032, 0.1004],
          [0.1086, 0.1056, 0.1034],
          [0.1055, 0.1049, 0.1043]]],


        [[[0.1069, 0.1032, 0.1004],
          [0.1086, 0.1056, 0.1034],
          [0.1055, 0.1049, 0.1043]]]])
1
tensor([[[[0.1168, 0.1128, 0.1097],
          [0.1187, 0.1154, 0.1129],
          [0.1152, 0.1146, 0.1139]]],


        [[[0.1168, 0.1128, 0.1097],
          [0.1187, 0.1154, 0.1129],
          [0.1152, 0.1146, 0.1139]]]])
2
tensor([[[[0.1250, 0.1206, 0.1174],
          [0.1270, 0.1234, 0.1208],
          [0.1233, 0.1226, 0.1219]]],


        [[[0.1250, 0.1206, 0.1174],
          [0.1270, 0.1234, 0.1208],
          [0.1233, 0.1226, 0.1219]]]])
3
tensor([[[[0.1225, 0.1182, 0.1150],
          [0.1244, 0.1210, 0.1184],
          [0.1208, 0.1201, 0.1194]]],


        [[[0.1225, 0.1182, 0.1150],
          [0.1244, 0.1210, 0.1184],
          [0.1208, 0.1201, 0.1194]]]])
4
tensor([[[[0.1216, 0.1173, 0.1142],
          [0.1235, 0.1201, 0.1175],
          [0.1199, 0.1192, 0.1185]]],


        [[[0.1216, 0.1173, 0.1142],
          [0.1235, 0.1201, 0.1175],
          [0.1199, 0.1192, 0.1185]]]])
5
tensor([[[[0.1166, 0.1125, 0.1095],
          [0.1184, 0.1151, 0.1127],
          [0.1150, 0.1144, 0.1137]]],


        [[[0.1166, 0.1125, 0.1095],
          [0.1184, 0.1151, 0.1127],
          [0.1150, 0.1144, 0.1137]]]])
6
tensor([[[[0.1281, 0.1236, 0.1203],
          [0.1301, 0.1265, 0.1238],
          [0.1264, 0.1256, 0.1249]]],


        [[[0.1281, 0.1236, 0.1203],
          [0.1301, 0.1265, 0.1238],
          [0.1264, 0.1256, 0.1249]]]])
7
tensor([[[[0.1114, 0.1075, 0.1046],
          [0.1131, 0.1100, 0.1077],
          [0.1099, 0.1092, 0.1086]]],


        [[[0.1114, 0.1075, 0.1046],
          [0.1131, 0.1100, 0.1077],
          [0.1099, 0.1092, 0.1086]]]])
8
tensor([[[[0.1179, 0.1138, 0.1107],
          [0.1197, 0.1164, 0.1139],
          [0.1163, 0.1156, 0.1149]]],


        [[[0.1179, 0.1138, 0.1107],
          [0.1197, 0.1164, 0.1139],
          [0.1163, 0.1156, 0.1149]]]])
9
tensor([[[[0.1183, 0.1141, 0.1111],
          [0.1202, 0.1168, 0.1143],
          [0.1167, 0.1160, 0.1153]]],


        [[[0.1183, 0.1141, 0.1111],
          [0.1202, 0.1168, 0.1143],
          [0.1167, 0.1160, 0.1153]]]])
</code></pre>
<p>gradients properly flow<br>
Your error seems to be related to None. Is the graph ok? Have you tried at least one run checking gradients are not None?</p>

username: user2
cooked: <p>thank you. i will check it.<br>
the first batch can get the right loss.<br>
but the error occurs before the second loss.backward(retain_graph=True).</p>

username: JuanFMontesinos
cooked: <p>Oh but you have to set retain_graph for all the runs <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"> even the 1st.</p>

username: user2
cooked: <p>So that’s what I’m confused about.<br>
thank you again.</p>

username: user2
cooked: <aside class="quote no-group" data-username="JuanFMontesinos" data-post="5" data-topic="65396">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/juanfmontesinos/48/76115_2.png" class="avatar"> JuanFMontesinos:</div>
<blockquote>
<p>print(list(model.parameters())[0].grad)</p>
</blockquote>
</aside>
<p>i use the code print(list(model.parameters())[0].grad)<br>
but get None even in the first batch.</p>

username: JuanFMontesinos
cooked: <p>Hi,<br>
it seems your network doesn’t backprop properly.</p>
<p>you can check where by doing</p>
<pre><code class="lang-auto">for name,param in model.named_parameters():
if param is None:
   print(name)
</code></pre>
<p>So if it happens just after the loss (which is probably the case as I assume it’s been coded by yourself, you may find the guilty.</p>

username: user2
cooked: <p>thank you for your kind help,i will check it.<img src="https://discuss.pytorch.org/images/emoji/apple/+1.png?v=9" title=":+1:" class="emoji" alt=":+1:"></p>