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

相关文章

Andrej Karpathy最新采访:认知核心模型10亿参数就够了,AI会打破教育不公的僵局

夕小瑶科技说 原创  作者 | 海野 AI圈子的红人,AI大神Andrej Karpathy,曾是OpenAI联合创始人之一,特斯拉AI总监。上一次的动态是官宣创办一家名为 Eureka Labs 的人工智能+教育公司 ,宣布将长期致力于AI原生教育。 近日,Andrej Karpathy接受了No Priors(投资博客)的采访,与硅谷知名投资人 Sara Guo 和 Elad G

C++11第三弹:lambda表达式 | 新的类功能 | 模板的可变参数

🌈个人主页: 南桥几晴秋 🌈C++专栏: 南桥谈C++ 🌈C语言专栏: C语言学习系列 🌈Linux学习专栏: 南桥谈Linux 🌈数据结构学习专栏: 数据结构杂谈 🌈数据库学习专栏: 南桥谈MySQL 🌈Qt学习专栏: 南桥谈Qt 🌈菜鸡代码练习: 练习随想记录 🌈git学习: 南桥谈Git 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈�

如何在页面调用utility bar并传递参数至lwc组件

1.在app的utility item中添加lwc组件: 2.调用utility bar api的方式有两种: 方法一,通过lwc调用: import {LightningElement,api ,wire } from 'lwc';import { publish, MessageContext } from 'lightning/messageService';import Ca

4B参数秒杀GPT-3.5:MiniCPM 3.0惊艳登场!

​ 面壁智能 在 AI 的世界里,总有那么几个时刻让人惊叹不已。面壁智能推出的 MiniCPM 3.0,这个仅有4B参数的"小钢炮",正在以惊人的实力挑战着 GPT-3.5 这个曾经的AI巨人。 MiniCPM 3.0 MiniCPM 3.0 MiniCPM 3.0 目前的主要功能有: 长上下文功能:原生支持 32k 上下文长度,性能完美。我们引入了

AI(文生语音)-TTS 技术线路探索学习:从拼接式参数化方法到Tacotron端到端输出

AI(文生语音)-TTS 技术线路探索学习:从拼接式参数化方法到Tacotron端到端输出 在数字化时代,文本到语音(Text-to-Speech, TTS)技术已成为人机交互的关键桥梁,无论是为视障人士提供辅助阅读,还是为智能助手注入声音的灵魂,TTS 技术都扮演着至关重要的角色。从最初的拼接式方法到参数化技术,再到现今的深度学习解决方案,TTS 技术经历了一段长足的进步。这篇文章将带您穿越时

如何确定 Go 语言中 HTTP 连接池的最佳参数?

确定 Go 语言中 HTTP 连接池的最佳参数可以通过以下几种方式: 一、分析应用场景和需求 并发请求量: 确定应用程序在特定时间段内可能同时发起的 HTTP 请求数量。如果并发请求量很高,需要设置较大的连接池参数以满足需求。例如,对于一个高并发的 Web 服务,可能同时有数百个请求在处理,此时需要较大的连接池大小。可以通过压力测试工具模拟高并发场景,观察系统在不同并发请求下的性能表现,从而

多路转接之select(fd_set介绍,参数详细介绍),实现非阻塞式网络通信

目录 多路转接之select 引入 介绍 fd_set 函数原型 nfds readfds / writefds / exceptfds readfds  总结  fd_set操作接口  timeout timevalue 结构体 传入值 返回值 代码 注意点 -- 调用函数 select的参数填充  获取新连接 注意点 -- 通信时的调用函数 添加新fd到

Python:豆瓣电影商业数据分析-爬取全数据【附带爬虫豆瓣,数据处理过程,数据分析,可视化,以及完整PPT报告】

**爬取豆瓣电影信息,分析近年电影行业的发展情况** 本文是完整的数据分析展现,代码有完整版,包含豆瓣电影爬取的具体方式【附带爬虫豆瓣,数据处理过程,数据分析,可视化,以及完整PPT报告】   最近MBA在学习《商业数据分析》,大实训作业给了数据要进行数据分析,所以先拿豆瓣电影练练手,网络上爬取豆瓣电影TOP250较多,但对于豆瓣电影全数据的爬取教程很少,所以我自己做一版。 目

struts2中的json返回指定的多个参数

要返回指定的多个参数,就必须在struts.xml中的配置如下: <action name="goodsType_*" class="goodsTypeAction" method="{1}"> <!-- 查询商品类别信息==分页 --> <result type="json" name="goodsType_findPgae"> <!--在这一行进行指定,其中lis是一个List集合,但

基于SSM+Vue+MySQL的可视化高校公寓管理系统

系统展示 管理员界面 宿管界面 学生界面 系统背景   当前社会各行业领域竞争压力非常大,随着当前时代的信息化,科学化发展,让社会各行业领域都争相使用新的信息技术,对行业内的各种相关数据进行科学化,规范化管理。这样的大环境让那些止步不前,不接受信息改革带来的信息技术的企业随时面临被淘汰,被取代的风险。所以当今,各个行业领域,不管是传统的教育行业