Intel oneAPI简介
概括来说,oneAPI是一个统一和简化的编程模型,它的诞生使命,在于广泛适用于不同架构和来自不同供应商的硬件、简化跨多架构的开发过程(如CPU、GPU、FPGA、加速器)。
oneAPI 行业计划鼓励生态系统内基于oneAPI规范的合作以及兼容 oneAPI的实践。
oneAPI beta产品,包括直接编程工具(Data Parallel C++)、基于API的编程工具、以及分析工具和调试工具等组件。
Intel oneAPI 是一个跨行业、开放、基于标准的统一的编程模型,为跨 CPU、GPU、FPGA等产品的开发者提供统一的体,是Intel基于oneAPI行业计划下的一款英特尔beta 产品。开发人员从现在开始就可以在英特尔 DevCloud for oneAPI 上对基于多种英特尔架构的代码和应用进行测试。
Intel oneAPI 相关产品如 Intel oneAPI Base Toolkit、Intel oneAPI HPC Toolkit以及Intel AI Analytics Toolkit,可以在官网进行下载:
Intel DAAL介绍
英特尔®数据分析加速库(Intel® DAAL)是英特尔®架构优化的构件库,涵盖了所有数据分析阶段:从数据源获取数据、预处理、转换、数据挖掘、建模、验证和决策。有助于提升机器学习和大数据分析能力,并帮助数据工程师缩短开发高性能应用的时间。为了在一系列英特尔®处理器上实现最佳性能,英特尔® DAAL使用英特尔®数学内核库和英特尔®集成性能基元的优化算法。
Intel DAAL安装
用conda 创建一个虚拟环境,python版本选3.7用conda 安装已经编译好的 intelpython
conda install -c intel intelpython
用conda安装已经编译好的 daal4py
conda install -c intel daal4py
Speed Up ML & DL with Intel® oneAPI Data Analytics Library
安装完成后查阅DAAL API使用 Algorithms — daal4py 2021.1 documentation
代码示例
下面是KNN算法的例子
# let's try to use pandas' fast csv reader
try:
import pandas
read_csv = lambda f, c, t=np.float64: pandas.read_csv(f, usecols=c, delimiter=',', header=None, dtype=t)
except:
# fall back to numpy loadtxt
read_csv = lambda f, c, t=np.float64: np.loadtxt(f, usecols=c, delimiter=',', ndmin=2)
def main(readcsv=read_csv, method='defaultDense'):
start = time.clock() # 开始计时
# Input data set parameters
train_file = os.path.join('data', 'k_nearest_neighbors_train.csv')
predict_file = os.path.join('data', 'k_nearest_neighbors_test.csv')
# Read data. Let's use 5 features per observation
nFeatures = 5
nClasses = 5
train_data = readcsv(train_file, range(nFeatures))
train_labels = readcsv(train_file, range(nFeatures, nFeatures+1))
# Create an algorithm object and call compute
train_algo = d4p.kdtree_knn_classification_training(nClasses=nClasses)
# 'weights' is optional argument, let's use equal weights
# in this case results must be the same as without weights
weights = np.ones((train_data.shape[0], 1))
train_result = train_algo.compute(train_data, train_labels, weights)
# Now let's do some prediction
predict_data = readcsv(predict_file, range(nFeatures))
predict_labels = readcsv(predict_file, range(nFeatures, nFeatures+1))
# Create an algorithm object and call compute
predict_algo = d4p.kdtree_knn_classification_prediction()
predict_result = predict_algo.compute(predict_data, train_result.model)
# We expect less than 170 mispredicted values
assert np.count_nonzero(predict_labels != predict_result.prediction) < 170
end=time.clock() #结束计时
print("time", end-start)
return (train_result, predict_result, predict_labels)
if __name__ == "__main__":
(train_result, predict_result, predict_labels) = main()
# print(predict_result.prediction.shape)
# print(predict_labels.shape)
print("accuracy_score", accuracy_score(predict_labels, predict_result.prediction))
print("precision_score", precision_score(predict_labels, predict_result.prediction,average="macro"))
print("recall_score", recall_score(predict_labels, predict_result.prediction, average="macro"))
print("f1_score", f1_score(predict_labels, predict_result.prediction, average="macro"))
print("confusion_matrix\n", confusion_matrix(predict_labels, predict_result.prediction))
运行结果: