如何在 Google Collab 中使用 scipy 最小化和 GPU?
pytorch 301
原文标题 :How to use scipy minimization with GPU in google collab?
我不得不在协作中将我的运行时类型更改为 GPU,否则 RAM 崩溃了。但是,当我使用 GPU 时,我在执行 scipy 最小化时遇到错误。错误如下:-
------Start--------
Traceback (most recent call last):
File "<ipython-input-8-4ca37ba86fbb>", line 119, in train
result=minimize(objective,val,constraints=cons,options={"disp":True})
File "/usr/local/lib/python3.7/dist-packages/scipy/optimize/_minimize.py", line 618, in minimize
constraints, callback=callback, **options)
File "/usr/local/lib/python3.7/dist-packages/scipy/optimize/slsqp.py", line 315, in _minimize_slsqp
for c in cons['ineq']]))
File "/usr/local/lib/python3.7/dist-packages/scipy/optimize/slsqp.py", line 315, in <listcomp>
for c in cons['ineq']]))
File "<ipython-input-8-4ca37ba86fbb>", line 64, in constraint
return -(A @ v)+alpha # scipy proves >= for constraints
File "/usr/local/lib/python3.7/dist-packages/torch/_tensor.py", line 678, in __array__
return self.numpy()
TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
------End--------
如何摆脱这个问题?我需要将哪个张量复制到主机内存?我的目标是最小化和约束如下: –
#Declaring the minimization equation here
def objective(x):
alpha = x[0]
v=x[1:len(x)]
vnorm=torch.linalg.vector_norm(v) * torch.linalg.vector_norm(v)
return alpha+(vnorm/2)
#Declaring the constraint here
def constraint(x):
alpha=x[0]
v=x[1:len(x)]
return -(A @ v)+alpha
cons={'type':'ineq','fun':constraint}
result=minimize(objective,val,constraints=cons,options={"disp":True})