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

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)

相关文章:

  • 《时序数据库全球格局:国产与国外主流方案的对比分析》
  • 数据库的SQLSTATE[23000]异常,通过自定义异常类来提供更友好的提示信息
  • 2021-11-15 C++下一个生日天数
  • UnityDots学习(五)
  • 【大模型】LLM概念相关问题(上)
  • C语言编程--19.括号生成
  • 【数据机构】2. 线性表之“顺序表”
  • 行业 |四大痛点待破:“拆解”DeepSeek一体机
  • 布隆过滤器:高效的数据结构与应用详解
  • Node.js 24.0 正式发布:性能跃升与开发体验全面升级
  • 【AI论文】ZeroSearch:在不搜索的情况下激励LLM的搜索能力
  • 基于CNN的猫狗图像分类系统
  • MQTT:轻量级物联网通信协议详解
  • 在ISOLAR A/B 工具使用UDS 0x14服务清除单个DTC故障的配置
  • 大模型提示词策略
  • 电子电路:白炽灯发光能说明电子正在消散消失吗?
  • Open CASCADE学习|实现裁剪操作
  • Kotlin中Lambda表达式和匿名函数的区别
  • ISP流程介绍(Rgb格式阶段)
  • 【数据结构】线性表--链表(二)
  • 国家发改委:美芯片药品等领域关税影响全球科技发展,损害人类共同利益
  • 75岁亚当·费舍尔坐镇,再现80分钟马勒《第九交响曲》
  • 盖茨:20年内将捐出几乎全部财富,盖茨基金会2045年关闭
  • 公募基金解读“一揽子金融政策”:增量财政空间或打开,有助于维持A股活力
  • 人民日报钟声:中方维护自身发展利益的决心不会改变
  • 美联储如期按兵不动,强调“失业率和通胀上升的风险均已上升”(声明全文)