Quantcast
Channel: 英特尔开发人员专区文章
Viewing all articles
Browse latest Browse all 583

使用英特尔® 数据分析加速库对英特尔® 至强® 处理器进行性能优化

$
0
0

摘要

本文对英特尔® 至强® 金牌处理器的性能进行了比较研究,选取了《人工智能:一种现代方法》(AIMA)教材(作者:Stuart Russell 和 Peter Norvig)中的朴素贝叶斯算法和 scikit-learn*(SkLearn),并运行了 PyDAAL 编程接口,以显示英特尔® 数据分析加速库(英特尔® DAAL)的优势。在英特尔® 至强® 处理器中计算并比较了上述不同类别朴素贝叶斯分类器的准确度。通过观察发现,PyDAAL(多项)中朴素贝叶斯的性能要明显优于 SkLearn 和 AIMA 的性能。我们还发现,相比 AIMA,SkLearn 的性能更高。

测试与系统配置

环境设置

我们使用以下环境设置运行代码,并确定测试处理器的性能。

处理器系统内核存储(RAM)Python* 版本PyDAAL 版本
英特尔®  至强®  金牌 6128 处理器 3.40 GHzCentOS*(7.4.1708)2492 GB3.6.22018.0.0.20170814

测试设置

我们使用以下规则和方法执行测试,并比较数值:

  • 为了运行 PyDAAL 中的朴素贝叶斯分类器,我们使用了 Conda* 虚拟环境。
  • AIMA 中描述的朴素贝叶斯分类器由 GitHub* 代码中的 learning_apps.ipynb文件提供。
  • 使用 AIMA 中的朴素贝叶斯学习方法计算 learning_apps.ipynb(转换为 .py)的平均执行时间和准确度。
  • 使用 SkLearn 和 PyDAAL 中的朴素贝叶斯分类器计算 learning_apps.ipynb(转换为 .py)的平均执行时间和准确度。
  • 为了计算平均执行时间,使用以下 Linux* time 命令:
    • 示例: time(cmd="python learning_apps.py"; for i in $(seq 10); do $cmd; done)
    • 平均执行时间=时间/10。
  • 为了计算准确度,在所有情况下均使用 SkLearn 中的 accuracy_score 方法。
  • 性能提升百分比= ((AIMA - PyDAAL)/AIMA)× 100 或者((SkLearn - PyDAAL)/SkLearn)× 100。
  • 性能提升(比值)= AIMA(s)/PyDAAL(s) 或者 Sklearn (s)/PyDAAL(s)。
  • 性能提升百分比的值越高,表明 PyDAAL 的性能越好。
  • 性能提升(比)值高于 1,表明 PyDAAL 的性能更好。
  • 仅比较 learning_apps.ipynb文件中的朴素贝叶斯部分。

代码和条件概率

将 AIMA 中给出的朴素贝叶斯学习方法代码部分与 SkLearn(高斯和多项)和 PyDAAL(多项)中的相应实施进行比较。以下是相关代码示例:

AIMA
from learning import

temp_train_lbl = train_lbl.reshape((60000,1))
training_examples = np.hstack((train_img, temp_train_lbl))

MNIST_DataSet = DataSet(examples=training_examples, distance=manhattan_distance)
nBD = NaiveBayesLearner(MNIST_DataSet, continuous=False)
y_pred = np.empty(len(test_img),dtype=np.int)
for i in range (0,len(test_img)-1):
y_pred[i] = nBD(test_img[i])

temp_test_lbl = test_lbl.reshape((10000,1))
temp_y_pred_np = y_pred.reshape((10000,1))
SkLearn(高斯)
from sklearn.naive_bayes import GaussianNB

classifier=GaussianNB()
classifier = classifier.fit(train_img, train_lbl)

churn_predicted_target=classifier.predict(test_img)
SkLearn(多项)
from sklearn.naive_bayes import MultinomialNB

