如何解决TensorBoard的keras.callbacks导入错误?

时间:2017-12-30 16:41:43

标签: python tensorflow keras tensorboard

尝试1

from keras.callbacks import TensorBoard

from keras.models import Sequential
from keras import layers
from keras.optimizers import RMSprop
tb_callback = keras.callbacks.TensorBoard(log_dir="/Users/baldur009/Documents/Scripts/log/model_gru", histogram_freq=0, write_graph=True, write_images=True)

model = Sequential()
model.add(layers.GRU(10, input_shape=(None, ts_matrix.shape[-1])))
model.add(layers.Dense(1))
model.compile(optimizer=RMSprop(), loss='mae')
history = model.fit_generator(train_gen,
                          steps_per_epoch=30,
                          epochs=30,
                          validation_data=val_gen,
                          validation_steps=val_steps, callbacks=[tb_callback])

错误:

NameError                                
Traceback (most recent call last)
<ipython-input-23-c43bea5e24d5> in <module>()
  2 from keras import layers
  3 from keras.optimizers import RMSprop
----> 4 tb_callback = keras.callbacks.TensorBoard(log_dir="/Users/baldur009/Documents/Scripts/log/pertussis_gru", histogram_freq=0, write_graph=True, write_images=True)
  5 
  6 model = Sequential()

NameError: name 'keras' is not defined

尝试2

已移除

keras.callbacks

那样:

tb_callback = TensorBoard(log_dir="/Users/catherineordun/Documents/Scripts/log/pertussis_gru", histogram_freq=0, write_graph=True, write_images=True)

运行时收到以下错误:

---> 22 from tensorflow.contrib import bayesflow
 23 from tensorflow.contrib import cloud
 24 from tensorflow.contrib import compiler

ImportError: cannot import name bayesflow

结果,搜索并找到https://github.com/tensorflow/tensorflow/issues/10811

conda update dask

继续收到同样的错误:

ImportError                               Traceback (most recent call last)
<ipython-input-48-c43bea5e24d5> in <module>()
  2 from keras import layers
  3 from keras.optimizers import RMSprop
----> 4 tb_callback = keras.callbacks.TensorBoard(log_dir="/Users/baldur009/Documents/Scripts/log/pertussis_gru", histogram_freq=0, write_graph=True, write_images=True)
  5 
  6 model = Sequential()

 /Users/baldur009/anaconda/lib/python2.7/site-packages/keras/callbacks.pyc in __init__(self, log_dir, histogram_freq, batch_size, write_graph, write_grads, write_images, embeddings_freq, embeddings_layer_names, embeddings_metadata)
641         global tf, projector
642         import tensorflow as tf
--> 643         from tensorflow.contrib.tensorboard.plugins import projector
644         self.log_dir = log_dir
645         self.histogram_freq = histogram_freq

/Users/baldur009/anaconda/lib/python2.7/site-packages/tensorflow/contrib/__init__.py in <module>()
 20 
 21 # Add projects here, they will show up under tf.contrib.
---> 22 from tensorflow.contrib import bayesflow
 23 from tensorflow.contrib import cloud
 24 from tensorflow.contrib import compiler

ImportError: cannot import name bayesflow

所以,我读了:keras.utils.to_categorical() - name keras not defined,因此导入了以下内容以涵盖尽可能多的依赖项:

import keras
import keras.callbacks
from keras.callbacks import TensorBoard

但是,继续面临同样的错误。

1 个答案:

答案 0 :(得分:0)

等待五分钟并重新启动Jupyter(重启和清除输出)后,它现在运行。