Why is the log likelihood used for the loss function in an RBM In an RBM, the loss function is defined like this:

Why are we using the log likelihood function? How does that measure the error?
 A: Hand-wavingly, the log-likelihood is a function that gives you an 'aggregate probability' of your samples, meaning that an increase on the probability of your samples increases the log-likelihood. You can think of the task of the RBM as tune the parameters so as to give high probabilities to the training instances $x^{(i)}$.
A bit more formally, the ideal function to minimize is the Kullback-Leibler divergence between that the probability distribution that the RBM learns (let's call it, as in your question, $p(x)$) and the real probability distribution over the instances $p_{real}(x)$. The KL divergence between from $p_{real}$ to $p$ is
$$
KL(p||p_{real})=-\sum_x p_{real}(x)\log\frac{p(x)}{p_{real}(x)} = \sum_x\left[p_{real}(x)\log p_{real}(x) - p_{real}(x)\log p(x)\right]
$$
Note that the first contribution does not depend on the parameters of the RBM and so it will not contribute to the gradients, so we can disregard it.
As for the second term, it already looks similar to the log likelihood. The sum is over all possible configurations of the visible layer $x$. However, 'non-natural' configurations (in the sense of configurations that do not correspond to examples of the dataset you are trying to learn, as all pixels set to zero when you are learning pictures of cats) ideally have probabilities $p_{real}$ much smaller than 'natural' ones, so the sum can be restricted to 'natural' configurations. Now, assuming that all 'natural' configurations have similar probabilities of appearing, you get a function which is quite well approximated by the log-likelihood of the training set, which is the loss function used when training RBMs.
