Version: 9.16.0
Methods dedicated to file export

Functions

void PMMLlib::PMMLlib::fillVectorsForExport (int nInput, int nOutput, int nHidden, int normType, std::vector< double > &minInput, std::vector< double > &maxInput, std::vector< double > &minOutput, std::vector< double > &maxOutput, std::vector< double > &valW)
 Specific to NeuralNetwork. More...
 
PMMLLIB_EXPORT void PMMLlib::PMMLlib::ExportCpp (std::string file, std::string functionName, std::string header)
 
PMMLLIB_EXPORT void PMMLlib::PMMLlib::ExportFortran (std::string file, std::string functionName, std::string header)
 
PMMLLIB_EXPORT void PMMLlib::PMMLlib::ExportPython (std::string file, std::string functionName, std::string header)
 
PMMLLIB_EXPORT std::string PMMLlib::PMMLlib::ExportPyStr (std::string functionName, std::string header)
 
void PMMLlib::PMMLlib::ExportNeuralNetworkCpp (std::string file, std::string functionName, std::string header)
 Specific to NeuralNetwork. More...
 
void PMMLlib::PMMLlib::ExportNeuralNetworkFortran (std::string file, std::string functionName, std::string header)
 Specific to NeuralNetwork. More...
 
void PMMLlib::PMMLlib::ExportNeuralNetworkPython (std::string file, std::string functionName, std::string header)
 Specific to NeuralNetwork. More...
 
std::string PMMLlib::PMMLlib::ExportNeuralNetworkPyStr (std::string functionName, std::string header)
 Specific to NeuralNetwork. More...
 
void PMMLlib::PMMLlib::ExportLinearRegressionCpp (std::string, std::string, std::string)
 Specific to RegressionModel
More...
 
void PMMLlib::PMMLlib::ExportLinearRegressionFortran (std::string, std::string, std::string)
 Specific to Regression. More...
 
void PMMLlib::PMMLlib::ExportLinearRegressionPython (std::string, std::string, std::string)
 Specific to Regression. More...
 
std::string PMMLlib::PMMLlib::ExportLinearRegressionPyStr (std::string functionName, std::string header)
 Specific to Regression. More...
 

Detailed Description

Methods dedicated to file export

Function Documentation

◆ ExportCpp()

void PMMLlib::PMMLlib::ExportCpp ( std::string  file,
std::string  functionName,
std::string  header 
)

Export the current model as a function in a Cpp file.

Parameters
fileName of the file
functionNameName of the function
headerHeader of the function

Definition at line 596 of file PMMLlib.cxx.

599{
600 if ( _currentModelType == kANN )
601 ExportNeuralNetworkCpp(file,functionName, header);
602 else if ( _currentModelType == kLR )
603 {
604 ExportLinearRegressionCpp(file, functionName, header);
605 }
606 else
607 throw string("ExportCpp : PMML type not handled.");
608}
PMMLType _currentModelType
Type of the current model.
Definition: PMMLlib.hxx:83
void ExportLinearRegressionCpp(std::string, std::string, std::string)
Specific to RegressionModel
Definition: PMMLlib.cxx:2640
void ExportNeuralNetworkCpp(std::string file, std::string functionName, std::string header)
Specific to NeuralNetwork.
Definition: PMMLlib.cxx:1695
@ kANN
Definition: PMMLlib.hxx:54

References PMMLlib::kANN, and PMMLlib::kLR.

◆ ExportFortran()

void PMMLlib::PMMLlib::ExportFortran ( std::string  file,
std::string  functionName,
std::string  header 
)

Export the current model as a function in a Fortran file.

Parameters
fileName of the file
functionNameName of the function
headerHeader of the function

Definition at line 616 of file PMMLlib.cxx.

619{
620 if ( _currentModelType == kANN )
621 ExportNeuralNetworkFortran(file,functionName, header);
622 else if ( _currentModelType == kLR )
623 ExportLinearRegressionFortran(file,functionName, header);
624 else
625 throw string("ExportFortran : PMML type not handled.");
626}
void ExportLinearRegressionFortran(std::string, std::string, std::string)
Specific to Regression.
Definition: PMMLlib.cxx:2705
void ExportNeuralNetworkFortran(std::string file, std::string functionName, std::string header)
Specific to NeuralNetwork.
Definition: PMMLlib.cxx:1865

References PMMLlib::kANN, and PMMLlib::kLR.

◆ ExportLinearRegressionCpp()

void PMMLlib::PMMLlib::ExportLinearRegressionCpp ( std::string  file,
std::string  functionName,
std::string  header 
)
private

Specific to RegressionModel

Export the current model as a NeuralNetwork function in a Cpp file.

Parameters
fileName of the file
functionNameName of the function
headerHeader of the function

Definition at line 2640 of file PMMLlib.cxx.

