def calculate_jacobian(self, times): ''' Calculates objective function jacobian many times.''' reproj_error = torch.empty((self.p, 2), dtype=torch.float64) for i in range(times): # reprojection error jacobian calculation for j in range(self.p): camIdx = self.obs[j, 0] ptIdx = self.obs[j, 1] cam = self.cams[camIdx] x = self.x[ptIdx] w = self.w[j] reproj_error[j], J = torch_jacobian(compute_reproj_err, (cam, x, w), (self.feats[j], )) self.jacobian.insert_reproj_err_block(j, camIdx, ptIdx, J) # weight error jacobian calculation for j in range(self.p): self.w_err[j], J = torch_jacobian(compute_w_err, (self.w[j], )) self.jacobian.insert_w_err_block(j, J) self.reproj_error = reproj_error.flatten()
def calculate_jacobian(self, times): '''Calculates objective function jacobian many times.''' for i in range(times): self.objective, J = torch_jacobian(self.objective_function, (self.inputs, ), self.params, False) if self.complicated: # getting us part of jacobian # Note: jacobian has the following structure: # # [us_part theta_part] # # where in us part is a block diagonal matrix with blocks of # size [3, 2] n_rows, n_cols = J.shape us_J = torch.empty([n_rows, 2]) for i in range(n_rows // 3): for k in range(3): us_J[3 * i + k] = J[3 * i + k][2 * i:2 * i + 2] us_count = 2 * n_rows // 3 theta_count = n_cols - us_count theta_J = torch.empty([n_rows, theta_count]) for i in range(n_rows): theta_J[i] = J[i][us_count:] self.jacobian = torch.cat((us_J, theta_J), 1) else: self.jacobian = J
def calculate_jacobian(self, times): ''' Calculates objective function jacobian many times.''' for i in range(times): self.objective, self.gradient = torch_jacobian( lstm_objective, self.inputs, self.params )