vinhkhuc / JFastText

Java interface for fastText

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The results are different.

pengqiuyuan opened this issue · comments

Use the same model weibo.bin

test one:

admindeiMac:fastText admin$ ./fasttext supervised -input weibo1.txt -output weibo  -lr 1.0 -epoch 35 -wordNgrams 2 -bucket 200000 -dim 50 -loss hs
admindeiMac:fastText admin$ ./fasttext test weibo.bin weibo2.txt 
N	101
P@1	0.891
R@1	0.452
admindeiMac:fastText admin$ ./fasttext  predict-prob  weibo.bin - 10
砒霜 堪比 过年 陌生 家中 负责 不会 买菜 食物 一定 一定不会 不会陌生 不能食用 买菜务必 以下常见 堪比砒霜 家中负责 常见食物 快报砒霜 砒霜蔬菜
__label__新闻内容 0.699219 __label__知识性内容 0.10588 __label__娱乐新闻 0.0444504 __label__新闻评价 0.0419737 __label__行业分析 0.0186155 __label__营销内容 0.0144202 __label__行业文章 0.0118233 __label__用户主动提及 0.00708137 __label__用户评价 0.00687072 __label__新闻调查 0.00646674

test two use JFastText:

	@Test
	public void test0() throws IOException, ParseException {	
		JFastText jft = new JFastText();     
		jft.loadModel("/Users/admin/pqy/github/fastText/weibo.bin");
		String text = "砒霜 堪比 过年 陌生 家中 负责 不会 买菜 食物 一定 一定不会 不会陌生 不能食用 买菜务必 以下常见 堪比砒霜 家中负责 常见食物 快报砒霜 砒霜蔬菜";
        List<ProbLabel> probLabels = jft.predictProba(text,10);
        System.out.println(probLabels);
        for (ProbLabel pro : probLabels) {
			System.out.println("处理之后的句子:"+text+" ,标签:"+pro.label+",打分:"+Math.exp(pro.logProb));
		}

        System.out.printf("Text: '%s'\n", text);
        for (JFastText.ProbLabel predictedProbLabel: jft.predictProba(text, 2)) {
            System.out.printf("\tlabel: '%s', probability: %f\n",
                    predictedProbLabel.label, Math.exp(predictedProbLabel.logProb));
        }
	}

result

[logProb = -0.774493, label = __label__新闻内容, logProb = -1.801197, label = __label__营销内容, logProb = -2.293256, label = __label__品牌推广, logProb = -2.761014, label = __label__活动推广, logProb = -3.310276, label = __label__其他, logProb = -3.408686, label = __label__话题推广, logProb = -3.663944, label = __label__行业文章, logProb = -4.000737, label = __label__产品推广, logProb = -4.260805, label = __label__知识性内容, logProb = -4.331049, label = __label__求助上访]
处理之后的句子:砒霜 堪比 过年 陌生 家中 负责 不会 买菜 食物 一定 一定不会 不会陌生 不能食用 买菜务必 以下常见 堪比砒霜 家中负责 常见食物 快报砒霜 砒霜蔬菜 ,标签:__label__新闻内容,打分:0.4609375365905082
处理之后的句子:砒霜 堪比 过年 陌生 家中 负责 不会 买菜 食物 一定 一定不会 不会陌生 不能食用 买菜务必 以下常见 堪比砒霜 家中负责 常见食物 快报砒霜 砒霜蔬菜 ,标签:__label__营销内容,打分:0.16510113524043515
处理之后的句子:砒霜 堪比 过年 陌生 家中 负责 不会 买菜 食物 一定 一定不会 不会陌生 不能食用 买菜务必 以下常见 堪比砒霜 家中负责 常见食物 快报砒霜 砒霜蔬菜 ,标签:__label__品牌推广,打分:0.1009372940469236
处理之后的句子:砒霜 堪比 过年 陌生 家中 负责 不会 买菜 食物 一定 一定不会 不会陌生 不能食用 买菜务必 以下常见 堪比砒霜 家中负责 常见食物 快报砒霜 砒霜蔬菜 ,标签:__label__活动推广,打分:0.06322765415162428
处理之后的句子:砒霜 堪比 过年 陌生 家中 负责 不会 买菜 食物 一定 一定不会 不会陌生 不能食用 买菜务必 以下常见 堪比砒霜 家中负责 常见食物 快报砒霜 砒霜蔬菜 ,标签:__label__其他,打分:0.03650609553075632
处理之后的句子:砒霜 堪比 过年 陌生 家中 负责 不会 买菜 食物 一定 一定不会 不会陌生 不能食用 买菜务必 以下常见 堪比砒霜 家中负责 常见食物 快报砒霜 砒霜蔬菜 ,标签:__label__话题推广,打分:0.0330846476100821
处理之后的句子:砒霜 堪比 过年 陌生 家中 负责 不会 买菜 食物 一定 一定不会 不会陌生 不能食用 买菜务必 以下常见 堪比砒霜 家中负责 常见食物 快报砒霜 砒霜蔬菜 ,标签:__label__行业文章,打分:0.025631217305801535
处理之后的句子:砒霜 堪比 过年 陌生 家中 负责 不会 买菜 食物 一定 一定不会 不会陌生 不能食用 买菜务必 以下常见 堪比砒霜 家中负责 常见食物 快报砒霜 砒霜蔬菜 ,标签:__label__产品推广,打分:0.01830215048111813
处理之后的句子:砒霜 堪比 过年 陌生 家中 负责 不会 买菜 食物 一定 一定不会 不会陌生 不能食用 买菜务必 以下常见 堪比砒霜 家中负责 常见食物 快报砒霜 砒霜蔬菜 ,标签:__label__知识性内容,打分:0.014110936696807902
处理之后的句子:砒霜 堪比 过年 陌生 家中 负责 不会 买菜 食物 一定 一定不会 不会陌生 不能食用 买菜务必 以下常见 堪比砒霜 家中负责 常见食物 快报砒霜 砒霜蔬菜 ,标签:__label__求助上访,打分:0.013153742429429115
Text: '砒霜 堪比 过年 陌生 家中 负责 不会 买菜 食物 一定 一定不会 不会陌生 不能食用 买菜务必 以下常见 堪比砒霜 家中负责 常见食物 快报砒霜 砒霜蔬菜'
	label: '__label__新闻内容', probability: 0.460938
	label: '__label__营销内容', probability: 0.165101
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.298 sec - in com.stq.FastTextTest

