Bayesian Neural Network 설명 - Bayesian Neural Network seolmyeong

Back to glossary

What Are Bayesian Neural Networks?

Bayesian Neural Networks (BNNs) refers to extending standard networks with posterior inference in order to control over-fitting. From a broader perspective, the Bayesian approach uses the statistical methodology so that everything has a probability distribution attached to it, including model parameters (weights and biases in neural networks). In programming languages, variables that can take a specific value will turn the same result every-time you access that specific variable. Let’s begin with the revision of a simple linear model, which will predict the output by the weighted sum of a series of input features.
Bayesian Neural Network 설명 - Bayesian Neural Network seolmyeong
In comparison, in the Bayesian world, you can have similar entities also known as random variables that will give you a different value every time you access it.  In Bayesian terms, the historical data represents our prior knowledge of the overall behavior with each variable having its own statistical properties which vary with time. Let’s assume that X is a random variable which represents the normal distribution, every time X gets accessed, the returned result will have different values This process of getting a new value from a random variable is called sampling. What value comes out depends on the random variable’s associated probability distribution. That means, in the parameter space, one can deduce the nature and shape of the neural network's learned parameters. Recently there has been a lot of activity in this area, with the advent of numerous probabilistic programming libraries such as PyMC3, Edward, Stan etc. Bayesian methods are used in lots of fields: from game development to drug discovery.

What Are Some of the Main Advantages of BNNs?

  • Bayesian neural nets are useful for solving problems in domains where data is scarce, as a way to prevent overfitting. Example applications are molecular biology and medical diagnosis (areas where data often come from costly and difficult experimental work).
  • Bayesian nets are universally useful
  • They can obtain better results for a vast number of tasks however they are extremely difficult to scale to large problems.
  • BNNs allow you to automatically calculate an error associated with your predictions when dealing with data of unknown targets.
  • allow you to estimate uncertainty in predictions, which is a great feature for fields like medicine

Why should you use Bayesian Neural Networks?

Instead of taking into account a single answer to one question, Bayesian methods allow you to consider an entire distribution of answers. With this approach, you can naturally address issues such as:
  • regularization (overfitting or not),
  • model selection/comparison,without the need for a separate cross-validation data set

Additional Resources


Back to glossary

 2020-2학기 이화여대 김정태 교수님 강의 내용을 바탕으로 본 글을 작성하였습니다.


 PRML 교재를 본격적으로 들어가기전에 기존 머신러닝 및 딥러닝에 베이지안 관점이 필요한 이유를 설명하고, 간단한 예제를 통해 관점을 파악해보자.


Overview


  • Machine Learning vs Bayesian Machine Learning
  • Coin flipping example

Machine Learning


 머신러닝 어플리케이션의 대부분은 "data driven"(데이터 기반)으로 인해 성공한 사례로 예를 들면 아래와 같은 것들이 존재한다.

  • 음성인식, 번역
  • 컴퓨터 비전, 객체 탐지
  • 자율 주행
  • 자동 거래 (금융, 전력 등) 

크게 ML을 카테고리 별로 분류하면 다음과 같다.

  • Supervised Learning
    • Regression
    • Classification
  • Unsupervised Learning
    • Clustering
    • Density Estimation
    • Visulization (PCA)
  • Semi-supervised Learning
  • Reinforcement Learning

Limitations of standard deep learning


  1.  기존 딥러닝의 한계점은 불확실성(uncertainties)을 표현이 불가능하다. 예를 들어 regression 문제를 보자. 예측값에 대한 변동성(variability)에 대해 알 수 없다. (제한된 data로 학습된 deep learning은 일반적으로 output이 scalar value 이며, 이런 방식을 point estimate라고 부름) 즉, 결정에 대한 확신성이 없는 경우 표현할 수 없음  
  2. 모델을 선택하는데 있어서, 경험적으로 선택해야함
  3. hyper-parameters를 튜닝해야함

Why Bayesian learning?


  • point estimate가 아닌 distribution을 얻음
  • ouput으로부터 distribution을 받기 때문에, 예측에 대한 불확실성을 계산할 수 있음
  • validation set 없이도 regularization 또는 model comparions 하는데 도움을 받음
  • 단점?으로는 posterior distribution을 계산하는데 어려움이 있어서, 경우에 따라서 variaional inference, markov chain Monte Carlo(MCMC) 같은 기술을 이용하여 도움을 받아야 함
  • standard neural network와 bayesian neural network의 그림을 비교해보자.
  • Prior distribution을 가중치에 적용하여 Regularization을 할 수 있음 

Bayesian Neural Network 설명 - Bayesian Neural Network seolmyeong

Standard vs Bayesian learning


