# Backprop One Layer at a Time

Hi all,

Im trying to make a somewhat custom optimizing algorithm however Im having a real tough time figuring out how to optimize one layer at a time without running the whole loss.backward() for every layer. I saw someone had basically the same question here: Computing Backward one layer at a time but the answer wasn’t descriptive enough for me to really understand. I also found this gem: Bit of fun with gradients which gets me close however I don’t know how to grab the gradient of the detached tensor of the previous layer so that I can give that to the previous layers backward call.

Here are clips of the relevant (not)working code:

``````class testNet(nn.Module):
def __init__(self, n_observations, n_actions):
super(testNet, self).__init__()
self.layer1 = nn.Linear(n_observations, 128)
self.layer2 = nn.Linear(128, 128)
self.layer3 = nn.Linear(128, n_actions)
self.activate = nn.LeakyReLU(.01)

def forward(self, x):
x1 = self.activate(self.layer1(x))

params = list(test_net.parameters())
params.reverse()

slope = nn.MSELoss()
loss = slope(state_action_values, expected_state_action_values.unsqueeze(1))

loss.backward(retain_graph=True)

# continue backward grad layer by layer
for i in range(1,len(params),2):
# optimize this layers parameters
{"ft_lang_label":"__label__en","ft_lang_prob":0.7435214,"math_prob":0.85467094,"size":1660,"snap":"2023-14-2023-23","text_gpt3_token_len":372,"char_repetition_ratio":0.112922706,"word_repetition_ratio":0.009478673,"special_character_ratio":0.24216868,"punctuation_ratio":0.16719243,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9873783,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-06-02T21:23:09Z\",\"WARC-Record-ID\":\"<urn:uuid:01502f7c-bc22-4114-955a-e98df368ab6b>\",\"Content-Length\":\"14837\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:65f4ba2d-47db-48c2-b16e-a35655dfc3f6>\",\"WARC-Concurrent-To\":\"<urn:uuid:ee0f311b-4afa-49c0-97f0-255a0e2ef63f>\",\"WARC-IP-Address\":\"159.203.145.104\",\"WARC-Target-URI\":\"https://discuss.pytorch.org/t/backprop-one-layer-at-a-time/171155\",\"WARC-Payload-Digest\":\"sha1:XX6LCLRZ5XAMHDNWMDM2SAFZATABCACT\",\"WARC-Block-Digest\":\"sha1:DCKS6DHM6ZBSHPAMXIJIDQNRQINDHQH5\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224648858.14_warc_CC-MAIN-20230602204755-20230602234755-00009.warc.gz\"}"}