def softassign(self):
"""
Run the softassign algorithm until convergence.
"""
# TODO add possibility of slack
for i, indices in enumerate(self.element_type_subset_indices):
M = self.match_matrix[indices]
old_M = M.copy()
for it in xrange(self.max_softassign_iterations):
# normalize across rows (except slack)
M /= np.sum(M,axis=1)[:,None]
# normalize across columns (except slack)
M /= np.sum(M,axis=0)
max_row_normalization_error = np.max(abs(np.sum(M, axis = 1)-1))
# break if converged
if max_row_normalization_error < self.softassign_convergence_threshold:
oprint(5, "Softassign algorithm for subset %d converged in iteration %d" % (i, it+1))
break
mean_squared_difference = np.max(abs(old_M-M))
if mean_squared_difference < self.softassign_convergence_threshold2:
oprint(5, "Softassign algorithm for subset %d converged in iteration %d" % (i, it+1))
break
if it == (self.max_softassign_iterations - 1):
eprint(3, "WARNING: Softassign algorithm for subset %d did not converge to %.2g (reached %.2g) in %d iterations" % (i, self.softassign_convergence_threshold, max_row_normalization_error, self.max_softassign_iterations))
np.copyto(old_M, M)
# M is NOT a view, but a copy
self.match_matrix[indices] = M
评论列表
文章目录