当前位置: 首页 > ops >正文

TensorFlow中数据集的创建

目录

  • 前言
  • 示例
    • 示例1
    • 示例2
    • 示例3
    • 示例4

前言

TensorFlow 的 tf.data.Dataset API 提供了一种灵活且高效的方式来加载和预处理数据。它可以轻松处理大规模数据集,并支持多种数据源格式。 所有数据集相关的内容都在tf.data中,from_tensor_slices:可以从元组, 列表, 字典, ndarray中创建dataset。

示例

示例1

import tensorflow as tf
import numpy as npdataset = tf.data.Dataset.from_tensor_slices(np.arange(10))  
print (dataset)# 数据集最基础的用法就是取数据
for item in dataset:print(item)

结果如下:

<TensorSliceDataset shapes: (), types: tf.int32>
tf.Tensor(0, shape=(), dtype=int32)
tf.Tensor(1, shape=(), dtype=int32)
tf.Tensor(2, shape=(), dtype=int32)
tf.Tensor(3, shape=(), dtype=int32)
tf.Tensor(4, shape=(), dtype=int32)
tf.Tensor(5, shape=(), dtype=int32)
tf.Tensor(6, shape=(), dtype=int32)
tf.Tensor(7, shape=(), dtype=int32)
tf.Tensor(8, shape=(), dtype=int32)
tf.Tensor(9, shape=(), dtype=int32)

示例2

import tensorflow as tf
import numpy as np# 从元组创建dataset, (x,y)
x = np.array([[1, 2], [3, 4], [5, 6]])
y = np.array(['cat', 'dog', 'fox'])
dataset = tf.data.Dataset.from_tensor_slices((x, y))
for item_x, item_y in dataset:print(item_x.numpy(), item_y.numpy().decode())

结果如下

[1 2] b'cat'
[3 4] b'dog'
[5 6] b'fox'

示例3

import tensorflow as tf
import numpy as np# 从元组创建dataset, (x,y)
x = np.array([[1, 2], [3, 4], [5, 6]])
y = np.array(['cat', 'dog', 'fox'])
dataset = tf.data.Dataset.from_tensor_slices({'feature': x,'label': y
})
for item in dataset:print(item['feature'].numpy(), item['label'].numpy())

结果如下

[1 2] b'cat'
[3 4] b'dog'
[5 6] b'fox'

示例4

import tensorflow as tf
import numpy as np# interleave
# 最常见用法 : 文件名dataset  --> 具体数据集
dataset = tf.data.Dataset.from_tensor_slices(np.arange(10))
dataset = dataset.repeat(3).batch(7)
# map_fn, cycle_length 并行长度, block_length 
dataset = dataset.interleave(lambda v: tf.data.Dataset.from_tensor_slices(v),cycle_length = 5,block_length = 5
)
for item in dataset:print(item)

结果如下

tf.Tensor(0, shape=(), dtype=int32)
tf.Tensor(1, shape=(), dtype=int32)
tf.Tensor(2, shape=(), dtype=int32)
tf.Tensor(3, shape=(), dtype=int32)
tf.Tensor(4, shape=(), dtype=int32)
tf.Tensor(7, shape=(), dtype=int32)
tf.Tensor(8, shape=(), dtype=int32)
tf.Tensor(9, shape=(), dtype=int32)
tf.Tensor(0, shape=(), dtype=int32)
tf.Tensor(1, shape=(), dtype=int32)
tf.Tensor(4, shape=(), dtype=int32)
tf.Tensor(5, shape=(), dtype=int32)
tf.Tensor(6, shape=(), dtype=int32)
tf.Tensor(7, shape=(), dtype=int32)
tf.Tensor(8, shape=(), dtype=int32)
tf.Tensor(1, shape=(), dtype=int32)
tf.Tensor(2, shape=(), dtype=int32)
tf.Tensor(3, shape=(), dtype=int32)
tf.Tensor(4, shape=(), dtype=int32)
tf.Tensor(5, shape=(), dtype=int32)
tf.Tensor(8, shape=(), dtype=int32)
tf.Tensor(9, shape=(), dtype=int32)
tf.Tensor(5, shape=(), dtype=int32)
tf.Tensor(6, shape=(), dtype=int32)
tf.Tensor(2, shape=(), dtype=int32)
tf.Tensor(3, shape=(), dtype=int32)
tf.Tensor(9, shape=(), dtype=int32)
tf.Tensor(0, shape=(), dtype=int32)
tf.Tensor(6, shape=(), dtype=int32)
tf.Tensor(7, shape=(), dtype=int32)
http://www.xdnf.cn/news/4802.html

相关文章:

  • 云原生环境下服务治理体系的构建与落地实践
  • 单片机-STM32部分:10、串口UART
  • SVT-AV1源码学习-EbMotionEstimation.h 学习
  • SSM 框架是指什么,其优缺点,怎样用到在你的程序里
  • 交流中的收获-250508
  • AI Agent | 深度剖析 AI Agent:从基础原理到关键能力
  • 如何在 Logback 日志框架中加入链路 ID
  • 嵌入式开发学习日志Day16
  • MAC电脑日期与时间问题和定位不能正常使用问题
  • mysql数据库体验
  • 国标GB28181软件EasyGBS雪亮工程打造智能高效的视频监控新体系
  • git的常用命令详解
  • 【redis】分片方案
  • 一文读懂Python之requests模块(36)
  • 扣子创建一个应用
  • 基于vm加密的php逆向分析
  • verilog循环仿真
  • Spark处理过程-案例数据清洗
  • Linux命令行参数注入详解
  • 深入剖析ThreadLocal:原理、应用与最佳实践
  • 笔试强训——第七周
  • 前端三大件---CSS
  • 塔能空压系统节能方案:为华东某电子厂降耗赋能
  • JavaSE核心知识点02面向对象编程02-02(封装、继承、多态)
  • 基于LLM的全自动视频生成工具:MoneyPrinterTurbo 技术解析
  • CAN总线通讯接口卡:工业通信的核心桥梁
  • wails3学习-runtime:Window无边框设置
  • 数据结构(四)——栈的应用—数制转换
  • Java线程阻塞方法LockSupport.park()/Thread.sleep()/Object.wait()详解:原理、区别
  • java实战(第六篇):统计投票信息