並列計算を使用する交差検証の実装
簡単な並列交差検証
この例では、crossval を使用して、回帰モデルの平均二乗誤差の交差検証の推定値を計算します。計算は並列で実行します。
mypool = parpool()
Starting parpool using the 'local' profile ... connected to 2 workers.
mypool =
Pool with properties:
AttachedFiles: {0x1 cell}
NumWorkers: 2
IdleTimeout: 30
Cluster: [1x1 parallel.cluster.Local]
RequestQueue: [1x1 parallel.RequestQueue]
SpmdEnabled: 1
opts = statset('UseParallel',true);
load('fisheriris');
y = meas(:,1);
X = [ones(size(y,1),1),meas(:,2:4)];
regf=@(XTRAIN,ytrain,XTEST)(XTEST*regress(ytrain,XTRAIN));
cvMse = crossval('mse',X,y,'Predfun',regf,'Options',opts)
cvMse =
0.1028
この簡単な例は並列計算の候補としては適切ではありません。
% How long to compute in serial?
tic;cvMse = crossval('mse',X,y,'Predfun',regf);toc
Elapsed time is 0.073438 seconds.
% How long to compute in parallel?
tic;cvMse = crossval('mse',X,y,'Predfun',regf,...
'Options',opts);toc
Elapsed time is 0.289585 seconds.再現性のある並列交差検証
crossval を再現性があるように並列実行するには、オプションを適切に設定し、乱数ストリームを適切にリセットします (再現性のある並列計算を実行するを参照)。
mypool = parpool()
Starting parpool using the 'local' profile ... connected to 2 workers.
mypool =
Pool with properties:
AttachedFiles: {0x1 cell}
NumWorkers: 2
IdleTimeout: 30
Cluster: [1x1 parallel.cluster.Local]
RequestQueue: [1x1 parallel.RequestQueue]
SpmdEnabled: 1
s = RandStream('mlfg6331_64');
opts = statset('UseParallel',true,...
'Streams',s,'UseSubstreams',true);
load('fisheriris');
y = meas(:,1);
X = [ones(size(y,1),1),meas(:,2:4)];
regf=@(XTRAIN,ytrain,XTEST)(XTEST*regress(ytrain,XTRAIN));
cvMse = crossval('mse',X,y,'Predfun',regf,'Options',opts)
cvMse =
0.1020ストリームをリセットします。
reset(s)
cvMse = crossval('mse',X,y,'Predfun',regf,'Options',opts)
cvMse =
0.1020