tensorflow报错:Make sure that your dataset or generator can generate at least `steps_per_epoch * epoch

这是我的代码:

import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

(train_image, train_label), (test_image, test_label) = tf.keras.datasets.fashion_mnist.load_data()

train_image = train_image/255  #归一化处理,使每个元素的大小范围为0-1
test_image = test_image/255  #归一化处理,使每个元素的大小范围为0-1

ds_train_img = tf.data.Dataset.from_tensor_slices(train_image)
ds_train_lab = tf.data.Dataset.from_tensor_slices(train_label)
ds_train = tf.data.Dataset.zip((ds_train_img, ds_train_lab))
ds_test = tf.data.Dataset.from_tensor_slices((test_image, test_label))

ds_train = ds_train.shuffle(60000).batch(64)

model=tf.keras.Sequential()
#train_image中的每个元素作为输入,每个元素大小是28*28的二维数据,所以需要通过Flatten将这个二维数据进行扁平化,
#即转化为大小为784的一维数据
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))  
model.add(tf.keras.layers.Dense(128, activation='relu'))
#输出层,共有10个概率输出。softmax的作用是把10个概率输出设置为概率值,总和为1。
model.add(tf.keras.layers.Dense(10, activation='softmax'))

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['acc'])

model.fit(ds_train, epochs=5, steps_per_epoch=steps_per_epochs)

 

以上是代码。在运行到最后一行的时候,报了以下错误:

0.8438WARNING:tensorflow:Your input ran out of data; interrupting training. Make sure that your dataset or generator can generate at least `steps_per_epoch * epochs` batches (in this case, 4685 batches). You may need to use the repeat() function when building your dataset.

 

报错的原因是在model.fit的参数中,当同时出现了epochs和steps_per_epoch时,每次epoch的遍历是对不同份的训练集和验证集进行遍历,那么就需要把训练集和验证集都拷贝N份(epochs份)。可以使用repeat(epochs)对数据集进行拷贝epochs份。

需要这么修改程序:把ds_train = ds_train.shuffle(60000).batch(64),修改为ds_train = ds_train.shuffle(10000).repeat().batch(64)或ds_train = ds_train.shuffle(10000).repeat(5).batch(64)

 

 

 

 

 

你可能感兴趣的:(技术问题解决,深度学习,tensorflow,神经网络)