Working fit/score

This commit is contained in:
2023-11-06 00:46:26 +01:00
parent f4928386bb
commit 3e92372d1c
7 changed files with 65 additions and 49 deletions

View File

@@ -6,4 +6,4 @@ add_executable(main main.cc STree.cc SVC.cc PyClassifier.cc PyWrap.cc)
add_executable(example example.cpp PyWrap.cc)
target_link_libraries(main ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" ${LIBTORCH_PYTHON} Boost::boost Boost::python Boost::numpy ArffFiles)
target_link_libraries(example ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" Boost::boost Boost::python Boost::numpy ArffFiles)
target_link_libraries(example ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" ${LIBTORCH_PYTHON} Boost::boost Boost::python Boost::numpy ArffFiles)

View File

@@ -54,10 +54,10 @@ namespace pywrap {
{
std::cout << "PyClassifier:fit:Converting X to PyObject" << std::endl;
auto [Xn, yn] = tensors2numpy(X, y);
CPyObject Xp = Xn.ptr();
CPyObject Xp = boost::python::incref(boost::python::object(Xn).ptr());
std::cout << "PyClassifier:fit:Converting y to PyObject" << std::endl;
print_array(yn);
CPyObject yp = yn.ptr();
CPyObject yp = boost::python::incref(boost::python::object(yn).ptr());
std::cout << "PyClassifier:fit:Calling fit" << std::endl;
pyWrap->fit(module, this->className, Xp, yp);
return *this;
@@ -65,18 +65,18 @@ namespace pywrap {
torch::Tensor PyClassifier::predict(torch::Tensor& X)
{
auto Xn = tensor2numpy(X);
print_array(Xn);
CPyObject Xp = Xn.ptr();
CPyObject Xp = boost::python::incref(boost::python::object(Xn).ptr());
auto PyResult = pyWrap->predict(module, className, Xp);
auto result = torch::tensor({ 1,2,3 });
return result;
}
double PyClassifier::score(torch::Tensor& X, torch::Tensor& y)
{
std::cout << "PyClassifier::Score:Converting X to PyObject" << std::endl;
auto [Xn, yn] = tensors2numpy(X, y);
CPyObject Xp = Xn.ptr();
CPyObject yp = yn.ptr();
CPyObject Xp = boost::python::incref(boost::python::object(Xn).ptr());
CPyObject yp = boost::python::incref(boost::python::object(yn).ptr());
print_array(yn);
auto result = pyWrap->score(module, className, Xp, yp);
return result;

View File

@@ -1,11 +1,12 @@
#ifndef PYCLASSIFER_H
#define PYCLASSIFER_H
#include "boost/python/detail/wrap_python.hpp"
#include <boost/python/numpy.hpp>
#include <string>
#include <map>
#include <vector>
#include <utility>
#include <torch/torch.h>
#include <boost/python/numpy.hpp>
#include "PyWrap.h"
namespace pywrap {

View File

@@ -3,9 +3,9 @@
#pragma once
// Code taken and adapted from
// https ://www.codeproject.com/Articles/820116/Embedding-Python-program-in-a-C-Cplusplus-code
#include <iostream>
#include <Python.h>
#include "boost/python/detail/wrap_python.hpp"
#include <boost/python/numpy.hpp>
#include <iostream>
namespace pywrap {
namespace p = boost::python;

View File

@@ -1,6 +1,6 @@
#ifndef PYWRAP_H
#define PYWRAP_H
#include <Python.h>
#include "boost/python/detail/wrap_python.hpp"
#include <string>
#include <map>
#include <tuple>

View File

@@ -1,9 +1,11 @@
#include "boost/python/detail/wrap_python.hpp"
#include <boost/python/numpy.hpp>
#include <torch/torch.h>
#include <torch/csrc/utils/tensor_numpy.h>
#include <string>
#include <iostream>
#include "ArffFiles.h"
#include "PyHelper.hpp"
#include <boost/python/numpy.hpp>
#include "PyWrap.h"
@@ -128,24 +130,59 @@ int main(int argc, char** argv)
{
cout << "--Fit Phase--" << endl;
auto [Xn, yn] = getData(dataset);
CPyObject Xp = Xn.ptr();
CPyObject yp = yn.ptr();
auto Xn_shapes = Xn.get_shape();
auto yn_shapes = yn.get_shape();
CPyObject Xp = boost::python::incref(boost::python::object(Xn).ptr());
CPyObject yp = boost::python::incref(boost::python::object(yn).ptr());
//print_array(yn);
// Call fit
cout << "Calling fit" << endl;
wrapper->fit(moduleName, className, Xp, yp);
cout << "--Fit Phase end--" << endl;
}
// Call score
// Score
{
cout << "--Score Phase--" << endl;
auto [Xn, yn] = getData(dataset);
CPyObject Xp = Xn.ptr();
CPyObject yp = yn.ptr();
auto Xn_shapes = Xn.get_shape();
auto yn_shapes = yn.get_shape();
CPyObject Xp = boost::python::incref(boost::python::object(Xn).ptr());
CPyObject yp = boost::python::incref(boost::python::object(yn).ptr());
//print_array(yn);
// Call score
cout << "Calling score" << endl;
auto score = wrapper->score(moduleName, className, Xp, yp);
cout << "Score: " << score << endl;
auto result = wrapper->score(moduleName, className, Xp, yp);
cout << "Score: " << result << endl;
cout << "--Score Phase end--" << endl;
}
// Call score
// {
// np::initialize();
// cout << "--Score Phase--" << endl;
// auto [X, y, featuresx, classNamex, statesx] = loadDataset(dataset, true);
// auto [Xn, yn] = tensors2numpy(X, y);
// auto Xn_shapes = Xn.get_shape();
// auto yn_shapes = yn.get_shape();
// cout << "Xn_shapes: " << Xn_shapes[0] << ", " << Xn_shapes[1] << endl;
// cout << "yn_shapes: " << yn_shapes[0] << endl;
// cout << "X shapes: " << X.sizes() << endl;
// cout << "y shapes: " << y.sizes() << endl;
// assert(Xn_shapes[0] == X.sizes()[0]);
// assert(Xn_shapes[1] == X.sizes()[1]);
// assert(yn_shapes[0] == y.sizes()[0]);
// CPyObject Xp = Xn.ptr();
// CPyObject yp = yn.ptr();
// print_array(yn);
// cout << "Calling score" << endl;
// auto instance = wrapper->getClass(moduleName, className);
// CPyObject result;
// if (!(result = PyObject_CallMethod(instance, "score", "OO", Xp.getObject(), yp.getObject())))
// errorAbort("Couldn't call method score");
// auto score = PyFloat_AsDouble(result);
// //auto score = wrapper->score(moduleName, className, Xp, yp);
// cout << "Score: " << score << endl;
// cout << "--Score Phase end--" << endl;
// }
// Clean module
{
cout << "--Clean Phase--" << endl;

View File

@@ -40,27 +40,6 @@ tuple<Tensor, Tensor, vector<string>, string, map<string, vector<int>>> loadData
return { Xd, torch::tensor(y, torch::kInt32), features, className, states };
}
// int main(int argc, char* argv[])
// {
// auto [X, y, features, className, states] = loadDataset("iris", true);
// auto stree = pywrap::STree();
// stree.version();
// auto svc = pywrap::SVC();
// svc.version();
// cout << "Graph: " << stree.graph() << endl;
// stree.version();
// cout << string(80, '-') << endl;
// cout << "X: " << X.sizes() << endl;
// cout << "y: " << y.sizes() << endl;
// auto result = stree.fit(X, y, features, className, states);
// result = svc.fit(X, y, features, className, states);
// cout << "Now calling score" << endl;
// // auto result1 = stree.score(X, y);
// // auto result2 = svc.score(X, y);
// // cout << "STree score " << result1 << endl;
// // cout << "SVC score " << result2 << endl;
// return 0;
// }
int main(int argc, char* argv[])
{
cout << "* Begin." << endl;
@@ -68,17 +47,16 @@ int main(int argc, char* argv[])
auto [X, y, features, className, states] = loadDataset("iris", true);
cout << "X: " << X.sizes() << endl;
cout << "y: " << y.sizes() << endl;
cout << "y: " << y << endl;
auto clf = pywrap::PyClassifier("stree", "Stree");
cout << "STree Version: " << clf.version() << endl;
// if (true) {
// auto svc = pywrap::PyClassifier("sklearn.svm", "SVC");
// cout << "SVC Version: " << svc.callMethodString("_repr_html_") << endl;
// cout << "Calling fit" << endl;
// svc.fit(X, y, features, className, states);
// cout << "Calling score" << endl;
// cout << "SVC Score: " << svc.score(X, y) << endl;
// }
if (true) {
auto svc = pywrap::PyClassifier("sklearn.svm", "SVC");
cout << "SVC Version: " << svc.callMethodString("_repr_html_") << endl;
cout << "Calling fit" << endl;
svc.fit(X, y, features, className, states);
cout << "Calling score" << endl;
cout << "SVC Score: " << svc.score(X, y) << endl;
}
cout << "Graph: " << clf.graph() << endl;
cout << "Calling fit" << endl;
clf.fit(X, y, features, className, states);