📄 runsm.m.svn-base
字号:
%% References:%% L. Itti, P. Baldi, A Principled Approach to Detecting Surprising % Events in Video, In: Proc. IEEE Conference on Computer Vision and % Pattern Recognition (CVPR), pp. 631-637, Jun 2005. %% L. Itti, P. Baldi, Bayesian Surprise Attracts Human Attention, In: % Advances in Neural Information Processing Systems, Vol. 19 % (NIPS*2005), pp. 1-8, Cambridge, MA:MIT Press, 2006.%% T. Nathan Mundhenk% mundhenk@usc.edu%% //////////////////////////////////////////////////////////////////// %% The Baysian Surprise Matlab Toolkit - Copyright (C) 2004-2007 %% by the University of Southern California (USC) and the iLab at USC. %% See http://iLab.usc.edu for information about this project. %% //////////////////////////////////////////////////////////////////// %% This file is part of the Baysian Surprise Matlab Toolkit %% %% The Baysian Surprise Matlab Toolkit is free software; you can %% redistribute it and/or modify it under the terms of the GNU General %% Public License as published by the Free Software Foundation; either %% version 2 of the License, or (at your option) any later version. %% %% The Baysian Surprise Matlab Toolkit is distributed in the hope %% that it will be useful, but WITHOUT ANY WARRANTY; without even the %% implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR %% PURPOSE. See the GNU General Public License for more details. %% %% You should have received a copy of the GNU General Public License %% along with the iBaysian Surprise Matlab Toolkit; if not, write %% to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, %% Boston, MA 02111-1307 USA. %% //////////////////////////////////////////////////////////////////// %%% Primary maintainer for this file: T. Nathan Mundhenk <mundhenk@usc.edu>%function smod = runsm(data,smod)% We require that smod be reset each run. This keeps values cleanif isfield(smod,'smodisset') if smod.smodisset == 0 fprintf('NOTICE: SMOD has been set once in newsm, but is no longer usable\n'); fprintf('Most likely you have tried to call runsm in batch mode twice on\n'); fprintf('the same smod. You must make a new smod each time you call a\n'); fprintf('batch mode run on your data\n'); error('SMOD is not valid'); endelse error('You must create SMOD using the function newsm before you call this function');end% Use the asymptotic max value for betaif strcmp(smod.options.setbetamax,'yes') smod.beta1 = smod.max.beta1; smod.beta2 = smod.max.beta2;end% Figure out if we are running sample by sample or in batch mode. If% running in batch mode, we may need to transpose the vector matrixif size(data,1) > 1 if smod.options.debug == 2 fprintf('Running in batch mode. Input is a column vector\n') end smod = runsmbatch(data,smod);elseif size(data,2) > 1 if smod.dim == 1 if smod.options.debug == 2 fprintf('Running in batch mode (Univariate). Input is a row vector\n') end data = data'; smod = runsmbatch(data,smod); else if smod.options.debug == 2 fprintf('Running in single step mode (Multivariate). Input is a row vector\n') end smod = runsmsingle(data,smod); end else if smod.options.debug == 2 fprintf('Running in single step mode (Univaraite)\n') end smod = runsmsingle(data,smod);end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Batch Run Function %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Call this to run surprise on a sample vectorfunction smod = runsmbatch(data,smod)SX = size(data,1);if smod.dim == 1 SY = 1;else SY = size(data,2);end% Create surprise values output matrixsmod.surprise = zeros(SX,SY);% create some debug information if requestedif smod.options.debug > 0 smod.debugdata = struct('Description','Debug values from runsm'); smod.debugdata.alpha1 = zeros(SX,SY); smod.debugdata.alpha2 = zeros(SX,SY); smod.debugdata.beta1 = zeros(SX,SY); smod.debugdata.beta2 = zeros(SX,SY);end% For each data item run the surprise model on it. This is the core of the% batch mode surprise model. We compute new beta and alpha values fromt the% sample value, then we compute the KL distance between the two Gamma PDF's% using klgamma. We take the absolute value to support negative data% values. However, using negative values as inputs may not in fact make% sense. for n = 1:SX smod = newalphabeta(data(n,:),smod); smod.surprise(n,:) = abs(klgamma(smod.alpha1,smod.alpha2,smod.beta1,smod.beta2)); if ~strcmp(smod.options.jointmodel,'none') smod.joint.surprise(n,:) = abs(klgamma(smod.joint.alpha1,smod.joint.alpha2,smod.joint.beta1,smod.joint.beta2)); end if smod.options.debug > 0 smod.debugdata.alpha1(n,:) = smod.alpha1; smod.debugdata.alpha2(n,:) = smod.alpha2; smod.debugdata.beta1(n,:) = smod.beta1; smod.debugdata.beta2(n,:) = smod.beta2; if smod.options.debug > 1 fprintf('RUNNING INPUT %f LOOP %d ALPHA [%f,%f] BETA [%f,%f] SURPRISE VALUE %f\n',data(n,1),n,smod.alpha1,smod.alpha2,smod.beta1,smod.beta2,smod.surprise(n,1)); fprintf('\n'); end end %smod.alpha1 = smod.alpha2; %if strcmp(smod.options.setbetamax,'no') %smod.beta1 = smod.beta2; %end smod.epoch = smod.epoch + 1; smod.smodisset = 0;end% Graph the surprise results if requested.if strcmp(smod.options.graph,'surprise') if strcmp(smod.options.setbetamax,'yes') Title = 'setbetamax'; elseif strcmp(smod.options.factordecay,'yes') Title = 'factordecay'; else Title = 'basic'; end res = basicsurprisegraph(smod.surprise,data,'Surprise Value','Input Value ',Title,smod);end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Single Run Function%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Call this to run surprise on one sample at a time. function smod = runsmsingle(data,smod,options)% For each data item run the surprise model on it. This is the core of the% batch mode surprise model. We compute new beta and alpha values fromt the% sample value, then we compute the KL distance between the two Gamma PDF's% using klgamma. We take the absolute value to support negative data% values. However, using negative values as inputs may not in fact make% sense. smod = newalphabeta(data,smod,options);smod.surprise = abs(klgamma(smod.alpha1,smod.alpha2,smod.beta1,smod.beta2));if ~strcmp(smod.options.jointmodel,'none') smod.joint.surprise = abs(klgamma(smod.joint.alpha1,smod.joint.alpha2,smod.joint.beta1,smod.joint.beta2));end%smod.alpha1 = smod.alpha2; %if strcmp(smod.options.setbetamax,'no')% smod.beta1 = smod.beta2;%endsmod.epoch = smod.epoch + 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -