使用GitHub项目boml学习双层优化

时间:2024年2月29日-2024年3月6日2024年3月13日-14日, 2024年3月30日-4月1日

额外安装相关内容见Colab配置版

最新进展看BDA项目学习

 

目录

前言

基于对于深度学习的python包的不熟悉程度,以及对于双层优化的不熟悉程度,在长时间自己写代码以保证能有较好运行结果失败的情况下,转而尝试使用TF更低一点的版本,至少保证在别人能运行的基础上可以证实这条路的可用性。

资源

vis-opt-group/BDA

1 构建可以运行的环境

通过执行shell脚本:https://github.com/Cz1544252489/DailyWork/releases/download/BDA/ToBDA.sh

 

2 具体分析代码

先看可以执行的Data_hyper_cleaning.py 文件

可以看此链接:https://github.com/vis-opt-group/BDA/blob/main/test_script/Data_hyper_cleaning.py

导入的主要包有:tensorflow.contrib.layers, sys, os, sklearn, numpy, tensorflow

还使用标准库中的包 argparse制作一个用户友好的命令行界面。

接着定义了如下函数:save_obj, cross_entropy_loss, outer_cross_entropy_loss, F1_score, pollute, get_data, get_fashion_mnist_data, g_logits

 

dut-media-lab/BOML

1 构建可以运行的环境

通过执行shell脚本:https://github.com/Cz1544252489/DailyWork/blob/main/shell%20script/ToBOML.sh

2 具体实例代码使用

下载文件后,安装,参考:https://boml.readthedocs.io/en/latest/

在BOML文件夹下,有example.py文件,可以运行成功。


安装记录:

2024年3月2日 13:15 出现需要降级protobuf包以解决问题的bug,重新安装。

2024年3月2日 14:40 由下面的安装流程后成功运行,实例文件 example.py


安装流程:

  1. 初始化完成后运行下面代码:

  1. 此时如果直接运行会出现先前面的bug,运行下面代码以降低protobuf:

虽然确实会出现一堆乱七八糟的bug,但是确实后面可以运行了。

3 Bug/Warning分析

此类型提示由Numpy给出,表示代码中使用的语法太旧,可以调整但影响不大。


暂未设置环境变量DATASETS_FOLDER,使用了默认位置。


此类型提示由tensorflow给出,表示代码中使用的语法太旧,可以调整但影响不大。


另外,可以使用以下方法避免显示Warning(来自ChatGPT,有交叉验证,但暂时仍然无效):

4 核心模组

见:https://boml.readthedocs.io/en/latest/modules.html

  1. load_data: boml.load_data manages different datasets and generate batches of tasks for training and testing.

  1. Experiment: boml.Experiment manages inputs, outputs and task-specific parameters.

  1. BOMLOptimizer: BOMLOptimizer is the main class in boml, which takes responsibility for the whole process of model construnction and back propagation.

Method: define basic method for following training process, it should be included in [MetaInit, MetaRepr], MetaInit type includes methods like MAML, FOMAML, MT-net, WarpGrad; MetaRepr type includes methods like BA, RHG, TG, HOAG, DARTS;

inner_method: method chosen for solving LLproblem, including [Trad , Simple, Aggr], MetaRepr type choose either Trad for traditional optimization strategies or Aggr for Gradient Aggragation optimization. MetaInit type should choose Simple, and set specific parameters for detailed method choices like FOMAML or MT-net.

outer_method: method chosen for solving ULproblem, including [Reverse ,Simple, DARTS, Implcit], MetaInit type should choose Simple, and set specific parameters for detailed method choices like FOMAML.

truncate_iter: specific parameter for Truncated Gradient method, defining number of iterations to truncate in the Back propagation process

experiments: list of Experiment objects that has already been initialized.

5 核心内置函数

参考页面:https://boml.readthedocs.io/en/latest/builtin.html

  1. BOMLOptimizer.meta_learner: This method must be called once at first to build meta modules and initialize meta parameters and neural networks.

  1. BOMLOptimizer.base_learner: This method has to be called for every experiment and takes responsibility for defining task-specific modules and inner optimizer.

  1. BOMLOptimizer.ll_problem: After construction of neural networks, solutions to lower level problems should be regulated in ll_problem.

  1. BOMLOptimizer.ul_problem: This method define upper level problems and choose optimizer to optimize meta parameters, which should be called afer ll_problem.

  1. aggregate_all: Finally, aggregate_all has to be called to aggregate gradient of different tasks, and define operations to apply outer gradients and update meta parametes.

  1. run

