Complete implementation but runtime failing
This commit is contained in:
@@ -9,5 +9,14 @@ find_package(Torch REQUIRED)
|
|||||||
|
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
|
||||||
|
|
||||||
|
# Temporary patch while find_package(Torch) is not fixed
|
||||||
|
file(
|
||||||
|
GLOB
|
||||||
|
LIBTORCH_PYTHON
|
||||||
|
"${TORCH_INSTALL_PREFIX}/lib/libtorch_python.so"
|
||||||
|
"${TORCH_INSTALL_PREFIX}/lib/libtorch_python.dylib"
|
||||||
|
)
|
||||||
|
message(STATUS "TORCH Libraries: ${LIBTORCH_PYTHON}")
|
||||||
|
|
||||||
add_subdirectory(lib/Files)
|
add_subdirectory(lib/Files)
|
||||||
add_subdirectory(src)
|
add_subdirectory(src)
|
@@ -2,5 +2,5 @@
|
|||||||
|
|
||||||
Python Wrapper to use Python classifiers from C++
|
Python Wrapper to use Python classifiers from C++
|
||||||
|
|
||||||
Explain issues with conda and libstdc++ and how to fix them
|
Explain issues in Linux with conda and libstdc++ and how to fix them
|
||||||
(moving conda libstdc++ to a different folder)
|
(moving conda libstdc++ to a different folder)
|
||||||
|
@@ -4,5 +4,5 @@ include_directories(${Python3_INCLUDE_DIRS})
|
|||||||
add_executable(main main.cc STree.cc SVC.cc PyClassifier.cc PyWrap.cc)
|
add_executable(main main.cc STree.cc SVC.cc PyClassifier.cc PyWrap.cc)
|
||||||
add_executable(example example.cpp)
|
add_executable(example example.cpp)
|
||||||
|
|
||||||
target_link_libraries(main ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" ArffFiles)
|
target_link_libraries(main ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" ${LIBTORCH_PYTHON} ArffFiles)
|
||||||
target_link_libraries(example "${TORCH_LIBRARIES}" ArffFiles)
|
target_link_libraries(example "${TORCH_LIBRARIES}" ArffFiles)
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
#include "PyClassifier.h"
|
#include "PyClassifier.h"
|
||||||
#include <torch/csrc/autograd/python_variable.h>
|
#include <torch/csrc/autograd/python_variable.h>
|
||||||
|
#include <torch/csrc/utils/tensor_numpy.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
namespace pywrap {
|
namespace pywrap {
|
||||||
|
|
||||||
@@ -13,7 +15,11 @@ namespace pywrap {
|
|||||||
{
|
{
|
||||||
pyWrap->clean(module, className);
|
pyWrap->clean(module, className);
|
||||||
}
|
}
|
||||||
|
PyObject* PyClassifier::toPyObject(torch::Tensor& tensor)
|
||||||
|
{
|
||||||
|
return torch::utils::tensor_to_numpy(tensor);
|
||||||
|
//return THPVariable_Wrap(tensor);
|
||||||
|
}
|
||||||
std::string PyClassifier::version()
|
std::string PyClassifier::version()
|
||||||
{
|
{
|
||||||
return pyWrap->version(module, className);
|
return pyWrap->version(module, className);
|
||||||
@@ -25,10 +31,35 @@ 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)
|
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)
|
||||||
{
|
{
|
||||||
PyObject* Xp = NULL;//THPVariable_Wrap(X);
|
std::cout << "Converting X to PyObject" << std::endl;
|
||||||
PyObject* yp = NULL;//THPVariable_Wrap(y);
|
std::cout << "X.defined() = " << X.defined() << std::endl;
|
||||||
|
//std::cout << "X.pyobj() = " << X.pyobj() << std::endl;
|
||||||
|
PyObject* Xp = toPyObject(X);
|
||||||
|
std::cout << "Converting y to PyObject" << std::endl;
|
||||||
|
PyObject* yp = toPyObject(y);
|
||||||
|
std::cout << "Calling fit" << std::endl;
|
||||||
pyWrap->fit(module, className, Xp, yp);
|
pyWrap->fit(module, className, Xp, yp);
|
||||||
|
Py_DECREF(Xp);
|
||||||
|
Py_DECREF(yp);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
torch::Tensor PyClassifier::predict(torch::Tensor& X)
|
||||||
|
{
|
||||||
|
PyObject* Xp = toPyObject(X);
|
||||||
|
auto PyResult = pyWrap->predict(module, className, Xp);
|
||||||
|
auto result = THPVariable_Unpack(PyResult);
|
||||||
|
Py_DECREF(Xp);
|
||||||
|
Py_DECREF(PyResult);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
double PyClassifier::score(torch::Tensor& X, torch::Tensor& y)
|
||||||
|
{
|
||||||
|
PyObject* Xp = toPyObject(X);
|
||||||
|
PyObject* yp = toPyObject(y);
|
||||||
|
auto result = pyWrap->score(module, className, Xp, yp);
|
||||||
|
Py_DECREF(Xp);
|
||||||
|
Py_DECREF(yp);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
} /* namespace PyWrap */
|
} /* namespace PyWrap */
|
@@ -17,6 +17,7 @@ namespace pywrap {
|
|||||||
std::string version();
|
std::string version();
|
||||||
std::string callMethodString(const std::string& method);
|
std::string callMethodString(const std::string& method);
|
||||||
private:
|
private:
|
||||||
|
PyObject* toPyObject(torch::Tensor& tensor);
|
||||||
PyWrap* pyWrap;
|
PyWrap* pyWrap;
|
||||||
std::string module;
|
std::string module;
|
||||||
std::string className;
|
std::string className;
|
||||||
|
@@ -7,8 +7,6 @@ namespace pywrap {
|
|||||||
public:
|
public:
|
||||||
STree() : PyClassifier("stree", "Stree") {};
|
STree() : PyClassifier("stree", "Stree") {};
|
||||||
~STree() = default;
|
~STree() = default;
|
||||||
private:
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} /* namespace pywrap */
|
} /* namespace pywrap */
|
||||||
|
@@ -8,8 +8,6 @@ namespace pywrap {
|
|||||||
SVC() : PyClassifier("sklearn.svm", "SVC") {};
|
SVC() : PyClassifier("sklearn.svm", "SVC") {};
|
||||||
~SVC() = default;
|
~SVC() = default;
|
||||||
std::string version();
|
std::string version();
|
||||||
private:
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} /* namespace pywrap */
|
} /* namespace pywrap */
|
||||||
|
@@ -48,5 +48,10 @@ int main(int argc, char* argv[])
|
|||||||
auto svc = pywrap::SVC();
|
auto svc = pywrap::SVC();
|
||||||
svc.version();
|
svc.version();
|
||||||
stree.version();
|
stree.version();
|
||||||
|
cout << string(80, '-') << endl;
|
||||||
|
cout << "X: " << X.sizes() << endl;
|
||||||
|
cout << "y: " << y.sizes() << endl;
|
||||||
|
auto result = svc.fit(X, y, features, className, states).score(X, y);
|
||||||
|
cout << "SVC score " << result << endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Reference in New Issue
Block a user