【发布时间】:2023-04-10 23:37:02
【问题描述】:
我想知道如何执行以下代码,但现在使用 pytorch, 其中 dtype = torch.cuda.FloatTensor。有代码直接python(使用numpy):
import numpy as np
import random as rand
xmax, xmin = 5, -5
pop = 30
x = (xmax-xmin)*rand.random(pop,1)
y = x**2
[minz, indexmin] = np.amin(y), np.argmin(y)
best = x[indexmin]
这是我的尝试:
import torch
dtype = torch.cuda.FloatTensor
def fit (position):
return position**2
def main():
pop = 30
xmax, xmin = 5, -5
x= (xmax-xmin)*torch.rand(pop, 1).type(dtype)+xmin
y = fit(x)
[miny, indexmin] = torch.min(y,0)
best = x[indexmin]
print(best)
我将变量最好定义为索引等于 indexmin 的 x 值的最后一部分不起作用。我在这里做错了什么。
出现以下消息:
RuntimeError: expecting vector of indices at /opt/conda/conda-bld/pytorch_1501971235237/work/pytorch-0.1.12/torch/lib/THC/generic/THCTensorIndex.cu:405
【问题讨论】:
-
嘿!我无法重现该错误。我在另一个 pytorch 版本上,它运行得很好。你能从终端运行
conda list | grep pytorch吗? -
@cleros 谢谢! 抱歉,我发布了错误的代码 import torch dtype = torch.cuda.FloatTensor def fit (x): return x2 def main(): pop = 30 xmax, xmin = 5 , -5 x = (xmax-xmin)*torch.rand(pop, 1).type(dtype)+xmin y = fit(x) [miny, indexmin] = torch.min(y,0) best = x[ indexmin] print(best) main() 这是给我错误的代码。 **2) 当我运行 grep pytorch 时:'grep' 不被识别为内部或外部命令、可运行程序或批处理文件。