TF.DATA 模块
tf.data模块简介
import tensorflow as tf
# 使用一个列表建立dataset
dataset = tf.data.Dataset.from_tensor_slices([1,2,3,4,5])
dataset
# 应用dataset
for ele in dataset:
print(ele)
# 把Tensor输出为numpy类型
for ele in dataset:
print(ele.numpy())
# 使用一个嵌套列表建立dataset
dataset = tf.data.Dataset.from_tensor_slices([[1,2,3,],[4,5,6],[7,8,9]])
dataset
for ele in dataset:
print(ele)
for ele in dataset:
print(ele.numpy())
dataset_dic = tf.data.Dataset.from_tensor_slices({"A":[1,2,3,4],
"B":[5,6,7,8],
"C":[10,11,12,13]
})
dataset_dic
for ele in dataset_dic:
print(ele)
import numpy as np
dataset = tf.data.Dataset.from_tensor_slices(np.array([1,2,3,4,5]))
for ele in dataset:
print(ele.numpy())
# 取值
dataset = tf.data.Dataset.from_tensor_slices([1,2,3,4,5,6,7,8,9])
for ele in dataset.take(4):
print(ele.numpy())
next(iter(dataset.take(1)))
# shuffle 乱序dataset
dataset = dataset.shuffle(9)
# repeat循环count=3重复3次
dataset = dataset.repeat(count=3)
for ele in dataset:
print(ele.numpy())
# batch 每一次请求出来3个数字
dataset = dataset.batch(3)
for ele in dataset:
print(ele.numpy())
# 总结
dataset = tf.data.Dataset.from_tensor_slices([1,2,3,4,5])
# shuffle 乱序dataset
dataset = dataset.shuffle(5)
# repeat循环count=3重复3次
dataset = dataset.repeat(count=3)
# batch 每一次请求出来3个数字
dataset = dataset.batch(3)
# repeat循环count=3重复3次
dataset = dataset.repeat(count=3)
for ele in dataset:
print(ele.numpy())
# tf.square做平方运算
dataset = tf.data.Dataset.from_tensor_slices([1,2,3,4,5])
dataset = dataset.map(tf.square)
for ele in dataset:
print(ele.numpy())