Refactor singleton to manage cleanup
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
#include "PyClassifier.h"
|
||||
#include <boost/python/numpy.hpp>
|
||||
#include <torch/csrc/autograd/python_variable.h>
|
||||
#include <torch/csrc/utils/tensor_numpy.h>
|
||||
//#include "tensorflow/python/lib/core/py_func.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace pywrap {
|
||||
@@ -13,35 +11,20 @@ namespace pywrap {
|
||||
pyWrap = PyWrap::GetInstance();
|
||||
pyWrap->importClass(module, className);
|
||||
}
|
||||
|
||||
PyClassifier::~PyClassifier()
|
||||
{
|
||||
std::cout << "Cleaning Classifier" << std::endl;
|
||||
pyWrap->clean(module, className);
|
||||
std::cout << "Classifier cleaned" << std::endl;
|
||||
}
|
||||
PyObject* PyClassifier::toPyObject(torch::Tensor& data_tensor)
|
||||
{
|
||||
|
||||
// return torch::utils::tensor_to_numpy(data_tensor);
|
||||
return THPVariable_Wrap(data_tensor);
|
||||
//auto data_numpy = np::from_data(data_tensor.data_ptr(), np::dtype::get_builtin<float>(), p::make_tuple(m, n), p::make_tuple(sizeof(data_tensor.dtype()) * 2 * n, sizeof(data_tensor.dtype()) * 2), p::object());
|
||||
// PyObject* numpyObject = data_numpy.ptr();
|
||||
|
||||
// return numpyObject;
|
||||
}
|
||||
// PyObject* PyClassifier::toPyObjecty(torch::Tensor& data_tensor)
|
||||
// {
|
||||
// //return THPVariable_Wrap(tensor);
|
||||
// auto y_numpy = np::from_data(data_tensor.data_ptr(), np::dtype::get_builtin<int32_t>(), p::make_tuple(m), p::make_tuple(sizeof(data_tensor.dtype()) * 2), p::object());
|
||||
// PyObject* numpyObject = y_numpy.ptr();
|
||||
|
||||
// }
|
||||
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);
|
||||
@@ -53,28 +36,32 @@ namespace pywrap {
|
||||
}
|
||||
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 << "Converting X to PyObject" << std::endl;
|
||||
std::cout << "PyClassifier:fit: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());
|
||||
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();
|
||||
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());
|
||||
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();
|
||||
std::cout << "Calling fit" << std::endl;
|
||||
std::cout << "PyClassifier:fit:Calling fit" << std::endl;
|
||||
pyWrap->fit(module, this->className, Xp, yp);
|
||||
return *this;
|
||||
}
|
||||
torch::Tensor PyClassifier::predict(torch::Tensor& X)
|
||||
{
|
||||
CPyObject Xp = toPyObject(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 PyResult = pyWrap->predict(module, className, Xp);
|
||||
auto result = THPVariable_Unpack(PyResult);
|
||||
auto result = torch::tensor({ 1,2,3 });
|
||||
return result;
|
||||
}
|
||||
double PyClassifier::score(torch::Tensor& X, torch::Tensor& y)
|
||||
@@ -95,5 +82,4 @@ namespace pywrap {
|
||||
auto result = pyWrap->score(module, className, Xp, yp);
|
||||
return result;
|
||||
}
|
||||
|
||||
} /* namespace PyWrap */
|
||||
} /* namespace pywrap */
|
Reference in New Issue
Block a user