95行代码实现最大熵模型训练

关于最大熵模型的介绍请看:http://www.cnblogs.com/hexinuaa/p/3353479.html

下面是GIS训练算法的python实现,代码不到100行。


from collections import defaultdict

import math


class MaxEnt(object):

    def __init__(self):

        self.feats = defaultdict(int)

        self.trainset = []

        self.labels = set()  

      

    def load_data(self,file):

        for line in open(file):

            fields = line.strip().split()

            # at least two columns

            if len(fields) < 2: continue

            # the first column is label

            label = fields[0]

            self.labels.add(label)

            for f in set(fields[1:]):

                # (label,f) tuple is feature 

                self.feats[(label,f)] += 1

            self.trainset.append(fields)

            

    def _initparams(self):

        self.size = len(self.trainset)

        # M param for GIS training algorithm

        self.M = max([len(record)-1 for record in self.trainset])

        self.ep_ = [0.0]*len(self.feats)

        for i,f in enumerate(self.feats):

            # calculate feature expectation on empirical distribution

            self.ep_[i] = float(self.feats[f])/float(self.size)

            # each feature function correspond to id

            self.feats[f] = i

        # init weight for each feature

        self.w = [0.0]*len(self.feats)

        self.lastw = self.w

        

    def probwgt(self,features,label):

        wgt = 0.0

        for f in features:

            if (label,f) in self.feats:

                wgt += self.w[self.feats[(label,f)]]

        return math.exp(wgt)

            

    """

    calculate feature expectation on model distribution

    """        

    def Ep(self):

        ep = [0.0]*len(self.feats)

        for record in self.trainset:

            features = record[1:]

            # calculate p(y|x)

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是最大模型的 IIS/DFP 算法的 Python 代码实现: ```python import numpy as np class MaxEnt(): def __init__(self, X, Y): self.X = X self.Y = Y self.N = len(X) self.M = len(X[0]) self.build_dict() self.w = np.zeros(self.feature_num) self.calcu_Z() def build_dict(self): self.feat_dict = {} idx = 0 for i in range(self.N): for j in range(self.M): feat = str(j) + '=' + str(self.X[i][j]) + ',' + str(Y[i]) if feat not in self.feat_dict: self.feat_dict[feat] = idx idx += 1 self.feature_num = idx def calcu_Z(self): self.Z = 0 for i in range(self.N): sum_f = 0 for j in range(self.M): f = str(j) + '=' + str(self.X[i][j]) + ',' + str(self.Y[i]) if f in self.feat_dict: sum_f += self.w[self.feat_dict[f]] self.Z += np.exp(sum_f) def calcu_px(self, X): sum_f = 0 for j in range(self.M): f = str(j) + '=' + str(X[j]) + ',' + str(0) if f in self.feat_dict: sum_f += self.w[self.feat_dict[f]] px = np.exp(sum_f) / self.Z return px def calcu_Epfi(self): Epfi = np.zeros(self.feature_num) for i in range(self.N): pxy = self.calcu_px(self.X[i]) for j in range(self.M): f = str(j) + '=' + str(self.X[i][j]) + ',' + str(self.Y[i]) if f in self.feat_dict: Epfi[self.feat_dict[f]] += pxy return Epfi / self.N def train(self, max_iter=1000, tol=1e-5): for it in range(max_iter): w_old = np.copy(self.w) Epfi = self.calcu_Epfi() for i in range(self.feature_num): delta = 1.0 / self.N * np.log(self.Z) - Epfi[i] self.w[i] += delta self.calcu_Z() diff = np.sum(np.abs(self.w - w_old)) if diff < tol: break def predict(self, X): px = self.calcu_px(X) py = np.zeros(2) for i in range(2): sum_f = 0 for j in range(self.M): f = str(j) + '=' + str(X[j]) + ',' + str(i) if f in self.feat_dict: sum_f += self.w[self.feat_dict[f]] py[i] = np.exp(sum_f) / self.Z return np.argmax(py), px ``` 其中,X 和 Y 分别为训练集的特征和标签,feature_num 表示特征的数量,w 是特征的权重,Z 表示归一化因子。calcu_Z 函数和 calcu_px 函数分别计算归一化因子和某个样本的概率值。calcu_Epfi 函数计算经验期望。train 函数使用 IIS/DFP 算法进模型训练。predict 函数根据模型预测输入样本的标签和概率值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值