Ray.tune可视化调整超参数Tensorflow 2.0

2023-12-21 21:18

本文主要是介绍Ray.tune可视化调整超参数Tensorflow 2.0,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


Ray.tune官方文档

调整超参数通常是机器学习工作流程中最昂贵的部分。 Tune专为解决此问题而设计,展示了针对此痛点的有效且可扩展的解决方案。 请注意,此示例取决于Tensorflow 2.0。
Code: ray/python/ray/tune at master · ray-project/ray · GitHub

Examples: https://github.com/ray-project/ray/tree/master/python/ray/tune/examples)

Documentation: Tune: Scalable Hyperparameter Tuning — Ray v1.6.0

Mailing List: https://groups.google.com/forum/#!forum/ray-dev

## If you are running on Google Colab, uncomment below to install the necessary dependencies 
## before beginning the exercise.# print("Setting up colab environment")
# !pip uninstall -y -q pyarrow
# !pip install -q https://s3-us-west-2.amazonaws.com/ray-wheels/latest/ray-0.8.0.dev5-cp36-cp36m-manylinux1_x86_64.whl
# !pip install -q ray[debug]# # A hack to force the runtime to restart, needed to include the above dependencies.
# print("Done installing! Restarting via forced crash (this is not an issue).")
# import os
# os._exit(0)
## If you are running on Google Colab, please install TensorFlow 2.0 by uncommenting below..# try:
#   # %tensorflow_version only exists in Colab.
#   %tensorflow_version 2.x
# except Exception:
#   pass

本教程将逐步介绍使用Tune进行超参数调整的几个关键步骤。

  1. 可视化数据。
  2. 创建模型训练过程(使用Keras)。
  3. 通过调整上述模型训练过程以使用Tune来调整模型。
  4. 分析Tune创建的模型。

请注意,这使用了Tune的基于函数的API。 这主要是用于原型制作。 后面的教程将介绍Tune更加强大的基于类的可训练 API。

import numpy as np
np.random.seed(0)import tensorflow as tf
try:tf.get_logger().setLevel('INFO')
except Exception as exc:print(exc)
import warnings
warnings.simplefilter("ignore")from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Densefrom tensorflow.keras.optimizers import SGD, Adam
from tensorflow.keras.callbacks import ModelCheckpointimport ray
from ray import tune
from ray.tune.examples.utils import get_iris_dataimport inspect
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('ggplot')
%matplotlib inline


Visualize your data

首先让我们看一下数据集的分布。

鸢尾花数据集由3种不同类型的鸢尾花(Setosa,Versicolour和Virginica)的花瓣和萼片长度组成,存储在150x4 numpy中。

行为样本,列为:隔片长度,隔片宽度,花瓣长度和花瓣宽度。


本教程的目标是提供一个模型,该模型可以准确地预测给定的萼片长度,萼片宽度,花瓣长度和花瓣宽度4元组的真实标签。

from sklearn.datasets import load_irisiris = load_iris()
true_data = iris['data']
true_label = iris['target']
names = iris['target_names']
feature_names = iris['feature_names']def plot_data(X, y):# Visualize the data setsplt.figure(figsize=(16, 6))plt.subplot(1, 2, 1)for target, target_name in enumerate(names):X_plot = X[y == target]plt.plot(X_plot[:, 0], X_plot[:, 1], linestyle='none', marker='o', label=target_name)plt.xlabel(feature_names[0])plt.ylabel(feature_names[1])plt.axis('equal')plt.legend();plt.subplot(1, 2, 2)for target, target_name in enumerate(names):X_plot = X[y == target]plt.plot(X_plot[:, 2], X_plot[:, 3], linestyle='none', marker='o', label=target_name)plt.xlabel(feature_names[2])plt.ylabel(feature_names[3])plt.axis('equal')plt.legend();plot_data(true_data, true_label)


创建模型训练过程(使用Keras)

现在,让我们定义一个函数,该函数将包含一些超参数并返回一个可用于训练的模型。

def create_model(learning_rate, dense_1, dense_2):assert learning_rate > 0 and dense_1 > 0 and dense_2 > 0, "Did you set the right configuration?"model = Sequential()model.add(Dense(int(dense_1), input_shape=(4,), activation='relu', name='fc1'))model.add(Dense(int(dense_2), activation='relu', name='fc2'))model.add(Dense(3, activation='softmax', name='output'))optimizer = SGD(lr=learning_rate)model.compile(optimizer, loss='categorical_crossentropy', metrics=['accuracy'])return model

下面是一个使用create_model函数训练模型并返回训练后的模型的函数。

def train_on_iris():train_x, train_y, test_x, test_y = get_iris_data()model = create_model(learning_rate=0.1, dense_1=2, dense_2=2)# This saves the top model. `acc

这篇关于Ray.tune可视化调整超参数Tensorflow 2.0的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/521530

相关文章

Java通过反射获取方法参数名的方式小结

《Java通过反射获取方法参数名的方式小结》这篇文章主要为大家详细介绍了Java如何通过反射获取方法参数名的方式,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、前言2、解决方式方式2.1: 添加编译参数配置 -parameters方式2.2: 使用Spring的内部工具类 -

Python调用另一个py文件并传递参数常见的方法及其应用场景

《Python调用另一个py文件并传递参数常见的方法及其应用场景》:本文主要介绍在Python中调用另一个py文件并传递参数的几种常见方法,包括使用import语句、exec函数、subproce... 目录前言1. 使用import语句1.1 基本用法1.2 导入特定函数1.3 处理文件路径2. 使用ex

MySQL中时区参数time_zone解读

《MySQL中时区参数time_zone解读》MySQL时区参数time_zone用于控制系统函数和字段的DEFAULTCURRENT_TIMESTAMP属性,修改时区可能会影响timestamp类型... 目录前言1.时区参数影响2.如何设置3.字段类型选择总结前言mysql 时区参数 time_zon

Python如何使用seleniumwire接管Chrome查看控制台中参数

《Python如何使用seleniumwire接管Chrome查看控制台中参数》文章介绍了如何使用Python的seleniumwire库来接管Chrome浏览器,并通过控制台查看接口参数,本文给大家... 1、cmd打开控制台,启动谷歌并制定端口号,找不到文件的加环境变量chrome.exe --rem

Python中的可视化设计与UI界面实现

《Python中的可视化设计与UI界面实现》本文介绍了如何使用Python创建用户界面(UI),包括使用Tkinter、PyQt、Kivy等库进行基本窗口、动态图表和动画效果的实现,通过示例代码,展示... 目录从像素到界面:python带你玩转UI设计示例:使用Tkinter创建一个简单的窗口绘图魔法:用

Linux中Curl参数详解实践应用

《Linux中Curl参数详解实践应用》在现代网络开发和运维工作中,curl命令是一个不可或缺的工具,它是一个利用URL语法在命令行下工作的文件传输工具,支持多种协议,如HTTP、HTTPS、FTP等... 目录引言一、基础请求参数1. -X 或 --request2. -d 或 --data3. -H 或

详解Spring Boot接收参数的19种方式

《详解SpringBoot接收参数的19种方式》SpringBoot提供了多种注解来接收不同类型的参数,本文给大家介绍SpringBoot接收参数的19种方式,感兴趣的朋友跟随小编一起看看吧... 目录SpringBoot接受参数相关@PathVariable注解@RequestHeader注解@Reque

Java向kettle8.0传递参数的方式总结

《Java向kettle8.0传递参数的方式总结》介绍了如何在Kettle中传递参数到转换和作业中,包括设置全局properties、使用TransMeta和JobMeta的parameterValu... 目录1.传递参数到转换中2.传递参数到作业中总结1.传递参数到转换中1.1. 通过设置Trans的

java如何调用kettle设置变量和参数

《java如何调用kettle设置变量和参数》文章简要介绍了如何在Java中调用Kettle,并重点讨论了变量和参数的区别,以及在Java代码中如何正确设置和使用这些变量,避免覆盖Kettle中已设置... 目录Java调用kettle设置变量和参数java代码中变量会覆盖kettle里面设置的变量总结ja

spring 参数校验Validation示例详解

《spring参数校验Validation示例详解》Spring提供了Validation工具类来实现对客户端传来的请求参数的有效校验,本文给大家介绍spring参数校验Validation示例详... 目录前言一、Validation常见的校验注解二、Validation的简单应用三、分组校验四、自定义校