2643{
2645
2646 // Write the file
2647 ofstream exportfile(file.c_str());
2648
2649 exportfile << "void " << functionName <<"(double *param, double *res)" << endl;
2650 exportfile << "{" << endl;
2651 // header
2652 exportfile << " ////////////////////////////// " << endl;
2653 exportfile << " //" << endl;
2654 // insert comments in header
2655 header = " // " + header;
2656 size_t pos = 0;
2657 while ((pos = header.find("\n", pos)) != std::string::npos)
2658 {
2659 header.replace(pos, 1, "\n //");
2660 pos += 5;
2661 }
2662 exportfile << header << endl;
2663 exportfile << " //" << endl;
2664 exportfile << " ////////////////////////////// " << endl << endl;
2665
2666 double intercept = 0.0;
2667 if ( HasIntercept() )
2668 {
2669 exportfile << " // Intercept"<< endl;
2670 intercept = GetRegressionTableIntercept();
2671 }
2672 else
2673 exportfile << " // No Intercept"<< endl;
2674 exportfile << " double y = " << intercept << ";";
2675 exportfile << endl << endl;
2676
2677 int nPred = GetNumericPredictorNb();
2678 for (int i=0; i<nPred; i++)
2679 {
2680 exportfile << " // Attribute : " << GetNumericPredictorName(i) << endl;
2681 exportfile << " y += param["<<i<<"]*" << GetNumericPredictorCoefficient(i) << ";";
2682 exportfile << endl << endl;
2683 }
2684 nPred = GetPredictorTermNb();
2685 for (int i=0; i<nPred; i++)
2686 {
2687 exportfile << " // Attribute : " << GetPredictorTermName(i) << endl;
2688 exportfile << " y += param["<<(i+nPred)<<"]*" << GetPredictorTermCoefficient(i) << ";";
2689 exportfile << endl << endl;
2690 }
2691
2692 exportfile << " // Return the value"<< endl;
2693 exportfile << " res[0] = y;" << endl;
2694 exportfile << "}" << endl;
2695 exportfile.close();
2696}
PMMLLIB_EXPORT double GetPredictorTermCoefficient(int pred_term_index)
(The coefficient is the value of property "coefficient")
Definition: PMMLlib.cxx:2534
PMMLLIB_EXPORT double GetRegressionTableIntercept()
Specific to RegressionModel
Definition: PMMLlib.cxx:2376
PMMLLIB_EXPORT std::string GetNumericPredictorName(int num_pred_index)
Specific to RegressionModel
Definition: PMMLlib.cxx:2438
PMMLLIB_EXPORT int GetNumericPredictorNb()
Specific to RegressionModel
Definition: PMMLlib.cxx:2392
PMMLLIB_EXPORT int GetPredictorTermNb()
Specific to RegressionModel
Definition: PMMLlib.cxx:2415
PMMLLIB_EXPORT bool HasIntercept()
Specific to RegressionModel
Definition: PMMLlib.cxx:2351
PMMLLIB_EXPORT std::string GetPredictorTermName(int num_pred_index)
Specific to RegressionModel
Definition: PMMLlib.cxx:2467
void CheckRegression()
Called in all methods specific to the RegressionModel model.
Definition: PMMLlib.cxx:2225
PMMLLIB_EXPORT double GetNumericPredictorCoefficient(int num_pred_index)
(The coefficient is the value of property "coefficient")
Definition: PMMLlib.cxx:2503

References yacsorb.CORBAEngineTest::i.

◆ ExportLinearRegressionFortran()

void PMMLlib::PMMLlib::ExportLinearRegressionFortran ( std::string  file,
std::string  functionName,
std::string  header 
)
private

Specific to Regression.

Export the current model as a NeuralNetwork function in a Fortran file.

Parameters
fileName of the file
functionNameName of the function
headerHeader of the function

Definition at line 2705 of file PMMLlib.cxx.

2708{
2710
2711 int nNumPred = GetNumericPredictorNb();
2712 int nPredTerm = GetPredictorTermNb();
2713 vector<string>strParam(nNumPred+nPredTerm);
2714 for(int i=0; i<(nNumPred+nPredTerm); i++)
2715 {
2716 strParam[i] = "P" + NumberToString(i) ;
2717 }
2718
2719 // Write the file
2720 ofstream exportfile(file.c_str());
2721
2722 exportfile << " SUBROUTINE " << functionName <<"(";
2723 for(int i=0; i<(nNumPred+nPredTerm); i++)
2724 {
2725 exportfile << strParam[i] << ", ";
2726 }
2727 exportfile << "RES)" << endl;
2728
2729 // header
2730 exportfile << "C --- *********************************************" << endl;
2731 exportfile << "C --- " << endl;
2732 // insert comments in header
2733 header = "C --- " + header;
2734 size_t pos = 0;
2735 while ((pos = header.find("\n", pos)) != std::string::npos)
2736 {
2737 header.replace(pos, 1, "\nC --- ");
2738 pos += 5;
2739 }
2740 exportfile << header << endl;
2741 exportfile << "C --- " << endl;
2742 exportfile << "C --- *********************************************" << endl << endl;
2743
2744 exportfile << " IMPLICIT DOUBLE PRECISION (P)" << endl;
2745 exportfile << " DOUBLE PRECISION RES" << endl;
2746 exportfile << " DOUBLE PRECISION Y" << endl;
2747 exportfile << endl;
2748
2749 double intercept = 0.0;
2750 if ( HasIntercept() )
2751 {
2752 exportfile << "C --- Intercept"<< endl;
2753 intercept = GetRegressionTableIntercept();
2754 }
2755 else
2756 exportfile << "C --- No Intercept"<< endl;
2757 exportfile << " Y = " << intercept << ";";
2758 exportfile << endl << endl;
2759
2760 for (int i=0; i<nNumPred; i++)
2761 {
2762 exportfile << "C --- Attribute : " << GetNumericPredictorName(i) << endl;
2763 exportfile << " Y += P["<<i<<"]*" << GetNumericPredictorCoefficient(i) << ";";
2764 exportfile << endl << endl;
2765 }
2766
2767 for (int i=0; i<nPredTerm; i++)
2768 {
2769 exportfile << "C --- Attribute : " << GetPredictorTermName(i) << endl;
2770 exportfile << " Y += P["<<(i+nNumPred)<<"]*" << GetPredictorTermCoefficient(i) << ";";
2771 exportfile << endl << endl;
2772 }
2773
2774 exportfile << "C --- Return the value"<< endl;
2775 exportfile << " RES = Y " << endl;
2776 exportfile << " RETURN" << endl;
2777 exportfile << " END" << endl;
2778 exportfile.close();
2779}
std::string NumberToString(T Number)
Definition: PMMLlib.hxx:39

