TensorFlow中数据集的创建

目录

前言

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

示例

示例1

python 复制代码
import tensorflow as tf
import numpy as np


dataset = tf.data.Dataset.from_tensor_slices(np.arange(10))  
print (dataset)

# 数据集最基础的用法就是取数据
for item in dataset:
    print(item)

结果如下:

powershell 复制代码
<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

python 复制代码
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())

结果如下

powershell 复制代码
[1 2] b'cat'
[3 4] b'dog'
[5 6] b'fox'

示例3

python 复制代码
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())

结果如下

powershell 复制代码
[1 2] b'cat'
[3 4] b'dog'
[5 6] b'fox'

示例4

python 复制代码
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)

结果如下

powershell 复制代码
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)
相关推荐
一切皆有可能!!3 小时前
实践篇:利用ragas在自己RAG上实现LLM评估②
人工智能·语言模型
月白风清江有声5 小时前
爆炸仿真的学习日志
人工智能
华奥系科技6 小时前
智慧水务发展迅猛:从物联网架构到AIoT系统的跨越式升级
人工智能·物联网·智慧城市
R²AIN SUITE6 小时前
MCP协议重构AI Agent生态:万能插槽如何终结工具孤岛?
人工智能
b***25117 小时前
动力电池点焊机:驱动电池焊接高效与可靠的核心力量|比斯特自动化
人工智能·科技·自动化
Gyoku Mint7 小时前
机器学习×第二卷:概念下篇——她不再只是模仿,而是开始决定怎么靠近你
人工智能·python·算法·机器学习·pandas·ai编程·matplotlib
小和尚同志7 小时前
通俗易懂的 MCP 概念入门
人工智能·aigc
dudly7 小时前
大语言模型评测体系全解析(下篇):工具链、学术前沿与实战策略
人工智能·语言模型
zzlyx997 小时前
AI大数据模型如何与thingsboard物联网结合
人工智能·物联网
说私域8 小时前
定制开发开源AI智能名片驱动下的海报工厂S2B2C商城小程序运营策略——基于社群口碑传播与子市场细分的实证研究
人工智能·小程序·开源·零售