@vinhkhuc thank you.

	for (int i = 0; i < 100; i++) {
		log.debug("测试接口:"+ fastTextService.findByText(text));
	}

	public String findByText(String text){
		JFastText jft = new JFastText();
		jft.loadModel(weibo_zong);
		String probLabel = jft.predict(text);
		jft.unloadModel();
		return probLabel;
	}
15:48:27.266 [main] DEBUG com.stq.FastTextTest - 测试接口:__label__行业文章
15:48:27.433 [main] DEBUG com.stq.FastTextTest - 测试接口:__label__行业文章
15:48:27.599 [main] DEBUG com.stq.FastTextTest - 测试接口:__label__行业文章
15:48:27.770 [main] DEBUG com.stq.FastTextTest - 测试接口:__label__行业文章
java(21732,0x700004cd6000) malloc: *** error for object 0x400007fdf0d59ccc: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
/bin/sh: line 1: 21732 Abort trap: 6           /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/bin/java -Xmx1024M -jar /Users/admin/pqy/github/es-server/target/surefire/surefirebooter6458186226506641090.jar /Users/admin/pqy/github/es-server/target/surefire/surefire5222514888140147610tmp /Users/admin/pqy/github/es-server/target/surefire/surefire_08480922186965078148tmp

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15.605 s
[INFO] Finished at: 2017-06-23T15:48:28+08:00
[INFO] Final Memory: 50M/561M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.16:test (default-test) on project stq: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.16:test failed: The forked VM terminated without saying properly goodbye. VM crash or System.exit called ?
[ERROR] Command was/bin/sh -c cd /Users/admin/pqy/github/es-server && /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/bin/java -Xmx1024M -jar /Users/admin/pqy/github/es-server/target/surefire/surefirebooter6458186226506641090.jar /Users/admin/pqy/github/es-server/target/surefire/surefire5222514888140147610tmp /Users/admin/pqy/github/es-server/target/surefire/surefire_08480922186965078148tmp
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

@pengqiuyuan have you resolved this issue yet? I have the same problem with you and I find out that when training model, FastText added </s>word whenever it read "\n" from training file. Read readWord function for more detail.
It means that </s> exists in vocabulary and has an impact on the predict probability. When you call "./fasttext test weibo.bin weibo2.txt", the text string in "weibo2.txt" contains "\n" and function getLine replaces it with </s>
However, the value in parameter text does not contain "\n", which results in the different result. In my case, I just added "\n" at the end of text string.
Hope it helps :)

Thanks very much harttruong, that fixed my problem too.