Halcon学习 多层神经网络

void testNeuralNets()
{
    //为分类和回归创建一个多层感知器
    //参数说明: NumInput输入变量的个数, NumHidden隐藏层的单元数, NumOutput输出变量的数目, OutputFunction输出层激励函数的类型,
    //Preprocessing转换特征向量的预处理类型, NumComponents转换特征的数目, RandSeed初始化多层感知器的随机数种子
    Hlong NumInput=3, NumHidden=1, NumOutput=2;
    HString OutputFunction = "softmax", Preprocessing = "normalization";
    Hlong NumComponents=3, RandSeed=42;

    HClassMlp classMlp;
    classMlp.CreateClassMlp(NumInput, NumHidden, NumOutput, OutputFunction, Preprocessing, NumComponents, RandSeed);

    //添加训练样本到指定多层感知器
    //参数说明:Features被存储的训练样本的特征向量, Target指定的类别或期望的返回向量
    HTuple Features = HTuple(0.1).Append(0.1).Append(0.1);
    Hlong Target=0;
    classMlp.AddSampleClassMlp(Features, Target);

    Features = HTuple(-0.1).Append(-0.1).Append(-0.1);
    Target = 1;
    classMlp.AddSampleClassMlp(Features, Target);


    //训练一个多层感知器
    //参数说明:MaxIterations优化算法的最大迭代次数, WeightTolerance最优算法在两次迭代的不同权重的阈值,
    //ErrorTolerance在训练数据上最优算法在两次迭代的平均错误的差值阈值,Error训练数据的平均错误, ErrorLog作为一个优化算法的迭代次数函数在训练数据上的平均错误
    HTuple MaxIterations=5, WeightTolerance=1.0, ErrorTolerance=0.01;
    HTuple Error, ErrorLog;
    Error=classMlp.TrainClassMlp(MaxIterations, WeightTolerance, ErrorTolerance, &ErrorLog);

    //计算一个特征向量的评估通过一个多层感知器
    HTuple Features1=HTuple(0.2).Append(0.1).Append(0.1), Result;
    Result = classMlp.EvaluateClassMlp(Features1);
    cout <<"根据多层感知器评估指定特征向量的结果: "<< (double)Result<<endl<<endl;

    //计算一个给定特征向量的所属类别
    //参数说明:Features特征向量, Num确定所属类的上限,Class特征向量根据多层神经网络的分类结果, Confidence分类结果的置信度
    HTuple Features2 = HTuple(-0.1).Append(-0.1).Append(-0.1), Num2 = 1;
    HTuple Class2;
    HTuple Confidence2;
    Class2 = classMlp.ClassifyClassMlp(Features2, Num2, &Confidence2);
    cout << "特征向量被分为类别: " << (Hlong)Class2 << endl;

    //读写多层神经网络模型到文件
    classMlp.WriteClassMlp("classMlp.gmc");
    HClassMlp fileclassMlp;
    fileclassMlp.ReadClassMlp("classMlp.gmc");

    //序列和逆序列化多层神经网络模型
    HSerializedItem item = classMlp.SerializeClassMlp();
    HClassMlp serialclassMlp;
    serialclassMlp.DeserializeClassMlp(item);


    //读写多层神经网络模型的训练数据到文件
    classMlp.WriteSamplesClassMlp("Mlp.mtf");
    HClassMlp fileSampleClassMlp;
    fileSampleClassMlp.CreateClassMlp(NumInput, NumHidden, NumOutput, OutputFunction, Preprocessing, NumComponents, RandSeed);
    fileSampleClassMlp.ReadSamplesClassMlp("Mlp.mtf");

    //获得多层神经网络的训练数据
    HClassTrainData classTrainData= classMlp.GetClassTrainDataMlp();

    //返回多层神经网络模型的参数
    //参数说明: NumInput输入特征向量的维度, NumHidden隐藏层的单元数, NumOutput输出变量的数目或者指定类别的个数, OutputFunction输出层激励函数的类型,
    //Preprocessing转换特征向量的预处理类型, NumComponents转换特征的数目
    Hlong NumInput2, NumHidden2, NumOutput2;
    HString OutputFunction2, Preprocessing2;
    Hlong NumComponents2;
    NumInput2 = classMlp.GetParamsClassMlp(&NumHidden2, &NumOutput2, &OutputFunction2, &Preprocessing2, &NumComponents2);
    cout << "输入特征向量的维度: " << NumInput2 << " 隐藏层的单元数: " << NumHidden2 << endl;
    cout << "指定类别的个数: " << OutputFunction << " 预处理类型: " << Preprocessing << endl;
    cout << " 转换特征的数目: " << NumComponents2 << endl;

}

你可能感兴趣的:(神经网络,halcon)