classifier=MultinomialNB()
classifier = classifier.fit(train_img, train_lbl)

churn_predicted_target=classifier.predict(test_img)
PyDAAL(多项)
from daal.data_management import HomogenNumericTable, BlockDescriptor_Float64, readOnly
from daal.algorithms import classifier
from daal.algorithms.multinomial_naive_bayes import training as nb_training
from daal.algorithms.multinomial_naive_bayes import prediction as nb_prediction

def getArrayFromNT(table, nrows=0):
bd = BlockDescriptor_Float64()
if nrows == 0:
nrows = table.getNumberOfRows()
table.getBlockOfRows(0, nrows, readOnly, bd)
npa = np.copy(bd.getArray())
table.releaseBlockOfRows(bd)
return npa

temp_train_lbl = train_lbl.reshape((60000,1))
train_img_nt = HomogenNumericTable(train_img)
train_lbl_nt = HomogenNumericTable(temp_train_lbl)
temp_test_lbl = test_lbl.reshape((10000,1))
test_img_nt = HomogenNumericTable(test_img)
nClasses=10
nb_train = nb_training.Online(nClasses)

# Pass new block of data from the training data set and dependent values to the algorithm
nb_train.input.set(classifier.training.data, train_img_nt)
nb_train.input.set(classifier.training.labels, train_lbl_nt)
# Update ridge regression model
nb_train.compute()
model = nb_train.finalizeCompute().get(classifier.training.model)

nb_Test = nb_prediction.Batch(nClasses)
nb_Test.input.setTable(classifier.prediction.data,  test_img_nt)
nb_Test.input.setModel(classifier.prediction.model, model)
predictions = nb_Test.compute().get(classifier.prediction.prediction)

predictions_np = getArrayFromNT(predictions)

aima-python-master”的“learning_apps.ipynb”被用作本实验的参考代码。该文件以传统的方式使用“朴素贝叶斯分类器”实施了 MNIST 数据集的分类。但是这种数据分类方法消耗了大量时间。

为了检查是否改进了性能,使用面向 Python* 的高性能数据分析库(PyDAAL)实施了相同的实验。在该实验中,数据结构主要使用“NumericTables”(一种通用数据类型)表示内存中的数据。

在代码中,使用“load_MNIST()”函数将数据加载为 train_imgtrain_lbltest_imgtest_lbl。“train_img”和“test_img” 表示训练数据和测试数据,train_lbltest_lbl表示用于训练和测试的标签。检查“C 连续”属性后,这些输入数据被转换为“HomogenNumericTable”。这样做的原因是只有在输入数据为“C 连续”时,才会发生转换。

创建一个算法对象(nb_train),用于在在线处理模式中训练多项朴素贝叶斯模型。使用“nb_train”算法对象中的“input.set”成员方法设置两种输入片段-数据和标签。此外,使用“compute()”方法更新部分模型。创建模型后,对测试对象(nb_Test)进行了定义。分别使用 input.setTable()nbTest.input.setModel()方法将测试数据集和经过训练的模型传输至算法。使用“compute()”方法得到预测结果后,实验准确度和用时也通过计算得出。使用 Linux 中的“SkLearn”库和“time”命令进行上述计算。

使用 SkLearn 中的“多项朴素贝叶斯”再次实施相同的代码,以比较传统方法和 PyDAAL。

通过分析实验用时可以清楚地发现,相比其他方法,PyDAAL 拥有更好的时间性能。

  • AIMA 中的条件概率分布假设为
    • 由观察和计数示例形成的概率分布。
    • 如果 p 为该类别的实例,o 为观测值,共有 3 个主要运算:
      • p.add(o)增加 1 个观测数量。
      • p.sample()从分布中返回一个随机元素。
      • p[o]返回 o 的概率(与常规 ProbDist 相同)。
  • 高斯朴素贝叶斯中的条件概率分布假设为高斯/正态分布。
  • 多项朴素贝叶斯中的条件概率分布假设为多项分布。

