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)
相关推荐
昵称是6硬币1 小时前
YOLO26论文精读(逐段解析)
人工智能·深度学习·yolo·目标检测·计算机视觉·yolo26
wwlsm_zql3 小时前
「赤兔」Chitu 框架深度解读(十四):核心算子优化
人工智能·1024程序员节
AKAMAI5 小时前
Fermyon推出全球最快边缘计算平台:WebAssembly先驱携手Akamai云驱动无服务器技术新浪潮
人工智能·云计算·边缘计算
云雾J视界6 小时前
TMS320C6000 VLIW架构并行编程实战:加速AI边缘计算推理性能
人工智能·架构·边缘计算·dsp·vliw·tms320c6000
想ai抽6 小时前
基于AI Agent的数据资产自动化治理实验
人工智能·langchain·embedding
小马过河R7 小时前
AIGC视频生成之Deepseek、百度妙笔组合实战小案例
人工智能·深度学习·计算机视觉·百度·aigc
june-Dai Yi7 小时前
免费的大语言模型API接口
人工智能·语言模型·自然语言处理·chatgpt·api接口
王哈哈^_^8 小时前
【数据集】【YOLO】【目标检测】农作物病害数据集 11498 张,病害检测,YOLOv8农作物病虫害识别系统实战训推教程。
人工智能·深度学习·算法·yolo·目标检测·计算机视觉·1024程序员节
数据库安全8 小时前
牛品推荐|分类分级效能飞跃:美创智能数据安全分类分级平台
大数据·人工智能·分类
却道天凉_好个秋8 小时前
卷积神经网络CNN(六):卷积、归一化与ReLU总结
人工智能·神经网络·cnn