向L-BFGS-B优化器传递maxcor参数时触发TypeError求助
Fixing the "unexpected keyword argument 'maxcor'" Error with L-BFGS-B in Basinhopping
Hey there! I spot the issue immediately—this is a super common mix-up when working with scipy's optimization tools. Let's get this sorted quickly.
What's Going Wrong
The maxcor parameter is specific to the L-BFGS-B optimizer, but you're passing it directly as a top-level key in minimizer_kwargs. The problem is that scipy.optimize.minimize() (which basinhopping uses under the hood) doesn't accept maxcor as a direct argument. Method-specific parameters like this need to be wrapped inside an options dictionary instead.
The Corrected Code
Here's how to adjust your code to pass maxcor properly:
minimizer_kwargs = { "method": "L-BFGS-B", "bounds": bounds, "options": {"maxcor": 190} # Move maxcor into the options sub-dictionary } BH_result = optimize.basinhopping( obj_Basinhopping, initial_guess, minimizer_kwargs=minimizer_kwargs, accept_test=mybounds, callback=printx, disp=True )
Why This Fixes It
- Top-level keys in
minimizer_kwargsare parameters thatminimize()accepts universally (likemethod,bounds, orjac). - Settings that are unique to a specific optimizer (like
maxcorfor L-BFGS-B, which controls the number of stored history updates for the limited-memory BFGS approximation) belong in theoptionsdict. This tellsminimize()to pass those settings directly to the chosen optimization method.
内容的提问来源于stack exchange,提问作者Paul Thomas
相关产品推荐
相关产品推荐

