2017-11-13 11 views
0

다음 코드는 가능하지만 현재는 pytorch를 사용하고 있는지 알고 싶습니다. 여기서 dtype = torch.cuda.FloatTensor. 코드 파이썬 (numpy 사용) : 기본적으로 피트 니스의 최소값을 생성하는 x의 값을 얻고 싶습니다. 나는 그것이 작동하지 않습니다 indexmin 같지 지수 x의 값으로 변수 최선의 정의Pytorch에 변수를 지정하십시오.

import torch 
dtype = torch.cuda.FloatTensor 
def fit (x): 
    return x**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] 
main() 

마지막 부분 :

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] 

그것을 할 내 시도이다. 내가 여기서 잘못하고있는 것은 무엇인가.

다음 messenge가 나타납니다 RuntimeError에 :

expecting vector of indices at /opt/conda/conda-bld/pytorch_1501971235237/work/pytorch-0.1.12/torch/lib/THC/generic/THCTensorIndex.cu:405 

답변

0

다음과 같이 간단하게 할 수 있습니다.

import torch 
dtype = torch.cuda.FloatTensor 
def main(): 
    pop, xmax, xmin = 30, 5, -5 
    x    = (xmax-xmin)*torch.rand(pop, 1).type(dtype)+xmin 
    y    = torch.pow(x, 2) 
    [miny, indexmin] = y.min(0) 
    best    = x.squeeze()[indexmin] # squeeze x to make it 1d 

main()