Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit。

1. Job 篇

接上篇,分析到EigenVerificationJob的run方法:

public int run(Path corpusInput,
Path eigenInput,
Path output,
Path tempOut,
double maxError,
double minEigenValue,
boolean inMemory,
Configuration conf) throws IOException {
this.outPath = output;
this.tmpOut = tempOut;
this.maxError = maxError;
this.minEigenValue = minEigenValue; if (eigenInput != null && eigensToVerify == null) {
prepareEigens(conf, eigenInput, inMemory);
}
DistributedRowMatrix c = new DistributedRowMatrix(corpusInput, tempOut, 1, 1);
c.setConf(conf);
corpus = c; // set up eigenverifier and orthoverifier TODO: allow multithreaded execution eigenVerifier = new SimpleEigenVerifier(); // we don't currently verify orthonormality here.
// VectorIterable pairwiseInnerProducts = computePairwiseInnerProducts(); Map<MatrixSlice, EigenStatus> eigenMetaData = verifyEigens(); List<Map.Entry<MatrixSlice, EigenStatus>> prunedEigenMeta = pruneEigens(eigenMetaData); saveCleanEigens(new Configuration(), prunedEigenMeta);
return 0;
}

这里先明确几个输入参数,inputPath、outputPath、tempPath就不用多说了, eigenPath就是上篇最后生成的 rawEigenvectors文件;inMemory是false、maxError是0.05,minEigenValue是0.0;顺便说一下,如果直接运行这个类是不行的,比如像下面这样调用:

package mahout.fansy.svd;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.mahout.math.hadoop.decomposer.EigenVerificationJob; public class RunEigenVerificationJob { /**
* 调用EigenVerificationJob
*/
private static Path prefix=new Path("hdfs://ubuntu:9000");
public static void main(String[] args) throws IOException {
Path inputPath=new Path(prefix,"/svd/input/wine");
Path rawEigenVectorPath=new Path(prefix,"/svd/output1/rawEigenvectors");
Path outputPath=new Path(prefix,"/svd/output1/cleanEigenvectors");
Path outputTmpPath=new Path(prefix,"/svd/temp");
double maxError=0.5; double minEigenvalue=0.0;
boolean inMemory=false;
Configuration conf=new Configuration();
conf.set("mapred.job.tracker", "ubuntu:9001");
int result=new EigenVerificationJob().run(inputPath,
rawEigenVectorPath,
outputPath,
outputTmpPath,
maxError,
minEigenvalue,
inMemory,
conf);
System.out.println("result success?"+(result==0?true:false));
} }

这样调用会出现Runtime Error和file not found的错误,Runtime就不是很明白了,file not found 是因为在temp中的文件是按照时间生成的,然后如果没有和前面的任务在一个总的文件中调用,那么找到的文件目录是找不到的,所以会出现这样的错误。

接下来,首先会进入prepareEigens方法:

private void prepareEigens(Configuration conf, Path eigenInput, boolean inMemory) {
DistributedRowMatrix eigens = new DistributedRowMatrix(eigenInput, tmpOut, 1, 1);
eigens.setConf(conf);
if (inMemory) {
List<Vector> eigenVectors = Lists.newArrayList();
for (MatrixSlice slice : eigens) {
eigenVectors.add(slice.vector());
}
eigensToVerify = new SparseRowMatrix(eigenVectors.size(), eigenVectors.get(0).size(),
eigenVectors.toArray(new Vector[eigenVectors.size()]),
true,
true); } else {
eigensToVerify = eigens;
}
}

首先初始化DistributedRowMatrix变量,由于inMemory是false,所以直接把 eigens变量赋值给了eigensToVerify变量;

然后就是两个赋值语句,分别初始化corpus、eigenVerifier;然后还是初始化变量eigenMetaData,不过这个是调用了一个函数:verifyEigens来进行的;

private Map<MatrixSlice, EigenStatus> verifyEigens() {
Map<MatrixSlice, EigenStatus> eigenMetaData = Maps.newHashMap(); for (MatrixSlice slice : eigensToVerify) {
EigenStatus status = eigenVerifier.verify(corpus, slice.vector());
eigenMetaData.put(slice, status);
}
return eigenMetaData;
}

