import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow import keras
print(tf.__version__)
print(sys.version_info)
for module in mpl, np, pd, sklearn, tf, keras:
print(module.__name__, module.__version__)
"""output:
2.0.0
sys.version_info(major=3, minor=7, micro=4, releaselevel='final', serial=0)
matplotlib 3.1.1
numpy 1.16.5
pandas 0.25.1
sklearn 0.21.3
tensorflow 2.0.0
"""
dataset = tf.data.Dataset.from_tensor_slices(np.arange(10))
print(dataset)
"""ouput:
<TensorSliceDataset shapes: (), types: tf.int64>
"""
for item in dataset:
print(item)
"""
output:tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(2, shape=(), dtype=int64)
tf.Tensor(3, shape=(), dtype=int64)
tf.Tensor(4, shape=(), dtype=int64)
tf.Tensor(5, shape=(), dtype=int64)
tf.Tensor(6, shape=(), dtype=int64)
tf.Tensor(7, shape=(), dtype=int64)
tf.Tensor(8, shape=(), dtype=int64)
tf.Tensor(9, shape=(), dtype=int64)
"""
dataset = dataset.repeat(3).batch(7)
for item in dataset:
print(item)
"""
output:
tf.Tensor([0 1 2 3 4 5 6], shape=(7,), dtype=int64)
tf.Tensor([7 8 9 0 1 2 3], shape=(7,), dtype=int64)
tf.Tensor([4 5 6 7 8 9 0], shape=(7,), dtype=int64)
tf.Tensor([1 2 3 4 5 6 7], shape=(7,), dtype=int64)
tf.Tensor([8 9], shape=(2,), dtype=int64)
"""
dataset2 = dataset.interleave(
lambda v: tf.data.Dataset.from_tensor_slices(v),
cycle_length=5,
block_length=5,
)
for item in dataset2:
print(item)
"""
output:
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) #从第一个取5个
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) # 从第二个取5个
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) # 从第三个取5个
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) # 从第四个取5个
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) #从第5个取两个(总共就两个)然后再从第一个取3个
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)
"""
x = np.array([[1, 2], [3, 4], [5, 6]])
y = np.array(['cat', 'dog', 'fox'])
dataset3 = tf.data.Dataset.from_tensor_slices((x, y))
print(dataset3)
for item_x, item_y in dataset3:
print(item_x.numpy(), item_y.numpy())
print(item_x,item_y)
"""output:
<TensorSliceDataset shapes: ((2,), ()), types: (tf.int32, tf.string)>
[1 2] b'cat'
tf.Tensor([1 2], shape=(2,), dtype=int32) tf.Tensor(b'cat', shape=(), dtype=string)
[3 4] b'dog'
tf.Tensor([3 4], shape=(2,), dtype=int32) tf.Tensor(b'dog', shape=(), dtype=string)
[5 6] b'fox'
tf.Tensor([5 6], shape=(2,), dtype=int32) tf.Tensor(b'fox', shape=(), dtype=string)
"""
dataset4 = tf.data.Dataset.from_tensor_slices({"feature": x,
"label": y})
for item in dataset4:
print(item["feature"].numpy(), item["label"].numpy())
print(item["feature"],item["label"])
"""output:
[1 2] b'cat'
tf.Tensor([1 2], shape=(2,), dtype=int32) tf.Tensor(b'cat', shape=(), dtype=string)
[3 4] b'dog'
tf.Tensor([3 4], shape=(2,), dtype=int32) tf.Tensor(b'dog', shape=(), dtype=string)
[5 6] b'fox'
tf.Tensor([5 6], shape=(2,), dtype=int32) tf.Tensor(b'fox', shape=(), dtype=string)
"""