PLN by Hand

From OpenCog
Jump to: navigation, search

PLN Example

Note: we assume you are starting in your home directory - to get there type $ cd ~


From the command prompt, enter the scheme environment by typing: $ guile


Load the required modules (as you are running from the scheme environment):

scheme@(guile-user)> (use-modules (opencog))
scheme@(guile-user)> (use-modules (opencog query))

(Note you may have to change the paths to the files to suit your installation) Load the deduction rule definitions - type the following 'load' commands for both deduction-rule.scm and simple-assertions.scm

Load the default deduction rules: (load "./opencog/opencog/opencog/pln/rules/term/deduction.scm") <- this contains the 'deduction-inheritance-rule' function used later in this tutorial

Expand to see the contents of deduction-rule.scm

;; =============================================================================
;; DeductionRule
;;
;; <LinkType>
;;   A
;;   B
;; <LinkType>
;;   B
;;   C
;; |-
;; <LinkType>
;;   A
;;   C
;;
;; Due to type system limitations, the rule has been divided into 3:
;;       deduction-inheritance-rule
;;       deduction-implication-rule
;;       deduction-subset-rule
;;
;; -----------------------------------------------------------------------------

(use-modules (opencog logger))

(load "formulas.scm")

;; Generate the corresponding deduction rule given its link-type.
(define (gen-deduction-rule link-type)
  (let* ((A (Variable "$A"))
         (B (Variable "$B"))
         (C (Variable "$C"))
         (AB (link-type A B))
         (BC (link-type B C))
         (AC (link-type A C)))
    (Bind
      (VariableList A B C)
      (And
        AB
        BC
        (Not (Identical A C)))
      (ExecutionOutput
        (GroundedSchema "scm: deduction-formula")
          (List
            ;; Premises
            AB
            BC
            ;; Conclusion
            AC)))))

(define deduction-inheritance-rule
    (gen-deduction-rule InheritanceLink))

(define deduction-implication-rule
    (gen-deduction-rule ImplicationLink))

(define deduction-subset-rule
    (gen-deduction-rule SubsetLink))



(define (deduction-formula AB BC AC)
    (let*
        ((sA (cog-stv-strength (gar AB)))
         (cA (cog-stv-confidence (gar AB)))
         (sB (cog-stv-strength (gar BC)))
         (cB (cog-stv-confidence (gar BC)))
         (sC (cog-stv-strength (gdr BC)))
         (cC (cog-stv-confidence (gdr BC)))
         (sAB (cog-stv-strength AB))
         (cAB (cog-stv-confidence AB))
         (sBC (cog-stv-strength BC))
         (cBC (cog-stv-confidence BC))
         (alpha 0.9) ; how much confidence is lost at each deduction step

         ;; Hacks to overcome the lack of distributional TV. If s=1
         ;; and c=0, then assign s to the mode value satisfying the
         ;; deduction consistency constraint (what a pain, let's use
         ;; 0.25 for now).
         (sA (if (and (< 0.99 sA) (<= cA 0)) 0.25 sA))
         (sB (if (and (< 0.99 sB) (<= cB 0)) 0.25 sB))
         (sC (if (and (< 0.99 sC) (<= cC 0)) 0.25 sC)))
      (if (and
           (or (= 0 cA) (= 0 cB) (= 0 cAB)
               (conditional-probability-consistency sA sB sAB))
           (or (= 0 cB) (= 0 cC) (= 0 cBC)
               (conditional-probability-consistency sB sC sBC)))
          (if (< 0.99 (* sB cB))
              ;; Hack to overcome for the lack of distributional
              ;; TV. This covers the case where B fully confidently
              ;; tends to 1. See formulas.scm Simple Deduction
              ;; Formula comment for more explanations. This
              ;; overlaps with the implication-construction-rule.
              (let ((sAC sC)
                    (cAC (* alpha cA cC)))
                (if (and (< 1e-8 sAC) (< 1e-8 cAC)) ;; Don't create zero
                                              ;; knowledge. Note that
                                              ;; sAC == 0 is not zero
                                              ;; knowledge but it's
                                              ;; annoying in the
                                              ;; current hacky
                                              ;; situation.
                    (cog-merge-hi-conf-tv! AC (stv sAC cAC))
                    (cog-undefined-handle)))
              ;; Branch if sB * cB <= 0.99
              (let* ((sAC (if (or (< 0.99 (* sAB cAB)) (< 0.99 (* sBC cBC)))
                              ;; Hack to overcome for the lack of
                              ;; distributional TV. This covers the case
                              ;; where little is known about A and B
                              ;; (i.e. their strength is meaningless), yet
                              ;; we can confidently calculate sAC because
                              ;; sAB and sBC are so high anyway.
                              (* sAB sBC)
                              ;; Otherwise fall back on the naive formula
                              (simple-deduction-strength-formula sA sB sC sAB sBC)))
                     (cAC (min cAB cBC))
                     ;; Unless the 2 implication are fully confident
                     ;; decrease the confidence by some factor. I'm not
                     ;; sure how justify this for now, it's perhaps a
                     ;; bad hack.
                     (cAC (* (if (< cAC 0.99) alpha 1.0) cAC)))
                (if (and (< 1e-8 sAC) (< 1e-8 cAC)) ;; Don't create zero
                                              ;; knowledge. Note that
                                              ;; sAC == 0 is not zero
                                              ;; knowledge but it's
                                              ;; annoying in the
                                              ;; current hacky
                                              ;; situation.
                    (cog-merge-hi-conf-tv! AC (stv sAC cAC))
                    (cog-undefined-handle))))
          (cog-undefined-handle))))

