主动学习框架

本文详细介绍了modAL,一个基于scikit-learn的主动学习框架。内容包括基本流程、定义查询策略(默认策略与自定义策略)、模型(scikit-learn和pytorch)的使用,以及具体方法和存疑问题。modAL支持自带和自定义查询策略,能与多种模型集成,提供灵活的主动学习解决方案。
摘要由CSDN通过智能技术生成

1.ALiPy

官网:http://parnec.nuaa.edu.cn/huangsj/alipy/
github:https://github.com/NUAA-AL/ALiPy
csdn:https://blog.csdn.net/weixin_44575152/article/details/100783835

2.modAL

官网:https://modal-python.readthedocs.io/en/latest/index.html
github:https://github.com/modAL-python/modAL

2.1 modAL基础教程

modAL是基于scikit-learn(机器学习的包)建立的一个框架!框架支持使用自带方法快速开始AL,也支持自主定义模型!目前modAL支持scikit-learn、keras以及pytorch模型!

2.1.1 基本流程

1.引包

2.加载数据

3.定义并初始化学习器,两个重要参数:

·模型(scikit-learn estimator)

·查询策略:若不指定查询策略(可自定义),则将采用默认的---> maximum uncertainty sampling.

4.查询出数据

5.人工标注

6.用查询出的未标注数据对模型进行再训练

上述流程中每个步骤中用到的方法的小综合:
https://modal-python.readthedocs.io/en/latest/content/models/ActiveLearner.html#initialization

#1.引包
from modAL.models import ActiveLearner
from modAL.uncertainty import entropy_sampling
from sklearn.ensemble import RandomForestClassifier

#2.加载数据
X_training="标注数据"
y_training="标注数据的标签"
X_pool="未标注数据"

#3.定义并初始化学习器,若不指定查询策略,则将采用默认的
learner = ActiveLearner(
    #learner里包含模型!
    estimator=RandomForestClassifier(),
    
    #定义学习器使用的查询策略;若这里不定义,那么将采用学习器默认的查询策略
    #query_strategy=entropy_sampling,
    
    #若传入训练数据,初始化learner时会自动训练模型;若未传入,目前发现都会报错
    X_training=X_training, y_training=y_training
)

#4.查询出数据
query_idx, query_inst = learner.query(X_pool)

#5.人工标注
#对查询出的数据进行标注并将标签放入y_new里

#6.用查询出的未标注数据对模型进行再训练
learner.teach(X_pool[query_idx], y_new)
note:关于上述ActiveLearner()中的X_training的一些看法:

X_training:

If the model hasn’t been fitted yet it is None, otherwise it contains the samples which the model has been trained on. If provided, the method fit() of estimator is called during init()

2.1.2 定义查询策略

采用默认查询策略
learner = ActiveLearner(
    #learner里包含模型!
    estimator=RandomForestClassifier(),
    
    #定义学习器使用的查询策略;若这里不定义,那么将采用学习器默认的查询策略
    #query_strategy=entropy_sampling,
    
    #若传入训练数据,初始化learner时会自动训练模型;若未传入,目前发现都会报错
    X_training=X_training, y_training=y_training
)
采用自定义查询策略

查询策略是在定义学习器learner里的,因此这里我们只需要改动learner就好

使用框架自带的策略来定义策略

使用随机抽样:

from modAL.models import ActiveLearner
from modAL.uncertainty import entropy_sampling
from sklearn.ensemble import RandomForestClassifier

learner = ActiveLearner(
    estimator=RandomForestClassifier(),
    #定义学习器使用的查询策略;若这里不定义,那么将采用学习器默认的查询策略
    query_strategy=entropy_sampling,
    X_training=X_training, y_training=y_training
)
完全自定义策略

query stategy in modAL is a function taking (at least) two arguments (an estimator object【训练的模型】 and a pool of examples【未标注数据集】), outputting the index of the queried instance and the instance itself

import numpy as np

#自己定义随机抽样/查询的函数
#classifier在函数中没有用到也要定义,X_pool为未标注数据集
def random_sampling(classifier, X_pool):
    n_samples = len(X_pool)
    query_idx = np.random.choice(range(n_samples))
    return query_idx, X_pool[query_idx]

#query_strategy等于函数名即可
learner = ActiveLearner(
    estimator=RandomForestClassifier(),
    query_strategy=random_sampling,
    X_training=X_training, y_training=y_training
)

采用高斯过程的回归实例

import numpy as np

X = np.random.choice(np.linspace(0, 20, 10000), size=200, replace=False).reshape(-1, 1)
y = np.sin(X) + np.random.normal(scale=0.3, size=X.shape)
#自定义查询策略函数
def GP_regression_std(regressor, X):
    _, std = regressor.predict(X, return_std=True)
    query_idx = np.argmax(std)
    return query_idx, X[query_idx]
from modAL.models import ActiveLearner
from sklearn.gaussian_process import GaussianProcessRegressor
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值