Gradient Descent algorithm not converging - recommendation-engine
I am trying to implement an algorithm for a Social Recommender System. The algorithm predicts the rating given to an item by a user, using the global reputation of the user and a similarity matrix. I am using matrix factorization to find the values of latent factors of user and item. I am using gradient descent algorithm. I have taken learning rate=0.0001, no of latent factors=10.
The value of cost function after each iteration is not following any pattern i.e. i am getting random values of cost function after each iteration.
What could be the possible reasons for this?
here is the algorithm
function [U,V,J] = algorithm(I,C,A,F,sim,n,m)
[U,V,k,delta,x1,x2]=parameters(n,m);
J=zeros(100,1);
count=1;
ntemp=zeros(n,k);
mtemp=zeros(m,k);
Jprev=Inf;
Jcurr=costFunc(U,V,I,C,A,sim,F,n,m,x1,x2);
J(count)=Jcurr;
count=count+1;
temp0=zeros(1,k);
temp1=zeros(1,k);
while(count<=100)
%Jprev-Jcurr>(10^(-6)) %convergence condition
for i=1:n
for j=1:m
temp0=temp0+(I(i,j)*C(i)*((U(i,:)*V(j,:)')-A(i,j)))*V(j,:);
end
for p=1:n
if F(i,p)==1
temp1=temp1+sim(i,p)*(U(i,:)-U(p,:));
end
end
ntemp(i,:)=U(i,:)-delta*(temp0+(U(i,:)*x1)+(temp1*x2));
end
temp0=zeros(1,k);
temp1=zeros(1,k);
for i=1:m
for j=1:n
temp0=temp0+(I(j,i)*C(j)*((U(j,:)*V(i,:)')-A(j,i)))*U(j,:);
end
mtemp(i,:)=V(i,:)-delta*(temp0+(x1*V(i,:)));
end
U=ntemp;
V=mtemp;
Jprev=Jcurr;
Jcurr=costFunc(U,V,I,C,A,sim,F,n,m,x1,x2);
J(count)=Jcurr;
count=count+1;
end
end
here is the cost function
function [temp0,temp1,J] = costFunc(U,V,I,C,A,sim,F,n,m,x1,x2)
J=0;
temp0=0;
temp1=0;
for i=1:n
for j=1:m
temp1=temp1+I(i,j)*C(i)*((A(i,j)-U(i,:)*V(j,:)')^2);
end
end
for i=1:n
for j=i:n
if F(i,j)==1
temp0=temp0+sim(i,j)*norm(U(i,:)-U(j,:),'fro');
end
end
end
J=temp1+x2*temp0+x1*(norm(U,'fro')+norm(V,'fro'));
J=J/2;
end
here is the parameter function
function [U,V,k,delta,x1,x2] = parameters(n,m)
k=10; %no of latent factors
U=rand(n,k); %user latent factor matrix
V=rand(m,k); %item latent factor matrix
delta=0.00001; %learning rate
x1=0.001; %regularization parameter
x2=0.001; %regularization parameter for similarity
end
Related
Robustify a particle filter
i would like to have your thought on the following code extract. I have the uniform probability density function: p(x) = 1/S if -S/2<x<S/2 p(x) = 0 else My goal is to avoid having the weight becoming 0, and for that I need to robustify the likelihood calculation so that it does not go to 0. For that I know I need to modify the if condition, but I am not sure what I should put instead. % Particles weight update S=20; for i=1:N for j=1:P_beacon % Innovation mu=abs(norm(particle(i).x(1:2)-P_beacon(:,j))-y(j,k)); if mu>= S/2 likelihood=0; else likelihood=1/S; end particle(i).w=particle(i).w*prod(likelihood); end end
Non homogeneous heat equation
I have this code which solves the heat equation with non homogeneous boundary conditons: $$u_t=ku_{xx}$$ where k is the diffusion coefficient but now I want to add a constant which makes the equation non homogeneous, like this: $$u_t=ku_{xx}+h$$ function [u,er]=ftcs(t0,tf,nt,a,b,nx,ci,cca,ccb,dif,cada,sol) % explicit method %u solution %er vector error %t0 y tf time limits %nt %nx %a b x value %ci initial condition %cca y ccb boundary conditions %dif diffusion coefficient %cada time lapse %sol solution if it's known x=linspace(a,b,nx); x=x'; dx=x(2)-x(1); %h t=linspace(t0,tf,nt); t=t'; dt=t(2)-t(1); %k r=dt/dx^2*dif; if r>.5 clc disp('stability not satisfied') pause end di=1-2*r; cca=inline(cca,'t'); ccb=inline(ccb,'t'); A=diag(di*ones(nx-2,1))+diag(r*ones(nx-3,1),1)+diag(r*ones(nx-3,1),-1); A=[[r;zeros(nx-3,1)] A [zeros(nx-3,1);r]]; ci=vectorize(inline(ci,'x')); u=ci(x); % vectorwith a solution for t=0, initial condition gra=plot(x,u); axis([a b min(u) max(u)]) pause z=u; u(1)=cca(t(1)); u(end)=ccb(t(1)); for i=1:nt-1; u=A*u; u=[cca(t(i+1)); u ; ccb(t(i+1))]; if mod(i,cada)==0 set(gra,'ydata',u'); pause(.1) z(:,end+1)=u; end end if nargin==12 & nargout==2 sol=vectorize(inline(sol,'x','t')); er=u-sol(x,tf); end pause figure surfc(z);shading interp; colormap(hot);set(gca,'ydir','reverse'); rotate3d
How calculate integration of multiplying two-looped function in matlab
I have a function that changes by loop , as below f(x,y)=(i-1)*x+(j-1)*y g(x,y)=(i+1)*x+(j+1)*y The real function is more Complex than this, however/ I want to calculate the integration of f(x,y)*g(x,y) when i & j are changed by the loop. I want to calculate like below in matlab, but this code isn't correct. for i=1:n for j=1:n h=#(x,y)(f*g) % f & g are functions int=integral2(h,xmin,xmax,ymin,ymax); end end function m=f(x,y,i,j) m=(i-1)*x+(j-1)*y end
calculate discrete S transform for given discrete time series
let us consider following Page : http://djj.ee.ntu.edu.tw/S_Transform.pdf paragraph 2.3 The Discrete S Transform let say that we have sampled version of signal x, and given sampling frequency fs,i have calculated discrete Fourier transform using following code function y=DFT(x); N=length(x); D=zeros(N,N); for k=1:N for n=1: N D(k,n)=exp((-j*(k-1)*2*pi*(n-1))/N); end end y=D*x'/N; end and started to estimate discrete S transform function [S]=discrete_s_transform(x,fs); %compute discrete s transform %fs-sampling frequency N=length(x); % length of signal T=1/fs; % sampling period Y=DFT(X); how can i continue related to this part ? clearly loops are not problem to implement,just they go from 1 to N instead of 0 to N-1 because of matlab vectors are 1 based,but what about main code?multiplication to exponential? coudl you please help me to finish S transform ?
Your sum depends only on m so I assume that other parameters as well as function H((m+n)/(NT)) are defined. Via for-loops the simplest one is: function [S]=discrete_s_transform(x); N=length(x); % length of signal S=0; m=0; for i=1:N S=S+H((m+n)/(NT))*exp(a)*exp(b*m/N); m=m+1; end Hope that helps!
Use Butterworth filter and recur function to filter Audio File.
I'm trying to filter a short audio file using a Butter worth filter and then a function to recursively solve the difference equation using the coefficients given by Butter.m. My code is as follows. [x,Fs]=wavread('bugsbunny1'); wn=2500/(Fs/2); n=10; [B,A]=butter(n,wn); C=zeros(1,10); for n=2:10 C(n-1)=-A(n); %rearrange A for the recur function end A=C; x=x'; % make x a row vector n=11:length(x); y0=zeros(1,11); x0=zeros(1,11); y1=recur(A,B,n,x,x0,y0); %calculate approximation recursively y1=[y0 y1]; %add initial conditions to vector doing this results in y1 being a matrix of invalid data given by 'NaN', any help is appreciated thanks. Code for the recur function: function [ y ] = recur(a,b,n,x,x0,y0) %Use Recursion to approximate solution to first order differential equation % Detailed explanation goes here N=length(a); M=length(b)-1; y=[y0 zeros(1,length(n))]; x=[x0 x]; a1=a(length(a):-1:1); b1=b(length(b):-1:1); for i=N+1:N+length(n), y(i)=-a1*y(i-N:i-1)'+b1*x(i-N:i-N+M)'; end y=y(N+1:N+length(n));