pastebin

Paste #81656: Untitled ASCII paste by 113.108.133.38

function ListNet(dataset,itertimes,learning_rate)
itertimes = 10;
learning_rate = 0.01;

mkdir(dataset,['regression']);
fprintf('Dataset: %s  \n',dataset);
fprintf('Algor: ListNet\n');
% fprintf('Folds\tNDCG@1\tNDCG@2\tNDCG@3\tNDCG@4\tNDCG@5\tNDCG@6\tNDCG@7\tNDCG@8\tNDCG@9\tNDCG@10\n');
for i1=1:5
    dname = [dataset '/Fold' num2str(i1) '/'];
    [X, Y ] = read_letor([dname '/train.txt']); 
    [Xt,Yt] = read_letor([dname '/vali.txt']);
    w = ones(1,size(X,2));


    for t=1:itertimes
         index = 1;
         for i = 1:size(Y,2)
             for j = 1:size(Y{1,i},1)
                 z(j)=exp(X(index,:)*w');
                 y(j) = exp(Y{1,i}(j,1));
                 x(j,:) = X(index,:);
                 index = index +1;
             end

             delta = (1/sum(z)).*(z*x)-(1/sum(y)).*(y*x);
             w=w-learning_rate.*delta;      
         end
    end
   %test
    [Xt,Yt] = read_letor([dname '/test.txt']);
    write_out(Xt*w',i1,'test',dataset);
    fname = [dataset '/regression/test.fold' num2str(i1) '.metric'];
    f = fopen(fname);
    for l1=1:4
        l = fgetl(f);
        if ~ischar(l), break; end; 
    end
    l = fgetl(f);
    if ~ischar(l)
        fprintf('error\n');
    else
       
        [Ndcg(i1,:),tmp] = sscanf(l,'%*s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f',[1,inf]);
        fprintf('Fold%d\t',i1);
        if size(Ndcg(i1,:),2) > 10
            sze = 10;
        else
            sze = size(Ndcg(i1,:),2);
        end
        for nd1=1:sze
            fprintf('%.4f\t',Ndcg(i1,nd1));
        end
        fprintf('\n');
    end
          
end

fprintf('average ');
ND = mean(Ndcg);
if size(ND,2) > 10
    sze = 10;
else
    sze = size(ND,2);
end
for nd1=1:sze
    fprintf('%.4f\t',ND(nd1));
end
fprintf('\n');


%????
function [X,Y] = read_letor(filename)
  f = fopen(filename);
  X = zeros(2e5,0);
  qid = '';
  i = 0; q = 0;
  while 1
    l = fgetl(f);
    if ~ischar(l), break; end;
    i = i+1; 
    [lab,  foo1, foo2, ind] = sscanf(l,'%d qid:',1); l(1:ind-1)=[];
    [nqid, foo1, foo2, ind] = sscanf(l,'%s',1); l(1:ind-1)=[]; 
    if ~strcmp(nqid,qid)
      q = q+1;
      qid = nqid;
      Y{q} = lab;
    else 
      Y{q} = [Y{q}; lab];
    end;
    tmp = sscanf(l,'%d:%f'); 
    X(i,tmp(1:2:end)) = tmp(2:2:end);
  end;
  X = X(1:i,:);
  fclose(f);

function write_out(output,i,name,dataset)
  output = output + 1e-10*randn(length(output),1); % Break ties at random
  fname = [dataset '/regression/' name '.fold' num2str(i)];
  save(fname,'output','-ascii');
  % Either copy the evaluation script in the current directory or
  % change the line below with the correct path 
  system(['perl Eval-Score-3.0.pl ' dataset '/Fold' num2str(i) '/' name ...
          '.txt ' fname ' ' fname '.metric 0']);
function map = compute_map(Y,Yt)
  ind = 0;
  for i=1:length(Yt)
    ind = ind(end)+[1:length(Yt{i})];
    [foo,ind2] = sort(-Y(ind));
    r = Yt{i}(ind2)>0;
    p = cumsum(r) ./ [1:length(r)]';
    if sum(r)> 0 
      map(i) = r'*p / sum(r);
    else
      map(i)=0;
    end;
  end;
  map=mean(map);

Private
Wrap long lines

2 + 3 =