library(torch)
torch_manual_seed(1) # setting seed for reproducibility
This vignette showcases the basic functionality of distributions in torch. Currently the distributions modules are considered ‘work in progress’ and are still experimental features in the torch package. You can see the progress in this link.
The distributions modules in torch are modelled after PyTorch’s distributions module which in turn is based on the TensorFlow Distributions package.
This vignette is based in the TensorFlow’s distributions tutorial.
Let’s start and create a new instance of a normal distribution:
<- distr_normal(loc = 0, scale = 1)
n n
We can draw samples from it with:
$sample() n
or, draw multiple samples:
$sample(3) n
We can evaluate the log probability of values:
$log_prob(0)
nlog(dnorm(0)) # equivalent R code
or, evaluate multiple log probabilities:
$log_prob(c(0, 2, 4)) n
A distribution can take a tensor as it’s parameters:
<- distr_bernoulli(probs = torch_tensor(c(0.25, 0.5, 0.75)))
b b
This object represents 3 independent Bernoulli distributions, one for each element of the tensor.
We can sample a single observation:
$sample() b
or, a batch of n
observations:
$sample(6) b
The log_prob
method of distributions can be
differentiated, thus, distributions can be used to train models in
torch.
Let’s implement a Gaussian linear model, but first let’s simulate some data
<- torch_randn(100, 1)
x <- 2*x + 1 + torch_randn(100, 1) y
and plot:
plot(as.numeric(x), as.numeric(y))
We can now define our model:
<- nn_module(
GaussianLinear initialize = function() {
# this linear predictor will estimate the mean of the normal distribution
$linear <- nn_linear(1, 1)
self# this parameter will hold the estimate of the variability
$scale <- nn_parameter(torch_ones(1))
self
},forward = function(x) {
# we estimate the mean
<- self$linear(x)
loc # return a normal distribution
distr_normal(loc, self$scale)
}
)
<- GaussianLinear() model
We can now train our model with:
<- optim_sgd(model$parameters, lr = 0.1)
opt
for (i in 1:100) {
$zero_grad()
opt<- model(x)
d <- torch_mean(-d$log_prob(y))
loss $backward()
loss$step()
optif (i %% 10 == 0)
cat("iter: ", i, " loss: ", loss$item(), "\n")
}
We can see the parameter estimates with:
$parameters model
and quickly compare with the glm()
function:
summary(glm(as.numeric(y) ~ as.numeric(x)))