이 for 루프에서 fminunc가 매번 동일한 결과를 제공하는 이유는 무엇입니까? (Why is fminunc in this for loop giving the same result each time?)


문제 설명

이 for 루프에서 fminunc가 매번 동일한 결과를 제공하는 이유는 무엇입니까? (Why is fminunc in this for loop giving the same result each time?)

for 루프에서 'fminunc'를 여러 번 실행하려고 합니다.

코드의 일부:

6
P = 0.70 ;
idx = randperm(m);
X_Training = X(idx(1:round(P*m)),:); 
X_Testing = X(idx(round(P*m)+1:end),:);
y_Traning = y(idx(1:round(P*m)),:);
y_Testing = y(idx(round(P*m)+1:end),:);

s             = 100;
border_all    = zeros( 2, s );
lambda        = 1;
accuracy      = 0;
initial_theta = zeros( n + 1, 1 );
options       = optimset( 'GradObj', 'on', 'MaxIter', 400 );

for i = 1 : 1 : s
    [theta, J, exit_flag] = fminunc( @(t) costFunctionReg( t, X_Training, y_Traning, lambda ), initial_theta, options );

    for border = 0.01 : 0.01 : 1
        p          = predict( theta, X_Testing, border );
        accuracy_t = mean( double( p == y_Testing ) ) * 100;

        if accuracy_t > accuracy
            accuracy      = accuracy_t;
            border_result = border;
        endif
    endfor

    border_all(1, i) = border_result;
    border_all(2, i) = accuracy;
endfor

나중에 "theta"의 출력 값을 사용하여 다른 변경 사항을 확인합니다. 변수 "i"(i=1:1:s..의 경우). 스크립트를 실행할 때마다 "ta"가 계산되지만 다른 "for 루프" 반복에서는 변경되지 않습니다. 그러나 모든 반복에서 약간씩 달라야 합니다.

동시에 스크립트를 다시 실행하면 "theta"라는 새로운 값을 얻습니다. 따라서 첫 번째 반복에서만 처음부터 계산되고 다른 반복에서는 계산되지 않는 것 같습니다. 아마도 메모리 문제가 있을 수 있습니다. 'clear' 기능을 시도했지만 결과는 동일합니다.

같은 문제에 직면한 사람이 있습니까?


참조 솔루션

방법 1:

Converting comment thread to an answer for anyone googling with a similar problem:

This is because once you enter the for loop, the inputs to fminunc never change again, so you are effectively always calling it with the same inputs, and therefore getting the same output out.

Move the randomized initializations at the top part of your code snippet inside the for loop, and you will get different outputs each time, as you intend.

(by UlaTasos Papastylianou)

참조 문서

  1. Why is fminunc in this for loop giving the same result each time? (CC BY‑SA 2.5/3.0/4.0)

#optimization #gradient #for-loop #octave #runtime






관련 질문

세 개의 작은 숫자를 하나의 두 배에 저장하는 방법은 무엇입니까? (How to store three small numbers into one double?)

중첩 최적화에서 솔루션을 반환하는 방법은 무엇입니까? (How to return solutions in a nested optimization?)

C에서 "signed int"가 "unsigned int"보다 빠른 이유는 무엇입니까? (In C, why is "signed int" faster than "unsigned int"?)

값이 없는 경우 HTML5 로컬 저장소 개체는 무엇을 반환합니까? (What does the HTML5 Local Storage object return if there is no value?)

200K 게시물이 있는 Wordpress 사이트는 JOIN이 느린 SQL_CALC_FOUND_ROWS를 게시합니까? 속도 최적화? (Wordpress site with 200K posts SQL_CALC_FOUND_ROWS with JOINs slow? Optimize for speed?)

xlx의 각 시트에서 특정 셀을 추출하기 위해 이 R 코드를 개선하는 데 도움이 필요합니다. (I need help to improve this R code to extract specific cell from each sheet in xlxs)

이 for 루프에서 fminunc가 매번 동일한 결과를 제공하는 이유는 무엇입니까? (Why is fminunc in this for loop giving the same result each time?)

정수 스칼라 배열만 Hyperopt에서 스칼라 인덱스 오류로 변환될 수 있습니다. (only integer scalar arrays can be converted to a scalar index error in Hyperopt)

벡터 병합 알고리즘 도움말 (Help with algorithm for merging vectors)

이 MIPS 프로그램이 이렇게 오래 걸리나요? (파이에 가까운 프로그램) (Is this MIPS program supposed to take this long? (program that approximates pi))

힘을 최소화하기 위한 최적의 궤적, 최종 조건 문제 (Optimal trajectory to minimize force, issues with final conditions)

단일 svg 요소를 만들기 위해 여러 모양 병합 (Merging number of shapes to make a single svg element)







코멘트