;; Name the rules
(define deduction-inheritance-rule-name
  (DefinedSchemaNode "deduction-inheritance-rule"))
(DefineLink deduction-inheritance-rule-name
  deduction-inheritance-rule)

(define deduction-implication-rule-name
  (DefinedSchemaNode "deduction-implication-rule"))
(DefineLink deduction-implication-rule-name
  deduction-implication-rule)

(define deduction-subset-rule-name
  (DefinedSchemaNode "deduction-subset-rule"))
(DefineLink deduction-subset-rule-name
  deduction-subset-rule)

Load this file containing custom functions to query the atomspace:(load "./opencog/opencog/tests/pln/rules/simple-assertions.scm")<- this contains the 'find-humans' function used later in this tutorial

Expand to see the contents of simple-assertions.scm

; Define the concepts

(ConceptNode "Socrates" (stv .001 0.9))
(ConceptNode "Einstein" (stv .001 0.9))
(ConceptNode "Peirce" (stv .001 0.9))
(ConceptNode "man" (stv .01 0.9))
(ConceptNode "human" (stv .02 0.9))

; Define the instances of man

(InheritanceLink (stv 0.9 0.9)
    (ConceptNode "Socrates")
    (ConceptNode "man"))

(InheritanceLink (stv 0.9 0.9)
    (ConceptNode "Einstein")
    (ConceptNode "man"))

(InheritanceLink (stv 0.9 0.9)
    (ConceptNode "Peirce")
    (ConceptNode "man"))

; Define what man is part of

(InheritanceLink (stv 0.9 0.9)
    (ConceptNode "man")
    (ConceptNode "human"))

; Assign some additional memberships as well

(InheritanceLink (stv 0.9 0.9)
    (ConceptNode "Einstein")
    (ConceptNode "violin-players"))

; Pattern to match to check the output

(define find-humans
    (BindLink
        (VariableNode "$X")
        (InheritanceLink
            (VariableNode "$X")
            (ConceptNode "human"))
        (ListLink
            (VariableNode "$X"))))




The command 'cog-bind find-humans' queries all humans - go ahead and run it from the scheme (guile) prompt:

scheme@(guile-user)> (cog-bind find-humans)

Observe that there is only one instance of human defined. Output should be:

$6 = (SetLink
   (ListLink
      (ConceptNode "man" (stv 0.0099999998 0.89999998))
   )
)


The command 'cog-bind deduction-inheritance-rule' creates new relationships - deduces new facts about relationships. It will find relationships between various existing nodes (Einstein, Socrates, Peirce) to the node 'human'. Go ahead, run the command it:

scheme@(guile-user)> (cog-bind deduction-inheritance-rule)

You can see from the output that it deduced that there were three other humans