6 例子分析

example.py的内容

 

对第一行 from boml import utils,查看文件 boml中的utils.py

7 要理解代码首先需要解决的问题

  1. 导入数据后,数据的结构如何?是以什么形式存储的?

  2. 生成的实验(ex)是什么样的形式?

  3. 生成的优化模型(boml_ho)是什么样的形式?

  4. 上下层损失函数,以及优化方法的具体流程(以一种为例);

  5. 完成计算后的数据如何保存,或者是否以某种格式进行了保存?

8 以上述问题为导向理解boml项目

这种类型OmniglotMetaDataset的所包含的属性/方法如下:

该类型的定义见(lines 189-266):https://github.com/dut-media-lab/BOML/blob/master/boml/load_data/datasets/load_full_dataset.py#L189

其属性或方法如下:

优化模型boml_ho的定义见(lines 43-629):https://github.com/dut-media-lab/BOML/blob/master/boml/boml_optimizer/optimizer.py#L43

 

使用boml模拟常见的所有类型的双层优化问题

参考地址:https://github.com/dut-media-lab/BOML

可能的选择

对于三个选项可能的选择

  1. MetaInit 包括 MAML, FOMAML, MT-net, WarpGard

  2. MetaFeat 包括 BDA, RHG, TRHG, Implicit HG, DARTS

  1. MetaFeat type choose either Trad for traditional optimization strategies or Aggr for Gradient Aggregation optimization

  2. MetaInit type should choose Simple, and set specific parameters for detailed method choices like FMAML or MT-net.

  1. MetaInit type should choose Simple, and set specific parameters for detailed method choices like FMAML

组合(2024年3月6日)

见:https://github.com/dut-media-lab/BOML/blob/master/boml/boml_optimizer/optimizer.py#L75

MetaFeat->Simple->Simple: 结果见MFSS

MetaFeat->Aggr->Darts: 结果见MFAD

MetaFeat->Aggr->Reverse: 结果见MFAR

MetaFeat->Trad->Reverse: 结果见MFTR

MetaFeat->Trad->Implicit: 结果见MFTI

MetaFeat->Trad->Darts: 结果见MFTD

MetaInit->Simple->Simple: 默认配置,结果见MISS

MetaInit->Aggr->Darts: 结果见MIAD, 有bug,参数配置问题

MetaInit->Aggr->Reverse: 结果见MIAR, 有bug,参数配置问题

MetaInit->Trad->Reverse : 结果见MITR, 有bug,

MetaInit->Trad->Implicit: 结果见MITI, 有bug,

MetaInit->Trad->Darts: 结果见MITD, 有bug,

问题总结分类:

  1. hpyer_parameter 需要被初始化,AssertionError (MIAD,MIAR)

  2. outer parameter 需要在优化中分离,AssertionError(MITR, MITI, MFTR, MFTR, MFTI)

  3. 值不存在,ValueError (MITD)

  4. first_order,KeyError (MFSS)

  5. BDA问题, AssertionError (MFAD, MFAR)

  6. 其他(MFTD)

具体Debug和分析(3月13日)


第一个:MFSS

MFSS: 关注字典param_dict的字段first_order没有定义的问题;

该字段的定义在:https://github.com/dut-media-lab/BOML/blob/master/boml/lower_iter/simple.py#L63

其类型为OrderedDict,在此处collections中引入了OrderedDict

其中collections是python的一个内置包,用于在python3.7之前保留字典的插入顺序。

此处给出了:param_dict是不同算法的通用参数的字典;

进一步发现first_order的定义在:https://github.com/dut-media-lab/BOML/blob/master/boml/boml_optimizer/optimizer.py#L271

其是一个特殊的参数用于定义是否执行一阶MAML(FMAML),默认是FALSE


第二个:MISS

这个可以运行,尝试从具体的运行逻辑上认识整个程序;

发现有其他三个可以运行的版本

下面目标更新为分析以下代码:https://github.com/dut-media-lab/BOML/tree/master/exp_scripts

  1. maml_simple_version.json文件:

  1. rhg_simple_version.json文件:

  1. trhg_simple_version.json文件:

例子脚本的使用与修改

  1. test_meta_init.py 参看:https://github.com/dut-media-lab/BOML/blob/master/test_script/test_meta_init.py

 

以下是一段可以用于修改的脚本

 

 

其他


一段用于在python命令行环境中的代码(容易复制)