8000 BUG: The attribute "nit" is not found when using the callback of scipy.optimize.minimize · Issue #22965 · scipy/scipy · GitHub
[go: up one dir, main page]

Skip to content
BUG: The attribute "nit" is not found when using the callback of scipy.optimize.minimize #22965
@zvun

Description

@zvun

Describe your issue.

When accessing the nit attribute in the callback function of a scipy.optimize.minimze call, it raises an attribute error, although it's stated in the documentation of OptimizeResult that nit exists and is equal to the number of iterations performed by the optimizer.

Reproducing Code Example

from scipy.optimize import minimize
import numpy as np

def objective(x):
    return np.sum((x - 1)**2)

def callback(intermediate_result):
    print(intermediate_result.nit)

minimize(objective, np.array([5, 5]), callback=callback)

Error message

Traceback (most recent call last):
  File "/work/draft/.env/lib/python3.11/site-packages/scipy/_lib/_util.py", line 1091, in __getattr__
    return self[name]
           ~~~~^^^^^^
KeyError: 'nit'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/work/draft/draft.py", line 10, in <module>
    minimize(objective, np.array([5, 5]), callback=callback)
  File "/work/draft/.env/lib/python3.11/site-packages/scipy/optimize/_minimize.py", line 733, in minimize
    res = _minimize_bfgs(fun, x0, args, jac, ca
7AB2
llback, **options)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/work/draft/.env/lib/python3.11/site-packages/scipy/optimize/_optimize.py", line 1440, in _minimize_bfgs
    if _call_callback_maybe_halt(callback, intermediate_result):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/work/draft/.env/lib/python3.11/site-packages/scipy/_lib/_util.py", line 1080, in _call_callback_maybe_halt
    callback(res)
  File "/work/draft/.env/lib/python3.11/site-packages/scipy/optimize/_optimize.py", line 97, in wrapped_callback
    return callback(intermediate_result=res)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/work/draft/draft.py", line 8, in callback
    print(intermediate_result.nit)
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/work/draft/.env/lib/python3.11/site-packages/scipy/_lib/_util.py", line 1093, in __getattr__
    raise AttributeError(name) from e
AttributeError: nit

SciPy/NumPy/Python version and system information

1.15.3 2.2.5 sys.version_info(major=3, minor=11, micro=2, releaselevel='final', serial=0)
Build Dependencies:
  blas:
    detection method: pkgconfig
    found: true
    include directory: /opt/_internal/cpython-3.11.10/lib/python3.11/site-packages/scipy_openblas32/include
    lib directory: /opt/_internal/cpython-3.11.10/lib/python3.11/site-packages/scipy_openblas32/lib
    name: scipy-openblas
    openblas configuration: OpenBLAS 0.3.28 DYNAMIC_ARCH NO_AFFINITY Haswell MAX_THREADS=64
    pc file directory: /project
    version: 0.3.28
  lapack:
    detection method: pkgconfig
    found: true
    include directory: /opt/_internal/cpython-3.11.10/lib/python3.11/site-packages/scipy_openblas32/include
    lib directory: /opt/_internal/cpython-3.11.10/lib/python3.11/site-packages/scipy_openblas32/lib
    name: scipy-openblas
    openblas configuration: OpenBLAS 0.3.28 DYNAMIC_ARCH NO_AFFINITY Haswell MAX_THREADS=64
    pc file directory: /project
    version: 0.3.28
  pybind11:
    detection method: config-tool
    include directory: unknown
    name: pybind11
    version: 2.13.6
Compilers:
  c:
    commands: cc
    linker: ld.bfd
    name: gcc
    version: 10.2.1
  c++:
    commands: c++
    linker: ld.bfd
    name: gcc
    version: 10.2.1
  cython:
    commands: cython
    linker: cython
    name: cython
    version: 3.0.12
  fortran:
    commands: gfortran
    linker: ld.bfd
    name: gcc
    version: 10.2.1
  pythran:
    include directory: ../../tmp/pip-build-env-4uho68t1/overlay/lib/python3.11/site-packages/pythran
    version: 0.17.0
Machine Information:
  build:
    cpu: x86_64
    endian: little
    family: x86_64
    system: linux
  cross-compiled: false
  host:
    cpu: x86_64
    endian: little
    family: x86_64
    system: linux
Python Information:
  path: /opt/python/cp311-cp311/bin/python
  version: '3.11'

Metadata

Metadata

Assignees

No one assigned

    Labels

    defectA clear bug or issue that prevents SciPy from being installed or used as expectedscipy.optimize

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0