Neural network hpneural参数调整-隐藏层数量可变(在do循环中)
我准备了一些训练和验证集,如:Neural network hpneural参数调整-隐藏层数量可变(在do循环中),neural-network,sas,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 Mode
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;
我想添加第二个循环来构建具有1到5个隐藏层的模型。在hp过程中,更多层意味着我需要添加额外的代码行。e、 g.有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;
所以基本上,我如何构建一些额外的宏,可以复制1到5倍的行隐藏&neurons代码>
多谢
[编辑]:
我已经构建了一个宏,可以为我执行此操作:
%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;
但我仍然希望有更好、更“优雅”的解决方案。您可以尝试以下方法来使用循环,而不是多次编写相同的语句
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);
您可以尝试以下方法来使用循环,而不是多次编写相同的语句
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);
在第二个宏中,使用%do
循环发出所需的源代码。在第一个宏中,使用宏调用而不是宏变量分辨率
%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
到
也可以只在原始宏中执行循环(而不必创建第二个宏)
在上面,您可以向原始宏中添加一个参数,例如%macro build\u prediction(layers\u count=)
,并在第二个宏中使用&layers\u count
代替3使用%do
循环来发出所需的源代码。在第一个宏中,使用宏调用而不是宏变量分辨率
%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
到
也可以只在原始宏中执行循环(而不必创建第二个宏)
在上面,您可以向原始宏添加一个参数,例如%macro build\u prediction(layers\u count=)
,并使用和layers\u count
代替3