tan模型训练代码

本文深入探讨了随机TAN算法的实现细节,包括条件互信息的计算、最大权重选择及网络构建过程。通过具体代码解析,揭示了算法如何通过迭代选择最佳属性节点,构建条件依赖网络。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

void randomtan::finalisePass()
{
    assert(trainingIsFinished_ == false);
    crosstab<float> cmi = crosstab<float>(noCatAtts_);
    getCondMutualInf(xxyDist_, cmi);
    
    // find the maximum spanning tree
   
    CategoricalAttribute firstAtt = 0;

    parents_[firstAtt] = NOPARENT;
    
    float *maxWeight;
    CategoricalAttribute *bestSoFar;
    CategoricalAttribute topCandidate = firstAtt;
    std::set<CategoricalAttribute> available;

    safeAlloc(maxWeight, noCatAtts_);
    safeAlloc(bestSoFar, noCatAtts_);

    maxWeight[firstAtt] = -std::numeric_limits<float>::max();

    //此处通过第一次遍历选择出来第一个节点之后的条件互信息最大的节点加入当前网络,并且更新
    //当前剩余节点的最好的父节点信息。
    for (CategoricalAttribute a = firstAtt + 1; a < noCatAtts_; a++)
    {
        //maxWeigt用来表示当前节点和已经加入网络中的所有节点集合中使得权重最大的节点的值
        maxWeight[a] = cmi[firstAtt][a];
        if (cmi[firstAtt][a] > maxWeight[topCandidate])
            //opCondidate用来在还没有加入网络的节点中选择和已经加入网络的节点使得条件互信息最大的节点
            topCandidate = a;
        bestSoFar[a] = firstAtt;
        //将除第一个节点外剩余的节点全部加入available
        available.insert(a);
    }

    while (!available.empty())
    {
        //current相当于是马上就要加入网络的最优节点
        const CategoricalAttribute current = topCandidate;
        parents_[current] = bestSoFar[current];
        //将第一个和firstAtt节点相连条件互信息最大的节点从available中除去
        available.erase(current);
        if (!available.empty())
        {
            //遍历剩余的还没有加入网络的节点
            topCandidate = *available.begin();
            for (std::set<CategoricalAttribute>::const_iterator it =
                    available.begin(); it != available.end(); it++)
            {
                //用来更新maxWeiight的值以及更新节点的最优父节点
                //如果对于每一个还未加入网络的节点和已经在网络中的节点的条件互信息小于
                //和刚刚加入的节点的条件互信息,则更新还未加入网络中节点和此时网络中节点的条件互信息。
                //并且更新还未加入网络中的最优父节点信息。使其指向刚刚加入网络中的节点
                if (maxWeight[*it] < cmi[current][*it])
                {
                    maxWeight[*it] = cmi[current][*it];
                    bestSoFar[*it] = current;
                }
                //不断更新找到下一个maxWeight最大的值
                if (maxWeight[*it] > maxWeight[topCandidate])
                    topCandidate = *it;
            }
        }
    }

    delete[] bestSoFar;
    delete[] maxWeight;

    trainingIsFinished_ = true;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值