Initialise the TensorFlow session and the sgmcmc algorithm. For algorithms with control variates this will find the MAP estimates of the log posterior and calculate the full log posterior gradient at this point. For algorithms without control variates this will simply initialise a TensorFlow session.

initSess(sgmcmc, verbose = TRUE)



an sgmcmc object created using *Setup e.g. sgldSetup, sgldcvSetup


optional. Default TRUE. Boolean specifying whether to print progress.


sess a TensorFlow session, see the TensorFlow for R website for more details.


# Simulate from a Normal Distribution, unknown location and known scale with uninformative prior
# Run sgmcmc step by step and calculate estimate of location on the fly to reduce storage
dataset = list("x" = rnorm(1000))
params = list("theta" = 0)
logLik = function(params, dataset) {
    distn = tf$distributions$Normal(params$theta, 1)
stepsize = list("theta" = 1e-4)
sgld = sgldSetup(logLik, dataset, params, stepsize)
nIters = 10^4L
# Initialize location estimate
locEstimate = 0
# Initialise TensorFlow session
sess = initSess(sgld)
for ( i in 1:nIters ) {
    sgmcmcStep(sgld, sess)
    locEstimate = locEstimate + 1 / nIters * getParams(sgld, sess)$theta
# For more examples see vignettes
# }