Minimise a function in Python - python

Could you help on how to minimize a function in Python? Particularly, it's the logistic function. Below is my cost function, I got it correctly because I checked the answer.
def g(z):
h = 1 / (1 + numpy.exp(-z))
return h
theta1_ravel = theta1.ravel().T
theta2_ravel = theta2.ravel().T
theta_conca = numpy.concatenate((theta1_ravel, theta2_ravel))
lambada = 1
def computecost(theta_conca):
theta1 = numpy.reshape(theta_conca[0:10025], (25, 401))
theta2 = numpy.reshape(theta_conca[10025:10285], (10, 26))
a1 = X_1
a2 = g(X_1 * theta1.T) # (5000, 25)
a2 = numpy.column_stack((numpy.ones((m, 1)), a2))
a3 = g(a2 * theta2.T) # (5000, 10)
reg_term = (lambada / (2*m)) * (numpy.sum(numpy.power(theta1[:, 1:], 2)) + numpy.sum(numpy.power(theta2[:, 1:], 2)))
J = -(1/m) * (numpy.sum(numpy.multiply(Y, numpy.log(a3)) + numpy.multiply((1 - Y), numpy.log(1 - a3)))) + reg_term
return J
J = computecost(theta_conca)
J
And then I try to minimize it, get theta by using "scipy.optimize.minimize" but it's not working.
theta_random = numpy.random.rand(m, 1)
theta_random
scipy.optimize.minimize(computecost, theta_random)
This is the error notification:
ValueError Traceback (most recent call last)
<ipython-input-36-05051c7a9e1f> in <module>
1 theta_random = numpy.random.rand(m, 1)
2 theta_random
----> 3 scipy.optimize.minimize(computecost, theta_random)
/usr/lib/python3/dist-packages/scipy/optimize/_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
592 return _minimize_cg(fun, x0, args, jac, callback, **options)
593 elif meth == 'bfgs':
--> 594 return _minimize_bfgs(fun, x0, args, jac, callback, **options)
595 elif meth == 'newton-cg':
596 return _minimize_newtoncg(fun, x0, args, jac, hess, hessp, callback,
/usr/lib/python3/dist-packages/scipy/optimize/optimize.py in _minimize_bfgs(fun, x0, args, jac, callback, gtol, norm, eps, maxiter, disp, return_all, **unknown_options)
996 else:
997 grad_calls, myfprime = wrap_function(fprime, args)
--> 998 gfk = myfprime(x0)
999 k = 0
1000 N = len(x0)
/usr/lib/python3/dist-packages/scipy/optimize/optimize.py in function_wrapper(*wrapper_args)
325 def function_wrapper(*wrapper_args):
326 ncalls[0] += 1
--> 327 return function(*(wrapper_args + args))
328
329 return ncalls, function_wrapper
/usr/lib/python3/dist-packages/scipy/optimize/optimize.py in approx_fprime(xk, f, epsilon, *args)
755
756 """
--> 757 return _approx_fprime_helper(xk, f, epsilon, args=args)
758
759
/usr/lib/python3/dist-packages/scipy/optimize/optimize.py in _approx_fprime_helper(xk, f, epsilon, args, f0)
689 """
690 if f0 is None:
--> 691 f0 = f(*((xk,) + args))
692 grad = numpy.zeros((len(xk),), float)
693 ei = numpy.zeros((len(xk),), float)
/usr/lib/python3/dist-packages/scipy/optimize/optimize.py in function_wrapper(*wrapper_args)
325 def function_wrapper(*wrapper_args):
326 ncalls[0] += 1
--> 327 return function(*(wrapper_args + args))
328
329 return ncalls, function_wrapper
<ipython-input-33-3761d25f71af> in computecost(theta_conca)
14 def computecost(theta_conca):
15
---> 16 theta1 = numpy.reshape(theta_conca[0:10025], (25, 401))
17 theta2 = numpy.reshape(theta_conca[10025:10285], (10, 26))
18
<__array_function__ internals> in reshape(*args, **kwargs)
/usr/lib/python3/dist-packages/numpy/core/fromnumeric.py in reshape(a, newshape, order)
299 [5, 6]])
300 """
--> 301 return _wrapfunc(a, 'reshape', newshape, order=order)
302
303
/usr/lib/python3/dist-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
59
60 try:
---> 61 return bound(*args, **kwds)
62 except TypeError:
63 # A TypeError occurs if the object does have such a method in its
ValueError: cannot reshape array of size 5000 into shape (25,401)
How can I implement it? I'm sorry that the code looks like a mess but I think I shouldn't cut off something. I'll give you the entire code and data if you need to try on your device.
Thank you.

Related

Trackback Error with Scipy Optimize Jac: IndexError: index 1 is out of bounds for axis 0 with size 1

I'm taking the Coursera Machine Learning course by Andrew Ng. I'm working on this exercise to optimize the cost function for the logistic regression. I was able to do it correctly on Octave but I got errors when implementing in Python.
Here's my code:
# Define Sigmoid Function
def sig(z):
return 1/(1+np.exp(-z))
# Initialize Theta
theta = np.zeros((X.shape[1], 1))
# Find Gradient
def find_cost_grad(theta, X, y):
h = sig(X.dot(theta))
m = X.shape[0]
theta_s = theta[1:]
J = (-1/m) * (y.T.dot(np.log(h)) + (1-y).T.dot(np.log(1-h))) + lamb/(2*m) * np.sum(theta_s**2)
grad1 = (1/m)*((h-y).T.dot(X)).flatten()
grad2 = (theta*lamb/m).flatten()
grad2[0] = 0
grad = grad1+grad2
return J, grad
find_cost_grad(theta, X, y)
## Use BFGS and L-BFGS optimization
from scipy.optimize import minimize
bfgs = minimize(find_cost, theta, (X, y), method='L-BFGS-B', jac=True)
final_theta2 = bfgs['x']
bfgs
I got the following error:
621 **options)
622 elif meth == 'l-bfgs-b':
--> 623 return _minimize_lbfgsb(fun, x0, args, jac, bounds,
624 callback=callback, **options)
625 elif meth == 'tnc':
~\anaconda3\lib\site-packages\scipy\optimize\lbfgsb.py in _minimize_lbfgsb(fun, x0, args, jac, bounds, disp, maxcor, ftol, gtol, eps, maxfun, maxiter, iprint, callback, maxls, finite_diff_rel_step, **unknown_options)
304 iprint = disp
305
--> 306 sf = _prepare_scalar_function(fun, x0, jac=jac, args=args, epsilon=eps,
307 bounds=new_bounds,
308 finite_diff_rel_step=finite_diff_rel_step)
~\anaconda3\lib\site-packages\scipy\optimize\optimize.py in _prepare_scalar_function(fun, x0, jac, args, bounds, epsilon, finite_diff_rel_step, hess)
259 # ScalarFunction caches. Reuse of fun(x) during grad
260 # calculation reduces overall function evaluations.
--> 261 sf = ScalarFunction(fun, x0, args, grad, hess,
262 finite_diff_rel_step, bounds, epsilon=epsilon)
263
~\anaconda3\lib\site-packages\scipy\optimize\_differentiable_functions.py in __init__(self, fun, x0, args, grad, hess, finite_diff_rel_step, finite_diff_bounds, epsilon)
138
139 self._update_fun_impl = update_fun
--> 140 self._update_fun()
141
142 # Gradient evaluation
~\anaconda3\lib\site-packages\scipy\optimize\_differentiable_functions.py in _update_fun(self)
231 def _update_fun(self):
232 if not self.f_updated:
--> 233 self._update_fun_impl()
234 self.f_updated = True
235
~\anaconda3\lib\site-packages\scipy\optimize\_differentiable_functions.py in update_fun()
135
136 def update_fun():
--> 137 self.f = fun_wrapped(self.x)
138
139 self._update_fun_impl = update_fun
~\anaconda3\lib\site-packages\scipy\optimize\_differentiable_functions.py in fun_wrapped(x)
132 # Overwriting results in undefined behaviour because
133 # fun(self.x) will change self.x, with the two no longer linked.
--> 134 return fun(np.copy(x), *args)
135
136 def update_fun():
~\anaconda3\lib\site-packages\scipy\optimize\optimize.py in __call__(self, x, *args)
72 def __call__(self, x, *args):
73 """ returns the the function value """
---> 74 self._compute_if_needed(x, *args)
75 return self._value
76
~\anaconda3\lib\site-packages\scipy\optimize\optimize.py in _compute_if_needed(self, x, *args)
67 self.x = np.asarray(x).copy()
68 fg = self.fun(x, *args)
---> 69 self.jac = fg[1]
70 self._value = fg[0]
71
IndexError: index 1 is out of bounds for axis 0 with size 1
I checked that the gradient returned is a row vector (1x28). The find_cost_grad function runs fine when I ran it itself.
However, it caused errors when I ran it within the minimize function. Been looking for a solution but can't find it anywhere.
Please help.
Thanks!

Python Symfit : bound issue in chained minimization

I am tackling an optimization problem, and to do so I chain 3 optimizers.
Symfit is imported as sft, and sympy as sy:
[EDIT] The code below is a Minimal example of my situation, producing the same error message.
k, a = sft.parameters('k, a') # parameters to be optimized
k.min = 0.01
k.max = 1
a.min, a.max = 0.01, 1
L = sft.Parameter('L', value = 5, fixed = True) #this parameter is known,
#therefore I don't wan't is to move
#variables
x = sft.Variable('x')
A = sft.Variable('A')
P = sft.Variable('P')
#model
model_dict = {
sy.Derivative(A, x): k * A - P**a/ L,
sy.Derivative(P, x): - k * (P**2)/L
}
odemodel = sft.ODEModel(model_dict, initial= {x : 0.,
A : 0,
P : 0
})
#some mock data ( inspired of tBuLi symfit doc)
x = np.linspace(0, 20, 40)
mock_data = odemodel(x=x, k=0.1, a = 0.08, L = 5)._asdict()
sigma_data = 0.5
np.random.seed(42)
for var in mock_data:
mock_data[var] += np.random.normal(0, sigma_data, size=len(x))
fit = sft.Fit(odemodel, x = x,
A = mock_data[A], P = mock_data[P],
minimizer = [DifferentialEvolution, LBFGSB, BasinHopping]) #DifferentialEvolution #BasinHopping
fit_result = fit.execute()
The following error message pops :
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-8-ea4a7a6e9a8e> in <module>
34 A = mock_data[A], P = mock_data[P],
35 minimizer = [DifferentialEvolution, LBFGSB, BasinHopping]) #DifferentialEvolution #BasinHopping
---> 36 fit_result = fit.execute()
C:\ProgramData\Anaconda3\lib\site-packages\symfit\core\fit.py in execute(self, **minimize_options)
577 :return: FitResults instance
578 """
--> 579 minimizer_ans = self.minimizer.execute(**minimize_options)
580 minimizer_ans.covariance_matrix = self.covariance_matrix(
581 dict(zip(self.model.params, minimizer_ans._popt))
C:\ProgramData\Anaconda3\lib\site-packages\symfit\core\minimizers.py in execute(self, **minimizer_kwargs)
270 for minimizer, kwargs in zip(self.minimizers, bound_arguments.arguments.values()):
271 minimizer.initial_guesses = next_guess
--> 272 ans = minimizer.execute(**kwargs)
273 next_guess = list(ans.params.values())
274 answers.append(ans)
C:\ProgramData\Anaconda3\lib\site-packages\symfit\core\support.py in wrapped_func(*args, **kwargs)
421 else:
422 bound_args.arguments[param.name] = param.default
--> 423 return func(*bound_args.args, **bound_args.kwargs)
424 return wrapped_func
425
C:\ProgramData\Anaconda3\lib\site-packages\symfit\core\minimizers.py in execute(self, **minimize_options)
408 if jacobian is None:
409 jacobian = self.wrapped_jacobian
--> 410 return super(ScipyGradientMinimize, self).execute(jacobian=jacobian, **minimize_options)
411
412 def scipy_constraints(self, constraints):
C:\ProgramData\Anaconda3\lib\site-packages\symfit\core\minimizers.py in execute(self, **minimize_options)
428 def execute(self, **minimize_options):
429 return super(ScipyBoundedMinimizer, self).execute(bounds=self.bounds,
--> 430 **minimize_options)
431
432
C:\ProgramData\Anaconda3\lib\site-packages\symfit\core\support.py in wrapped_func(*args, **kwargs)
421 else:
422 bound_args.arguments[param.name] = param.default
--> 423 return func(*bound_args.args, **bound_args.kwargs)
424 return wrapped_func
425
C:\ProgramData\Anaconda3\lib\site-packages\symfit\core\minimizers.py in execute(self, bounds, jacobian, hessian, constraints, **minimize_options)
353 jac=jacobian,
354 hess=hessian,
--> 355 **minimize_options
356 )
357 return self._pack_output(ans)
C:\ProgramData\Anaconda3\lib\site-packages\scipy\optimize\_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
608 elif meth == 'l-bfgs-b':
609 return _minimize_lbfgsb(fun, x0, args, jac, bounds,
--> 610 callback=callback, **options)
611 elif meth == 'tnc':
612 return _minimize_tnc(fun, x0, args, jac, bounds, callback=callback,
C:\ProgramData\Anaconda3\lib\site-packages\scipy\optimize\lbfgsb.py in _minimize_lbfgsb(fun, x0, args, jac, bounds, disp, maxcor, ftol, gtol, eps, maxfun, maxiter, iprint, callback, maxls, **unknown_options)
275 bounds = [(None, None)] * n
276 if len(bounds) != n:
--> 277 raise ValueError('length of x0 != length of bounds')
278 # unbounded variables must use None, not +-inf, for optimizer to work properly
279 bounds = [(None if l == -np.inf else l, None if u == np.inf else u) for l, u in bounds]
ValueError: length of x0 != length of bounds
As mentioned in this (long) message it is the second minimizer lbfgsb which is causing troubles but I don't know at all how to overcome this.
In my full code, when I try to put only one minimizer, the program runs forever and I cannot find out why. This is why one minimizer only seems to be not enough and I want to chain them. I think this is due to the complexity of the problem : two coupled ODE with 7 parameters to optimize, and a initial guess performed with InteractiveGuess ( very great tool by the way).
Thanks by advance for your help !

curve_fit impossible if the function calls minimize

Say I have a function f1, of an independent variable x and a parameter a.
The function f1, in turn, needs a parameter m that can be obtained minimizing a second function f2, and this function needs the variable x as a parameter.
I have no problem calling f1(x,a) with any value, but if I try to make a fit with curve_fit of it, it throws a ValueError: setting an array element with a sequence. I guess it does it since it assigns not a single x value, but the whole array.
There is no way to make curve_fit work ?
Toy code, to check the problem:
import matplotlib.pyplot as plt
from scipy.optimize import minimize
from scipy.optimize import curve_fit
from random import uniform
def f1(x,a):
m = minimize(f2,0.,args=(x)).x[0]
return (x + a + m)**2
def f2(theta,a):
return (theta-a)**2
xs = np.linspace(-3.,3.,10)
ys = [round(f1(x,0.5)-uniform(-1,1),2) for x in xs]
plt.scatter(xs,ys)
popt,pcov = curve_fit(f1, xs, ys, [0.3])
plt.plot(xs,[f1(x,popt[0]) for x in xs])
Full traceback:
ValueErrorTraceback (most recent call last)
<ipython-input-87-5db669cee585> in <module>()
10 plt.scatter(xs,ys)
11 plt.plot(xs,[f1(x,0.5) for x in xs])
---> 12 popt,pcov = curve_fit(f1, xs, ys, [0.3])
13 plt.plot(xs,[f1(x,popt[0]) for x in xs])
/usr/lib64/python2.7/site-packages/scipy/optimize/minpack.pyc in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs)
749 # Remove full_output from kwargs, otherwise we're passing it in twice.
750 return_full = kwargs.pop('full_output', False)
--> 751 res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
752 popt, pcov, infodict, errmsg, ier = res
753 cost = np.sum(infodict['fvec'] ** 2)
/usr/lib64/python2.7/site-packages/scipy/optimize/minpack.pyc in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
381 if not isinstance(args, tuple):
382 args = (args,)
--> 383 shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
384 m = shape[0]
385 if n > m:
/usr/lib64/python2.7/site-packages/scipy/optimize/minpack.pyc in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape)
25 def _check_func(checker, argname, thefunc, x0, args, numinputs,
26 output_shape=None):
---> 27 res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
28 if (output_shape is not None) and (shape(res) != output_shape):
29 if (output_shape[0] != 1):
/usr/lib64/python2.7/site-packages/scipy/optimize/minpack.pyc in func_wrapped(params)
461 if transform is None:
462 def func_wrapped(params):
--> 463 return func(xdata, *params) - ydata
464 elif transform.ndim == 1:
465 def func_wrapped(params):
<ipython-input-87-5db669cee585> in f1(x, a)
1 from scipy.optimize import minimize
2 def f1(x,a):
----> 3 m = minimize(f2,0.,args=(x)).x[0]
4 return (x + a + m)**2
5 def f2(theta,a):
/usr/lib64/python2.7/site-packages/scipy/optimize/_minimize.pyc in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
595 return _minimize_cg(fun, x0, args, jac, callback, **options)
596 elif meth == 'bfgs':
--> 597 return _minimize_bfgs(fun, x0, args, jac, callback, **options)
598 elif meth == 'newton-cg':
599 return _minimize_newtoncg(fun, x0, args, jac, hess, hessp, callback,
/usr/lib64/python2.7/site-packages/scipy/optimize/optimize.pyc in _minimize_bfgs(fun, x0, args, jac, callback, gtol, norm, eps, maxiter, disp, return_all, **unknown_options)
961 else:
962 grad_calls, myfprime = wrap_function(fprime, args)
--> 963 gfk = myfprime(x0)
964 k = 0
965 N = len(x0)
/usr/lib64/python2.7/site-packages/scipy/optimize/optimize.pyc in function_wrapper(*wrapper_args)
291 def function_wrapper(*wrapper_args):
292 ncalls[0] += 1
--> 293 return function(*(wrapper_args + args))
294
295 return ncalls, function_wrapper
/usr/lib64/python2.7/site-packages/scipy/optimize/optimize.pyc in approx_fprime(xk, f, epsilon, *args)
721
722 """
--> 723 return _approx_fprime_helper(xk, f, epsilon, args=args)
724
725
/usr/lib64/python2.7/site-packages/scipy/optimize/optimize.pyc in _approx_fprime_helper(xk, f, epsilon, args, f0)
661 ei[k] = 1.0
662 d = epsilon * ei
--> 663 grad[k] = (f(*((xk + d,) + args)) - f0) / d[k]
664 ei[k] = 0.0
665 return grad
ValueError: setting an array element with a sequence.

How to call scipy.minimize over part of an array?

I'm struggling to get scipy.minimize to work for an optimization parameter that's an array, where I'm only looking at a part of the array inside the objective function.
import numpy as np
from scipy.optimize import minimize
n = 5
X_true = np.random.normal(size=(n,n))
X_guess = np.random.normal(size=(n,n))
indices = np.triu_indices(n)
def mean_square_error(X):
return ((X.flatten() - X_true.flatten()) ** 2).mean()
def mean_square_error_over_indices(X):
return ((X[indices].flatten() - X_true[indices].flatten()) ** 2).mean()
# works fine
print(mean_square_error(X_guess))
# works fine
print(mean_square_error_over_indices(X_guess))
# works fine (flatten is necessary inside the objective function)
print(minimize(mean_square_error, X_guess).x)
# IndexError
print(minimize(mean_square_error_over_indices, X_guess).x)
The traceback:
IndexError Traceback (most recent call last)
<ipython-input-1-08d40604e22a> in <module>
20 print(minimize(mean_square_error, X_guess).x) # works fine
21
---> 22 print(minimize(mean_square_error_over_indices, X_guess).x) # error
C:\Anaconda\lib\site-packages\scipy\optimize\_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
593 return _minimize_cg(fun, x0, args, jac, callback, **options)
594 elif meth == 'bfgs':
--> 595 return _minimize_bfgs(fun, x0, args, jac, callback, **options)
596 elif meth == 'newton-cg':
597 return _minimize_newtoncg(fun, x0, args, jac, hess, hessp, callback,
C:\Anaconda\lib\site-packages\scipy\optimize\optimize.py in _minimize_bfgs(fun, x0, args, jac, callback, gtol, norm, eps, maxiter, disp, return_all, **unknown_options)
968 else:
969 grad_calls, myfprime = wrap_function(fprime, args)
--> 970 gfk = myfprime(x0)
971 k = 0
972 N = len(x0)
C:\Anaconda\lib\site-packages\scipy\optimize\optimize.py in function_wrapper(*wrapper_args)
298 def function_wrapper(*wrapper_args):
299 ncalls[0] += 1
--> 300 return function(*(wrapper_args + args))
301
302 return ncalls, function_wrapper
C:\Anaconda\lib\site-packages\scipy\optimize\optimize.py in approx_fprime(xk, f, epsilon, *args)
728
729 """
--> 730 return _approx_fprime_helper(xk, f, epsilon, args=args)
731
732
C:\Anaconda\lib\site-packages\scipy\optimize\optimize.py in _approx_fprime_helper(xk, f, epsilon, args, f0)
662 """
663 if f0 is None:
--> 664 f0 = f(*((xk,) + args))
665 grad = numpy.zeros((len(xk),), float)
666 ei = numpy.zeros((len(xk),), float)
C:\Anaconda\lib\site-packages\scipy\optimize\optimize.py in function_wrapper(*wrapper_args)
298 def function_wrapper(*wrapper_args):
299 ncalls[0] += 1
--> 300 return function(*(wrapper_args + args))
301
302 return ncalls, function_wrapper
<ipython-input-1-08d40604e22a> in mean_square_error_over_indices(X)
11
12 def mean_square_error_over_indices(X):
---> 13 return ((X[indices].flatten() - X_true[indices].flatten()) ** 2).mean()
14
15
IndexError: too many indices for array
Based on the docs scipy.optimize.minimize accepts 1d arrays, so you are right about using "flatten()" but you should also use it for the initial guess that you pass to minimize()`. Here my suggestion to solve your problem:
import numpy as np
from scipy.optimize import minimize
# init
n = 5
x_true = np.random.normal(size=(n,n))
x_guess = np.random.normal(size=(n,n))
indices = np.triu_indices(n)
# flatten initial values for minimize
guess_x0 = x_guess.flatten()
guess_indeices_x0 = x_guess[indices].flatten()
# define objective funcs
mse = lambda x: ((x - x_true.flatten()) ** 2).mean()
mse_over_indices = lambda x: ((x - x_true[indices].flatten()) ** 2).mean()
# works fine
print("MSE: %5f" % mse(guess_x0))
print("MSE for indices: %5f" % mse_over_indices(guess_indeices_x0))
# works fine (flatten is necessary inside the objective function)
print("Result 1:", minimize(mse, guess_x0).x)
print("Result 2:", minimize(mse_over_indices, guess_indeices_x0).x)
Output:
MSE: 2.763674
MSE for indices: 3.192139
Result 1: [-1.2828193 0.49468516 -0.99500157 -0.47284983 1.6380719 -0.33051017
0.13769163 -0.23920633 -0.87430572 0.63945803 1.38327467 0.8484247
0.31888506 -1.15764468 1.06891773 -0.28372002 1.34104286 1.21024251
-0.11020374 1.37024001 1.08940389 1.82391261 0.32469148 0.64567877
0.54364199]
Result 2: [-1.28281964 0.49468503 -0.99500147 -0.47284976 1.63807209 0.13769154
-0.23920624 -0.87430606 0.63945812 0.31888521 -1.15764475 1.06891776
-0.11020373 1.37024006 0.54364213]

Negative binomial model cannot find starting position to sample

I am having difficulties running a PYMC3 model when the observed data is discrete. Oddly, if the observed data contains the value zero (0.), the model will run.
I've read in other posts that that suggest using
start = pm.find_MAP(fmin=scipy.optimize.fmin_powell) but that does not resolve the issue.
pymc3.__version__ = '3.0'
theano.__version__ = '0.7.0.dev-RELEASE'
numpy.__version__ = '1.8.0rc1'
Python 2.7.10
See iPython notebook
The code and error are below.
import pymc3 as pm
data = [6.0,12.0,12.0,46.0,5.0,11.0,11.0,39.0,4.0,10.0,25.0,11.0,8.0,5.0,10.0,2.0,30.0,21.0]
with pm.Model() as model:
alpha = pm.Uniform('alpha', lower=0, upper=100)
mu = pm.Uniform('mu', lower=0, upper=100)
y_pred = pm.NegativeBinomial('y_pred', mu=mu, alpha=alpha)
y_est = pm.NegativeBinomial('y_est',
mu=mu,
alpha=alpha,
observed=data)
start = pm.find_MAP()
step = pm.Metropolis()
trace = pm.sample(20000, step, start, progressbar=True)
The error I get is:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-5-b9f2264fccfc> in <module>()
14 observed=data)
15
---> 16 start = pm.find_MAP()
17
18 step = pm.Metropolis()
/Library/Python/2.7/site-packages/pymc3/tuning/starting.pyc in find_MAP(start, vars, fmin, return_raw, disp, model, *args, **kwargs)
79 if 'fprime' in getargspec(fmin).args:
80 r = fmin(logp_o, bij.map(
---> 81 start), fprime=grad_logp_o, disp=disp, *args, **kwargs)
82 else:
83 r = fmin(logp_o, bij.map(start), disp=disp, *args, **kwargs)
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/optimize/optimize.pyc in fmin_bfgs(f, x0, fprime, args, gtol, norm, epsilon, maxiter, full_output, disp, retall, callback)
775 'return_all': retall}
776
--> 777 res = _minimize_bfgs(f, x0, args, fprime, callback=callback, **opts)
778
779 if full_output:
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/optimize/optimize.pyc in _minimize_bfgs(fun, x0, args, jac, callback, gtol, norm, eps, maxiter, disp, return_all, **unknown_options)
830 else:
831 grad_calls, myfprime = wrap_function(fprime, args)
--> 832 gfk = myfprime(x0)
833 k = 0
834 N = len(x0)
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/optimize/optimize.pyc in function_wrapper(*wrapper_args)
279 def function_wrapper(*wrapper_args):
280 ncalls[0] += 1
--> 281 return function(*(wrapper_args + args))
282
283 return ncalls, function_wrapper
/Library/Python/2.7/site-packages/pymc3/tuning/starting.pyc in grad_logp_o(point)
74
75 def grad_logp_o(point):
---> 76 return nan_to_num(-dlogp(point))
77
78 # Check to see if minimization function actually uses the gradient
/Library/Python/2.7/site-packages/pymc3/blocking.pyc in __call__(self, x)
117
118 def __call__(self, x):
--> 119 return self.fa(self.fb(x))
/Library/Python/2.7/site-packages/pymc3/model.pyc in __call__(self, state)
397
398 def __call__(self, state):
--> 399 return self.f(**state)
400
401 class LoosePointFunc(object):
/Library/Python/2.7/site-packages/theano/compile/function_module.pyc in __call__(self, *args, **kwargs)
862 node=self.fn.nodes[self.fn.position_of_error],
863 thunk=thunk,
--> 864 storage_map=getattr(self.fn, 'storage_map', None))
865 else:
866 # old-style linkers raise their own exceptions
/Library/Python/2.7/site-packages/theano/gof/link.pyc in raise_with_op(node, thunk, exc_info, storage_map)
312 # extra long error message in that case.
313 pass
--> 314 reraise(exc_type, exc_value, exc_trace)
315
316
/Library/Python/2.7/site-packages/theano/compile/function_module.pyc in __call__(self, *args, **kwargs)
850 t0_fn = time.time()
851 try:
--> 852 outputs = self.fn()
853 except Exception:
854 if hasattr(self.fn, 'position_of_error'):
ValueError: Input dimension mis-match. (input[0].shape[0] = 1, input[4].shape[0] = 18)
Apply node that caused the error: Elemwise{Composite{Switch(i0, i1, Switch(i2, Switch(i3, i1, i4), i1))}}(TensorConstant{(1,) of 0}, TensorConstant{(1,) of 0}, Elemwise{mul,no_inplace}.0, InplaceDimShuffle{x}.0, TensorConstant{[ 6. 12... 30. 21.]})
Toposort index: 33
Inputs types: [TensorType(int8, vector), TensorType(int8, (True,)), TensorType(int8, (True,)), TensorType(int8, (True,)), TensorType(float64, vector)]
Inputs shapes: [(1,), (1,), (1,), (1,), (18,)]
Inputs strides: [(1,), (1,), (1,), (1,), (8,)]
Inputs values: [array([0], dtype=int8), array([0], dtype=int8), array([1], dtype=int8), array([0], dtype=int8), 'not shown']
Outputs clients: [[Sum{acc_dtype=float64}(Elemwise{Composite{Switch(i0, i1, Switch(i2, Switch(i3, i1, i4), i1))}}.0)]]
HINT: Re-running with most Theano optimization disabled could give you a back-trace of when this node was created. This can be done with by setting the Theano flag 'optimizer=fast_compile'. If that does not work, Theano optimizations can be disabled with 'optimizer=None'.
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.

Categories

Resources