References yacsorb.CORBAEngineTest::i, and PMMLlib::NumberToString().

◆ ExportLinearRegressionPyStr()

std::string PMMLlib::PMMLlib::ExportLinearRegressionPyStr ( std::string  functionName,
std::string  header 
)
private

Specific to Regression.

Export the current model as a NeuralNetwork function in a Python string.

Parameters
functionNameName of the function
headerHeader of the function

Definition at line 2805 of file PMMLlib.cxx.

2807{
2809
2810 ostringstream out;
2811
2812 // Shebang et imports
2813 out << "#!/usr/bin/env python3" << endl;
2814 out << "# -*- coding: utf-8 -*-" << endl;
2815 out << endl;
2816
2817 // Function
2818 out << "def " << functionName <<"(param):" << endl;
2819 out << endl;
2820
2821 // header
2822 out << " ############################## " << endl;
2823 out << " # " << endl;
2824 // insert comments in header
2825 header = " # " + header;
2826 size_t pos = 0;
2827 while ((pos = header.find("\n", pos)) != std::string::npos)
2828 {
2829 header.replace(pos, 1, "\n #");
2830 pos += 5;
2831 }
2832 out << header << endl;
2833 out << " # " << endl;
2834 out << " ############################## " << endl << endl;
2835
2836 double intercept = 0.0;
2837 if ( HasIntercept() )
2838 {
2839 out << " # Intercept"<< endl;
2840 intercept = GetRegressionTableIntercept();
2841 }
2842 else
2843 out << " # No Intercept"<< endl;
2844 out << " y = " << intercept << ";";
2845 out << endl << endl;
2846
2847 int nPred = GetNumericPredictorNb();
2848 for (int i=0; i<nPred; i++)
2849 {
2850 out << " # Attribute : " << GetNumericPredictorName(i) << endl;
2851 out << " y += param["<<i<<"]*" << GetNumericPredictorCoefficient(i) << ";";
2852 out << endl << endl;
2853 }
2854 nPred = GetPredictorTermNb();
2855 for (int i=0; i<nPred; i++)
2856 {
2857 out << " # Attribute : " << GetPredictorTermName(i) << endl;
2858 out << " y += param["<<(i+nPred)<<"]*" << GetPredictorTermCoefficient(i) << ";";
2859 out << endl << endl;
2860 }
2861
2862 out << " # Return the value"<< endl;
2863 out << " return [y];" << endl;
2864
2865 return out.str() ;
2866}

References yacsorb.CORBAEngineTest::i.

◆ ExportLinearRegressionPython()

void PMMLlib::PMMLlib::ExportLinearRegressionPython ( std::string  file,
std::string  functionName,
std::string  header 
)
private

Specific to Regression.

Export the current model as a NeuralNetwork function in a Python file.

Parameters
fileName of the file
functionNameName of the function
headerHeader of the function

Definition at line 2788 of file PMMLlib.cxx.

2791{
2792 string str(ExportLinearRegressionPyStr(functionName, header));
2793 // Write the file
2794 ofstream exportfile(file.c_str());
2795 exportfile << str;
2796 exportfile.close();
2797}
std::string ExportLinearRegressionPyStr(std::string functionName, std::string header)
Specific to Regression.
Definition: PMMLlib.cxx:2805

◆ ExportNeuralNetworkCpp()

void PMMLlib::PMMLlib::ExportNeuralNetworkCpp ( std::string  file,
std::string  functionName,
std::string  header 
)
private

Specific to NeuralNetwork.

Export the current model as a NeuralNetwork function in a Cpp file.

Parameters
fileName of the file
functionNameName of the function
headerHeader of the function

Definition at line 1695 of file PMMLlib.cxx.

