From eab97ea3d867a73bd2c0a2558e36b684e91e9908 Mon Sep 17 00:00:00 2001 From: Finkelman Date: Fri, 4 May 2018 09:29:38 -0400 Subject: [PATCH 1/3] worn in the case on non-convergence --- urbansim/urbanchoice/mnl.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/urbansim/urbanchoice/mnl.py b/urbansim/urbanchoice/mnl.py index f9ed73c8..41e06385 100644 --- a/urbansim/urbanchoice/mnl.py +++ b/urbansim/urbanchoice/mnl.py @@ -245,6 +245,10 @@ def mnl_estimate(data, chosen, numalts, GPU=False, coeffrange=(-3, 3), approx_grad=False, bounds=bounds ) + + if bfgs_result[2]['warnflag'] > 0: + logger.warn("mnl did not converge correctly: %s", bfgs_result) + beta = bfgs_result[0] stderr = mnl_loglik( beta, data, chosen, numalts, weights, stderr=1, lcgrad=lcgrad) From d84469d01faee2001daed8ed34ce26c0c149f160 Mon Sep 17 00:00:00 2001 From: Finkelman Date: Fri, 4 May 2018 09:30:44 -0400 Subject: [PATCH 2/3] fix numeric overflow fix non-convergence in tests --- urbansim/urbanchoice/mnl.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/urbansim/urbanchoice/mnl.py b/urbansim/urbanchoice/mnl.py index 41e06385..f3b2b30a 100644 --- a/urbansim/urbanchoice/mnl.py +++ b/urbansim/urbanchoice/mnl.py @@ -35,6 +35,10 @@ def mnl_probs(data, beta, numalts): raise Exception("Number of alternatives is zero") utilities.reshape(numalts, utilities.size() // numalts) + # https://stats.stackexchange.com/questions/304758/softmax-overflow + if clamp: + utilities.mat -= utilities.mat.max(0) + exponentiated_utility = utilities.exp(inplace=True) if clamp: exponentiated_utility.inftoval(1e20) From 9794d9ef3057a4ccc90f1431e5914a8f20e96bc7 Mon Sep 17 00:00:00 2001 From: Finkelman Date: Fri, 4 May 2018 14:37:23 -0400 Subject: [PATCH 3/3] support cuda in fix for numeric overflow --- urbansim/urbanchoice/mnl.py | 3 +-- urbansim/urbanchoice/pmat.py | 6 ++++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/urbansim/urbanchoice/mnl.py b/urbansim/urbanchoice/mnl.py index f3b2b30a..463eb252 100644 --- a/urbansim/urbanchoice/mnl.py +++ b/urbansim/urbanchoice/mnl.py @@ -36,8 +36,7 @@ def mnl_probs(data, beta, numalts): utilities.reshape(numalts, utilities.size() // numalts) # https://stats.stackexchange.com/questions/304758/softmax-overflow - if clamp: - utilities.mat -= utilities.mat.max(0) + utilities = utilities.subtract(utilities.max(0)) exponentiated_utility = utilities.exp(inplace=True) if clamp: diff --git a/urbansim/urbanchoice/pmat.py b/urbansim/urbanchoice/pmat.py index d25148b2..96e1e889 100644 --- a/urbansim/urbanchoice/pmat.py +++ b/urbansim/urbanchoice/pmat.py @@ -79,6 +79,12 @@ def cumsum(self, axis): # elif self.typ == 'cuda': # return PMAT(misc.cumsum(self.mat,axis=axis)) + def max(self, axis): + if self.typ == 'numpy': + return PMAT(np.max(self.mat, axis=axis)) + elif self.typ == 'cuda': + return PMAT(self.mat.max(axis=axis)) + def argmax(self, axis): if self.typ == 'numpy': return PMAT(np.argmax(self.mat, axis=axis))