Bayesian Neural Network 설명 - Bayesian Neural Network seolmyeong

 두 기법들에 대해 비교하면 위와 같이 정리할 수 있다. 다음은 Standard(ML) 기법과 Bayesian 기법을 비교한 예제로 차이점을 확인해보자.  

Coin flipping example - ML approach


 동전을 반복해서 던지고 앞면(head)이 나올 확률을 추정해보자. n번 시행 한 후 head를 k번 관찰했다고 가정한다. 그러면 앞면 확률이 $\theta$ 일 때 사건 $D$의 확률은 다음과 같이 Bernouill의 법칙으로 계산할 수 있습니다.

$$p(D|\theta) = \theta^k (1-\theta)^{n-k}$$

Q & A : 위 식을 probability function 과 likelihood function으로도  불리는데, 차이점은 무엇인가요?

case 1) $\theta$ 를 고정시킨 경우 아래 식을 probability function이라고 부름

case 2) $D$를 고정시킨 경우  위 식을 likelihood function 이라고 부름

$$p(D|\theta) = \theta^k (1-\theta)^{n-k} \leftarrow likelihood \ function$$

 우도 함수(likelihood function)를 최대화하는 값으로 $\theta$를 추정하는 것은 자연스러운 일이며, 이는 최대 우도 추정기(MLE, maximum likelihood estimator)로 이어집니다. MLE는 종종 로그 가능도(log-likelihood)라고하는 가능도 함수의 로그를 최대화하여 결정할 수 있습니다. (로그를 사용한 뒤 미분한 결과와 원 문제를 미분한 것과 동일하기 때문에 계산이 편리한 로그를 사용)

$$ \log L(\theta) = k log \theta + (n-k) \log (1-\theta)$$

위 식을 $\theta$로 미분하여 0을 만족하는 MLE는 다음과 같다.

$$\hat{\theta}_{M L}=\frac{k}{n}$$

 위 MLE 를 해석해보면, 총 시도 횟수에 대한 앞면(head) 발생 횟수의 비율로 head 확률을 추정하는 것은 당연하다고 볼 수 있습니다.

 Q & A ) 그러나 2 회 시행 중 haed 발생을 2 회 관찰했다면 어떨까요? $p(\theta)$ 의 확률이 1이라고 믿는 것이 합리적인가요?

 일반적인 동전의 head가 나올 확률은 $\frac{1}{2}$ 이므로 합리적이지 않음!

Coin flipping example - MAP approach


 알려지지 않은 변수에 대한 prior information를 도입하여 MLE의 문제를 완화 할 수 있습니다.  beta distrubution는 확률에 대한 prior belief를 통합하는 좋은 방법이 될 수 있습니다. Conjugate prior는 posterior distribution가 prior distributation와 동일한 모양을 갖도록 만드는 것입니다.

$$\begin{array}{l}
\text { Beta }(\theta ; a, b)=\frac{1}{B(\alpha, \beta)} \theta^{\alpha-1}(1-\theta)^{\beta-1} \\
\qquad B(\alpha, \beta)=\int_{0}^{1} \theta^{\alpha-1}(1-\theta)^{\beta-1} d \theta
\end{array}$$

Beta distributation


Mean and variance of Beta distribution

$$\begin{aligned}
E[\theta] &=\frac{\alpha}{\alpha+\beta} \\
\operatorname{Var}(\theta) &=\frac{\alpha \beta}{(\alpha+\beta)^{2}(\alpha+\beta+1)}
\end{aligned}$$

Note that the mode and the mean of the Beta distribution is different as the mode is defined by

$$\frac{\alpha - 1}{\alpha + \beta - 2}$$

Coin flipping example-MAP approach


Fort coin tossing example, (bayes theory 이용하여 전개 후 로그 취함)

$$\log p(\theta \mid D) \propto(k+\alpha-1) \log \theta+(n+\beta-1-k) \log (1-\theta)$$

The MAP estimator is determined as follows:

$$\hat{\theta}_{M A P}=\frac{k+\alpha-1}{n+\alpha+\beta-2}$$


MAP 추정은 $\theta$를 추정하는 우리의 이전 믿음을 반영 할 수 있습니다. 그러나 이는 추정에서 불확실성의 척도를 제공하지 않는 포인트 추정치입니다.

Bayesian Neural Network 설명 - Bayesian Neural Network seolmyeong
MAP 추론 과정 일부 (Poestrior 전개)

prior distribution and likelihood


Bayesian Neural Network 설명 - Bayesian Neural Network seolmyeong

posterior distribution


Bayesian Neural Network 설명 - Bayesian Neural Network seolmyeong

Bayesian learning


Bayesian Neural Network 설명 - Bayesian Neural Network seolmyeong

Reference


  • Pattern Recognition and Machine Learning
  • PRML Example Code (git) : github.com/ctgk/PRML