1698{
1700
1701 // Get the different values required
1702 int nInput = GetNbInputs();
1703 int nOutput = GetNbOutputs();
1704 int nHidden = GetNbNeuronsAtLayer(0);
1705 int nNeurons = nInput+nOutput+nHidden;
1706 int nWeights = nHidden*(nInput+nOutput+1)+nOutput;
1707 int normType = GetNormalizationType();
1708 // Build min/max input/output vectors
1709 vector<double> minInput(nInput);
1710 vector<double> maxInput(nInput);
1711 vector<double> minOutput(nOutput);
1712 vector<double> maxOutput(nOutput);
1713 vector<double> valW(nWeights);
1714 fillVectorsForExport(nInput,nOutput,nHidden,normType,minInput,maxInput,minOutput,maxOutput,valW);
1715 // Write the file
1716 ofstream sourcefile(file.c_str());
1717 // ActivationFunction
1718 if( normType==0 )
1719 { // kMinusOneOne
1720 sourcefile << "#define ActivationFunction(sum) ( tanh(sum) )" << endl;
1721 }
1722 else
1723 { // kCR, kZeroOne
1724 sourcefile << "#define ActivationFunction(sum) ( 1.0 / ( 1.0 + exp( -1.0 * sum )) )" << endl;
1725 }
1726 //
1727 sourcefile << "void " << functionName <<"(double *param, double *res)" << endl;
1728 sourcefile << "{" << endl;
1729 // header
1730 sourcefile << " ////////////////////////////// " << endl;
1731 sourcefile << " //" << endl;
1732 // insert comments in header
1733 header = " // " + header;
1734 size_t pos = 0;
1735 while ((pos = header.find("\n", pos)) != std::string::npos)
1736 {
1737 header.replace(pos, 1, "\n //");
1738 pos += 5;
1739 }
1740 sourcefile << header << endl;
1741 sourcefile << " //" << endl;
1742 sourcefile << " ////////////////////////////// " << endl;
1743 sourcefile << endl;
1744 sourcefile << " int nInput = " << nInput << ";" << endl;
1745 sourcefile << " int nOutput = " << nOutput << ";" << endl;
1746 // sourcefile << " int nWeights = " << _nWeight << ";" << endl;
1747 sourcefile << " int nHidden = " << nHidden << ";" << endl;
1748 sourcefile << " const int nNeurones = " << nNeurons << ";" << endl;
1749 sourcefile << " double " << functionName << "_act[nNeurones];" << endl;
1750 sourcefile << endl;
1751 sourcefile << " // --- Preprocessing of the inputs and outputs" << endl;
1752 sourcefile << " double " << functionName << "_minInput[] = {" << endl << " ";
1753 for(int i=0 ; i<nInput ; i++)
1754 {
1755 sourcefile << minInput[i] << ", ";
1756 if( (i+1)%5==0 )
1757 sourcefile << "\n ";
1758 }
1759 if( nInput%5 != 0 )
1760 sourcefile << endl;
1761 sourcefile << " };" << endl;
1762 //
1763 sourcefile << " double " << functionName << "_minOutput[] = {" << endl << " ";
1764 sourcefile << minOutput[0] << ", ";
1765 sourcefile << " };" << endl;
1766 //
1767 sourcefile << " double " << functionName << "_maxInput[] = {" << endl << " ";
1768 for(int i=0 ; i<nInput ; i++)
1769 {
1770 sourcefile << maxInput[i] << ", ";
1771 if( (i+1)%5==0 )
1772 sourcefile << "\n ";
1773 }
1774 if( nInput%5 != 0 )
1775 sourcefile << endl;
1776 sourcefile << " };" << endl;
1777 //
1778 sourcefile << " double " << functionName << "_maxOutput[] = {" << endl << " ";
1779 sourcefile << maxOutput[0] << ", ";
1780 sourcefile << " };" << endl;
1781 // Weights vector
1782 sourcefile << endl;
1783 sourcefile << " // --- Values of the weights" << endl;
1784 sourcefile << " double " << functionName << "_valW[] = {" << endl << " ";
1785 for(int i=0 ; i<nWeights ; i++)
1786 {
1787 sourcefile << valW[i] << ", ";
1788 if ( (i+1)%5 == 0 )
1789 sourcefile << endl << " ";
1790 }
1791 sourcefile << endl << " };"<<endl;
1792 //
1793 sourcefile << " // --- Constants";
1794 sourcefile << endl;
1795 sourcefile << " int indNeurone = 0;"<<endl;
1796 sourcefile << " int CrtW;"<<endl;
1797 sourcefile << " double sum;"<<endl;
1798
1799 // couche entree
1800 sourcefile << endl;
1801 sourcefile << " // --- Input Layers"<<endl;
1802 sourcefile << " for(int i = 0; i < nInput; i++) {"<<endl;
1803 if( normType==0 )
1804 { // kMinusOneOne
1805 sourcefile << " " << functionName << "_act[indNeurone++] = 2.0 * ( param[i] - "
1806 << functionName << "_minInput[i] ) / ( " << functionName << "_maxInput[i] - "
1807 << functionName << "_minInput[i] ) - 1.0;"<<endl;
1808 }
1809 else
1810 { // kCR, kZeroOne
1811 sourcefile << " " << functionName << "_act[indNeurone++] = ( param[i] - "
1812 << functionName << "_minInput[i] ) / " << functionName << "_maxInput[i];"
1813 << endl;
1814 }
1815 sourcefile << " }"<<endl;
1816
1817
1818 // couche cachee
1819 sourcefile << endl;
1820 sourcefile << " // --- Hidden Layers"<<endl;
1821 sourcefile << " for (int member = 0; member < nHidden; member++) {"<<endl;
1822 sourcefile << " int CrtW = member * ( nInput + 2) + 2;" << endl;
1823 sourcefile << " sum = " << functionName << "_valW[CrtW++];" << endl;
1824 sourcefile << " for (int source = 0; source < nInput; source++) {" << endl;
1825 sourcefile << " sum += " << functionName << "_act[source] * " << functionName << "_valW[CrtW++];" << endl;
1826 sourcefile << " }" << endl;
1827 sourcefile << " " << functionName << "_act[indNeurone++] = ActivationFunction(sum);" << endl;
1828 sourcefile << " }"<<endl;
1829 // couche sortie
1830 sourcefile << endl;
1831 sourcefile << " // --- Output"<<endl;
1832 sourcefile << " for (int member = 0; member < nOutput; member++) {"<<endl;
1833 sourcefile << " sum = " << functionName << "_valW[0];"<<endl;
1834 sourcefile << " for (int source = 0; source < nHidden; source++) {"<<endl;
1835 sourcefile << " CrtW = source * ( nInput + 2) + 1;"<<endl;
1836 sourcefile << " sum += " << functionName << "_act[nInput+source] * " << functionName << "_valW[CrtW];"<<endl;
1837 sourcefile << " }"<<endl;
1838 sourcefile << " " << functionName << "_act[indNeurone++] = sum;"<<endl;
1839 if( normType==0 )
1840 { // kMinusOneOne
1841 sourcefile << " res[member] = " << functionName
1842 << "_minOutput[member] + 0.5 * ( " << functionName
1843 << "_maxOutput[member] - " << functionName
1844 << "_minOutput[member] ) * ( sum + 1.0);" << endl;
1845 }
1846 else
1847 { // kCR, kZeroOne
1848 sourcefile << " res[member] = " << functionName
1849 << "_minOutput[member] + " << functionName
1850 << "_maxOutput[member] * sum;" << endl;
1851 }
1852 sourcefile << " }"<<endl;
1853 //
1854 sourcefile << "}" << endl;
1855 sourcefile.close();
1856}
PMMLLIB_EXPORT int GetNbOutputs()
Specific to NeuralNetwork.
Definition: PMMLlib.cxx:871
PMMLLIB_EXPORT int GetNbNeuronsAtLayer(int layer_index)
Definition: PMMLlib.cxx:1170
PMMLLIB_EXPORT int GetNormalizationType()
Specific to NeuralNetwork.
Definition: PMMLlib.cxx:971
void CheckNeuralNetwork()
Called in all methods specific to the NeuralNetwork model.
Definition: PMMLlib.cxx:728
PMMLLIB_EXPORT int GetNbInputs()
Specific to NeuralNetwork.
Definition: PMMLlib.cxx:847
void fillVectorsForExport(int nInput, int nOutput, int nHidden, int normType, std::vector< double > &minInput, std::vector< double > &maxInput, std::vector< double > &minOutput, std::vector< double > &maxOutput, std::vector< double > &valW)
Specific to NeuralNetwork.
Definition: PMMLlib.cxx:1602

