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)
相关推荐
FONE_Platform3 分钟前
能源化工行业全面预算解决方案:重塑双碳目标下的财务新动能
大数据·人工智能
前沿在线4 分钟前
从“用系统”到“跑 Agent”:AI 钉钉 1.1 在重新定义企业操作系统 | 前沿在线
人工智能
智能相对论5 分钟前
CES深度观察|AI硬件消费时代的“幕后英雄”走向台前
人工智能·百度
Terrence Shen9 分钟前
【CUDA编程系列】之01
c++·人工智能·深度学习·机器学习
老吴学AI10 分钟前
系列报告十:(Menlo)《2025: The State of Generative AI in the Enterprise》
人工智能·vibe coding
AI即插即用10 分钟前
超分辨率重建 | CVPR 2024 DarkIR:轻量级低光照图像增强与去模糊模型(代码实践)
图像处理·人工智能·深度学习·神经网络·计算机视觉·超分辨率重建
喜欢吃豆15 分钟前
深度解析:FFmpeg 远程流式解复用原理与工程实践
人工智能·架构·ffmpeg·大模型·音视频·多模态
ChaITSimpleLove17 分钟前
AI时代编程范式:“游击战”与“阵地战”的灵活应用
人工智能·ai编程范式·战略思维·战术思维·灵活策略·游击战与阵地战
hacker70717 分钟前
精进Excel图表:AI赋能,成为Excel图表高手
人工智能·信息可视化·excel
OpenBayes19 分钟前
HY-MT1.5-1.8B 支持多语言神经机器翻译;Med-Banana-50K 提供医学影像编辑基准数据
人工智能·深度学习·自然语言处理·数据集·机器翻译·图像生成