MCPcopy Create free account
hub / github.com/InternScience/InternAgent / forward

Method forward

tasks/AutoPower/code/experiment.py:742–834  ·  view source on GitHub ↗
(self, batch, flag_return_losses=False, flag_use_ema_infer=False, num_loop_infer=0)

Source from the content-addressed store, hash-verified

740
741
742 def forward(self, batch, flag_return_losses=False, flag_use_ema_infer=False, num_loop_infer=0):
743 # get size
744 num_PQ = batch['PQ'].x.shape[0]
745 num_PV = batch['PV'].x.shape[0]
746 num_Slack = batch['Slack'].x.shape[0]
747 Vm, Va, P_net, Q_net, Gs, Bs = 0, 1, 2, 3, 4, 5
748
749 # use different loops during inference phase
750 if num_loop_infer < 1:
751 num_loops = self.num_loops
752 else:
753 num_loops = num_loop_infer
754
755 # whether use ema model for inference
756 if not self.flag_use_ema:
757 flag_use_ema_infer = False
758
759 # loss record
760 loss = 0.0
761 res_dict = {"loss_equ": 0.0, "loss_pq_vm": 0.0, "loss_pq_va": 0.0, "loss_pv_va": 0.0}
762 Ybus = create_Ybus(batch.detach())
763 delta_p, delta_q = deltapq_loss(batch, Ybus)
764
765 # iterative loops
766 for i in range(num_loops):
767 # print("-"*50, i)
768 # ----------- updated input ------------
769 cur_batch = batch.clone()
770
771 # use ema for better iterative fittings
772 if self.flag_use_ema and i > 0 and not flag_use_ema_infer:
773 self.ema_model.eval()
774 with torch.no_grad():
775 output_ema = self.ema_model(cur_batch_hist)
776 del cur_batch_hist
777 cur_batch['PV'].x[:, Va] = cur_batch['PV'].x[:, Va] - output['PV'][:, Va] * self.scaling_factor_va + output_ema['PV'][:, Va] * self.scaling_factor_va
778 cur_batch['PQ'].x[:, Vm] = cur_batch['PQ'].x[:, Vm] - output['PQ'][:, Vm] * self.scaling_factor_vm + output_ema['PQ'][:, Vm] * self.scaling_factor_vm
779 cur_batch['PQ'].x[:, Va] = cur_batch['PQ'].x[:, Va] - output['PQ'][:, Va] * self.scaling_factor_va + output_ema['PQ'][:, Va] * self.scaling_factor_va
780
781 delta_p, delta_q = deltapq_loss(cur_batch, Ybus)
782 self.ema_model.train()
783 # print("#"*20, cur_batch['PQ'].x.shape)
784
785 # update the inputs --- use deltap and deltaq
786 cur_batch['PQ'].x[:, P_net] = delta_p[:num_PQ] # deltap
787 cur_batch['PQ'].x[:, Q_net] = delta_q[:num_PQ] # deltaq
788 cur_batch['PV'].x[:, P_net] = delta_p[num_PQ:num_PQ+num_PV]
789 cur_batch = cur_batch.detach()
790 cur_batch_hist = cur_batch.clone().detach()
791
792 # ----------- forward ------------
793 if flag_use_ema_infer:
794 output = self.ema_model(cur_batch)
795 else:
796 output = self.net(cur_batch)
797
798 # --------------- update vm and va --------------
799 batch['PV'].x[:, Va] += output['PV'][:, Va] * self.scaling_factor_va

Callers

nothing calls this directly

Calls 3

create_YbusFunction · 0.70
deltapq_lossFunction · 0.70
trainMethod · 0.45

Tested by

no test coverage detected