References yacsorb.CORBAEngineTest::i.

◆ ExportNeuralNetworkFortran()

void PMMLlib::PMMLlib::ExportNeuralNetworkFortran ( std::string  file,
std::string  functionName,
std::string  header 
)
private

Specific to NeuralNetwork.

Export the current model as a NeuralNetwork function in a Fortran file.

Parameters
fileName of the file
functionNameName of the function
headerHeader of the function

Definition at line 1865 of file PMMLlib.cxx.

1868{
1870
1871 // Get the different values required
1872 int nInput = GetNbInputs();
1873 int nOutput = GetNbOutputs();
1874 int nHidden = GetNbNeuronsAtLayer(0);
1875 int nWeights = nHidden*(nInput+nOutput+1)+nOutput;
1876 int normType = GetNormalizationType();
1877 // Build min/max input/output vectors
1878 vector<double> minInput(nInput);
1879 vector<double> maxInput(nInput);
1880 vector<double> minOutput(nOutput);
1881 vector<double> maxOutput(nOutput);
1882 vector<double> valW(nWeights);
1883 fillVectorsForExport(nInput,nOutput,nHidden,normType,minInput,maxInput,minOutput,maxOutput,valW);
1884 // Write the file
1885 ofstream sourcefile(file.c_str());
1886
1887 sourcefile << " SUBROUTINE " << functionName << "(";
1888 for(int i=0 ; i<GetNbInputs() ; i++)
1889 {
1890 sourcefile << GetNameInput(i) << ",";
1891 }
1892 sourcefile << GetNameOutput(0) << ")" << endl;
1893 // header
1894 sourcefile << "C --- *********************************************" << endl;
1895 sourcefile << "C --- " << endl;
1896 // insert comments in header
1897 header = "C --- " + header;
1898 size_t pos = 0;
1899 while ((pos = header.find("\n", pos)) != std::string::npos)
1900 {
1901 header.replace(pos, 1, "\nC --- ");
1902 pos += 5;
1903 }
1904 sourcefile << header << endl;
1905 sourcefile << "C --- " << endl;
1906 sourcefile << "C --- *********************************************" << endl;
1907
1908 sourcefile << " IMPLICIT DOUBLE PRECISION (V)" << endl;
1909 for(int i=0 ; i<GetNbInputs() ; i++)
1910 {
1911 sourcefile << " DOUBLE PRECISION " << GetNameInput(i) << endl;
1912 }
1913 sourcefile << " DOUBLE PRECISION " << GetNameOutput(0) << endl;
1914 sourcefile << endl;
1915
1916 sourcefile << "C --- Preprocessing of the inputs" << endl;
1917 for(int i=0 ; i<GetNbInputs() ; i++)
1918 {
1919 sourcefile << " VXN" << GetNameInput(i) << " = ";
1920
1921 if( normType==0 )
1922 { // kMinusOneOne
1923 sourcefile << "2.D0 * ( " << GetNameInput(i) << " - " << minInput[i] << "D0 ) / " << maxInput[i] - minInput[i] << "D0 - 1.0" << endl;
1924 }
1925 else
1926 { // kCR, kZeroOne
1927 sourcefile << "( " << GetNameInput(i) << " - " << minInput[i] << "D0 ) / " << maxInput[i] << "D0" << endl;
1928 }
1929 }
1930
1931 // Weights vector
1932 sourcefile << endl;
1933 sourcefile << "C --- Values of the weights" << endl;
1934 for(int i=0 ; i<nWeights ; i++)
1935 {
1936 sourcefile << " VW" << i+1 << " = " << valW[i] << endl;
1937 }
1938 // Loop on hidden neurons
1939 sourcefile << endl;
1940 for(int member = 0; member < nHidden; member++)
1941 {
1942 sourcefile << "C --- hidden neural number " << member+1 << endl;
1943 int CrtW = member * ( nInput + 2) + 3;
1944 sourcefile << " VAct" << member+1 << " = VW" << CrtW++ << endl;
1945 for (int source = 0; source < nInput; source++)
1946 {
1947 sourcefile << " 1 + VW"<< CrtW++ << " * VXN" << GetNameInput(source) << endl;
1948 }
1949 sourcefile << endl;
1950
1951
1952 if( normType==0 )
1953 { // kMinusOneOne
1954 sourcefile << " VPot" << member+1 << " = 2.D0 / (1.D0 + DEXP(-2.D0 * VAct" << member+1 <<")) - 1.D0" << endl;
1955 }
1956 else
1957 { // kCR, kZeroOne
1958 sourcefile << " VPot" << member+1 << " = 1.D0 / (1.D0 + DEXP(-1.D0 * VAct" << member+1 <<"))" << endl;
1959 }
1960 sourcefile << endl;
1961 }
1962
1963 // Ouput of the model
1964 sourcefile << "C --- Output" << endl;
1965 sourcefile << " VOut = VW1" << endl;
1966 for(int source=0 ; source < nHidden ; source++)
1967 {
1968 int CrtW = source * ( nInput + 2) + 2;
1969 sourcefile << " 1 + VW"<< CrtW << " * VPot" << source+1 << endl;
1970 }
1971
1972 // Denormalize Output
1973 sourcefile << endl;
1974 sourcefile << "C --- Pretraitment of the output" << endl;
1975 if( normType==0 )
1976 { // kMinusOneOne
1977 sourcefile << " VDelta = " << 0.5*(maxOutput[0]-minOutput[0]) << "D0 * ( VOut + 1.0D0)" << endl;
1978 sourcefile << " " << GetNameOutput(0) << " = " << minOutput[0] << "D0 + VDelta" << endl;
1979
1980 }
1981 else
1982 { // kCR, kZeroOne
1983 sourcefile << " " << GetNameOutput(0) << " = "<< minOutput[0] << "D0 + " << maxOutput[0] << "D0 * VOut;" << endl;
1984 }
1985
1986 sourcefile << endl;
1987 sourcefile << "C --- " << endl;
1988 sourcefile << " RETURN" << endl;
1989 sourcefile << " END" << endl;
1990
1991 sourcefile.close();
1992}
PMMLLIB_EXPORT std::string GetNameOutput(int output_index)
Specific to NeuralNetwork.
Definition: PMMLlib.cxx:936
PMMLLIB_EXPORT std::string GetNameInput(int input_index)
Specific to NeuralNetwork.
Definition: PMMLlib.cxx:897

