@ayalaa2 With given formulation I agree with you, maybe below formulation will closely represent my intention. Sorry for the confusion.

L1 = f1(out1,out2)
L2 = g1(out1,out2)

Will this take care of training both NN1 and NN2 together

L1.backward()
Opt1.step()
Opt1.zero_grad(),Opt2.zero_grad()
L2.backward()
Opt2.step()
Opt1.zero_grad(),Opt2.zero_grad()