CH06最大熵模型

1.最大熵模型:对于未知变量,倾向于最大熵(均匀分布),因为这样最保险

2.学习时候进行最大似然估计

约束条件:也就是求出的条件分布尽量符合数据情况

3.求解的时候使用拉格朗日方法,

先求w才行,但是这个对于P是凸函数,就对P求导,

求导得到这样的式子:

我们要继续求解wi是什么,有几个方法,

2.logistic regression代码:

import sklearn
from sklearn import datasets
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
data = datasets.load_iris()
X = data.data[:100, :2]
X_ap = np.ones(100)
X = np.c_[X, X_ap]
y = data.target[:100]
setosa = plt.scatter(X[:50, 0],X[:50, 1], c='b')
ver = plt.scatter(X[50:, 0],X[50:, 1], c='r')
plt.xlabel("Sepal Length")
plt.ylabel("Seqal Width")
plt.legend((setosa, ver),("setosa","ver"))
def sigmoid(theta, x):
    return 1.0 / (1 + np.exp(-(x * theta)))


def gradient_descent(theta, x, y, alpha=0.001):
 #   x = (x - np.mean(x, axis=0)) / (np.std(x, axis=0))
    
    
    for _ in range(500):
        
        theta = theta - alpha * x.transpose() * (sigmoid(theta, x) - y) 
        if _ >= 450:
            print(theta)
        
    return theta
def predict(theta, x):
    a = sigmoid(theta, x)
    pred_value = np.where(a>=0.5, 1, 0)
    return pred_value

theta = np.ones((3,1))
X = np.mat(X)
y = np.mat(y).transpose()

theta = gradient_descent(theta, X, y)
print(y)
predict(theta, X)

3.最大熵模型如何求解:

L(p,w)对p求导,得到p用w表示,然后带入L,称为L关于w的最大求值,。

不明白,为什么这一步不求导呢,得到w的取值,是因为可能鞍点吗?

网上给出的方法:梯度下降,牛顿法,拟牛顿法,还有IIS(改进的迭代尺度)

模型学的结果和极大似然的结果是一样的,

日常时候我们使用的是模型慢慢学习,极大似然只是一种理论?

12.30关于最大熵模型的f(x,y)

f(x,y)是指,只要有这样的x和y配对,那么f就等于1,是想列举样本,

f被称为特征函数,是想提取特征从而送到下一步。x和y不一定非要取特定的某个值,而是xhey满足某一类关系也可以,比如说

f_3(x,y) = \begin{cases} 1, & \text{if } x[2] = {\text{'not'}} \text{ and } x[3] \in{\text{positive\_words}} \text{ and } y=-1\\ 0, & \text{others} \end{cases}

实现过程:

  1. 得到特征和特征函数f
  2. 得到经验分布p~(x,y)和p~(x),和它们的期望
  3. 计算w的改变量。(使用IIS算法)
  4. 参考博客:https://blog.csdn.net/openSUSE1995/article/details/77835782

猜你喜欢

转载自blog.csdn.net/yagreenhand/article/details/85274914