无监督学习:受限玻尔兹曼机与自编码器相关技术解析
立即解锁
发布时间: 2025-09-09 00:26:32 阅读量: 10 订阅数: 16 AIGC 


深度学习实战:TensorFlow数学解析
### 无监督学习:受限玻尔兹曼机与自编码器相关技术解析
在机器学习领域,无监督学习是一个重要的研究方向,其中涉及到多种技术和算法,如马尔可夫链蒙特卡罗方法、受限玻尔兹曼机等。下面将详细介绍这些技术的原理、实现步骤以及相关代码示例。
#### 马尔可夫链蒙特卡罗方法基础
马尔可夫链蒙特卡罗(MCMC)方法是一类用于从概率分布中采样的算法。在使用 MCMC 方法时,需要满足一些条件以确保采样的有效性。
- **详细平衡条件**:在稳态下,概率分布需要满足详细平衡条件,即 $P(A)\sum_{B\neq A}P(B\rightarrow A)=P(B)P(A|B)$。这是概率分布平稳性的充分但非必要条件。这种形式在可能状态空间无限时,在数学上较为方便,因此在 MCMC 方法中被广泛应用。
- **马尔可夫链的条件**
- **不可约性**:马尔可夫链应具备从一个状态转移到任何其他状态的能力。虽然我们通常希望以高概率探索给定状态的附近状态,但有时也需要进行跳跃,以探索可能的高概率区域。
- **非周期性**:马尔可夫链不应过于频繁地重复,否则将无法遍历整个状态空间。例如,在一个有 20 个状态的空间中,如果链在探索 5 个状态后就重复,那么就无法遍历所有 20 个状态,导致采样效果不佳。
#### 梅特罗波利斯算法
梅特罗波利斯算法是一种 MCMC 方法,它利用当前接受的状态来确定下一个状态。以下是该算法的详细实现步骤:
1. **初始化**:选择任意随机样本点 $X(1)$。
2. **生成下一个点**:选择条件依赖于 $X(1)$ 的下一个点 $X(2)$。可以从均值为 $X(1)$、方差为 $S(2)$ 的正态分布中选择 $X(2)$,即 $X(2)\sim Normal(X(1),S(2))$。选择合适的方差 $S(2)$ 非常关键,方差不宜过大或过小。过大的方差会使下一个样本远离当前样本,可能导致高概率区域无法被充分探索;过小的方差则会使下一个样本几乎总是靠近当前点,降低探索不同高概率区域的可能性。
3. **判断是否接受**:
- 如果 $\frac{P(X(2))}{P(X(1))}\geq1$,则接受 $X(2)$ 作为有效样本点,并将其作为生成下一个样本的 $X(1)$。
- 如果 $\frac{P(X(2))}{P(X(1))}<1$,则当该比值大于从均匀分布 $U[0,1]$ 中随机生成的数时,接受 $X(2)$。
下面是使用梅特罗波利斯算法从二元高斯分布中采样的 Python 代码示例:
```python
import numpy as np
import matplotlib.pyplot as plt
# Now let’s generate this with one of the Markov Chain Monte Carlo methods called Metropolis
# Hastings algorithm
# Our assumed transition probabilities would follow normal distribution X2 ~
# N(X1,Covariance= [[0.2 , 0],[0,0.2]])
import time
start_time = time.time()
# Set up constants and initial variable conditions
num_samples = 100000
prob_density = 0
## Plan is to sample from a Bivariate Gaussian Distribution with mean (0,0) and covariance of
## 0.7 between the two variables
mean = np.array([0, 0])
cov = np.array([[1, 0.7], [0.7, 1]])
cov1 = np.matrix(cov)
mean1 = np.matrix(mean)
x_list, y_list = [], []
accepted_samples_count = 0
## Normalizer of the Probability distibution
## This is not actually required since we are taking ratio of probabilities for inference
normalizer = np.sqrt(((2 * np.pi) ** 2) * np.linalg.det(cov))
## Start wtih initial Point (0,0)
x_initial, y_initial = 0, 0
x1, y1 = x_initial, y_initial
for i in range(num_samples):
## Set up the Conditional Probability distribution, taking the existing point
## as the mean and a small variance = 0.2 so that points near the existing point
## have a high chance of getting sampled.
mean_trans = np.array([x1, y1])
cov_trans = np.array([[0.2, 0], [0, 0.2]])
x2, y2 = np.random.multivariate_normal(mean_trans, cov_trans).T
X = np.array([x2, y2])
X2 = np.matrix(X)
X1 = np.matrix(mean_trans)
## Compute the probability density of the existing point and the new sampled
## point
mahalnobis_dist2 = (X2 - mean1) * np.linalg.inv(cov) * (X2 - mean1).T
prob_density2 = (1 / float(normalizer)) * np.exp(-0.5 * mahalnobis_dist2)
mahalnobis_dist1 = (X1 - mean1) * np.linalg.inv(cov) * (X1 - mean1).T
prob_density1 = (1 /
```
0
0
复制全文
相关推荐









