AttributeError: 'numpy.ndarray' object has no attribute 'dim' with pytorch and scipy.optimize minimize
I have the following small code snippet:
import torch
from scipy.optimize import minimize 

def f(x):
    return torch.norm(x)

x = torch.tensor([1.0, 1.0])
y = minimize(f, x)
print(y)

However, this results in this error message:
&gt; AttributeError                            Traceback (most recent call
&gt; last) &lt;ipython-input-17-cb070be7a142&gt; in &lt;module&gt;
&gt;       6 
&gt;       7 x = [1.0, 1.0]
&gt; ----&gt; 8 y = minimize(f, x)
&gt;       9 print(y)
&gt; 
&gt; ~\Anaconda3\lib\site-packages\scipy\optimize\_minimize.py in
&gt; minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints,
&gt; tol, callback, options)
&gt;     610         return _minimize_cg(fun, x0, args, jac, callback, **options)
&gt;     611     elif meth == 'bfgs':
&gt; --&gt; 612         return _minimize_bfgs(fun, x0, args, jac, callback, **options)
&gt;     613     elif meth == 'newton-cg':
&gt;     614         return _minimize_newtoncg(fun, x0, args, jac, hess, hessp, callback,
&gt; 
&gt; ~\Anaconda3\lib\site-packages\scipy\optimize\optimize.py in
&gt; _minimize_bfgs(fun, x0, args, jac, callback, gtol, norm, eps, maxiter, disp, return_all, finite_diff_rel_step, **unknown_options)    1100    
&gt; 1101     sf = _prepare_scalar_function(fun, x0, jac, args=args,
&gt; epsilon=eps,
&gt; -&gt; 1102                                   finite_diff_rel_step=finite_diff_rel_step)    1103     1104     f =
&gt; sf.fun
&gt; 
&gt; ~\Anaconda3\lib\site-packages\scipy\optimize\optimize.py in
&gt; _prepare_scalar_function(fun, x0, jac, args, bounds, epsilon, finite_diff_rel_step, hess)
&gt;     260     # calculation reduces overall function evaluations.
&gt;     261     sf = ScalarFunction(fun, x0, args, grad, hess,
&gt; --&gt; 262                         finite_diff_rel_step, bounds, epsilon=epsilon)
&gt;     263 
&gt;     264     return sf
&gt; 
&gt; ~\Anaconda3\lib\site-packages\scipy\optimize\_differentiable_functions.py
&gt; in __init__(self, fun, x0, args, grad, hess, finite_diff_rel_step,
&gt; finite_diff_bounds, epsilon)
&gt;      74 
&gt;      75         self._update_fun_impl = update_fun
&gt; ---&gt; 76         self._update_fun()
&gt;      77 
&gt;      78         # Gradient evaluation
&gt; 
&gt; ~\Anaconda3\lib\site-packages\scipy\optimize\_differentiable_functions.py
&gt; in _update_fun(self)
&gt;     164     def _update_fun(self):
&gt;     165         if not self.f_updated:
&gt; --&gt; 166             self._update_fun_impl()
&gt;     167             self.f_updated = True
&gt;     168 
&gt; 
&gt; ~\Anaconda3\lib\site-packages\scipy\optimize\_differentiable_functions.py
&gt; in update_fun()
&gt;      71 
&gt;      72         def update_fun():
&gt; ---&gt; 73             self.f = fun_wrapped(self.x)
&gt;      74 
&gt;      75         self._update_fun_impl = update_fun
&gt; 
&gt; ~\Anaconda3\lib\site-packages\scipy\optimize\_differentiable_functions.py
&gt; in fun_wrapped(x)
&gt;      68         def fun_wrapped(x):
&gt;      69             self.nfev += 1
&gt; ---&gt; 70             return fun(x, *args)
&gt;      71 
&gt;      72         def update_fun():
&gt; 
&gt; &lt;ipython-input-17-cb070be7a142&gt; in f(x)
&gt;       3 
&gt;       4 def f(x):
&gt; ----&gt; 5     return torch.norm(x)
&gt;       6 
&gt;       7 x = [1.0, 1.0]
&gt; 
&gt; ~\Anaconda3\lib\site-packages\torch\functional.py in norm(input, p,
&gt; dim, keepdim, out, dtype)
&gt;     738         (tensor(3.7417), tensor(11.2250))
&gt;     739     &quot;&quot;&quot;
&gt; --&gt; 740     ndim = input.dim()
&gt;     741 
&gt;     742     # catch default case
&gt; 
&gt; AttributeError: 'numpy.ndarray' object has no attribute 'dim'

I'm not sure why this is happening as I don't think I'm converting anything to a numpy.ndarray.
Any help would be appreciated.

You can use NumPy instead of torch.
import numpy
from scipy.optimize import minimize

def f(x):
    return numpy.linalg.norm(x)

x = numpy.array([1.0, 1.0])
y = minimize(f, x)
print(y)