简介

在测试中,使用英特尔® 至强® 金牌处理器运行 AIMA、SkLearn(高斯和多项)和 PyDAAL(多项)中的朴素贝叶斯。为了确定处理器的性能提升,我们比较所有相关场景的准确率。我们还计算出与其他方法相比,PyDAAL 的性能提升(比值)。朴素贝叶斯(高斯)不包含在计算中,假定它更适合比较 SkLearn 和 PyDAAL 的多项版本。

观察

英特尔® DAAL借助批量、在线和分布式处理的计算模式面向所有数据分析阶段(预处理、转换、分析、建模、验证和决策制定)提供高度优化的算法构建模块,有助于提升大数据分析的速度。

  • 帮助应用以更快的速度交付更好的预测
  • 使用相同的计算资源分析更大的数据集
  • 优化数据收集和算法计算,以实现最佳性能
  • 支持线下、流和分布式使用模式,以满足不同的应用需求
  • 提供优先支持-与英特尔工程师就技术问题进行私下沟通

准确度

我们运行了 AIMA 中的朴素贝叶斯学习方法,并发现 PyDAAL 和 SkLearn(多项)的准确率相同(请参阅测试与系统配置)。

图 1 显示了朴素贝叶斯的精度值。

graph of accuracy values
图 1.英特尔® 至强® 金牌 6128 处理器-精度值表。

Benchmark results were obtained prior to the implementation of recent software patches and firmware updates intended to address exploits referred to as "Spectre" and "Meltdown". Implementation of these updates may make these results inapplicable to your device or system.

Software and workloads used in performance tests may have been optimized for performance only on Intel microprocessors. Performance tests, such as SYSmark and MobileMark, are measured using specific computer systems, components, software, operations and functions. Any change to any of those factors may cause the results to vary. You should consult other information and performance tests to assist you in fully evaluating your contemplated purchases, including the performance of that product when combined with other products. For more information, see Performance Benchmark Test Disclosure.

配置:英特尔® 至强® 金牌 6128 处理器 3.40 GHz;系统:CentOS*(7.4.1708);内核数量:24 个;存储(RAM):92 GB;Python* 版本:3.6.2;PyDAAL 版本:2018.0.0.20170814。
性能指标评测资料来源:英特尔公司。更多注释和声明请参阅下方。1

性能提升

已计算出朴素贝叶斯(AIMA 和 PyDAAL,以及 SkLearn 和 PyDAAL)的时间性能提升(比值),通过观察发现,PyDAAL 上的性能(请参阅测试与系统配置)更高。

图 2 和图 3 提供了性能提升加速值。

AIMA versus PyDAAL
图 2.英特尔® 至强® 金牌 6128 处理器- AIMA 与 PyDAAL 性能提升对比图。

Benchmark results were obtained prior to the implementation of recent software patches and firmware updates intended to address exploits referred to as "Spectre" and "Meltdown". Implementation of these updates may make these results inapplicable to your device or system.

Software and workloads used in performance tests may have been optimized for performance only on Intel microprocessors. Performance tests, such as SYSmark and MobileMark, are measured using specific computer systems, components, software, operations and functions. Any change to any of those factors may cause the results to vary. You should consult other information and performance tests to assist you in fully evaluating your contemplated purchases, including the performance of that product when combined with other products. For more information, see Performance Benchmark Test Disclosure.

配置:英特尔® 至强® 金牌 6128 处理器 3.40 GHz;系统:CentOS*(7.4.1708);内核数量:24 个;存储(RAM):92 GB;Python* 版本:3.6.2;PyDAAL 版本:2018.0.0.20170814。
性能指标评测资料来源:英特尔公司。更多注释和声明请参阅下方。1

SkLearn versus PyDAAL
图 3.英特尔® 至强® 金牌 6128 处理器- SkLearn 与 PyDAAL 性能提升对比图。

