keras优化算法_超参优化工具总结(4)——Ray.tune

本文介绍了Ray.tune这一高度集成的AutoML框架,特别是其在超参数优化上的应用。Ray.tune支持多种优化算法,如Hyperband、PBT,并能在Ray分布式计算框架上运行,适用于PyTorch、Keras等多个机器学习框架。通过简单的代码,用户可以进行多节点分布式超参数搜索,并使用TensorBoard进行结果可视化。文章提供了Keras在MNIST数据集上的优化示例,展示了如何定义搜索空间和配置资源。
摘要由CSDN通过智能技术生成

5095969c35d4d43cecd5757fac56dbca.png

Homepage:https://github.com/ray-project/ray/tree/master/python/ray/tune

特性:集成了多种超参优化方法,运行在Ray分布式计算框架上,拓展性强。Ray是一种高度集成的Automl框架,内容较多,我们在此只总结其结合超参优化的部分——Ray.tune

Tune 结构总览

2ffe588c6933b116bd3cdc55a05217fb.png
Tune可接受用户定义的Python function或class,并根据从超参空间中取出的一组超参配置(hyperparameter configurations)对其进行评估;每组超参配置(hyperparameter configurations)组成的评估可称为一次Trail,并且Tune支持多个Trails并行运行。其中配置(configuration)可以从Tune中生成,也可以从用户指定的搜索算法中获得。而Trail由Schedulers进行安排和管理。

使用特色:

  1. 用不到10行代码即可完成多节点分布式超参数搜索
  2. 支持所有现有机器学习框架(framework),包括PyTorch, XGBoost, MXNet, 和Keras等
  3. 使用TensorBoard可视化结果。
  4. 能选择任何可扩展的先进算法,例如Population Based Training(PBT), Vizier’s Median Stopping Rule, HyperBand/ASHA。

使用方法及配置

使用方法

https://ray.readthedocs.io/en/latest/tune.html

基于pytoch利用small grid search 优化CNN的示例

import torch.optim as optim
from ray import tune
from ray.tune.examples.mnist_pytorch import get_data_loaders, ConvNet, train, test
​
#定义训练模型及优化器,搜索空间
def train_mnist(config):
    train_loader, test_loader = get_data_loaders()
    model = ConvNet()
    optimizer = optim.SGD(model.parameters(), lr=config["lr"])
    for i in range(10):
        train(model, optimizer, train_loader)
        acc = test(model, test_loader)
        tune.track.log(mean_accuracy=acc)
​
#调用ray.tune进行优化
analysis = tune.run(
    train_mnist, config={"lr": tune.grid_search([0.001, 0.01, 0.1])})
​
#输出结果
print("Best config: ", analysis.get_best_config(metric="mean_accuracy"))
#获取分析实验结果的dataframe
df = analysis.dataframe()
#利用tensoboard进行分析
tensorboard --logdir ~/ray_results

分布式使用方法入门

支持系统:macOS + linux

是否支持GPU:是

优化库基于

  • ray
  • 所有现有机器学习框架(framework),包括PyTorch, XGBoost, MXNet, 和Keras等

优化算法

  • 先进算法,如Hyperband(最低限度地训练模型来确定超参数的影响)
  • AsyncHyperBand
  • 基于群体的训练算法(Population Based Training,在共享超参数下同时训练和优化一系列网络)
  • Nevergrad()
  • 贝叶斯优化算法和Ax(AX 是PyTorch中贝叶斯优化的一个平台)
  • Hyperopt方法搜索(Tree-structured Parzen Estimators)和中值停止规则(如果模型性能低于中等性能则停止训练)
  • Grid Search and Random Search(only uses the default search space and variant generation process 其他都需单独定义)
  • BayesOpt
  • Scikit-Optimize
  • BOHB(Bayesian Optimization HyperBand)

适用范围:Deep Learning, Reinforcement Learning

分布式计算:Ray分布式计算框架(ray.remote执行远程进程操作)

分布式计算

