赞
踩
tf.data.Dataset
,将数据inputs和标签targets联立在一起。inputs[i]
和targets[i]
组合到一起,从而Dateset有n个组合。import numpy as np import tensorflow as tf # 一维 dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3]) print(list(dataset.as_numpy_iterator())) # [1, 2, 3] # 二维 dataset = tf.data.Dataset.from_tensor_slices([[1, 2], [3, 4]]) print(list(dataset.as_numpy_iterator())) # [array([1, 2]), array([3, 4])] # 三维 dataset = tf.data.Dataset.from_tensor_slices( [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) print(list(dataset.as_numpy_iterator())) # [array([[1, 2], # [3, 4]]), # array([[5, 6], # [7, 8]])]
[1, 2, 3]
是一维:切完还是一样。[[1, 2], [3, 4]]
是二维:a[0]是[1,2]
,a[1]是[3,4]
,结果两行组合[array([1, 2]), array([3, 4])]
。[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
是三维:a[0]是[[1, 2], [3, 4]]
,a[1]是[[5, 6], [7, 8]]
,结果两行组合[array([[1, 2],[3, 4]]), array([[5, 6],[7, 8]])]
。# 元组
dataset = tf.data.Dataset.from_tensor_slices(([1, 2], [3, 4], [5, 6]))
print(list(dataset.as_numpy_iterator()))
# [(1, 3, 5), (2, 4, 6)]
元组的拆维,不是元组[0]为第一维,而是元组内的数组[0]为第一维,即([1, 2], [3, 4], [5, 6])
→[0]:1,3,5;[1]:2,4,6
# 字典
dataset = tf.data.Dataset.from_tensor_slices({"a": [1, 2], "b": [3, 4]})
print(list(dataset.as_numpy_iterator()))
# [{'a': 1, 'b': 3}, {'a': 2, 'b': 4}]
同元组。
features = [[1, 3], [2, 1], [3, 3]] # shape(3,2),3个样本,2个特征
labels = [['A'], ['B'], ['A']] # shape(3,1),3个样本,1个标签
dataset = tf.data.Dataset.from_tensor_slices((features, labels))
print(list(dataset.as_numpy_iterator()))
# [(array([1, 3], dtype=int32), array([b'A'], dtype=object)),
# (array([2, 1], dtype=int32), array([b'B'], dtype=object)),
# (array([3, 3], dtype=int32), array([b'A'], dtype=object))]
features = [[[1, 3], [2, 3]],
[[2, 1], [1, 2]],
[[3, 3], [3, 2]]] # shape=(3, 2, 2),3个样本,特征是2*2(图片)
labels = [['A', 'A'],
['B', 'B'],
['A', 'B']]) # shape=(3, 2),3个样本,2个标签
dataset = tf.data.Dataset.from_tensor_slices((features, labels ))
print(list(dataset.as_numpy_iterator()))
# [(array([[1, 3],
# [2, 3]], dtype=int32), array([b'A', b'A'], dtype=object)), (array([[2, 1],
# [1, 2]], dtype=int32), array([b'B', b'B'], dtype=object)), (array([[3, 3],
# [3, 2]], dtype=int32), array([b'A', b'B'], dtype=object))]
tf.data.Dataset.zip()
拼features_dataset = tf.data.Dataset.from_tensor_slices(features)
labels_dataset = tf.data.Dataset.from_tensor_slices(labels)
dataset = tf.data.Dataset.zip((features_dataset, labels_dataset))
print(list(dataset.as_numpy_iterator()))
# [(array([1, 3]), b'A'), (array([2, 1]), b'B'), (array([3, 3]), b'A')]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。