我正在使用Tensorflow 1.14和Python 3.5。 我收到以下错误:
UnboundLocalError: local variable 'batch_index' referenced before assignment
完整的追溯信息是:
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<timed exec> in <module>
/usr/local/lib/python3.5/dist-packages/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)
1237 steps_per_epoch=steps_per_epoch,
1238 validation_steps=validation_steps,
-> 1239 validation_freq=validation_freq)
1240
1241 def evaluate(self,
/usr/local/lib/python3.5/dist-packages/keras/engine/training_arrays.py in fit_loop(model, fit_function, fit_inputs, out_labels, batch_size, epochs, verbose, callbacks, val_function, val_inputs, shuffle, initial_epoch, steps_per_epoch, validation_steps, validation_freq)
203 break
204
--> 205 if batch_index == len(batches) - 1: # Last batch.
206 if do_validation and should_run_validation(validation_freq, epoch):
207 val_outs = test_loop(model, val_function, val_inputs,
UnboundLocalError: local variable 'batch_index' referenced before assignment
在尝试了来自不同SO答案的多个建议后,我设法通过从以下导入语句切换来解决了该问题:
from keras.layers import LSTM, Dense
from keras.models import Sequential
对于这些导入语句:
from tensorflow.python.keras.layers import LSTM, Dense
from tensorflow.python.keras.models import Sequential
这确实解决了我的问题,但令我感到困惑的是:两者有何不同?
tf.keras
和keras
是否使用不同的方法和类?
答案 0 :(得分:1)
tf.keras与keras之间的区别。
Keras:是用于训练神经网络的高级神经网络API。它独立于tensorflow
,并且可以在多个后端(例如tensorflow, Theano and CNTK
)上运行。文档here
tf.keras::tf.keras
是tensorflow中keras
API的特定高级实现,并增加了对某些tensorflow
功能的支持。