Advertisement
Guest User

Untitled

a guest
Dec 6th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 3.09 KB | None | 0 0
  1. function [r] = baseline2(u,v,au0,av0,bu0,bv0,bbu0,bbv0,abu0,abv0,al0,bl0,train,N,nbu,nbv)
  2. %UNTITLED3 Summary of this function goes here
  3. %   Detailed explanation goes here
  4.     rsave=0;
  5.     r=0;
  6.     nbatch=size(train,1)/N;
  7.     sv=size(v,1);
  8.     su=size(u,1);
  9.     alu=gamrnd(au0,bu0);
  10.     alv=gamrnd(av0,bv0);
  11.     albu=gamrnd(abu0,bbu0);
  12.     albv=gamrnd(abv0,bbv0);
  13.     lam=gamrnd(al0,bl0);
  14.     d=size(u,2);
  15.     bu=0.01*randn(nbu);
  16.     bv=0.01*randn(nbv);
  17.     %ub=double(train((batch-1)*N+1:batch*N,4));
  18.     %vb=double(train((batch-1)*N+1:batch*N,5));
  19.     %rb=double(train((batch-1)*N+1:batch*N,3));
  20.     ub=train(:,4);
  21.     vb=train(:,5);
  22.     rb=train(:,3);
  23.     listub=unique(ub); % List of users in the batch
  24.     listvb=unique(vb); % List of items in the batch
  25.     nbub=size(listub,1); % Number of different users in the batch
  26.     nbvb=size(listvb,1); % Number of diferent items in the batch
  27.     for i=1:350        
  28.         clear ru;
  29.         clear rv;    
  30.         rv=randsample(listvb,nbvb); % To choose the items in a random order
  31.         for j=1:nbvb
  32.             clear mu;
  33.             clear sigma;
  34.             sigma=zeros(d,d);
  35.             mu=0;
  36.             f=find(vb==rv(j));
  37.             for k=1:size(f,1)
  38.                 sigma=sigma+u(ub(k),:)'*u(ub(k),:);
  39.            end
  40.            sigma=inv(alv*eye(d)+lam*sigma);
  41.            mu=sigma*lam*(u(ub(f),:)'*(rb(f)-bu(ub(f))-bv(vb(f))));
  42.             v(rv(j),:)=mvnrnd(mu,sigma); % We sample the items features according to (8)
  43.             clear mu;
  44.             clear sigma;
  45.             sigma=1/(albv+size(f,1)*lam);
  46.             mu=sigma*lam*sum(rb(f)-diag(u(ub(f),:)*v(vb(f),:)')-bu(ub(f)));
  47.            bv(rv(j))=normrnd(mu,sigma); % We sample the items' biases according to (9)
  48.         end
  49.         ru=randsample(listub,nbub); % To choose the users in a random order
  50.         for j=1:nbub
  51.             clear mu;
  52.             clear sigma;
  53.             sigma=zeros(d,d);
  54.             mu=0;
  55.             f=find(ub==ru(j));
  56.             for k=1:size(f,1)
  57.                 sigma=sigma+v(vb(k),:)'*v(vb(k),:);
  58.            end
  59.            sigma=inv(alu*eye(d)+lam*sigma);
  60.            mu=sigma*lam*(v(vb(f),:)'*(rb(f)-bv(vb(f))-bu(ub(f))));
  61.             u(ru(j),:)=mvnrnd(mu,sigma); % We sample the users features according to (8)
  62.             clear mu;
  63.             clear sigma;
  64.             sigma=1/(albu+size(f,1)*lam);
  65.             mu=sigma*lam*sum(rb(f)-diag(u(ub(f),:)*v(vb(f),:)')-bv(vb(f)));
  66.            bu(ru(j))=normrnd(mu,sigma); % We sample the users' biases according to (9)
  67.         end
  68.         alu=gamrnd(au0+nbub*d/2,bu0+sum(sum(u(listub,:).^2))/2); % We sample the priors according to (10),(11) and (12)
  69.         alv=gamrnd(av0+nbvb*d/2,bv0+sum(sum(v(listvb,:).^2))/2);
  70.         albu=gamrnd(abu0+nbub/2,bbu0+sum(bu(listub).^2)/2);
  71.         albv=gamrnd(abv0+nbvb/2,bbv0+sum(bv(listvb).^2)/2);
  72.         lam=gamrnd(al0+size(rb,1)/2,bl0+sum((rb-diag(u(ub,:)*v(vb,:)')-bu(ub)-bv(vb)).^2));
  73.        if (i>50)
  74.            rsave=rsave+u*v'+repmat(bu,1,nbvb)+repmat(bv',nbub,1);
  75.            r=1/(i-50)*rsave;
  76.        end
  77.        i
  78.    end      
  79. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement