⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trainandcv.m

📁 Continuous Profile Models (CPM) Matlab Toolbox.
💻 M
字号:
%function nothing=trainAndCV(lambda)%% Validate, keeping track of likelihood over each iterationfunction nothing=trainAndCV(lambdas)TEST=0;%% number of iterations of EM on training setmaxIter=100;if (TEST)  lambdas=0;  maxIter=10;end%%%%% Set up files, etc.clear myhost;[garb,myhost]=system('hostname'); myhost=myhost(1:(end-4));dateCode = datestr(datevec(now),31);dateCode(11)='.';savevars = ['numTrain numTest testSetInd trainSetInd trainLL maxIter* testNumIt keepScans numLam lambdas allGtest allGtrain finalTraces holdOutLL myThresh updateScale updateSigma updateT updateZ'];savedir = '/u/jenn/phd/MS/matlabCode/workspaces/cvLambda/';if (TEST)  savefile=['''' savedir 'test.mat'''];  errorLogFile = [savedir 'test.LOG'];  system(['\rm ' errorLogFile]);else  savefile = ['''' savedir 'cvLambdaIter.' num2str(lambdas,2) '.' dateCode '.' myhost '.mat'''];  errorLogFile = savefile(1:(end-4));  errorLogFile = [errorLogFile(2:end) 'LOG'];end  cmd1 = ['save ' savefile ';'];cmd2 = ['save ' savefile ' ' savevars ];eval(cmd1);display(['Will save results to: ' savefile]);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LOAD THE DATAmyDir = '/u/jenn/phd/MS/data/cocktail16/';eval(['load ' '''' myDir 'data.mat''']); clear header qmz;%%%%% SET UP THE DATAdd=headerAbun;headerAbun'if (TEST)  dd=getFakeSinCurves(20);end%showHeaderAbun(dd);%%%%%% SET UP EXPERIMENT PARAMSnumLam = length(lambdas);numRealTimes = length(dd{1});%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Just hard code the test and training sets:trainSetInd = [1 3 5 7 9]; if (TEST)  trainSetInd = [1 3 5]; endnumTrain=length(trainSetInd);%testSetInd  = [2 4 6 9 10];testSetInd  = [2 4 6];if (TEST)  testSetInd  = [2];endnumTest=length(testSetInd);extraPercent=0.1;initGTrain = getHMMParams(numTrain,numRealTimes);initGTest = getHMMParams(1,numRealTimes);traceLength = initGTest.numTaus;%% Params we want to keep for every iteration of trained model%% final tracefinalTraces = zeros(maxIter,traceLength);%% likekihood of trained modeltrainLL = zeros(1,maxIter);%% likelihood of the hold out tracesholdOutLL = zeros(maxIter,numTest);%% recovered transition parameters from training setallGtrain = cell(maxIter);%% recovered transition parameters from hold out caseallGtest = cell(maxIter,numTest);%% number of iterations of EM in test settestNumIt = zeros(maxIter,numTest);%% EM options for both training and testingupdateSigma=1; updateT=1; myThresh=5*10^-4; trainSet = dd(trainSetInd);testSet = dd(testSetInd);latentTrace = initializeLatentTrace(initGTrain,trainSet);oldTrace=latentTrace;%% Fit the modelsmooth=lambdas(1); tmp = ['smooth=' num2str(smooth,3)];display(tmp);for thisIt=1:maxIter;  msg=sprintf('%s\n',['Train Iteration ' num2str(thisIt)])  FPT=fopen(errorLogFile,'a');  fprintf(FPT,msg);  fclose(FPT);  maxIterNow=1; %Only do one at a time so we can calculate hold out  updateZ=1; updateScale=1;  if (thisIt>1)    oldTrace=newTrace;    oldTmpLikes=tmpLikes1;            trainG = reviseG(initGTrain,G.S,G.D);    [newTrace,G,tmpLikes1]=trainFBHMM(trainG,trainSet,oldTrace,smooth,updateSigma,updateT,updateScale,myThresh,updateZ,errorLogFile,maxIterNow,G.sigmas);  else    trainG=initGTrain;    [newTrace,G,tmpLikes1]=trainFBHMM(trainG,trainSet,oldTrace,smooth,updateSigma,updateT,updateScale,myThresh,updateZ,errorLogFile,maxIterNow);    %imstats(newTrace-oldTrace)    %return;  end    finalTraces(thisIt,:)=newTrace;  clear tempG;  tempG.D=G.D;  tempG.S=G.S;  tempG.sigmas=G.sigmas;  allGtrain{thisIt}=tempG;  trainLL(thisIt)=tmpLikes1(end);  %% check that likelhood matches up  if ((thisIt>1) & (oldTmpLikes(end)~=tmpLikes1(1)))    [oldTmpLikes(end)-tmpLikes1(1), oldTmpLikes(end), tmpLikes1(1)]     error(['Likelihoods dont match']);  end  %% Calculate likelihood of the hold-out samples  for thisHold=1:numTest    msg=sprintf('%s\n',['Hold Out Set ' num2str(thisHold)])    FPT=fopen(errorLogFile,'a');    fprintf(FPT,msg);    fclose(FPT);    %% these changes were not being passed on!!    %initGTest.S=tempG.S;    testG = reviseG(initGTest,G.S);        if (TEST)      maxIterTest=3;    else      maxIterTest=30;	      end          if (0)      display('NOW');      imstats(oldTrace-newTrace)      return;    end    display('NOW');    imstats(newTrace)    updateZ=0; updateScale=0;        [newTrace2,G2,tmpLikes]=trainFBHMM(testG,testSet(thisHold),newTrace,smooth,updateSigma,updateT,updateScale,myThresh,updateZ,errorLogFile,maxIterTest);    clear tempG;    tempG.D=G2.D;    tempG.S=G2.S;    tempG.sigmas=G2.sigmas;    allGtest{thisIt,thisHold}=tempG;    holdOutLL(thisIt,thisHold)=tmpLikes(end) - getSmoothLike(smooth,newTrace2);    testNumIt(thisIt,thisHold)=length(tmpLikes);  end  display(['Saving results to: ' savefile]);  eval(cmd2);end    %% CREATE A MARKER FILE WHICH SHOWS THE COMPUTATION IS DONEsavefileFINISHED = savefile(1:(end-4));savefileFINISHED = [savefileFINISHED 'FINISHED'];cmd3 = ['save ' savefileFINISHED ''' savefileFINISHED ;'];cmd3eval(cmd3);return;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -