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)
相关推荐
静西子2 分钟前
LLM大语言模型部署到本地(个人总结)
人工智能·语言模型·自然语言处理
cxr82825 分钟前
基于Claude Code的 规范驱动开发(SDD)指南
人工智能·hive·驱动开发·敏捷流程·智能体
Billy_Zuo29 分钟前
人工智能机器学习——决策树、异常检测、主成分分析(PCA)
人工智能·决策树·机器学习
小王爱学人工智能1 小时前
OpenCV的图像金字塔
人工智能·opencv·计算机视觉
北京地铁1号线1 小时前
Qwen-VL(阿里通义千问视觉语言模型)模型架构和损失函数介绍
人工智能·语言模型·自然语言处理
阿豪31 小时前
2025 年职场转行突围:除实习外,这些硬核证书让你的简历脱颖而出(纯经验分享)
大数据·人工智能·经验分享·科技·信息可视化·产品经理
阿杜杜不是阿木木1 小时前
开始 ComfyUI 的 AI 绘图之旅-Stable Diffusion图生图之局部重绘(Inpaint)和扩图(Outpaint)(三)
人工智能·ai·ai作画·aigc·图生图
阿杜杜不是阿木木1 小时前
开始 ComfyUI 的 AI 绘图之旅-Stable Diffusion图生图(二)
人工智能·ai·ai作画·aigc·图生图
九章云极AladdinEdu2 小时前
存算一体芯片生态评估:从三星PIM到知存科技WTM2101
人工智能·pytorch·科技·架构·开源·gpu算力