qq_16237125 2019-11-24 14:39 采纳率: 60%
浏览 4064
已采纳

Tensorflow 2.0 : When using data tensors as input to a model, you should specify the `steps_per_epoch` argument.

下面代码每次执行到epochs 中的最后一个step 都会报错,请教大牛这是什么问题呢?

import tensorflow_datasets as tfds
dataset, info = tfds.load('imdb_reviews/subwords8k', with_info=True,
                         as_supervised=True) 

train_dataset,test_dataset = dataset['train'],dataset['test']

tokenizer = info.features['text'].encoder
print('vocabulary size: ', tokenizer.vocab_size)

sample_string = 'Hello world, tensorflow'
tokenized_string = tokenizer.encode(sample_string)
print('tokened id: ', tokenized_string)

src_string= tokenizer.decode(tokenized_string)
print(src_string)

for t in tokenized_string:
    print(str(t) + ': '+ tokenizer.decode([t]))


        BUFFER_SIZE=6400
BATCH_SIZE=64

num_train_examples = info.splits['train'].num_examples
num_test_examples=info.splits['test'].num_examples

print("Number of training examples: {}".format(num_train_examples))
print("Number of test examples:     {}".format(num_test_examples))

train_dataset=train_dataset.shuffle(BUFFER_SIZE)

train_dataset=train_dataset.padded_batch(BATCH_SIZE,train_dataset.output_shapes)

test_dataset=test_dataset.padded_batch(BATCH_SIZE,test_dataset.output_shapes)

def get_model():
    model=tf.keras.Sequential([
        tf.keras.layers.Embedding(tokenizer.vocab_size,64),
        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)),
        tf.keras.layers.Dense(64,activation='relu'),
        tf.keras.layers.Dense(1,activation='sigmoid')
    ])
    return model


model =get_model()
model.compile(loss='binary_crossentropy',
             optimizer='adam',
             metrics=['accuracy'])

                        import math
#from    tensorflow import keras
#train_dataset= keras.preprocessing.sequence.pad_sequences(train_dataset, maxlen=BUFFER_SIZE)

history =model.fit(train_dataset,
                epochs=2,
                steps_per_epoch=(math.ceil(BUFFER_SIZE/BATCH_SIZE) -90 ),
                validation_data= test_dataset)

Train on 10 steps
Epoch 1/2

9/10 [==========================>...] - ETA: 3s - loss: 0.6955 - accuracy: 0.4479

ValueError Traceback (most recent call last)
in
6 epochs=2,
7 steps_per_epoch=(math.ceil(BUFFER_SIZE/BATCH_SIZE) -90 ),
----> 8 validation_data= test_dataset)

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
726 max_queue_size=max_queue_size,
727 workers=workers,
--> 728 use_multiprocessing=use_multiprocessing)
729
730 def evaluate(self,

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_arrays.py in fit(self, model, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, **kwargs)
672 validation_steps=validation_steps,
673 validation_freq=validation_freq,
--> 674 steps_name='steps_per_epoch')
675
676 def evaluate(self,

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_arrays.py in model_iteration(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks, val_inputs, val_targets, val_sample_weights, shuffle, initial_epoch, steps_per_epoch, validation_steps, validation_freq, mode, validation_in_fit, prepared_feed_values_from_dataset, steps_name, **kwargs)
437 validation_in_fit=True,
438 prepared_feed_values_from_dataset=(val_iterator is not None),
--> 439 steps_name='validation_steps')
440 if not isinstance(val_results, list):
441 val_results = [val_results]

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_arrays.py in model_iteration(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks, val_inputs, val_targets, val_sample_weights, shuffle, initial_epoch, steps_per_epoch, validation_steps, validation_freq, mode, validation_in_fit, prepared_feed_values_from_dataset, steps_name, **kwargs)
174 if not is_dataset:
175 num_samples_or_steps = _get_num_samples_or_steps(ins, batch_size,
--> 176 steps_per_epoch)
177 else:
178 num_samples_or_steps = steps_per_epoch

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_arrays.py in _get_num_samples_or_steps(ins, batch_size, steps_per_epoch)
491 return steps_per_epoch
492 return training_utils.check_num_samples(ins, batch_size, steps_per_epoch,
--> 493 'steps_per_epoch')
494
495

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_utils.py in check_num_samples(ins, batch_size, steps, steps_name)
422 raise ValueError('If ' + steps_name +
423 ' is set, the batch_size must be None.')
--> 424 if check_steps_argument(ins, steps, steps_name):
425 return None
426

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_utils.py in check_steps_argument(input_data, steps, steps_name)
1199 raise ValueError('When using {input_type} as input to a model, you should'
1200 ' specify the {steps_name} argument.'.format(
-> 1201 input_type=input_type_str, steps_name=steps_name))
1202 return True
1203

ValueError: When using data tensors as input to a model, you should specify the steps_per_epoch argument.

  • 写回答

2条回答 默认 最新

  • unsterbliche 2020-03-10 13:36
    关注

    There is not only steps_per_epoch but also validation_steps parameter, which you also have to specify.
    history =model.fit(train_dataset,
    epochs=2,
    steps_per_epoch=(math.ceil(BUFFER_SIZE/BATCH_SIZE) -90 ),
    validation_data= test_dataset,
    validation_steps = your_test_size/batch_size)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已采纳回答 11月13日

悬赏问题

  • ¥15 暴力法无法解出,可能要使用dp和数学知识
  • ¥15 wpf通过绑定控件自身的值,来实现背景颜色的切换
  • ¥15 CDH6.3 运行hive -e hive -e "show databases;"报错:hive-env.sh:行24: hbase-common.jar: 权限不够
  • ¥15 SSRS制作的报表打开报错,无法正常显示网页
  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件