References yacsorb.CORBAEngineTest::i.

◆ ExportNeuralNetworkPyStr()

std::string PMMLlib::PMMLlib::ExportNeuralNetworkPyStr ( std::string  functionName,
std::string  header 
)
private

Specific to NeuralNetwork.

Export the current model as a function in a Python string.

Parameters
functionNameName of the function
headerHeader of the function
Returns
Function as a string

Definition at line 2020 of file PMMLlib.cxx.

2022{
2024
2025 ostringstream out;
2026
2027 // Get the different values required
2028 int nInput = GetNbInputs();
2029 int nOutput = GetNbOutputs();
2030 int nHidden = GetNbNeuronsAtLayer(0);
2031 int nNeurons = nInput+nOutput+nHidden;
2032 int nWeights = nHidden*(nInput+nOutput+1)+nOutput;
2033 int normType = GetNormalizationType();
2034 // Build min/max input/output vectors
2035 vector<double> minInput(nInput);
2036 vector<double> maxInput(nInput);
2037 vector<double> minOutput(nOutput);
2038 vector<double> maxOutput(nOutput);
2039 vector<double> valW(nWeights);
2040 fillVectorsForExport(nInput,nOutput,nHidden,normType,minInput,maxInput,minOutput,maxOutput,valW);
2041
2042 // Shebang et imports
2043 out << "#!/usr/bin/env python3" << endl;
2044 out << "# -*- coding: utf-8 -*-" << endl;
2045 out << endl;
2046 out << "from math import tanh, exp" << endl;
2047 out << endl;
2048
2049 // ActivationFunction
2050 if( normType==0 )
2051 { // kMinusOneOne
2052 out << "def ActivationFunction(sum): " << endl;
2053 out << " return tanh(sum); " << endl;
2054 }
2055 else
2056 { // kCR, kZeroOne
2057 out << "def ActivationFunction(sum): " << endl;
2058 out << " return ( 1.0 / ( 1.0 + exp( -1.0 * sum ) ) ); " << endl;
2059 }
2060
2061 out << endl;
2062 out << "def " << functionName <<"(param):" << endl;
2063 out << endl;
2064
2065 // header
2066 out << " ############################## " << endl;
2067 out << " #" << endl;
2068 // insert comments in header
2069 header = " # " + header;
2070 size_t pos = 0;
2071 while ((pos = header.find("\n", pos)) != std::string::npos)
2072 {
2073 header.replace(pos, 1, "\n #");
2074 pos += 5;
2075 }
2076 out << header << endl;
2077 out << " #" << endl;
2078 out << " ############################## " << endl;
2079 out << endl;
2080
2081 // Initialisations
2082 out << " nInput = " << nInput << ";" << endl;
2083 out << " nOutput = " << nOutput << ";" << endl;
2084 out << " nHidden = " << nHidden << ";" << endl;
2085 out << " nNeurones = " << nNeurons << ";" << endl;
2086 out << " " << functionName << "_act = [];" << endl;
2087 out << " res = [];" << endl;
2088 out << endl;
2089
2090 out << " # --- Preprocessing of the inputs and outputs" << endl;
2091 out << " " << functionName << "_minInput = [" << endl << " ";
2092 out << " " ;
2093 for(int i=0 ; i<nInput ; i++)
2094 {
2095 out << minInput[i] << ", ";
2096 if( (i+1)%5==0 )
2097 {
2098 out << endl ;
2099 out << " " ;
2100 }
2101 }
2102 out << endl << " ];" << endl;
2103
2104 out << " " << functionName << "_minOutput = [" << endl << " ";
2105 out << " " << minOutput[0] ;
2106 out << endl << " ];" << endl;
2107
2108 out << " " << functionName << "_maxInput = [" << endl << " ";
2109 for(int i=0 ; i<nInput ; i++)
2110 {
2111 out << maxInput[i] << ", ";
2112 if( (i+1)%5==0 )
2113 {
2114 out << endl;
2115 out << " " ;
2116 }
2117 }
2118 out << endl << " ];" << endl;
2119
2120 out << " " << functionName << "_maxOutput = [" << endl << " ";
2121 out << " " << maxOutput[0] ;
2122 out << endl << " ];" << endl;
2123
2124 // Weights vector
2125 out << " # --- Values of the weights" << endl;
2126 out << " " << functionName << "_valW = [" << endl << " ";
2127 for(int i=0 ; i<nWeights ; i++)
2128 {
2129 out << valW[i] << ", ";
2130 if ( (i+1)%5 == 0 )
2131 {
2132 out << endl;
2133 out << " " ;
2134 }
2135 }
2136 out << endl << " ];"<<endl;
2137
2138 out << " # --- Constants" << endl;
2139 out << " indNeurone = 0;" << endl;
2140 out << endl;
2141
2142 // couche entree
2143 out << " # --- Input Layers" << endl;
2144 out << " for i in range(nInput) :" << endl;
2145 if( normType==0 )
2146 { // kMinusOneOne
2147 out << " " << functionName << "_act.append( 2.0 * ( param[i] - "
2148 << functionName << "_minInput[i] ) / ( " << functionName << "_maxInput[i] - "
2149 << functionName << "_minInput[i] ) - 1.0 ) ;"
2150 << endl;
2151 }
2152 else
2153 { // kCR, kZeroOne
2154 out << " " << functionName << "_act.append( ( param[i] - "
2155 << functionName << "_minInput[i] ) / " << functionName << "_maxInput[i] ) ;"
2156 << endl;
2157 }
2158 out << " indNeurone += 1 ;" << endl;
2159 out << " pass" << endl;
2160
2161 // couche cachee
2162 out << endl;
2163 out << " # --- Hidden Layers" << endl;
2164 out << " for member in range(nHidden):" << endl;
2165 out << " CrtW = member * ( nInput + 2) + 2;" << endl;
2166 out << " sum = " << functionName << "_valW[CrtW];" << endl;
2167 out << " CrtW += 1 ;" << endl;
2168 out << " for source in range(nInput) :" << endl;
2169 out << " sum += " << functionName << "_act[source] * " << functionName << "_valW[CrtW];" << endl;
2170 out << " CrtW += 1 ;" << endl;
2171 out << " pass" << endl;
2172 out << " " << functionName << "_act.append( ActivationFunction(sum) ) ;" << endl;
2173 out << " indNeurone += 1 ;" << endl;
2174 out << " pass" << endl;
2175 out << endl;
2176
2177 // couche sortie
2178 out << " # --- Output"<<endl;
2179 out << " for member in range(nOutput):" << endl;
2180 out << " sum = " << functionName << "_valW[0];" << endl;
2181 out << " for source in range(nHidden):" << endl;
2182 out << " CrtW = source * ( nInput + 2) + 1;"<<endl;
2183 out << " sum += " << functionName << "_act[nInput+source] * " << functionName << "_valW[CrtW];" << endl;
2184 out << " pass" << endl;
2185 out << " " << functionName << "_act.append( sum );" << endl;
2186 out << " indNeurone += 1 ;" << endl;
2187 if( normType==0 )
2188 { // kMinusOneOne
2189 out << " res[member] = " << functionName
2190 << "_minOutput[member] + 0.5 * ( " << functionName
2191 << "_maxOutput[member] - " << functionName
2192 << "_minOutput[member] ) * ( sum + 1.0);" << endl;
2193 }
2194 else
2195 { // kCR, kZeroOne
2196 out << " res.append( " << functionName
2197 << "_minOutput[member] + " << functionName
2198 << "_maxOutput[member] * sum );" << endl;
2199 }
2200 out << " pass" << endl;
2201 out << endl;
2202
2203 // return result
2204 out << " return res;" << endl << endl;
2205 out << endl;
2206
2207 return out.str();
2208}

