Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var attributes = ['shape', 'color'];
  2.  
  3. var shapes = ['sphere', 'cube', 'pyramid']
  4. var colors = ['red', 'blue', 'green', 'black']
  5. var properties = {shape: shapes, color: colors}
  6.  
  7. var terms = _.concat(shapes, colors)
  8. var syntaxes = ['base', 'preN'];
  9.  
  10. var objs = [
  11.   {idx: 0, shape: "sphere", color: "red"},
  12.   {idx: 1, shape: "cube", color: "red"},
  13.   {idx: 2, shape: "pyramid", color: "red"},
  14.  
  15.   {idx: 3, shape: "sphere", color: "blue"},
  16.   {idx: 4, shape: "cube", color: "blue"},
  17.   {idx: 5, shape: "pyramid", color: "blue"},
  18.  
  19.   {idx: 6, shape: "sphere", color: "green"},
  20.   {idx: 7, shape: "cube", color: "green"},
  21.   {idx: 8, shape: "pyramid", color: "green"},
  22. ]
  23. var objsByTerm = {
  24.   red: [objs[0], objs[1], objs[2]],
  25.   blue: [objs[3], objs[4], objs[5]],
  26.   green: [objs[6], objs[7], objs[8]],
  27.  
  28.   sphere: [objs[0], objs[3], objs[6]],
  29.   cube: [objs[1], objs[4], objs[7]],
  30.   pyramid: [objs[2], objs[5], objs[8]],
  31. }
  32.  
  33. var observedData = [{ref: objsByTerm.sphere[0], attr: 'shape', term: 'sphere', syntax: 'base'},
  34.                     {ref: objsByTerm.sphere[1], attr: 'shape', term: 'sphere', syntax: 'base'},
  35.                     {ref: objsByTerm.sphere[2], attr: 'shape', term: 'sphere', syntax: 'base'},
  36.                     {ref: objsByTerm.cube[0], attr: 'shape', term: 'cube', syntax: 'base'},
  37.                     {ref: objsByTerm.cube[1], attr: 'shape', term: 'cube', syntax: 'base'},
  38.                     {ref: objsByTerm.cube[2], attr: 'shape', term: 'cube', syntax: 'base'},
  39.                     {ref: objsByTerm.pyramid[0], attr: 'shape', term: 'pyramid', syntax: 'base'},
  40.                     {ref: objsByTerm.pyramid[1], attr: 'shape', term: 'pyramid', syntax: 'base'},
  41.                     {ref: objsByTerm.pyramid[2], attr: 'shape', term: 'pyramid', syntax: 'base'},
  42.  
  43.                     {ref: objsByTerm.red[0], attr: 'color', term: 'red', syntax: 'preN'},
  44.                     {ref: objsByTerm.red[1], attr: 'color', term: 'red', syntax: 'preN'},
  45.                     {ref: objsByTerm.red[2], attr: 'color', term: 'red', syntax: 'preN'},
  46.                     {ref: objsByTerm.blue[0], attr: 'color', term: 'blue', syntax: 'preN'},
  47.                     {ref: objsByTerm.blue[1], attr: 'color', term: 'blue', syntax: 'preN'},
  48.                     {ref: objsByTerm.blue[2], attr: 'color', term: 'blue', syntax: 'preN'},
  49.                     {ref: objsByTerm.green[0], attr: 'color', term: 'green', syntax: 'preN'},
  50.                     {ref: objsByTerm.green[1], attr: 'color', term: 'green', syntax: 'preN'},
  51.                     {ref: objsByTerm.green[2], attr: 'color', term: 'green', syntax: 'preN'}]
  52.  
  53. var attrPrior = Dirichlet({alpha: ones([attributes.length, 1])})
  54. var syntaxPrior = Dirichlet({alpha: ones([syntaxes.length, 1])})
  55.  
  56. var query = function(querier) {return function() {
  57.   // prior p(attribute type)
  58.   var attr = Categorical({vs: attributes, ps: sample(attrPrior)})
  59.  
  60.   // p(attribute value | attribute type) -- e.g. p(color | attribute type == color)
  61.   var attrVal = mem(function(attr) {return Categorical({vs: properties[attr], ps: ones([properties[attr].length, 1])})})
  62.  
  63.   // p(syntax | attribute type)
  64.   var syntax = mem(function(attr) {return Categorical({vs: syntaxes, ps: sample(syntaxPrior)})})
  65.  
  66.   querier(attr, attrVal, syntax)
  67. }}
  68.  
  69. var posterior = Infer({method: "MCMC", samples: 10000, callbacks: [editor.MCMCProgress()]},
  70.                      query(function(attr, attrVal, syntax) {
  71.   var obsFn = function(datum) {
  72.     observe(attr, datum.attr)
  73.     observe(attrVal(datum.attr), datum.ref[datum.attr])
  74.     observe(syntax(datum.attr), datum.syntax)
  75.   }
  76.  
  77.   mapData({data: observedData}, obsFn)
  78.  
  79.   // Sanity check: shape should be expected as a base noun.
  80.   // Works!
  81.   // return {shapeSyntax: sample(syntax("shape"))}
  82.  
  83.   // TODO: This pattern doesn't work for this case.
  84.   // I want to get a predictive posterior over attribute type for a term
  85.   // with observed syntax and observed term value. But the structure built
  86.   // here requires that I invoke `syntax` with some `attr` value -- the
  87.   // latent value. What gives .. ?
  88.   // observe(syntax("black"), "preN")
  89.   observe(syntax("color"), "preN")
  90.   return {blackAttr: sample(attr)}
  91. }))
  92.  
  93. viz.marginals(posterior)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement