batch(batch_size)方法
将数据以batch_size的大小读取。
repeat(count=None)方法
将数据重复多少份,相当于epoch
-
代码示例
此份代码中,原始官网是错的,此版本是正确的
import numpy as np
from tensorflow import keras
import tensorflow as tf
from tensorflow.keras import layers
# build network
model = tf.keras.Sequential([
layers.Dense(64, activation='relu'),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
# compile
model.compile(optimizer=tf.train.AdamOptimizer(0.001),
loss='categorical_crossentropy',
metrics=['accuracy'])
data =np.random.random((1000,32))
labels =np.random.random((1000,10))
dataset = tf.data.Dataset.from_tensor_slices((data, labels))
dataset = dataset.batch(32)
# repeat()代表无限制复制原始数据,这里可以用count指明复制份数,但要注意要比fit中的epochs大才可
dataset = dataset.repeat()
model = keras.Sequential([
# 需要在输入层中指明输入的数据量input_shape
layers.Dense(64,activation='relu',input_shape=(32,)),
layers.Dense(64,activation='relu',input_shape=(32,)),
layers.Dense(10,activation='softmax',input_shape=(32,)),
])
model.compile(optimizer=tf.train.AdamOptimizer(0.001),
loss='categorical_crossentropy',
metrics=['accuracy'])
# epochs 不能比repeat()中的count大,所以一般repeat()使用默认的count=None即可
model.fit(dataset ,epochs=10,steps_per_epoch=100)