Tune Distributed Experiments

本地集群

cluster_name: local-default
provider:
    type: local
    head_ip: YOUR_HEAD_NODE_HOSTNAME
    worker_ips: [WORKER_NODE_1_HOSTNAME, WORKER_NODE_2_HOSTNAME, ... ]
auth: {ssh_user: YOUR_USERNAME, ssh_private_key: ~/.ssh/id_rsa}
## Typically for local clusters, min_workers == max_workers.
min_workers: 3
max_workers: 3
setup_commands:  # Set up each node.
    - pip install ray torch torchvision tabulate tensorboard

云集群

cluster_name: tune-default
provider: {type: aws, region: us-west-2}
auth: {ssh_user: ubuntu}
min_workers: 3
max_workers: 3
# Deep Learning AMI (Ubuntu) Version 21.0
head_node: {InstanceType: c5.xlarge, ImageId: ami-0b294f219d14e6a82}
worker_nodes: {InstanceType: c5.xlarge, ImageId: ami-0b294f219d14e6a82}
setup_commands: # Set up each node.
    - pip install ray torch torchvision tabulate tensorboard"

搜索空间定义

hyperparameter_space = {
    ""lr"": tune.loguniform(0.001, 0.1),  
    ""dense_1"": tune.uniform(2, 128),
    ""dense_2"": tune.uniform(2, 128),
}(tensorflow) //定义超参和它的搜索空间
def generate_hyperparameters():
    return {
        ""learning_rate"": 10**np.random.uniform(-5, 1),
        ""batch_size"": np.random.randint(1, 100),
        ""momentum"": np.random.uniform(0, 1)
    }(torch)
"
​
​

输出示例

1.直接输出结果

以基于Keras对Mnist的优化结果为例:

超参空间为:

config={
            "threads": 2,
            "lr": tune.sample_from(lambda spec: np.random.uniform(0.001, 0.1)),
            "momentum": tune.sample_from(
                lambda spec: np.random.uniform(0.1, 0.9)),
            "hidden": tune.sample_from(
                lambda spec: np.random.randint(32, 512)),
        })

10个Trails,使用24GPUs,8GPUs的环境,每个Trail指定3CPUs+1GPUs,同时可以并行训练6个Trails。结果如下:

98c62df6a2bcc9e3adf246caa921612a.png

训练结果中可看出第3个Trail准确率最高,为0.992,其中Result放入路径:Result logdir: /root/ray_results/exp中,后续可用Tensorboard读取分析。

而每个Trail具体结果也进行输出:(以第八个一个Trail为例)

d5a730b8d2650cf1ea5a6b89d203bb77.png

以基于Torch对Mnist的优化结果为例:

3个Trails,超参空间为:

config={"lr": tune.grid_search([0.001, 0.01, 0.1])})

b005f2826b455c27e7e7a4bf2165e39b.png

983025cca7b99c2555b0ee0afb620711.png

2.分析结果:

输出结果可用Tensorboard读取,包括训练时每个trial用到的不同超参,以及每个trail loss随时间变化

输出~/ray_results/tune_iris(example)
可用tensorboard读取

5a0af1ca7724780d11401934f43978ed.png

更多的示例代码

Ray中GPU使用方法

https://ray.readthedocs.io/en/latest/using-ray-with-gpus.html#gpu-support

1.Ray对GPU的基本调用方法

通过Ray.init(),Ray会自动初始化并检测可用GPU的数量,即tensorflow会自动检测gpu并进行调用(通常会检测所有可用资源,包括CPU,Memory等);同时,也可以通过ray.init(num_gpus=N) or ray start --num-gpus=N指定(也可通过num_cpusmemoryobject_store_memory等对整个程序可调用的cpu数量,内存等进行限制),类似于CUDA_VISIBLE_DEVICES

如:本身有机器性能为:12 CPUs,12 GPUs

Ray.init()后,该程序总共可支配12 CPUs,12 GPUs

Ray.init(num_cpus=6,num_gpus=3)后,该程序可总共可支配6 CPUs,3 GPUs