Benchmark results were obtained prior to the implementation of recent software patches and firmware updates intended to address exploits referred to as "Spectre" and "Meltdown". Implementation of these updates may make these results inapplicable to your device or system.

Software and workloads used in performance tests may have been optimized for performance only on Intel microprocessors. Performance tests, such as SYSmark and MobileMark, are measured using specific computer systems, components, software, operations and functions. Any change to any of those factors may cause the results to vary. You should consult other information and performance tests to assist you in fully evaluating your contemplated purchases, including the performance of that product when combined with other products. For more information, see Performance Benchmark Test Disclosure.

配置:英特尔® 至强® 金牌 6128 处理器 3.40 GHz;系统:CentOS*(7.4.1708);内核数量:24 个;存储(RAM):92 GB;Python* 版本:3.6.2;PyDAAL 版本:2018.0.0.20170814。
性能指标评测资料来源:英特尔公司。更多注释和声明请参阅下方。1

总结

英特尔至强金牌处理器上的优化测试显示,相比 AIMA 和 SkLearn,PyDAAL 花费了更少的时间(见图 4),因此提供了更高的性能(请参阅测试与系统配置)。在本场景中,SkLearn(多项)和 PyDAAL 拥有相同的准确度。AIMA 中的条件概率分布假设是一个简单的距离测量。但是,SkLearn 和 PyDAAL 中的假设为高斯分布或多项,这就是我们观测到准确度差异的原因。

graph of performance time.
图 4.英特尔® 至强® 金牌 6128 处理器-性能时间图。

Benchmark results were obtained prior to the implementation of recent software patches and firmware updates intended to address exploits referred to as "Spectre" and "Meltdown". Implementation of these updates may make these results inapplicable to your device or system.

Software and workloads used in performance tests may have been optimized for performance only on Intel microprocessors. Performance tests, such as SYSmark and MobileMark, are measured using specific computer systems, components, software, operations and functions. Any change to any of those factors may cause the results to vary. You should consult other information and performance tests to assist you in fully evaluating your contemplated purchases, including the performance of that product when combined with other products. For more information, see Performance Benchmark Test Disclosure.

配置:英特尔® 至强® 金牌 6128 处理器 3.40 GHz;系统:CentOS*(7.4.1708);内核数量:24 个;存储(RAM):92 GB;Python* 版本:3.6.2;PyDAAL 版本:2018.0.0.20170814。
性能指标评测资料来源:英特尔公司。更多注释和声明请参阅下方。1

参考资料

  1. AIMA 代码:
    https://github.com/aimacode/aima-python
  2. AIMA 数据文件夹:
    https://github.com/aimacode/aima-python(单独下载)
  3. 书目:
    《人工智能:一种现代方法》(作者:Stuart Russell 和 Peter Norvig)

1性能测试中使用的软件和工作负荷可能仅在英特尔® 微处理器上进行了性能优化。诸如 SYSmark 和 MobileMark 等测试均系基于特定计算机系统、硬件、软件、操作系统及功能。上述任何要素的变动都有可能导致测试结果的变化。请参考其他信息及性能测试(包括结合其他产品使用时的运行性能)以对目标产品进行全面评估。更多信息,详见 www.intel.cn/content/www/cn/zh/benchmarks/benchmark.html

Intel’s compilers may or may not optimize to the same degree for non-Intel microprocessors for optimizations that are not unique to Intel microprocessors. These optimizations include SSE2, SSE3, and SSSE3 instruction sets and other optimizations. Intel does not guarantee the availability, functionality, or effectiveness of any optimization on microprocessors not manufactured by Intel. Microprocessor-dependent optimizations in this product are intended for use with Intel microprocessors. Certain optimizations not specific to Intel microarchitecture are reserved for Intel microprocessors. Please refer to the applicable product User and Reference Guides for more information regarding the specific instruction sets covered by this notice. Notice revision #20110804


Viewing all articles
Browse latest Browse all 583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>