fixed the SCG optimizer, thanks to Yarin Gal

This commit is contained in:
James Hensman 2014-01-16 16:22:16 +00:00
parent 9a7198447f
commit 4807967ce7
2 changed files with 46 additions and 3 deletions

View file

@ -1,4 +1,4 @@
# Copyright I. Nabney, N.Lawrence and James Hensman (1996 - 2012)
# Copyright I. Nabney, N.Lawrence and James Hensman (1996 - 2014)
# Scaled Conjuagte Gradients, originally in Matlab as part of the Netlab toolbox by I. Nabney, converted to python N. Lawrence and given a pythonic interface by James Hensman
@ -154,9 +154,9 @@ def SCG(f, gradf, x, optargs=(), maxiters=500, max_f_eval=np.inf, display=True,
break
else:
# Update variables for new position
gradold = gradnew
gradnew = gradf(x, *optargs)
current_grad = np.dot(gradnew, gradnew)
gradold = gradnew
fold = fnew
# If the gradient is zero then we are done.
if current_grad <= gtol:

View file

@ -86,7 +86,6 @@ def kmm_init(X, m = 10):
def fast_array_equal(A, B):
if config.getboolean('parallel', 'openmp'):
pragma_string = '#pragma omp parallel for private(i, j)'
else:
@ -174,6 +173,50 @@ def fast_array_equal(A, B):
return value
def fast_array_equal2(A, B):
if (A == None) and (B == None):
return True
elif ((A == None) and (B != None)) or ((A != None) and (B == None)):
return False
elif not (A.shape == B.shape):
return False
if config.getboolean('parallel', 'openmp'):
pragma_string = '#include <omp.h>'
weave_options = {'headers' : ['<omp.h>'],
'extra_compile_args': ['-fopenmp -O3'],
'extra_link_args' : ['-lgomp'],
'libraries' : ['gomp']}
else:
weave_options = {'extra_compile_args': ['-O3']}
pragma_string = ''
support_code = """
%s
#include <math.h>
""" % pragma_string
code = """
int i;
return_val = 1;
%s
for(i=0;i<N;i++){
if(A[i] != B[i]){
return_val = 0;
break;
}
}
""" % pragma_string
N = A.size
value = weave.inline(code, support_code=support_code,
arg_names=['A', 'B', 'N'],
**weave_options)
return bool(value)
if __name__ == '__main__':
import pylab as plt