hpneural参数调整-隐藏层数可变(在do循环中)

时间:2019-01-09 12:45:02

标签: neural-network sas

我准备了一些训练和验证集,例如:

data train;
     retain Make Model DriveTrain EngineSize Horsepower MSRP;
     set sashelp.cars(where=(Origin <> 'Asia'));
     keep Make Model DriveTrain EngineSize Horsepower MSRP;

run;

data validation;
     retain Make Model DriveTrain EngineSize Horsepower MSRP;
     set sashelp.cars(where=(Origin = 'Asia'));
     keep Make Model MSRP DriveTrain EngineSize Horsepower;
run;

就目前而言,我建立了一个宏来训练具有可变数量神经元的nn模型。

%macro build_predictions();

    data validations_scores;
        set validation;
        keep Make Model MSRP;
    run;

    %do neurons = 1 % to 10;

            proc hpneural data=train;
                input Make -- Horsepower / level=nom;
                target MSRP / level=int;
                hidden &neurons.;
                train outmodel=model_neural_network maxiter=1000;
            run;

            proc hpneural data=validation;
               score model=model_neural_network out=scored_test_data;
            run;

            data scored_test_data;
                set scored_test_data(keep=P_MSRP);
                P_MSRP = ceil(P_MSRP);
                rename P_MSRP = Forecast_neurons_&neurons.;
            run;

            data validations_scores;
                set validations_scores;
                set scored_test_data;
            run;

        %end;

%mend;

%build_predictions;

我想添加第二个循环以建立具有1到5个隐藏层的模型。在hp过程中,更多的层意味着我需要添加其他代码行。例如具有5个神经元的3层将是:

proc hpneural data=train;
            input Make -- Horsepower / level=nom;
            target MSRP / level=int;
            hidden 5;
            hidden 5;
            hidden 5;
            train outmodel=model_neural_network maxiter=1000;
run;

所以基本上,我该如何构建一些附加宏,将其复制到hidden &neurons.;行的1到5倍

非常感谢!

[编辑]:

我已经建立了一个可以为我做的宏:

%macro copy_lines(i, neurons);

    %global hidden_layers;

    %if &i. eq 1 %then %do;
        %let hidden_layers = %str(hidden &neurons.;); 
    %end;
    %if &i. eq 2 %then %do;
        %let hidden_layers = %str(hidden &neurons.; hidden &neurons.;);
    %end;
    %if &i. eq 3 %then %do;
        %let hidden_layers = %str(hidden &neurons.; hidden &neurons.; hidden &neurons.;);
    %end;
    %if &i. eq 4 %then %do;
        %let hidden_layers = %str(hidden &neurons.; hidden &neurons.; hidden &neurons.; hidden &neurons.;);
    %end;
    %if &i. eq 5 %then %do;
        %let hidden_layers = %str(hidden &neurons.; hidden &neurons.; hidden &neurons.; hidden &neurons.; hidden &neurons.;);
    %end;

%mend;

它是这样的:

%copy_lines(3, 5);
proc hpneural data=train;
            input Make -- Horsepower / level=nom;
            target MSRP / level=int;
            &hidden_layers.         
            train outmodel=model_neural_network maxiter=1000;
run;

但是我仍然希望有更好,更“优雅”的解决方案。

2 个答案:

答案 0 :(得分:1)

您可以尝试以下方法使用循环,而不是多次编写相同的语句

options merror mlogic mprint symbolgen;
%macro copy_lines(i, neurons);
    %global hidden_layers_temp;
    %let hidden_layers_temp='';
    /*loop through the number of given iterations*/
    %do j=1 %to &i;
        %let hidden_layers_temp=%str(&hidden_layers_temp,hidden &neurons.;);
    %end;
    /*Remove the first 3 characters which are '',*/
    %let hidden_layers=%qsysfunc(substr(&hidden_layers_temp,4,%sysfunc(length(&hidden_layers_temp))-3));
    %put &hidden_layers;
%mend;

%copy_lines(3, 5);
%copy_lines(5, 23);

答案 1 :(得分:1)

在第二个宏中,使用%do循环来发出所需的源代码。在第一个宏中,使用宏调用而不是宏变量 resolution

%macro hidden_layers (layers=, neurons=);

  %local i;
  %do i = 1 %to &layers;
    hidden &neurons;   /* macro will emit this source code &layer times */
  %end;

%mend;

调整原始宏
            … 
            target MSRP / level=int;
            hidden &neurons.;
            train outmodel=model_neural_network maxiter=1000

            … 
            target MSRP / level=int;
            %hidden_layers (3, &neurons)
            train outmodel=model_neural_network maxiter=1000

您也可以只在原始宏内执行循环(而不必创建第二个宏)。

%macro build_predictions
  %do neurons = 1 % to 10;
        proc hpneural data=train;
            input Make -- Horsepower / level=nom;
            target MSRP / level=int;

            %local index;
            %do index = 1 %to 3; hidden &neurons.; %end;

            train outmodel=model_neural_network maxiter=1000;
        run;
        …
    %end; %* neurons loop;
%mend;

在上面,您可以向原始宏中添加一个参数,例如%macro build_prediction (layers_count=)并使用&layers_count代替3