eigensToVerify遍历的是rawEigenvector里面的那三个eigenVectors;然后就调用verify方法,其中corpus是所有的输入数据(5行13列的数据),slice.vector()是eigenVectors中的一个,看verify的操作:

public EigenStatus verify(VectorIterable corpus, Vector vector) {
Vector resultantVector = corpus.timesSquared(vector);
double newNorm = resultantVector.norm(2);
double oldNorm = vector.norm(2);
double eigenValue;
double cosAngle;
if (newNorm > 0 && oldNorm > 0) {
eigenValue = newNorm / oldNorm;
cosAngle = resultantVector.dot(vector) / newNorm * oldNorm;
} else {
eigenValue = 1.0;
cosAngle = 0.0;
}
return new EigenStatus(eigenValue, cosAngle, false);
}

corpus.timesSquared(vector)就是前面的Job1,回顾下job1做的事情:就是把corpus矩阵中的每一个行向量乘以slice.vector()的转置得到的值d,然后使用原来的这个行向量中的项值乘以d在加上原值,得到更新后的行向量,在corpus中所有的行向量加起来得到最后的行向量,即是resultantVector,比如针对vector=[0.01671441233225078, 0.0935655369363106, 0.09132650234523473, -0.0680324702834075, -0.9461123439509093, 0.10210271255992123, 0.10042714365337412, 0.11137954332150339, 0.10331974823993555, 0.10621406378767596, 0.10586960137353602, 0.09262650242313884, 0.09059904726143547];原始数据还是wine,那么得到的resultantVector就是:

{0:-285.43017035605783,1:-61.30237570857193,2:-68.94124551381431,3:-520.2302762811703,4:-3232.201254912267,5:-32.31785150049481,6:-37.63572264009423,7:-12.025276244275622,8:-28.58260635344015,9:-6.8801603142200065,10:-28.491567864130573,11:-68.13521243410383,12:4382.173720122737}

excel中得到的结果是:

-217.2301704 -50.91237571 -56.33124551 -437.0302763 -2673.201255 -17.4178515 -22.39572264 -10.55527624 -18.20260635 20.93983969 -23.47156786 -51.26521243 9897.17372

看到和java计算的结果还是有点误差的,这个等下在后面补充下(主要是验证写个job1的仿制代码);

newNorm就是resultantVector自己点乘然后开根号,值为:5479.061620543984,excel中的是:10263.9596402234。汗,这个值就很不一样了,看来的确是要验证下了;oldNorm就是vector的自乘然后开根号;然后就返回了。

这里就新建了rank个Job了,所以一共有5个Job,这样job都完了。

2.验证篇:

前面说怎么后面计算的误差值那么大?按理说也只是小数点后面的误差而已,通过前面编写的TimesSquareMapperFollow来进行debug(注意这里的设置的路径,在运行RunSVD的时候需要修改DistributedRowMatrix的276、277行,把其注释掉,这样就不会删除那个临时文件了),然后就可以进行测试了,首先来看第一条输出向量的d值:

额,感觉和前面的后面8位小数点后才不一样而已;然后是计算的是第一个值:

这里就是和excel不一样,咋回事呢?回去看源代码,才发现原来不需要再加上原始值的,如下:

这样就对了。

分享,成长,快乐

转载请注明blog地址:http://blog.csdn.net/fansy1990

