首页 > 解决方案 > 使用 fmin_l_bfgs_b() 函数时出现 TypeError

问题描述

fmin_l_bfgs_b在进行神经风格迁移时使用函数并不断获得

TypeError: 'numpy.float32' object is not callable

错误块的详细信息是:

TypeError                                 Traceback (most recent call last)
<ipython-input-10-4699dceebbd9> in <module>()
----> 1 generate_art('/content/nst_images/5.jpg', '/content/nst_images/a.jpg',1, img_height=400)

4 frames
<ipython-input-9-67cbbed20548> in generate_art(content_image_path, style_image_path, iterations, img_height)
     38   for i in range(iterations):
     39 
---> 40     x, min_val, info = fmin_l_bfgs_b(evaluator.loss(x,img_height,img_width,fetch_loss_and_grads),x, fprime=evaluator.grads, maxfun=20)
     41     img = x.copy().reshape((img_height, img_width, 3))
     42     img = deprocess_image(img)

/usr/local/lib/python3.6/dist-packages/scipy/optimize/lbfgsb.py in fmin_l_bfgs_b(func, x0, fprime, args, approx_grad, bounds, m, factr, pgtol, epsilon, iprint, maxfun, maxiter, disp, callback, maxls)
    197 
    198     res = _minimize_lbfgsb(fun, x0, args=args, jac=jac, bounds=bounds,
--> 199                            **opts)
    200     d = {'grad': res['jac'],
    201          'task': res['message'],

/usr/local/lib/python3.6/dist-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)
    343             # until the completion of the current minimization iteration.
    344             # Overwrite f and g:
--> 345             f, g = func_and_grad(x)
    346         elif task_str.startswith(b'NEW_X'):
    347             # new iteration

/usr/local/lib/python3.6/dist-packages/scipy/optimize/lbfgsb.py in func_and_grad(x)
    293     else:
    294         def func_and_grad(x):
--> 295             f = fun(x, *args)
    296             g = jac(x, *args)
    297             return f, g

/usr/local/lib/python3.6/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

TypeError: 'numpy.float32' object is not callable

有关如何解决此问题的任何建议?谢谢!

标签: pythonnumpydeep-learningscipy

解决方案


这是一个愚蠢的错误。对于 fmin_l_bfgs_b,损失函数的参数必须作为 args = () 单独传递


推荐阅读