References yacsorb.CORBAEngineTest::i.

◆ ExportNeuralNetworkPython()

void PMMLlib::PMMLlib::ExportNeuralNetworkPython ( std::string  file,
std::string  functionName,
std::string  header 
)
private

Specific to NeuralNetwork.

Export the current model as a NeuralNetwork function in a Python file.

Parameters
fileName of the file
functionNameName of the function
headerHeader of the function

Definition at line 2001 of file PMMLlib.cxx.

2004{
2005 string str(ExportNeuralNetworkPyStr(functionName, header));
2006 // Write the file
2007 ofstream exportfile(file.c_str());
2008 exportfile << str;
2009 exportfile.close();
2010}
std::string ExportNeuralNetworkPyStr(std::string functionName, std::string header)
Specific to NeuralNetwork.
Definition: PMMLlib.cxx:2020

◆ ExportPyStr()

std::string PMMLlib::PMMLlib::ExportPyStr ( std::string  functionName,
std::string  header 
)

Export the current model as a function in a Python string.

Parameters
fileName of the file
functionNameName of the function
headerHeader of the function
Returns
Function as a string

Definition at line 653 of file PMMLlib.cxx.

655{
656 if ( _currentModelType == kANN )
657 return ExportNeuralNetworkPyStr(functionName, header);
658 else if ( _currentModelType == kLR )
659 return ExportLinearRegressionPyStr(functionName, header);
660 else
661 throw string("ExportPyStr : PMML type not handled.");
662}

References PMMLlib::kANN, and PMMLlib::kLR.

◆ ExportPython()

