Data structure:Dataset.element_spec/

本文介绍了如何使用TensorFlow 2.2.0中的Dataset.element_spec来查看数据集元素的类型。包括单个组件类型、组合成元组的两种数据类型以及嵌套元组的组件类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

tensorflow==2.2.0

1.用Dataset.element_spec查看一个elementde 的类型 (1 ) fit in a single component

import tensorflow as tf
import pathlib
import matplotlib.pyplot as plt
import numpy as np

dataset1 = tf.data.Dataset.from_tensor_slices(tf.random.uniform([4,10]))
print(dataset1.element_spec) # 查看的是每一个element的属性
for element in dataset1:
    print(element)

shuchu

TensorSpec(shape=(10,), dtype=tf.float32, name=None)  # a single tuple
tf.Tensor(
[0.767676   0.73015213 0.5323138  0.50220263 0.95328426 0.19030821
 0.80313885 0.7522975  0.38625753 0.55641997], shape=(10,), dtype=float32)
tf.Tensor(
[0.02963817 0.6099347  0.94547665 0.51258385 0.45178926 0.9401567
 0.6142694  0.8651656  0.47888255 0.6972141 ], shape=(10,), dtype=float32)
tf.Tensor(
[0.1285268  0.9309876  0.5018612  0.3873167  0.98936284 0.35167396
 0.5177171  0.8133037  0.03449237 0.9925858 ], shape=(10,), dtype=float32)
tf.Tensor(
[0.24921525 0.45211852 0.5103594  0.30220544 0.30971515 0.15253949
 0.00923371 0.22665703 0.8954836  0.2224822 ], shape=(10,), dtype=float32)

(2)对于两种数据的结合也同样使用(1,1) fit in a tuple of components

import tensorflow as tf
# for two
dataset2 = tf.data.Dataset.from_tensor_slices(
    (tf.random.uniform([4]),
     tf.random.uniform([4,100]))
)
print(dataset2.element_spec)

dataset3 = tf.data.Dataset.from_tensor_slices((tf.random.uniform([4])))
for element in dataset3:
    print(element)

输出

(TensorSpec(shape=(), dtype=tf.float32, name=None), TensorSpec(shape=(100,), dtype=tf.float32, name=None)) # a tuple of component
tf.Tensor(0.17025971, shape=(), dtype=float32)
tf.Tensor(0.49341345, shape=(), dtype=float32)
tf.Tensor(0.49253404, shape=(), dtype=float32)
tf.Tensor(0.8137529, shape=(), dtype=float32)

(3) (1,(1,1)) fit in a nested tuple of components

import tensorflow as tf
dataset1 = tf.data.Dataset.from_tensor_slices(tf.random.uniform([4,10])) # a single component
dataset2 = tf.data.Dataset.from_tensor_slices((
    tf.random.uniform([4]),
    tf.random.uniform([4,100],maxval=100,dtype=tf.int32)
))
dataset3 = tf.data.Dataset.zip((dataset1,

shuchu

(TensorSpec(shape=(10,), dtype=tf.float32, name=None), (TensorSpec(shape=(), dtype=tf.float32, name=None), TensorSpec(shape=(100,), dtype=tf.int32, name=None)))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值