Skip to main content
 首页 » 编程设计

python之无法在 tensorflow keras 中附加 tensorboard 回调

2024年08月14日2852php

我正在训练一个基于逻辑回归的模型,并试图在张量板上查看计算图。但是,当我运行代码时,出现下面提到的错误。

没有附加回调,我的 model.fit() 运行完美。同样按照建议,我添加了 update_freq = 1000

logdir = os.path.join("logs", datetime.datetime.now().strftime("%Y%m%d-%H%M%S")) 
tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1, update_freq=1000) 
 
model = tf.keras.models.Sequential([ 
tf.keras.layers.Flatten(), 
tf.keras.layers.Dense(10, activation=tf.nn.softmax)]) 
 
model.compile(optimizer='sgd', 
loss='sparse_categorical_crossentropy', 
metrics=['accuracy']) 
 
model.fit(np.matrix(train_X), np.matrix(train_y).T, epochs=10, callbacks=[tensorboard_callback]) 

但是,我收到了这个错误:

    AttributeError                            Traceback (most recent call last) 
<ipython-input-73-90c175274a55> in <module>() 
      8 metrics=['accuracy']) 
      9  
---> 10 model.fit(np.matrix(train_X), np.matrix(train_y).T, epochs=20, callbacks=[tensorboard_callback]) 
 
/usr/local/lib/python3.6/dist-packages/tensorflow/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, max_queue_size, workers, use_multiprocessing, **kwargs) 
    878     """Returns the loss value & metrics values for the model in test mode. 
    879  
--> 880     Computation is done in batches. 
    881  
    882     Arguments: 
 
/usr/local/lib/python3.6/dist-packages/tensorflow/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, mode, validation_in_fit, **kwargs) 
    250         # Loop over dataset for the specified number of steps. 
    251         target_steps = steps_per_epoch 
--> 252  
    253       step = 0 
    254       while step < target_steps: 
 
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/callbacks.py in on_epoch_begin(self, epoch, logs, mode) 
    235           'to the batch update (%f). Check your callbacks.', hook_name, 
    236           delta_t_median) 
--> 237  
    238   def _call_begin_hook(self, mode): 
    239     """Helper function for on_{train|test|predict}_begin methods.""" 
 
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/callbacks.py in on_epoch_begin(self, epoch, logs) 
   1139         samples. Note that writing too frequently to TensorBoard can slow down 
   1140         your training. 
-> 1141       profile_batch: Profile the batch to sample compute characteristics. By 
   1142         default, it will profile the second batch. Set profile_batch=0 to 
   1143         disable profiling. Must run in TensorFlow eager mode. 
 
AttributeError: 'NoneType' object has no attribute 'fetches' 

需要帮助!

请您参考如下方法:

文档指出:

histogram_freq: frequency (in epochs) at which to compute activation and weight histograms for the layers of the model. If set to 0, histograms won't be computed. Validation data (or split) must be specified for histogram visualizations.

设置 histogram_freq = 1 使其在每个时期都计算直方图。因此错误。 将其设置为 0 将不会计算直方图,将其设置为 5 将在每 5 个时期计算直方图。