void PMMLlib::PMMLlib::ExportPython ( std::string  file,
std::string  functionName,
std::string  header 
)

Export the current model as a function in a Python file.

Parameters
fileName of the file
functionNameName of the function
headerHeader of the function

Definition at line 634 of file PMMLlib.cxx.

637{
638 if ( _currentModelType == kANN )
639 ExportNeuralNetworkPython(file,functionName, header);
640 else if ( _currentModelType == kLR )
641 ExportLinearRegressionPython(file,functionName, header);
642 else
643 throw string("ExportPython : PMML type not handled.");
644}
void ExportNeuralNetworkPython(std::string file, std::string functionName, std::string header)
Specific to NeuralNetwork.
Definition: PMMLlib.cxx:2001
void ExportLinearRegressionPython(std::string, std::string, std::string)
Specific to Regression.
Definition: PMMLlib.cxx:2788

References PMMLlib::kANN, and PMMLlib::kLR.

◆ fillVectorsForExport()

void PMMLlib::PMMLlib::fillVectorsForExport ( int  nInput,
int  nOutput,
int  nHidden,
int  normType,
std::vector< double > &  minInput,
std::vector< double > &  maxInput,
std::vector< double > &  minOutput,
std::vector< double > &  maxOutput,
std::vector< double > &  valW 
)
private

Specific to NeuralNetwork.

Fill the vectors used by the ExportXXX methods.

Parameters
nInput
nOutput
nHidden
normType
minInput
maxInput
minOutput
maxOutput
valW

Definition at line 1602 of file PMMLlib.cxx.

1611{
1613
1614 xmlNodePtr netNode = _currentModelNode ;
1615 // Get the different values required
1616 // Build min/max input/output vectors
1617 for(int i=0 ; i<nInput ; i++)
1618 {
1619 xmlNodePtr node_inputs = GetChildByName(netNode,"NeuralInputs");
1620 node_inputs = node_inputs->children;
1621 for(int j = 0;j<i;j++)
1622 {
1623 node_inputs = node_inputs->next;
1624 }
1625 node_inputs = node_inputs->children; // DerivedField
1626 node_inputs = node_inputs->children; // NormContinuous
1627 node_inputs = node_inputs->children; // LinearNorm
1628 string strOrig1 = _getProp(node_inputs, string("orig") );
1629 double orig1 = atof( strOrig1.c_str() );
1630 string strNorm1 = _getProp(node_inputs, string("norm") );
1631 double norm1 = atof( strNorm1.c_str() );
1632 node_inputs = node_inputs->next;
1633 string strOrig2 = _getProp(node_inputs, string("orig") );
1634 double orig2 = atof( strOrig2.c_str() );
1635 string strNorm2 = _getProp(node_inputs, string("norm") );
1636 if( normType==0 )
1637 { // kMinusOneOne
1638 minInput[i] = orig1;
1639 maxInput[i] = orig2;
1640 }
1641 else
1642 { // kCR, kZeroOne
1643 minInput[i] = orig2;
1644 maxInput[i] = -1.0*norm1*orig2;
1645 }
1646 }
1647 xmlNodePtr node_outputs = GetChildByName(netNode,"NeuralOutputs");
1648 node_outputs = node_outputs->children;
1649 node_outputs = node_outputs->children; // DerivedField
1650 node_outputs = node_outputs->children; // NormContinuous
1651 node_outputs = node_outputs->children; // LinearNorm
1652 string strOrig1 = _getProp(node_outputs, string("orig") );
1653 double orig1 = atof( strOrig1.c_str() );
1654 string strNorm1 = _getProp(node_outputs, string("norm") );
1655 double norm1 = atof( strNorm1.c_str() );
1656 node_outputs = node_outputs->next;
1657 string strOrig2 = _getProp(node_outputs, string("orig") );
1658 double orig2 = atof( strOrig2.c_str() );
1659 if( normType==0 )
1660 { // kMinusOneOne
1661 minOutput[0] = orig1;
1662 maxOutput[0] = orig2;
1663 }
1664 else
1665 { // kCR, kZeroOne
1666 minOutput[0] = orig2;
1667 maxOutput[0] = -1.0*norm1*orig2;
1668 }
1669 // Build weight vector
1670 for(int j=0 ; j<nHidden ; j++) // hidden layers
1671 {
1672 valW[j*(nInput+nOutput+1)+2] = GetNeuronBias( 0, j);
1673 for(int i=0 ; i<nInput ; i++)
1674 {
1675 valW[j*(nInput+nOutput+1)+3+i] = GetPrecNeuronSynapse( 0, j, i);
1676 }
1677 }
1678 for(int j=0 ; j<nOutput ; j++) // output layers
1679 {
1680 valW[0] = GetNeuronBias( 1, j);
1681 for(int i=0 ; i<nHidden ; i++)
1682 {
1683 valW[i*(nInput+nOutput+1)+1] = GetPrecNeuronSynapse( 1, j, i);
1684 }
1685 }
1686}
xmlNodePtr _currentModelNode
Pointer to the current model node
Definition: PMMLlib.hxx:84
std::string _getProp(const xmlNodePtr node, std::string const &prop) const
Definition: PMMLlib.cxx:694
PMMLLIB_EXPORT double GetPrecNeuronSynapse(int layer_index, int neu_index, int prec_index)
Specific to NeuralNetwork.
Definition: PMMLlib.cxx:1241
PMMLLIB_EXPORT double GetNeuronBias(int layer_index, int neu_index)
Specific to NeuralNetwork.
Definition: PMMLlib.cxx:1204
xmlNodePtr GetChildByName(xmlNodePtr node, std::string nodename)
Definition: PMMLlib.cxx:310

References yacsorb.CORBAEngineTest::i.