沉思语录

取次花丛懒回顾,半缘修道半缘君


  • 首页

  • 归档

  • 标签

  • 搜索

自然语言处理

发表于 2019-11-01 |

介绍

本文主要记录自己在学习NLP过程中的心得体会

数据集下载

  • http://statmt.org/wmt18/translation-task.html#download
  • http://www.manythings.org/anki/
  • kaggle

常用工具

  • NLTK

    1
    2
    3
    4
    5
    install NLTK Corpus.
    1) Go to http://www.nltk.org/nltk_data/ and download your desired NLTK Corpus file.
    2) Now in a Python shell check the value of nltk.data.path
    3) Choose one of the path that exists on your machine, and unzip the data files into the corpora sub directory inside.
    4) Now you can import the data from nltk.corpos import stopwords
  • Gensim

  • Tensorflow

逻辑回归与Softmax回归用于电影评论的情绪分析

问题描述

https://www.kaggle.com/c/sentiment-analysis-on-movie-reviews

基本步骤

  • 数据清洗
    利用nltk库删除非alphabet的字符
    利用nltk库删除停顿词
  • 利用tensorflow建立Softmax回归
  • 训练集合划分为训练集,验证集以及测试集
  • 训练直到验证集精度不再提高或达到指定的epoch数量

示例代码

https://github.com/Leslie-Fang/kaggle/tree/master/SentimentAnalysisonMovieReviews/SoftmaxRegression

Seq2seq

Seq2seq模型主要用于翻译,也可以用于其它任何先加码再解码的应用需求

seq2seq 模型的介绍:

https://blog.csdn.net/wangyangzhizhou/article/details/77883152
https://zhuanlan.zhihu.com/p/27608348

利用seq2seq模型进行中英文翻译:

https://audier.github.io/2018/11/08/seq2seq-model-for-translation/

seq2seq_tool用于explainable AI

https://seq2seq-vis.io/

eigen

发表于 2019-08-30 |

介绍

数值计算库,在tensorflow中作为默认的数学库

安装

直接下载即可,不需要安装
全部都是头文件

测试安装

编写 test.cc

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <Eigen/Dense>
using Eigen::MatrixXd;
int main()
{
MatrixXd m(2,2);
m(0,0) = 3;
m(1,0) = 2.5;
m(0,1) = -1;
m(1,1) = m(1,0) + m(0,1);
std::cout << m << std::endl;
}

编译:
g++ -I ./eigen/eigen-eigen-323c052e1731 test.cc -o test

计算对数值

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <Eigen/Core>
#include "Eigen/src/Core/functors/UnaryFunctors.h"
#include <iomanip>
using namespace Eigen;
using namespace std;
int main()
{
double res = Eigen::internal::scalar_log_op<double>()(280.41303540865516);
std::cout << res << std::endl;
}

修改数组

使用unaryExpr的方法修改数组中的每个元素

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <Eigen/Core>
#include "Eigen/src/Core/functors/UnaryFunctors.h"
#include <iomanip>
using namespace Eigen;
using namespace std;
int main()
{
Eigen::Matrix<float, 100, 1> tmp = Eigen::Matrix<float, 100, 1>::Constant(100, 1, 280.41303540865516);
cout << setprecision(8) << tmp << endl << "becomes: " << endl << tmp.unaryExpr(internal::scalar_log_op<float>()) << endl;
}

tensorflow的Log等去对数的函数就调用了unaryExpr方法去计算数组的每个元素的对数

1
2
#./tensorflow/core/kernels/cwise_ops_common.h:541:
Assign(d, out, in.unaryExpr(typename Functor::func()));

python调用C++

发表于 2019-08-17 |

介绍

Python调用C++时十分常见的方案,结合了Python的灵活以及C++的高效
一般的实现是将C++代码编译是动态链接库,提供Python到C++调用的API,然后将Python库打包成wheel包的形式发布

实现方案

常见的实现方案有boost.python, swig, ctypes, pybind11等
tensorflow采用的是swig的方案
boost.python为了兼容所有老的C++编译器,太重了
本文重点介绍pybind11

pybind11的实现方案介绍

setuptools定义wheel打包以及编译

使用setuptools去打包成wheel包,编写setup.py文件,定义setup函数
参考MLPerf的setup.py文件
需要编译C++代码,setup函数中定义ext_modules字段指向一个Extension对象
Extension对象里面指定了需要编译的文件,编译宏以及依赖文件
之后借助setup.py我们可以进行打包,编译C++文件,生成wheel包

1
2
3
4
5
CFLAGS="-std=c++14 -O3" python setup.py bdist_wheel
##debug version
CFLAGS="-std=c++14 -O0 -g" python setup.py bdist_wheel
可以直接用gdb去debug

Pybind11 python到C++的接口

参考介绍 以及官网
PYBIND11_MODULE 语法糖去定义一个python接口到C++接口的转换关系

1234…28
Leslie

Leslie

记录心情与能力的成长

82 日志
15 标签
© 2021 Leslie
由 Hexo 强力驱动
主题 - NexT.Pisces
本站访客数 本站总访问量