pdf文档 webppl Documentation

265.88 KB 64 页 0 下载 35 浏览 0 评论 0 收藏
语言 格式 评分
英语
.pdf
3
概览
webppl Documentation Release 0.9.13 the webppl contributors Nov 08, 2018 Contents 1 Getting Started 1 1.1 Try WebPPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.3 Need help? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2 Language Overview 3 2.1 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2.2 Calling JavaScript Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 3 Installation 5 3.1 Updating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 4 Usage 7 4.1 Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 4.2 Passing arguments to the program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 5 Debugging 9 6 Sample 11 6.1 Guides . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 6.2 Drift Kernels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 7 Distributions 15 7.1 Primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 8 Inference 21 8.1 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 8.2 Conditioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 9 Optimization 29 9.1 Optimize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 9.2 Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 9.3 Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 9.4 Parallelization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 10 Built-in Functions 35 10.1 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 10.2 Tensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 i 10.3 Neural networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 10.4 Other . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 11 The Global Store 45 11.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 11.2 Introducing the global store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 11.3 Marginal inference and the global store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 11.4 When to use the store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 11.5 When not to use the global store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 12 Packages 49 12.1 WebPPL code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 12.2 JavaScript functions and libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 12.3 Additional header files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 12.4 Package template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 12.5 Useful packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 13 Workflow 53 13.1 Installation from GitHub . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 13.2 Updating the npm package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 13.3 Committing changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 13.4 Modifying .ad.js files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 13.5 Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 13.6 Linting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 13.7 Browser version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Bibliography 57 ii CHAPTER 1 Getting Started 1.1 Try WebPPL The quickest way to get started using WebPPL is by running programs using the code box on webppl.org. WebPPL can also be installed locally and run from the command line. 1.2 Learning If you’re new to probabilistic programming, Probabilistic Models of Cognition is a great place to start learning the paradigm. The best guide to using WebPPL is The Design and Implementation of Probabilistic Programming Languages. The examples will also be helpful in learning the syntax. 1.3 Need help? If you have any questions about installing WebPPL or need help with your code, you can get help on the Google group. 1 webppl Documentation, Release 0.9.13 2 Chapter 1. Getting Started CHAPTER 2 Language Overview The WebPPL language begins with a subset of JavaScript, and adds to it primitive distributions and operations to perform sampling, conditioning and inference. 2.1 Syntax Following the notation from the Mozilla Parser API, the language consists of the subset of JavaScript that can be built from the following syntax elements, each shown with an example: Element Example Program A complete program, consisting of a sequence of statements BlockStatement A sequence of statements surrounded by braces, {var x = 1; var y = 2;} ExpressionStatement A statement containing a single expression, 3 + 4; ReturnStatement return 3; EmptyStatement A solitary semicolon, ; IfStatement if (x > 1) { return 1; } else { return 2; } VariableDeclaration var x = 5; Identifier x Literal 3 FunctionExpression function (x) { return x; } CallExpression f(x) ConditionalExpression x ? y : z ArrayExpression [1, 2, 3] MemberExpression Math.log BinaryExpression 3 + 4 LogicalExpression true || false UnaryExpression -5 ObjectExpression {a: 1, b: 2} AssignmentExpression globalStore.a = 1 (Assignment is only supported by the global store.) 3 webppl Documentation, Release 0.9.13 Note that general assignment expressions and looping constructs are not currently supported (e.g. for, while, do). This is because a purely functional language is much easier to transform into Continuation-Passing Style (CPS), which the WebPPL implementation uses to implement inference algorithms such as enumeration and SMC. While these restrictions mean that common JavaScript programming patterns aren’t possible, this subset is still universal, because we allow recursive and higher-order functions. It encourages a functional style, similar to Haskell or LISP, that is pretty easy to use (once you get used to thinking functionally!). Here is a (very boring) program that uses much of the available syntax: var foo = function(x) { var bar = Math.exp(x); var baz = x === 0 ? [] : [Math.log(bar), foo(x-1)]; return baz; } foo(5); 2.2 Calling JavaScript Functions JavaScript functions can be called from a WebPPL program, with a few restrictions: 1. JavaScript functions must be deterministic and cannot carry state from one call to another. (That is, the functions must be ‘referentially transparent’: calling obj.foo(args) must always return the same value when called with given arguments.) 2. JavaScript functions can’t be called with a WebPPL function as an argument (that is, they can’t be higher-order). 3. JavaScript functions must be invoked as the method of an object (indeed, this is the only use of object method invocation currently possible in WebPPL). All of the JavaScript functions built into the enviro
下载文档到本地,方便使用
共 64 页, 还有 8 页可预览, 继续阅读
文档评分
请文明评论,理性发言.