implement cross-凯发k8网页登录

main content

implement cross-validation using parallel computing

simple parallel cross validation

in this example, use to compute a cross-validation estimate of mean-squared error for a regression model. run the computations in parallel.

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

this simple example is not a good candidate for parallel computation:

% 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.

reproducible parallel cross validation

to run crossval in parallel in a reproducible fashion, set the options and reset the random stream appropriately (see running reproducible parallel computations).

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 the stream:

reset(s)
cvmse = crossval('mse',x,y,'predfun',regf,'options',opts)
cvmse =
    0.1020
网站地图