singnet / opencog

A framework for integrated Artificial Intelligence & Artificial General Intelligence (AGI)
http://wiki.opencog.org/w/Development
Other
15 stars 3 forks source link

Reimplement PLN with pytorch tensors as truth values #70

Open noskill opened 5 years ago

noskill commented 5 years ago

It is possible now to attach pytorch tensors to nodes and links using PtrValue. Reimplementing pln in pytorch would allow to use backpropagation to learn truth values.

Example of fuzzy conjunction rule is in this repository https://github.com/singnet/semantic-vision/blob/master/experiments/opencog/cog_module/pln.py Since such implementation could be generally useful the idea is to continue to implement it in opencog repository

Necr0x0Der commented 5 years ago

@ngeiswei , we started to implement a pytorch version of PLN for our purposes within CogNets project, but came up with the idea that it can be abstracted away from CogNets, and can be used in other projects. So, the question is mostly for you - whether you think that such implementation worth being a part of the main opencog repo along with the traditional implementation in Scheme with the use of traditional truth values?

ngeiswei commented 5 years ago

Wow, I don't have any real idea what a pytorch version of PLN means but that sounds awesome!

I wonder, since the opencog repo is already an unmanageable pile of various things, what do you think about moving the pln scheme code in its own repo and adding your implementation there? That would be especially meaningful if your version can be used as a drop-in/alternate replacement of the scheme version. Perhaps less meaningful if it is a different idea. Will look at your code soon...

Necr0x0Der commented 5 years ago

Well, PLN is used by many projects as a simple way to do reasoning. And PLN itself is simple enough to not be in a separate repo. Maybe, it can be in Atomspace to be easily accessible by anyone. Another possibility is to create a repo with different rules for different styles of reasoning. For example, if our attempt to perform full-fledged reasoning over Bayesian nets using URE will be successful, it can be put in this "reasoning rules" repo along with PLN... Yeah, maybe a collection of different reasoning rules worths a separate repo... Our implementation of PLN with PyTorch (as I believe) will be usable as a drop-in replacement of the scheme version, but with some drawbacks. One will need to install PyTorch to use it. Also, PtrValues are "hacky" with possible persistency and serialization issues (which, I guess, should be solved one day if they will be massively used)...

ngeiswei commented 5 years ago

PLN, once completed isn't that simple. There's still contextual, intensional reasoning to implement, and uncertainty requires more non trivial work as well.

I don't think it should be in the atomspace repo either. But for the time it certainly can stay in the opencog repo, less work that way, and by extension I suppose pytorch PLN can also be added to the opencog repo (the singnet fork since it relies on PtrValue).

Necr0x0Der commented 5 years ago

PLN, once completed isn't that simple. There's still contextual, intensional reasoning to implement, and uncertainty requires more non trivial work as well.

Well, I meant current simplistic implementation, of course.

I don't think it should be in the atomspace repo either. But for the time it certainly can stay in the opencog repo, less work that way, and by extension I suppose pytorch PLN can also be added to the opencog repo (the singnet fork since it relies on PtrValue).

OK

noskill commented 5 years ago

There are many ways to implement pytorch tensors as truth values. 1) Using PtrValue instead of atom.tv, that is implemented in https://github.com/singnet/opencog/pull/71 2) subclass torch.Tensor in cython:

class TorchValue(torch.Tensor): 
         cdef cTorchValue * _val;

Create c++ class cTorchValue holding pointer to PyObject - which is TorchValue object.
Initialize _val = new cTorchValue(self). Use PyObject_GetAttr/SetAttr in c++ to access mean / confidence.

use TTruthValue in python just like some torch class

3) subclass at::Tensor in c++

class TorchValue: public at::Tensor, public Value

compile using pybind11 instead of cython - https://pytorch.org/tutorials/advanced/cpp_extension.html

Variant 3 and Variant 2 would allow simply write: atom.tv = TTruthValue(0.1, 1.0) Variant 3 and Variant 2 should allow to use this value type in Schema equations with same amount of work as for FloatValue.

noskill commented 5 years ago

PR https://github.com/singnet/atomspace/pull/92 implements variant 2 except Inheritance part which doesn't work in cython. Variant 3 requires some more work since pytorch uses it's own binding tool - pybind11