|
842 | 842 | "href": "tutorials/docs-10-using-turing-autodiff/index.html", |
843 | 843 | "title": "Automatic Differentiation", |
844 | 844 | "section": "", |
845 | | - "text": "Turing currently supports four automatic differentiation (AD) backends for sampling: ForwardDiff for forward-mode AD; and ReverseDiff, Zygote, and Tracker for reverse-mode AD. While Tracker is still available, its use is discouraged due to a lack of active maintenance. ForwardDiff is automatically imported by Turing. To utilize Zygote or ReverseDiff for AD, users must explicitly import them with using Zygote or using ReverseDiff, alongside using Turing.\nAs of Turing version v0.30, the global configuration flag for the AD backend has been removed in favour of AdTypes.jl, allowing users to specify the AD backend for individual samplers independently. Users can pass the adtype keyword argument to the sampler constructor to select the desired AD backend, with the default being AutoForwardDiff(; chunksize=0).\nFor ForwardDiff, pass adtype=AutoForwardDiff(; chunksize) to the sampler constructor. A chunksize of 0 permits the chunk size to be automatically determined. For more information regarding the selection of chunksize, please refer to related section of ForwardDiff’s documentation. For ReverseDiff, pass adtype=AutoReverseDiff() to the sampler constructor. An additional argument can be provided to AutoReverseDiff to specify whether to to cache the tape only once and reuse it later use (false by default, which means no caching tape). This can substantially improve performance, but risks silently incorrect results if not used with care.\nCached tapes should only be used if you are absolutely certain that the sequence of operations performed in your code does not change between different executions of your model. Thus, e.g., in the model definition and all implicitly and explicitly called functions in the model, all loops should be of fixed size, and if-statements should consistently execute the same branches. For instance, if-statements with conditions that can be determined at compile time or conditions that depend only on fixed properties of the data will always execute the same branches during sampling (if the data is constant throughout sampling and, e.g., no mini-batching is used). However, if-statements that depend on the model parameters can take different branches during sampling; hence, the compiled tape might be incorrect. Thus you must not use compiled tapes when your model makes decisions based on the model parameters, and you should be careful if you compute functions of parameters that those functions do not have branching which might cause them to execute different code for different values of the parameter.\nFor Zygote, pass adtype=AutoZygote() to the sampler constructor.\nAnd the previously used interface functions including ADBackend, setadbackend, setsafe, setchunksize, and setrdcache are deprecated and removed.", |
| 845 | + "text": "Turing currently supports four automatic differentiation (AD) backends for sampling: ForwardDiff for forward-mode AD; and ReverseDiff, Zygote, and Tracker for reverse-mode AD. While Tracker is still available, its use is discouraged due to a lack of active maintenance. ForwardDiff is automatically imported by Turing. To utilize Zygote or ReverseDiff for AD, users must explicitly import them with using Zygote or using ReverseDiff, alongside using Turing.\nAs of Turing version v0.30, the global configuration flag for the AD backend has been removed in favour of AdTypes.jl, allowing users to specify the AD backend for individual samplers independently. Users can pass the adtype keyword argument to the sampler constructor to select the desired AD backend, with the default being AutoForwardDiff(; chunksize=0).\nFor ForwardDiff, pass adtype=AutoForwardDiff(; chunksize) to the sampler constructor. A chunksize of 0 permits the chunk size to be automatically determined. For more information regarding the selection of chunksize, please refer to related section of ForwardDiff’s documentation. For ReverseDiff, pass adtype=AutoReverseDiff() to the sampler constructor. An additional argument can be provided to AutoReverseDiff to specify whether to to cache the tape only once and reuse it later use (false by default, which means no caching). This can substantially improve performance, but risks silently incorrect results if not used with care.\nCached tapes should only be used if you are absolutely certain that the sequence of operations performed in your code does not change between different executions of your model. Thus, e.g., in the model definition and all implicitly and explicitly called functions in the model, all loops should be of fixed size, and if-statements should consistently execute the same branches. For instance, if-statements with conditions that can be determined at compile time or conditions that depend only on fixed properties of the data will always execute the same branches during sampling (if the data is constant throughout sampling and, e.g., no mini-batching is used). However, if-statements that depend on the model parameters can take different branches during sampling; hence, the compiled tape might be incorrect. Thus you must not use compiled tapes when your model makes decisions based on the model parameters, and you should be careful if you compute functions of parameters that those functions do not have branching which might cause them to execute different code for different values of the parameter.\nFor Zygote, pass adtype=AutoZygote() to the sampler constructor.\nAnd the previously used interface functions including ADBackend, setadbackend, setsafe, setchunksize, and setrdcache are deprecated and removed.", |
846 | 846 | "crumbs": [ |
847 | 847 | "Get Started", |
848 | 848 | "Users", |
|
855 | 855 | "href": "tutorials/docs-10-using-turing-autodiff/index.html#switching-ad-modes", |
856 | 856 | "title": "Automatic Differentiation", |
857 | 857 | "section": "", |
858 | | - "text": "Turing currently supports four automatic differentiation (AD) backends for sampling: ForwardDiff for forward-mode AD; and ReverseDiff, Zygote, and Tracker for reverse-mode AD. While Tracker is still available, its use is discouraged due to a lack of active maintenance. ForwardDiff is automatically imported by Turing. To utilize Zygote or ReverseDiff for AD, users must explicitly import them with using Zygote or using ReverseDiff, alongside using Turing.\nAs of Turing version v0.30, the global configuration flag for the AD backend has been removed in favour of AdTypes.jl, allowing users to specify the AD backend for individual samplers independently. Users can pass the adtype keyword argument to the sampler constructor to select the desired AD backend, with the default being AutoForwardDiff(; chunksize=0).\nFor ForwardDiff, pass adtype=AutoForwardDiff(; chunksize) to the sampler constructor. A chunksize of 0 permits the chunk size to be automatically determined. For more information regarding the selection of chunksize, please refer to related section of ForwardDiff’s documentation. For ReverseDiff, pass adtype=AutoReverseDiff() to the sampler constructor. An additional argument can be provided to AutoReverseDiff to specify whether to to cache the tape only once and reuse it later use (false by default, which means no caching tape). This can substantially improve performance, but risks silently incorrect results if not used with care.\nCached tapes should only be used if you are absolutely certain that the sequence of operations performed in your code does not change between different executions of your model. Thus, e.g., in the model definition and all implicitly and explicitly called functions in the model, all loops should be of fixed size, and if-statements should consistently execute the same branches. For instance, if-statements with conditions that can be determined at compile time or conditions that depend only on fixed properties of the data will always execute the same branches during sampling (if the data is constant throughout sampling and, e.g., no mini-batching is used). However, if-statements that depend on the model parameters can take different branches during sampling; hence, the compiled tape might be incorrect. Thus you must not use compiled tapes when your model makes decisions based on the model parameters, and you should be careful if you compute functions of parameters that those functions do not have branching which might cause them to execute different code for different values of the parameter.\nFor Zygote, pass adtype=AutoZygote() to the sampler constructor.\nAnd the previously used interface functions including ADBackend, setadbackend, setsafe, setchunksize, and setrdcache are deprecated and removed.", |
| 858 | + "text": "Turing currently supports four automatic differentiation (AD) backends for sampling: ForwardDiff for forward-mode AD; and ReverseDiff, Zygote, and Tracker for reverse-mode AD. While Tracker is still available, its use is discouraged due to a lack of active maintenance. ForwardDiff is automatically imported by Turing. To utilize Zygote or ReverseDiff for AD, users must explicitly import them with using Zygote or using ReverseDiff, alongside using Turing.\nAs of Turing version v0.30, the global configuration flag for the AD backend has been removed in favour of AdTypes.jl, allowing users to specify the AD backend for individual samplers independently. Users can pass the adtype keyword argument to the sampler constructor to select the desired AD backend, with the default being AutoForwardDiff(; chunksize=0).\nFor ForwardDiff, pass adtype=AutoForwardDiff(; chunksize) to the sampler constructor. A chunksize of 0 permits the chunk size to be automatically determined. For more information regarding the selection of chunksize, please refer to related section of ForwardDiff’s documentation. For ReverseDiff, pass adtype=AutoReverseDiff() to the sampler constructor. An additional argument can be provided to AutoReverseDiff to specify whether to to cache the tape only once and reuse it later use (false by default, which means no caching). This can substantially improve performance, but risks silently incorrect results if not used with care.\nCached tapes should only be used if you are absolutely certain that the sequence of operations performed in your code does not change between different executions of your model. Thus, e.g., in the model definition and all implicitly and explicitly called functions in the model, all loops should be of fixed size, and if-statements should consistently execute the same branches. For instance, if-statements with conditions that can be determined at compile time or conditions that depend only on fixed properties of the data will always execute the same branches during sampling (if the data is constant throughout sampling and, e.g., no mini-batching is used). However, if-statements that depend on the model parameters can take different branches during sampling; hence, the compiled tape might be incorrect. Thus you must not use compiled tapes when your model makes decisions based on the model parameters, and you should be careful if you compute functions of parameters that those functions do not have branching which might cause them to execute different code for different values of the parameter.\nFor Zygote, pass adtype=AutoZygote() to the sampler constructor.\nAnd the previously used interface functions including ADBackend, setadbackend, setsafe, setchunksize, and setrdcache are deprecated and removed.", |
859 | 859 | "crumbs": [ |
860 | 860 | "Get Started", |
861 | 861 | "Users", |
|
868 | 868 | "href": "tutorials/docs-10-using-turing-autodiff/index.html#compositional-sampling-with-differing-ad-modes", |
869 | 869 | "title": "Automatic Differentiation", |
870 | 870 | "section": "Compositional Sampling with Differing AD Modes", |
871 | | - "text": "Compositional Sampling with Differing AD Modes\nTuring supports intermixed automatic differentiation methods for different variable spaces. The snippet below shows using ForwardDiff to sample the mean (m) parameter, and using ReverseDiff for the variance (s) parameter:\n\nusing Turing\nusing ReverseDiff\n\n# Define a simple Normal model with unknown mean and variance.\n@model function gdemo(x, y)\n s² ~ InverseGamma(2, 3)\n m ~ Normal(0, sqrt(s²))\n x ~ Normal(m, sqrt(s²))\n return y ~ Normal(m, sqrt(s²))\nend\n\n# Sample using Gibbs and varying autodiff backends.\nc = sample(\n gdemo(1.5, 2),\n Gibbs(\n HMC(0.1, 5, :m; adtype=AutoForwardDiff(; chunksize=0)),\n HMC(0.1, 5, :s²; adtype=AutoReverseDiff(false)),\n ),\n 1000,\n progress=false,\n)\n\nChains MCMC chain (1000×3×1 Array{Float64, 3}):\n\nIterations = 1:1:1000\nNumber of chains = 1\nSamples per chain = 1000\nWall duration = 8.21 seconds\nCompute duration = 8.21 seconds\nparameters = s², m\ninternals = lp\n\nSummary Statistics\n parameters mean std mcse ess_bulk ess_tail rhat e ⋯\n Symbol Float64 Float64 Float64 Float64 Float64 Float64 ⋯\n\n s² 1.8879 1.2973 0.1103 145.2983 221.8518 0.9991 ⋯\n m 1.1075 0.7849 0.0756 111.4962 138.4216 1.0015 ⋯\n 1 column omitted\n\nQuantiles\n parameters 2.5% 25.0% 50.0% 75.0% 97.5%\n Symbol Float64 Float64 Float64 Float64 Float64\n\n s² 0.5700 1.0442 1.5262 2.3260 5.5711\n m -0.6168 0.6352 1.1076 1.5951 2.6550\n\n\nGenerally, reverse-mode AD, for instance ReverseDiff, is faster when sampling from variables of high dimensionality (greater than 20), while forward-mode AD, for instance ForwardDiff, is more efficient for lower-dimension variables. This functionality allows those who are performance sensitive to fine tune their automatic differentiation for their specific models.\nIf the differentiation method is not specified in this way, Turing will default to using whatever the global AD backend is. Currently, this defaults to ForwardDiff.", |
| 871 | + "text": "Compositional Sampling with Differing AD Modes\nTuring supports intermixed automatic differentiation methods for different variable spaces. The snippet below shows using ForwardDiff to sample the mean (m) parameter, and using ReverseDiff for the variance (s) parameter:\n\nusing Turing\nusing ReverseDiff\n\n# Define a simple Normal model with unknown mean and variance.\n@model function gdemo(x, y)\n s² ~ InverseGamma(2, 3)\n m ~ Normal(0, sqrt(s²))\n x ~ Normal(m, sqrt(s²))\n return y ~ Normal(m, sqrt(s²))\nend\n\n# Sample using Gibbs and varying autodiff backends.\nc = sample(\n gdemo(1.5, 2),\n Gibbs(\n HMC(0.1, 5, :m; adtype=AutoForwardDiff(; chunksize=0)),\n HMC(0.1, 5, :s²; adtype=AutoReverseDiff(false)),\n ),\n 1000,\n progress=false,\n)\n\nChains MCMC chain (1000×3×1 Array{Float64, 3}):\n\nIterations = 1:1:1000\nNumber of chains = 1\nSamples per chain = 1000\nWall duration = 8.12 seconds\nCompute duration = 8.12 seconds\nparameters = s², m\ninternals = lp\n\nSummary Statistics\n parameters mean std mcse ess_bulk ess_tail rhat e ⋯\n Symbol Float64 Float64 Float64 Float64 Float64 Float64 ⋯\n\n s² 2.0774 1.4830 0.1277 145.3344 279.7036 1.0002 ⋯\n m 0.9677 0.7205 0.0635 131.6626 198.3992 1.0000 ⋯\n 1 column omitted\n\nQuantiles\n parameters 2.5% 25.0% 50.0% 75.0% 97.5%\n Symbol Float64 Float64 Float64 Float64 Float64\n\n s² 0.5867 1.0839 1.5844 2.5602 6.2920\n m -0.4030 0.4813 0.9696 1.4499 2.3487\n\n\nGenerally, reverse-mode AD, for instance ReverseDiff, is faster when sampling from variables of high dimensionality (greater than 20), while forward-mode AD, for instance ForwardDiff, is more efficient for lower-dimension variables. This functionality allows those who are performance sensitive to fine tune their automatic differentiation for their specific models.\nIf the differentiation method is not specified in this way, Turing will default to using whatever the global AD backend is. Currently, this defaults to ForwardDiff.", |
872 | 872 | "crumbs": [ |
873 | 873 | "Get Started", |
874 | 874 | "Users", |
|
0 commit comments