mahout源码分析之DistributedLanczosSolver(五)Job over的更多相关文章

  1. mahout源码分析之DistributedLanczosSolver(六)完结篇

    Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 接上篇,分析完3个Job后得到继续往下:其实就剩下两个函数了: List<Map.Entry< ...

  2. 手机自动化测试:appium源码分析之bootstrap五

    手机自动化测试:appium源码分析之bootstrap五   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.poptest测试 ...

  3. Mahout源码分析:并行化FP-Growth算法

    FP-Growth是一种常被用来进行关联分析,挖掘频繁项的算法.与Aprior算法相比,FP-Growth算法采用前缀树的形式来表征数据,减少了扫描事务数据库的次数,通过递归地生成条件FP-tree来 ...

  4. Mahout源码分析之 -- 文档向量化TF-IDF

    fesh个人实践,欢迎经验交流!Blog地址:http://www.cnblogs.com/fesh/p/3775429.html Mahout之SparseVectorsFromSequenceFi ...

  5. Android源码分析(十五)----GPS冷启动实现原理分析

    一:原理分析 主要sendExtraCommand方法中传递两个参数, 根据如下源码可以知道第一个参数传递delete_aiding_data,第二个参数传递null即可. @Override pub ...

  6. ABP源码分析二十五:EventBus

    IEventData/EventData: 封装了EventData信息,触发event的源对象和时间 IEventBus/EventBus: 定义和实现了了一系列注册,注销和触发事件处理函数的方法. ...

  7. ABP源码分析三十五:ABP中动态WebAPI原理解析

    动态WebAPI应该算是ABP中最Magic的功能之一了吧.开发人员无须定义继承自ApiController的类,只须重用Application Service中的类就可以对外提供WebAPI的功能, ...

  8. ABP源码分析四十五:ABP ZERO中的EntityFramework模块

    AbpZeroDbContext:配置ABP.Zero中定义的entity的Dbset EntityFrameworkModelBuilderExtensions:给PrimitiveProperty ...

  9. Mahout源码分析之 -- QR矩阵分解

    一.算法原理 请参考我在大学时写的<QR方法求矩阵全部特征值>,其包含原理.实例及C语言实现:http://www.docin.com/p-114587383.html 二.源码分析 这里 ...

随机推荐

  1. 使用chrales抓包IOS的https(pc+手机)

    1.安装SSL证书到手机 点击 Help -> SSL Proxying -> Install Charles Root Certificate on a Mobile Device 2. ...

  2. mysql 错误解决:Plugin 'FEDERATED' is disabled. /usr/sbin/mysqld: Table 'mysql.plugin' doesn't exist

    今天安装完Mysql后,开启发生了错误: 1.打开相应文件夹,查看错误信息: 2.打开错误信息文件,查看错误原因是:Plugin 'FEDERATED' is disabled. /usr/sbin/ ...

  3. Stream中reduce()使用记录

    一.reduce()使用1.第一个参数是我们给出的初值,2.第二个参数是累加器,可以自己用实现接口完成想要的操作,这里使用Bigdecimal的add方法 3.最后reduce会返回计算后的结果 Bi ...

  4. CentOS日志的简单介绍

    在CentOS7中,系统的日志消息由两个服务负责处理:system-journald和rsyslog. (1).常见的日志及作用 /var/log目录里存放了一些特定于系统和服务的日志文件,由rsys ...

  5. leetcode 无重复字符的最长子串 python实现

    这道题需要借助哈希查找key的O(n) 时间复杂度, 否则就会超时 初始化一个 哈希表\字典  dic 头指针start 初始为0 当前指针 cur 初始为0 最大长度变量 l 初始为0 用cur变量 ...

  6. SPOJ8791 DYNALCA LCT

    考虑\(LCT\) 不难发现,我们不需要换根... 对于操作\(1\),\(splay(u)\)然后连虚边即可 对于操作\(3\),我们可以先\(access(u)\),然后再\(access(v)\ ...

  7. 洛谷.4512.[模板]多项式除法(NTT)

    题目链接 多项式除法 & 取模 很神奇,记录一下. 只是主要部分,更详细的和其它内容看这吧. 给定一个\(n\)次多项式\(A(x)\)和\(m\)次多项式\(D(x)\),求\(deg(Q) ...

  8. Codeforces Round #279 (Div. 2) B - Queue 水题

    #include<iostream> #include<mem.h> using namespace std; ],q[]; int main() { int n,x,y; m ...

  9. 基于(Redis | Memcache)实现分布式互斥锁

    设计一个缓存系统,不得不要考虑的问题就是:缓存穿透.缓存击穿与失效时的雪崩效应. 缓存击穿 缓存穿透是指查询一个一定不存在的数据,由于缓存是不命中时被动写的,并且出于容错考虑,如果从存储层查不到数据则 ...

  10. SMB协议概述

    一.概述 SMB(Server Message Block)是由微软开发的一种软件程序级的网络传输协议,主要用来使得一个网络上的计算机共享计文件.打印机.串行端口和通讯等资源.它也提供认证的进行进程间 ...