Begin predict

This commit is contained in:
2023-11-06 15:22:27 +01:00
parent 3e92372d1c
commit ed21f90b69
8 changed files with 55 additions and 69 deletions

View File

@@ -1,5 +1,5 @@
#include "PyClassifier.h"
#include "numpy/arrayobject.h"
#include <iostream>
namespace pywrap {
@@ -16,11 +16,6 @@ namespace pywrap {
pyWrap->clean(module, className);
std::cout << "Classifier cleaned" << std::endl;
}
void print_array(np::ndarray& array)
{
std::cout << "Array: " << std::endl;
std::cout << p::extract<char const*>(p::str(array)) << std::endl;
}
np::ndarray tensor2numpy(torch::Tensor& X)
{
int m = X.size(0);
@@ -33,51 +28,63 @@ namespace pywrap {
{
int n = X.size(1);
auto yn = np::from_data(y.data_ptr(), np::dtype::get_builtin<int32_t>(), p::make_tuple(n), p::make_tuple(sizeof(y.dtype()) * 2), p::object());
//std::cout << "Printing from within tensors2numpy" << std::endl;
// print_array(yn);
return { tensor2numpy(X), yn };
}
std::string PyClassifier::version()
{
return pyWrap->version(module, className);
}
std::string PyClassifier::graph()
{
return pyWrap->graph(module, className);
}
std::string PyClassifier::callMethodString(const std::string& method)
{
return pyWrap->callMethodString(module, className, method);
}
PyClassifier& PyClassifier::fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states)
{
std::cout << "PyClassifier:fit:Converting X to PyObject" << std::endl;
auto [Xn, yn] = tensors2numpy(X, y);
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 = boost::python::incref(boost::python::object(yn).ptr());
std::cout << "PyClassifier:fit:Calling fit" << std::endl;
CPyObject Xp = p::incref(p::object(Xn).ptr());
CPyObject yp = p::incref(p::object(yn).ptr());
pyWrap->fit(module, this->className, Xp, yp);
return *this;
}
void print_array(np::ndarray& array)
{
std::cout << "Array: " << std::endl;
std::cout << p::extract<char const*>(p::str(array)) << std::endl;
}
torch::Tensor PyClassifier::predict(torch::Tensor& X)
{
int dimension = X.size(1);
auto Xn = tensor2numpy(X);
CPyObject Xp = boost::python::incref(boost::python::object(Xn).ptr());
auto PyResult = pyWrap->predict(module, className, Xp);
auto result = torch::tensor({ 1,2,3 });
CPyObject Xp = p::incref(p::object(Xn).ptr());
PyObject* incoming = pyWrap->predict(module, className, Xp);
std::cout << "Return from predict" << std::endl;
p::handle<> handle(incoming);
p::object object(handle);
np::ndarray prediction = np::from_object(object);
print_array(prediction);
// import_array();
// if (!PyArray_Check(incoming)) {
// throw std::logic_error("Returned value is not array");
// }
// std::cout << "Returned value is array" << std::endl;
// PyArrayObject* np_ret = (PyArrayObject*)incoming;
// if (PyArray_NDIM(np_ret) != dimension - 1) {
// throw std::logic_error("Returned array has wrong dimension" + std::to_string(PyArray_NDIM(np_ret)) + "!=" + std::to_string(dimension - 1));
// }
// std::cout << "Returned array has correct dimension" << PyArray_NDIM(np_ret) << std::endl;
// int len{ PyArray_SHAPE(np_ret)[0] };
// int* data = reinterpret_cast<int*>(PyArray_DATA(np_ret));
return result;
// int* data = reinterpret_cast<int*>(prediction.get_data());
// auto resultTensor = torch::tensor({ data }, torch::kInt32);
auto resultTensor = torch::zeros({ prediction.shape(0) }, torch::kInt32);
return resultTensor;
}
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 = boost::python::incref(boost::python::object(Xn).ptr());
CPyObject yp = boost::python::incref(boost::python::object(yn).ptr());
print_array(yn);
CPyObject Xp = p::incref(p::object(Xn).ptr());
CPyObject yp = p::incref(p::object(yn).ptr());
auto result = pyWrap->score(module, className, Xp, yp);
return result;
}