Almost working

This commit is contained in:
2023-11-05 18:43:15 +01:00
parent 8b159f239b
commit f4928386bb
8 changed files with 327 additions and 207 deletions

View File

@@ -1,6 +1,5 @@
#include "PyClassifier.h"
#include <boost/python/numpy.hpp>
#include <torch/csrc/utils/tensor_numpy.h>
#include <iostream>
namespace pywrap {
@@ -17,6 +16,27 @@ 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);
int n = X.size(1);
auto Xn = np::from_data(X.data_ptr(), np::dtype::get_builtin<float>(), p::make_tuple(m, n), p::make_tuple(sizeof(X.dtype()) * 2 * n, sizeof(X.dtype()) * 2), p::object());
Xn = Xn.transpose();
return Xn;
}
std::pair<np::ndarray, np::ndarray> tensors2numpy(torch::Tensor& X, torch::Tensor& y)
{
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);
@@ -29,56 +49,35 @@ namespace pywrap {
{
return pyWrap->callMethodString(module, className, method);
}
void print_array(np::ndarray& array)
{
std::cout << "Array: " << std::endl;
std::cout << p::extract<char const*>(p::str(array)) << std::endl;
}
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;
std::cout << "X.defined() = " << X.defined() << std::endl;
int m = X.size(0);
int n = X.size(1);
auto data_numpy = np::from_data(X.data_ptr(), np::dtype::get_builtin<float>(), p::make_tuple(m, n), p::make_tuple(sizeof(X.dtype()) * 2 * n, sizeof(X.dtype()) * 2), p::object());
data_numpy = data_numpy.transpose();
print_array(data_numpy);
CPyObject Xp = data_numpy.ptr();
auto [Xn, yn] = tensors2numpy(X, y);
CPyObject Xp = Xn.ptr();
std::cout << "PyClassifier:fit:Converting y to PyObject" << std::endl;
auto y_numpy = 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());
print_array(y_numpy);
CPyObject yp = y_numpy.ptr();
print_array(yn);
CPyObject yp = yn.ptr();
std::cout << "PyClassifier:fit:Calling fit" << std::endl;
pyWrap->fit(module, this->className, Xp, yp);
return *this;
}
torch::Tensor PyClassifier::predict(torch::Tensor& X)
{
int m = X.size(0);
int n = X.size(1);
auto data_numpy = np::from_data(X.data_ptr(), np::dtype::get_builtin<float>(), p::make_tuple(m, n), p::make_tuple(sizeof(X.dtype()) * 2 * n, sizeof(X.dtype()) * 2), p::object());
data_numpy = data_numpy.transpose();
print_array(data_numpy);
CPyObject Xp = data_numpy.ptr();
auto Xn = tensor2numpy(X);
print_array(Xn);
CPyObject Xp = 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 << "Converting X to PyObject" << std::endl;
std::cout << "X.defined() = " << X.defined() << std::endl;
//std::cout << "X.pyobj() = " << X.pyobj() << std::endl;
//PyObject* Xp = torch::utils::tensor_to_numpy(X);
auto XX = X.transpose(0, 1);
int m = XX.size(0);
int n = XX.size(1);
auto data_numpy = np::from_data(XX.data_ptr(), np::dtype::get_builtin<float>(), p::make_tuple(m, n), p::make_tuple(sizeof(XX.dtype()) * 2 * n, sizeof(XX.dtype()) * 2), p::object());
print_array(data_numpy);
CPyObject Xp = data_numpy.ptr();
std::cout << "Converting y to PyObject" << std::endl;
auto y_numpy = np::from_data(y.data_ptr(), np::dtype::get_builtin<int32_t>(), p::make_tuple(m), p::make_tuple(sizeof(y.dtype()) * 2), p::object());
CPyObject yp = y_numpy.ptr();
std::cout << "PyClassifier::Score:Converting X to PyObject" << std::endl;
auto [Xn, yn] = tensors2numpy(X, y);
CPyObject Xp = Xn.ptr();
CPyObject yp = yn.ptr();
print_array(yn);
auto result = pyWrap->score(module, className, Xp, yp);
return result;
}