$6 = (SetLink
   (InheritanceLink (stv 0.81111109 0.81)
      (ConceptNode "Einstein" (stv 0.001 0.89999998))
      (ConceptNode "human" (stv 0.02 0.89999998))
   )
   (InheritanceLink (stv 0.81111109 0.81)
      (ConceptNode "Socrates" (stv 0.001 0.89999998))
      (ConceptNode "human" (stv 0.02 0.89999998))
   )
   (InheritanceLink (stv 0.81111109 0.81)
      (ConceptNode "Peirce" (stv 0.001 0.89999998))
      (ConceptNode "human" (stv 0.02 0.89999998))
   )
)

Run (cog-bind find-humans) command again. Now you should see more humans. Observe that there are 3 additional instances of human defined (Socrates, Einstein & Peirce):

$7 = (SetLink
   (ListLink
      (ConceptNode "Socrates" (stv 0.001 0.89999998))
   )
   (ListLink
      (ConceptNode "Einstein" (stv 0.001 0.89999998))
   )
   (ListLink
      (ConceptNode "man" (stv 0.0099999998 0.89999998))
   )
   (ListLink
      (ConceptNode "Peirce" (stv 0.001 0.89999998))
   )
)

Done!

Create new concept nodes yourself and make a new inheritance link

You can create new ConceptNodes yourself with inheritance from human. For example type:

Create a new ConceptNode 'Adam Ford': (ConceptNode "Adam Ford" (stv .001 0.9)) Note: stv stands for SimpleTruthValue

Create an inheritance link to man: (Inheritance (stv .9 .9) (ConceptNode "Adam Ford") (ConceptNode "man")) Note, the PLN deduction rule is probabilistic and will check whether the stvs on the concept nodes and inheritance links are consistent with probability theory, will silently discard inferences if they're not, see [1] for more details. Likewise the confidence (second value of the stv should be strictly positive, otherwise the deduction will likely not be able to infer anything useful.

Try to find-humans (before running '(cog-bind deduction-inheritance-rule)' to deduce inheritances): (cog-bind find-humans) the new ConceptNode shouldn't be displayed yet.

Next deduce the inheritances : (cog-bind deduction-inheritance-rule)

Now find the humans again : (cog-bind find-humans) Now 'Adam Ford' should be listed as one of the humans.

== Did you notice above that we set up an inheritance link such that 'Adam Ford' inherits from 'man'? Why didn't we just inherit from human?

Create a new concept node 'Ben Goerzel' the same way as we did above with 'Adam Ford'. Then create an inheritance link to 'human': (Inheritance (stv .9 .9) (ConceptNode "Ben Goerzel") (ConceptNode "human"))

Next deduce the inheritances (as above). What do you expect the output of (cog-bind deduction-inheritance-rule) will be?

Does "Ben Goertzel" appear...? why (or why not)? :-)

Expand to see the answer:

"Ben Goertzel" shouldn't appear. Because the function 'deduction-inheritance-rule' defined in 'deduction-rule.scm' only displays findings that inherit from "human" at one step away. Note that the out doesn't include "man", which inherits from "human" as well, where as 'find-humans' (in 'simple-assertions.scm' does).


More PLN examples

See github, instructions in markdown

A deduction-rule example is given here: This link no longer exists:

Is this the correct link?

  • Add an example stepping through some PLN inferences by hand in the Scheme shell, applying each rule by hand instead of using a chainer. The smokes example may be good for this.


PLN rules on git: https://github.com/opencog/opencog/tree/master/opencog/pln/rules


Quiz

1. How do you enter the opencog scheme environment?

Type 'scheme'.
Type 'guile'.
Click on the 'scheme' app icon on the desktop of your windows phone.
Type 'scheme -up', then supply your username & password.

2. What does PLN stand for?

Type 'Probabilistic Logic Networks'.
Type 'Puny Linguistic Nodes'.
Type 'Nothing'.

3. What is the scheme command create new relationships between existing nodes based on deducing facts about these relationships?

Type '(cog-bind deduction-rule)'.
Type '(cog-bind deduction-inherit-rule)'.
Type '(cog-bind deduction-inheritance-rule)'.
Type '(cog-bind deduction-interface-rule)'.
Type '(cog-bind deduction-inherit-rule)'.

Your score is 0 / 0

*If you dear reader think of some good quiz questions, add them to the discussion branch of this wiki page

Notes

Maintained by: Adam, Misagana

I found that the opencog install has deduction examples in python, but not in scheme

./opencog/opencog/python/pln_old/examples/deduction/