Ray.init(num_cpus=15,num_gpus=3)后,该程序运行错误,因为超出可支配范围

2.指定每个Trail中GPU等资源使用情况(基于Tensorflow和keras)示例程序

通过给tune.run()传入参数resources_per_trial={}来指定每个Trial所占用的资源如下:

tune.run(
        num_samples=10,
        resources_per_trial={
            "cpu": 3,
            "gpu": 1,
        },
        )

其中num_samples指的是从搜索空间中采样的数量,即Trial的总数量;在机器仍具备可获资源进行Trial时,会并行进行多个Trail,如按以上设定,总可用12GPUs,12CPUs,那么会最多同时并行4个Trail,其余Trail在队列中等待。

3.Ray远程调用GPU(未成功复现,缺少相应远程调用环境)

Ray使远程功能可以在ray.remote() 装饰器中指定其GPU要求:@ray.remote(num_gpus=1)

在远程函数内部,对的调用ray.get_gpu_ids()将返回一个整数列表,指示允许远程函数使用哪些GPU。通常,不需要调用ray.get_gpu_ids()因为Ray会自动设置CUDA_VISIBLE_DEVICES环境变量。实际使用时Ray只能保留GPU,通过TensorFlow的GPU版本来调用GPU

基于ray.remote()分割GPU的例子:

import ray
import time
​
ray.init(num_cpus=4, num_gpus=1)
​
@ray.remote(num_gpus=0.25)
def f():
    time.sleep(1)
​
# The four tasks created here can execute concurrently.
ray.get([f.remote() for _ in range(4)])

原理是在多个任务共享一个GPU时,通过人为的限制,让每个任务所占用的GPU memory不超过所设比例;并且TensorFlow可以通过设定来限制其GPU Memory使用率

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
APSO(Adaptive Particle Swarm Optimization)是一种优化算法,可以用于优化LSTM的超参数。Keras提供了一个方便的接口,可以轻松地使用APSO优化LSTM。下面是一个简单的例子: 首先,定义一个函数来构建LSTM模型,该函数将超参数作为参数输入,并返回一个编译好的模型: ```python from keras.models import Sequential from keras.layers import LSTM, Dense def build_model(num_units, dropout_rate, input_shape, output_shape): model = Sequential() model.add(LSTM(num_units, input_shape=input_shape, dropout=dropout_rate)) model.add(Dense(output_shape, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model ``` 接下来,定义一个函数来计算LSTM模型的适应度(即模型的性能)。在这个例子中,我们将使用交叉验证来评估模型的性能: ```python from sklearn.model_selection import KFold from keras.utils import to_categorical def compute_fitness(params, X, y): num_folds = 3 kf = KFold(n_splits=num_folds) scores = [] for train_index, test_index in kf.split(X): model = build_model(params['num_units'], params['dropout_rate'], X[train_index].shape[1:], y.shape[1]) model.fit(X[train_index], to_categorical(y[train_index]), epochs=10, batch_size=32, verbose=0) score = model.evaluate(X[test_index], to_categorical(y[test_index]), verbose=0) scores.append(score[1]) return sum(scores) / len(scores) ``` 最后,使用APSO算法来优化LSTM的超参数: ```python from apso import APSO X = ... y = ... def objective_function(params): return -compute_fitness(params, X, y) pso = APSO(num_particles=10, dim=2, lb=[16, 0.1], ub=[128, 0.5], max_iter=20, objective_function=objective_function) pso.run() best_params = pso.get_global_best_position() print('Best params:', best_params) print('Best fitness:', -pso.get_global_best_fitness()) ``` 在这个例子中,我们使用APSO算法来搜索LSTM的num_units和dropout_rate两个超参数的最佳值。我们将这两个值限制在一定的范围内,并设置粒子数为10,最大迭代次数为20。运行完毕后,我们得到了最佳的超参数组合。 这是一个简单的例子,你可以根据自己的需求来修改和定制化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值