深度学习框架【CNTK】的安装


前言

鉴于之前介绍的深度学习框架都是基于python写的,本次学习CNTK的时候决定使用C#来尝试,而且CNTK因各种原因,暂时无法在高于3.6版本进行安装,因此决定尝试使用C#来做CNTK的开发。


一、CNTK的前世今生

Computational Network Toolkit (CNTK) 是微软出品的开源深度学习工具包,改名后叫The Microsoft Cognitive Toolkit.。
CNTK只是一个框架或者说是一套简单的工具帮助我们实现我们所涉及的深度学习或者是神经网络。其中已经集成好很多经典的算法。当然大家也可以根据实际情况去自己定义具体的算法或者输入输出的方式。

特点:

  • 支持各种神经网络模型;
  • 一个简单的配置文件配置特定网络;
  • CNTK 可以用GPU,支持CUDA编程;
  • 自动计算所需的导数;
  • 可扩展;

二、CNTK的安装

本次使用的CNTK由于是使用C# 版的,因此我这边只需要对一些CNTK的标准库进行安装即可。
本次安装的一些工具如下:

1.CNTK预训练模型

2.CNTK各版本下载【GPU/CPU】

3.Ubuntu安装python3.6版的CNTK

安装openmpi

sudo apt install libevent-dev libhwloc-dev libibverbs-dev flex gfortran
sudo apt-get install openmpi-bin openmpi-common openmpi-doc libopenmpi-dev

下载文件:添加链接描述

tar -zxvf openmpi-4.0.5.tar.gz
cd openmpi-4.0.5
./configure --prefix="/usr/local/openmpi"
sudo make
sudo make install
sudo gedit ~/.bashrc
     添加:
         export PATH="$PATH:/usr/local/openmpi/bin"
         export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/openmpi/lib/"
sudo ldconfig  或者 source ~/.bashrc
mpirun   # 测试

pip安装

文件下载:whl

三、CNTK环境测试【逻辑回归为例】

本次使用VisualStudio 2022 CNTK2.7版本弄一个测试的工程

1.准备上述dll依赖库

直接放入到工程上

2.本次工程目录结构如下

按照如下引入依赖库
在这里插入图片描述

3.程序引入命名空间

using CNTK;

4.程序主入口

static void Main(string[] args)
{
    //逻辑回归输入3个,输出2个
    int inputDim = 3;
    int numOutputClasses = 2;
    //使用GPU
    var device = DeviceDescriptor.GPUDevice(0);
    //设置输入变量及输出变量
    Variable featureVariable = Variable.InputVariable(new int[] { inputDim }, DataType.Float);
    Variable labelVariable = Variable.InputVariable(new int[] { numOutputClasses }, DataType.Float);
    //创建简单的全连接层
    var classifierOutput = CreateLinearModel(featureVariable, numOutputClasses, device);
    //使用API获得softmax的结果及误差
    var loss = CNTKLib.CrossEntropyWithSoftmax(classifierOutput, labelVariable);
    var evalError = CNTKLib.ClassificationError(classifierOutput, labelVariable);
    //学习率的设置
    TrainingParameterScheduleDouble learningRatePerSample = new TrainingParameterScheduleDouble(0.02, 1);
    IList<Learner> parameterLearners = new List<Learner>() { Learner.SGDLearner(classifierOutput.Parameters(), learningRatePerSample) };
    var trainer = Trainer.CreateTrainer(classifierOutput, loss, evalError, parameterLearners);
    //批量大小
    int minibatchSize = 64;
    //训练次数
    int numMinibatchesToTrain = 10000;
    //最小更新大小
    int updatePerMinibatches = 10;

    // 循环训练数据
    for (int minibatchCount = 0; minibatchCount < numMinibatchesToTrain; minibatchCount++)
    {
        Value features, labels;
        GenerateValueData(minibatchSize, inputDim, numOutputClasses, out features, out labels, device);
        
#pragma warning disable 618
        trainer.TrainMinibatch(new Dictionary<Variable, Value>() { { featureVariable, features }, { labelVariable, labels } }, device);
#pragma warning restore 618
        PrintTrainingProgress(trainer, minibatchCount, updatePerMinibatches);
    }

    // 测试数据
    int testSize = 100;
    Value testFeatureValue, expectedLabelValue;
    GenerateValueData(testSize, inputDim, numOutputClasses, out testFeatureValue, out expectedLabelValue, device);

    IList<IList<float>> expectedOneHot = expectedLabelValue.GetDenseData<float>(labelVariable);
    IList<int> expectedLabels = expectedOneHot.Select(l => l.IndexOf(1.0F)).ToList();

    var inputDataMap = new Dictionary<Variable, Value>() { { featureVariable, testFeatureValue } };
    var outputDataMap = new Dictionary<Variable, Value>() { { classifierOutput.Output, null } };
    classifierOutput.Evaluate(inputDataMap, outputDataMap, device);
    var outputValue = outputDataMap[classifierOutput.Output];
    IList<IList<float>> actualLabelSoftMax = outputValue.GetDenseData<float>(classifierOutput.Output);
    var actualLabels = actualLabelSoftMax.Select((IList<float> l) => l.IndexOf(l.Max())).ToList();
    int misMatches = actualLabels.Zip(expectedLabels, (a, b) => a.Equals(b) ? 0 : 1).Sum();

    Console.WriteLine($"Validating Model: Total Samples = {testSize}, Misclassify Count = {misMatches}");
    Console.ReadLine();
}

5.程序测试效果

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱学习的广东仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值