tensorflow2.0中data API 总结
tf.data.Dataset.list_files(filename)
参数:filename:文件路径或者文件路径列表,如:’…/csvData/iris.csv’,[’…/csvData/iris.csv’,’…/csvData/boston_house_prices.csv’]。
返回值:文件路径或者文件路径列表的Dataset形式
代码:
import tensorflow as tf
dataset1 = tf.data.Dataset.list_files('../csvData/iris.csv')
print ("dataset1:",dataset1)
for dataset in dataset1:
print("dataset:",dataset)
返回结果:
dataset1: <DatasetV1Adapter shapes: (), types: tf.string>
dataset: tf.Tensor(b'..\\csvData\\iris.csv', shape=(), dtype=string)
tf.data.TextLineDataset(filename)
参数:filename:文件路径或者文件路径列表。还可以是:tf.data.Dataset.list_files(filename)的返回值
返回值:文件路径或者文件路径列表 按行排列的 字符串Dataset形式
代码:
import tensorflow as tf
dataset2 = tf.data.TextLineDataset('../csvData/iris.csv')
for dataset in dataset2:
print(dataset)
返回结果:
tf.Tensor(b'150,4,setosa,versicolor,virginica', shape=(), dtype=string)
tf.Tensor(b'5.1,3.5,1.4,0.2,0', shape=(), dtype=string)
tf.Tensor(b'4.9,3.0,1.4,0.2,0', shape=(), dtype=string)
tf.Tensor(b'4.7,3.2,1.3,0.2,0', shape=(), dtype=string)
tf.Tensor(b'4.6,3.1,1.5,0.2,0', shape=(), dtype=string)
tf.Tensor(b'5.0,3.6,1.4,0.2,0', shape=(), dtype=string)
tf.Tensor(b'5.4,3.9,1.7,0.4,0', shape=(), dtype=string)
tf.Tensor(b'4.6,3.4,1.4,0.3,0', shape=(), dtype=string)
tf.Tensor(b'5.0,3.4,1.5,0.2,0', shape=(), dtype=string)
tf.Tensor(b'4.4,2.9,1.4,0.2,0', shape=(), dtype=string)
.......
csv文件:
dataset.interleave(function)
参数:
dataset:输入的dataset形式的值。
function:经过的函数function
返回值:通过function函数产生的返回值。
代码:
import tensorflow as tf
dataset1 = tf.data.Dataset.list_files('../csvData/iris.csv')
dataset4 = dataset1.interleave(tf.data.TextLineDataset)
print("dataset4:",dataset4)
for dataset in dataset4:
print("dataset:",dataset)
效果:
ataset4: <DatasetV1Adapter shapes: (), types: tf.string>
dataset: tf.Tensor(b'150,4,setosa,versicolor,virginica', shape=(), dtype=string)
dataset: tf.Tensor(b'5.1,3.5,1.4,0.2,0', shape=(), dtype=string)
dataset: tf.Tensor(b'4.9,3.0,1.4,0.2,0', shape=(), dtype=string)
dataset: tf.Tensor(b'4.7,3.2,1.3,0.2,0', shape=(), dtype=string)
dataset: tf.Tensor(b'4.6,3.1,1.5,0.2,0', shape=(), dtype=string)
dataset: tf.Tensor(b'5.0,3.6,1.4,0.2,0', shape=(), dtype=string)
dataset: tf.Tensor(b'5.4,3.9,1.7,0.4,0', shape=(), dtype=string)
dataset: tf.Tensor(b'4.6,3.4,1.4,0.3,0', shape=(), dtype=string)
dataset: tf.Tensor(b'5.0,3.4,1.5,0.2,0', shape=(), dtype=string)
......