title: Python scipy.optimize.fmin_l_bfgs_b error occurs
body: <p>My code is to implement an active learning algorithm, using L-BFGS optimization. I want to optimize four parameters: <code>alpha</code>, <code>beta</code>, <code>w</code> and <code>gamma</code>. </p>
  
  <p>However, when I run the code below, I got an error:    </p>
  
  <pre><code>optimLogitLBFGS = sp.optimize.fmin_l_bfgs_b(func, x0 = x0, args = (X,Y,Z), fprime = func_grad)                                           
    File "C:\Python27\lib\site-packages\scipy\optimize\lbfgsb.py", line 188, in fmin_l_bfgs_b
      **opts)
    File "C:\Python27\lib\site-packages\scipy\optimize\lbfgsb.py", line 311, in _minimize_lbfgsb
      isave, dsave)
      _lbfgsb.error: failed in converting 7th argument ``g' of _lbfgsb.setulb to C/Fortran array 
      0-th dimension must be fixed to 22 but got 4
  </code></pre>
  
  <p>My code is:</p>
  
  <pre><code># -*- coding: utf-8 -*-
  import numpy as np
  import scipy as sp
  import scipy.stats as sps
  
  num_labeler = 3
  num_instance = 5
  
  X = np.array([[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4],[5,5,5,5]])
  Z = np.array([1,0,1,0,1])
  Y = np.array([[1,0,1],[0,1,0],[0,0,0],[1,1,1],[1,0,0]])
  
  W = np.array([[1,1,1,1],[2,2,2,2],[3,3,3,3]])
  gamma = np.array([1,1,1,1,1])
  alpha = np.array([1,1,1,1])
  beta = 1
  para = np.array([1,1,1,1,1,1,1,1,1,2,2,2,2,3,3,3,3,1,1,1,1,1])
  
  def get_params(para):
      # extract parameters from 1D parameter vector
      assert len(para) == 22
      alpha = para[0:4]
      beta = para[4]
      W = para[5:17].reshape(3, 4)
      gamma = para[17:]
      return alpha, beta, gamma, W
  
  def log_p_y_xz(yit,zi,sigmati): #log P(y_it|x_i,z_i)
      return np.log(sps.norm(zi,sigmati).pdf(yit))#tested
  
  def log_p_z_x(alpha,beta,xi): #log P(z_i=1|x_i)
      return -np.log(1+np.exp(-np.dot(alpha,xi)-beta))#tested
  
  def sigma_eta_ti(xi, w_t, gamma_t): # 1+exp(-w_t x_i -gamma_t)^-1
      return 1/(1+np.exp(-np.dot(xi,w_t)-gamma_t)) #tested
  
  def df_alpha(X,Y,Z,W,alpha,beta,gamma):#df/dalpha
      return np.sum((2/(1+np.exp(-np.dot(alpha,X[i])-beta))-1)*np.exp(-np.dot(alpha,X[i])-beta)*X[i]/(1+np.exp(-np.dot(alpha,X[i])-beta))**2 for i in range (num_instance))
      #tested
  def df_beta(X,Y,Z,W,alpha,beta,gamma):#df/dbelta
      return np.sum((2/(1+np.exp(-np.dot(alpha,X[i])-beta))-1)*np.exp(-np.dot(alpha,X[i])-beta)/(1+np.exp(-np.dot(alpha,X[i])-beta))**2 for i in range (num_instance))
  
  def df_w(X,Y,Z,W,alpha,beta,gamma):#df/sigma * sigma/dw
      return np.sum(np.sum((-3)*(Y[i][t]**2-(-np.log(1+np.exp(-np.dot(alpha,X[i])-beta)))*(2*Y[i][t]-1))*(1/(1/(1+np.exp(-np.dot(X[i],W[t])-gamma[t])))**4)*(1/(1+np.exp(-np.dot(X[i],W[t])-gamma[t])))*(1-(1/(1+np.exp(-np.dot(X[i],W[t])-gamma[t]))))*X[i]+(1/(1/(1+np.exp(-np.dot(X[i],W[t])-gamma[t])))**2)*(1/(1+np.exp(-np.dot(X[i],W[t])-gamma[t])))*(1-(1/(1+np.exp(-np.dot(X[i],W[t])-gamma[t]))))*X[i]for t in range(num_labeler)) for i in range (num_instance))
  
  def df_gamma(X,Y,Z,W,alpha,beta,gamma):#df/sigma * sigma/dgamma
      return np.sum(np.sum((-3)*(Y[i][t]**2-(-np.log(1+np.exp(-np.dot(alpha,X[i])-beta)))*(2*Y[i][t]-1))*(1/(1/(1+np.exp(-np.dot(X[i],W[t])-gamma[t])))**4)*(1/(1+np.exp(-np.dot(X[i],W[t])-gamma[t])))*(1-(1/(1+np.exp(-np.dot(X[i],W[t])-gamma[t]))))+(1/(1/(1+np.exp(-np.dot(X[i],W[t])-gamma[t])))**2)*(1/(1+np.exp(-np.dot(X[i],W[t])-gamma[t])))*(1-(1/(1+np.exp(-np.dot(X[i],W[t])-gamma[t]))))for t in range(num_labeler)) for i in range (num_instance))
  
  def func(para, *args):
      alpha, beta, gamma, W = get_params(para)
      #args
      X = args [0]
      Y = args[1]
      Z = args[2]        
      return  np.sum(np.sum(log_p_y_xz(Y[i][t], Z[i], sigma_eta_ti(X[i],W[t],gamma[t]))+log_p_z_x(alpha, beta, X[i]) for t in range(num_labeler)) for i in range (num_instance))
      #tested
  
  def func_grad(para, *args):
      alpha, beta, gamma, W = get_params(para)
      #args
      X = args [0]
      Y = args[1]
      Z = args[2]
      #gradiants
      d_f_a = df_alpha(X,Y,Z,W,alpha,beta,gamma)
      d_f_b = df_beta(X,Y,Z,W,alpha,beta,gamma)
      d_f_w = df_w(X,Y,Z,W,alpha,beta,gamma)
      d_f_g = df_gamma(X,Y,Z,W,alpha,beta,gamma)
      return np.array([d_f_a, d_f_b,d_f_w,d_f_g])
  
  x0 = np.concatenate([np.ravel(alpha), np.ravel(beta), np.ravel(W), np.ravel(gamma)])
  
  optimLogitLBFGS = sp.optimize.fmin_l_bfgs_b(func, x0 = x0, args = (X,Y,Z), fprime = func_grad)  
  </code></pre>
  
  <p>I am not sure what is the problem. Maybe, the <code>func_grad</code> cause the problem? Could anyone have a look? thanks</p>