isl-org / MultiObjectiveOptimization

Source code for Neural Information Processing Systems (NeurIPS) 2018 paper "Multi-Task Learning as Multi-Objective Optimization"
MIT License
978 stars 171 forks source link

local variable 'sol' referenced before assignment #42

Open lskdream opened 1 year ago

lskdream commented 1 year ago

I encounter an error UnboundLocalError: local variable 'sol' referenced before assignment

In min_norm_solvers.py, def _min_norm_2d(vecs, dps): """ Find the minimum norm solution as combination of two points This is correct only in 2D ie. min_c |\sum c_i x_i|_2^2 st. \sum c_i = 1 , 1 >= c_1 >= 0 for all i, c_i + c_j = 1.0 for some i, j """ dmin = 1e8 for i in range(len(vecs)): for j in range(i+1,len(vecs)): if (i,j) not in dps: dps[(i, j)] = 0.0 for k in range(len(vecs[i])): dps[(i,j)] += torch.mul(vecs[i][k], vecs[j][k]).sum().data.cpu() dps[(j, i)] = dps[(i, j)] if (i,i) not in dps: dps[(i, i)] = 0.0 for k in range(len(vecs[i])): dps[(i,i)] += torch.mul(vecs[i][k], vecs[i][k]).sum().data.cpu() if (j,j) not in dps: dps[(j, j)] = 0.0
for k in range(len(vecs[i])): dps[(j, j)] += torch.mul(vecs[j][k], vecs[j][k]).sum().data.cpu() c,d = MinNormSolver._min_norm_element_from2(dps[(i,i)], dps[(i,j)], dps[(j,j)]) if d < dmin: dmin = d sol = [(i,j),c,d] return sol, dps

The sol variable is only assigned if d < dmin, what if d >= dmin always, what should the sol variable be? I assume that it will be sol = [(i,j),c,dmin], is that right?

zcx-language commented 11 months ago

I solve it by initializing dmin to a larger number, i.e. dmin=float('inf')