forked from opencog/atomspace
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pytorch tensor truth value #92
Merged
Merged
Changes from 12 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
f9614b4
Torch truth value type
noskill 209c091
cython part of TTruthValue
noskill d87d428
Merge branch 'master' of github.com:singnet/atomspace
noskill a800a6a
create_python_value_from_c_value for truth values
noskill 7bb9ce1
tests for TTruthValue
noskill 5dd3665
SimpleTruthValue cython wrapper
noskill f1b8f22
add pyton includes if available
noskill fc4b403
build torch truth value only if found cython
noskill e764225
add count to TTruthValue
noskill 89c4baa
acquire gil in TTruthValue::clone
noskill 8bdd665
link truthvalue to python
noskill f73743a
add __getitem__ to cython wrapper class
noskill ccaf035
Update opencog/cython/opencog/atomspace.pxd
vsbogd 6cb7274
rename TTruthValue -> TorchTruthValue
noskill 6681693
update test
noskill 9fb5042
Merge branch 'pytorch-tv' of github.com:noskill/atomspace into pytorc…
noskill 37582b9
Update opencog/cython/opencog/atomspace.pxd
vsbogd 57799e6
rename TorchTruthValue -> TensorTruthValue
noskill 27c0b06
Merge branch 'pytorch-tv' of github.com:noskill/atomspace into pytorc…
noskill 1564e20
add to_string to TensorTruthValue
noskill 1842367
unused import
noskill bb6e375
TensorTruthValue -> TTruthValue
noskill File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,40 @@ | ||
if(HAVE_CYTHON) | ||
INCLUDE_DIRECTORIES( | ||
${PYTHON_INCLUDE_DIRS} | ||
${CMAKE_CURRENT_SOURCE_DIR} | ||
${CMAKE_CURRENT_BINARY_DIR}) | ||
endif(HAVE_CYTHON) | ||
|
||
ADD_LIBRARY (truthvalue | ||
CountTruthValue.cc | ||
EvidenceCountTruthValue.cc | ||
FuzzyTruthValue.cc | ||
IndefiniteTruthValue.cc | ||
ProbabilisticTruthValue.cc | ||
SimpleTruthValue.cc | ||
TruthValue.cc | ||
TruthValue.cc | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whitespace issue here |
||
) | ||
|
||
if(HAVE_CYTHON) | ||
set_property(TARGET truthvalue | ||
APPEND PROPERTY SOURCES | ||
TTruthValue.cc) | ||
endif(HAVE_CYTHON) | ||
|
||
# Without this, parallel make will race and crap up the generated files. | ||
ADD_DEPENDENCIES(truthvalue opencog_atom_types) | ||
|
||
TARGET_LINK_LIBRARIES(truthvalue | ||
value | ||
${COGUTIL_LIBRARY} | ||
) | ||
if(HAVE_CYTHON) | ||
TARGET_LINK_LIBRARIES(truthvalue | ||
${PYTHON_LIBRARIES} | ||
) | ||
endif(HAVE_CYTHON) | ||
|
||
|
||
|
||
INSTALL (TARGETS truthvalue EXPORT AtomSpaceTargets | ||
DESTINATION "lib${LIB_DIR_SUFFIX}/opencog" | ||
|
@@ -28,5 +48,6 @@ INSTALL (FILES | |
SimpleTruthValue.h | ||
EvidenceCountTruthValue.h | ||
TruthValue.h | ||
TTruthValue.h | ||
DESTINATION "include/opencog/atoms/truthvalue" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* opencog/atoms/truthvalue/TTruthValue.cc | ||
* | ||
* Written by Anatoly Belikov <[email protected]> | ||
* All Rights Reserved | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License v3 as | ||
* published by the Free Software Foundation and including the exceptions | ||
* at http://opencog.org/wiki/Licenses | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program; if not, write to: | ||
* Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
|
||
#include <opencog/atoms/truthvalue/TTruthValue.h> | ||
#include "SimpleTruthValue.h" | ||
|
||
#include <Python.h> | ||
|
||
using namespace opencog; | ||
|
||
|
||
TTruthValue::TTruthValue(PyObject * p):TruthValue(TTRUTH_VALUE), ptr(p), _count(1){ | ||
PyGILState_STATE state = PyGILState_Ensure(); | ||
Py_INCREF(p); | ||
PyGILState_Release(state); | ||
}; | ||
|
||
float TTruthValue::getAttr(std::string p_attrname) const{ | ||
PyGILState_STATE state = PyGILState_Ensure(); | ||
PyObject * tensor = (PyObject*)(this->ptr); | ||
PyObject * const attrname = PyUnicode_FromString(p_attrname.c_str()); | ||
PyObject * res_obj = PyObject_GetAttr(tensor, attrname); | ||
PyObject * float_obj = nullptr; | ||
Py_DECREF(attrname); | ||
bool failed = false; | ||
double result; | ||
if (res_obj){ | ||
float_obj = PyObject_CallMethod(res_obj, "__float__", NULL); | ||
if(float_obj) { | ||
result = PyFloat_AsDouble(float_obj); | ||
} | ||
} else { | ||
failed = true; | ||
} | ||
if(res_obj) Py_DECREF(res_obj); | ||
if(float_obj) Py_DECREF(float_obj); | ||
PyGILState_Release(state); | ||
if(failed) | ||
throw RuntimeException(TRACE_INFO, "failed to get element of tensor"); | ||
return result; | ||
} | ||
|
||
|
||
strength_t TTruthValue::get_mean() const { | ||
return this->getAttr("mean"); | ||
} | ||
|
||
|
||
confidence_t TTruthValue::get_confidence() const { | ||
return this->getAttr("confidence"); | ||
} | ||
|
||
|
||
count_t TTruthValue::get_count() const { | ||
return this->_count; | ||
} | ||
|
||
|
||
TruthValuePtr TTruthValue::clone() const { | ||
PyObject * tensor = (PyObject*)(this->ptr); | ||
PyGILState_STATE state = PyGILState_Ensure(); | ||
PyObject * res_obj = PyObject_CallMethod(tensor, "clone", NULL); | ||
PyGILState_Release(state); | ||
if(res_obj == nullptr){ | ||
throw RuntimeException(TRACE_INFO, "failed to clone object"); | ||
} | ||
return createTTruthValue(res_obj); | ||
} | ||
|
||
bool TTruthValue::operator==(const Value& other) const { | ||
return this == &other; | ||
} | ||
|
||
TruthValuePtr TTruthValue::merge(const TruthValuePtr& other, | ||
const MergeCtrl& mc) const | ||
{ | ||
throw RuntimeException(TRACE_INFO, | ||
"merge is not implemented"); | ||
} | ||
|
||
TTruthValue::~TTruthValue(){ | ||
PyGILState_STATE state = PyGILState_Ensure(); | ||
Py_DECREF(this->ptr); | ||
PyGILState_Release(state); | ||
} | ||
|
||
void * TTruthValue::getPtr(){ | ||
return this->ptr; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* opencog/atoms/truthvalue/TTruthValue.h | ||
* | ||
* Written by Anatoly Belikov <[email protected]> | ||
* All Rights Reserved | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License v3 as | ||
* published by the Free Software Foundation and including the exceptions | ||
* at http://opencog.org/wiki/Licenses | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program; if not, write to: | ||
* Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
|
||
#ifndef _OPENCOG_TORCH_TRUTH_VALUE_H_ | ||
#define _OPENCOG_TORCH_TRUTH_VALUE_H_ | ||
|
||
#include <opencog/atoms/truthvalue/TruthValue.h> | ||
#include <opencog/atoms/value/PtrValue.h> | ||
|
||
#ifndef PyObject_HEAD | ||
struct _object; | ||
typedef _object PyObject; | ||
#endif | ||
|
||
|
||
namespace opencog | ||
{ | ||
|
||
/* | ||
* class TTruthValue holds pointer to torch.Tensor wrapper. | ||
* methods such as get_mean, get_confidence call this python wrapper. | ||
* Otherwise it is similiar to SimpleTruthValue. | ||
*/ | ||
|
||
class TTruthValue: public TruthValue | ||
{ | ||
private: | ||
PyObject * ptr; | ||
unsigned int _count; | ||
|
||
float getAttr(std::string attrname) const; | ||
public: | ||
TTruthValue(const TTruthValue &) = delete; | ||
TTruthValue(PyObject * p); | ||
virtual strength_t get_mean() const; | ||
virtual confidence_t get_confidence() const; | ||
virtual count_t get_count() const; | ||
|
||
virtual TruthValuePtr clone() const; | ||
virtual bool operator==(const Value&) const; | ||
virtual TruthValuePtr merge(const TruthValuePtr& other, | ||
const MergeCtrl& mc) const; | ||
virtual ~TTruthValue(); | ||
virtual void * getPtr(); | ||
|
||
}; | ||
|
||
typedef std::shared_ptr<TTruthValue> TTruthValuePtr; | ||
template<typename ... Type> | ||
static inline TTruthValuePtr createTTruthValue(Type&&... args) { | ||
return std::make_shared<TTruthValue>(std::forward<Type>(args)...); | ||
} | ||
|
||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest name it
TORCH_TRUTH_VALUE
asTTRUTH_VALUE
is unclear and ambiguous - usuallyT
at the beginning sounds asType
. And renameTTruthValue
toTorchTruthValue
as well for clarity.