让 tensorboard 与 keras 一起工作

Getting tensorboard to work with keras

我有一个问题在 Keras 中似乎没有直接的解决方案。我的服务器运行在 ubuntu 14.04,带有后端 tensorflow 的 keras。

这是问题所在:

我想使用 tensorboard 绘制直方图和其他训练指标图。 我按照以下程序进行操作。

  1. 我通过 ssh 连接到远程 GPU 服务器并使用以下命令行启动了 tensorboard:

    tensorboard --port 13987 --log==/home/tharun/Desktop/logs 
    
  2. 进一步生成以下输出

    tensorboard --port 13987 --log==/home/tharun/Desktop/logs
    /home/tharun/anaconda2/lib/python2.7/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
    from ._conv import register_converters as _register_converters
    TensorBoard 0.4.0rc3 at http://sidh-pc:13987 (Press CTRL+C to quit)
    
  3. 然后我使用以下命令行连接到远程服务器以在该系统上打开 firefox

    ssh -X tharun@172.26.175.67 firefox -no-remote
    
  4. 在打开的firefox浏览器中输入http://sidh-pc:13987,打开tensorboard页面。 window.

    中显示以下信息
    No dashboards are active for the current data set.
    
    Probable causes:
    
    You haven’t written any data to your event files.
    TensorBoard can’t find your event files. 
    
    If you’re new to using TensorBoard, and want to find out how to add data and set up your event files, check out the README and perhaps the TensorBoard tutorial.
    
    If you think TensorBoard is configured properly, please see the section of the README devoted to missing data problems and consider filing an issue on GitHub.
    
    Last reload: Tue Jan 16 2018 18:17:12 GMT+0530 (IST)
    
    Log directory: =/home/tharun/Desktop/logs 
    
  5. 我很困惑地检查了 link https://github.com/tensorflow/tensorboard 的 tensorboard readme.txt 。然后我在终端中输入以下命令并收到输出:

    (deep-learning) tharun@sidh-pc:~$
    find /home/tharun/Desktop/logs/ | grep tfevents
    /home/tharun/Desktop/logs/events.out.tfevents.1516101897.sidh-pc
    /home/tharun/Desktop/logs/events.out.tfevents.1516101849.sidh-pc
    
  6. 运行 tensorboard 在检查器模式下检查事件文件的内容。

    (deep-learning) tharun@sidh-pc:~$
    tensorboard --inspect --logdir /home/tharun/Desktop/logs
    /home/tharun/anaconda2/lib/python2.7/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
      from ._conv import register_converters as _register_converters
    ======================================================================
    Processing event files... (this can take a few minutes)
    ======================================================================
    
    Found event files in:
    /home/tharun/Desktop/logs
    
    These tags are in /home/tharun/Desktop/logs:
    audio -
    histograms
       dense_1/bias_0
       dense_1/bias_0_grad
       dense_1/kernel_0
       dense_1/kernel_0_grad
       dense_1_out
       dense_2/bias_0
       dense_2/bias_0_grad
       dense_2/kernel_0
       dense_2/kernel_0_grad
       dense_2_out
       dense_3/bias_0
       dense_3/bias_0_grad
       dense_3/kernel_0
       dense_3/kernel_0_grad
       dense_3_out
    images -
    scalars
       acc
       loss
       val_acc
       val_loss
    tensor -
    ======================================================================
    
    Event statistics for /home/tharun/Desktop/logs:
    audio -
    graph
       first_step           0
       last_step            0
       max_step             0
       min_step             0
       num_steps            1
       outoforder_steps     []
    histograms
       first_step           0
       last_step            149
       max_step             149
       min_step             0
       num_steps            150
       outoforder_steps     []
    images -
    scalars
       first_step           0
       last_step            149
       max_step             149
       min_step             0
       num_steps            150
       outoforder_steps     []
    sessionlog:checkpoint -
    sessionlog:start -
    sessionlog:stop -
    tensor -
    ======================================================================
    
    (deep-learning) tharun@sidh-pc:~$
    

    在下一阶段,以下脚本(位于 /home/tharun/Desktop/)是 运行 生成更多事件文件并在 tensorboard 中绘制结果

    from keras.models import Sequential
    from keras.layers import Dense
    import numpy
    from keras.callbacks import TensorBoard
    from time import time
    # fix random seed for reproducibility
    numpy.random.seed(7)
    # load pima indians dataset
    dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
    # split into input (X) and output (Y) variables
    X = dataset[:,0:8]
    Y = dataset[:,8]
    # create model
    model = Sequential()
    model.add(Dense(12, input_dim=8, activation='relu'))
    model.add(Dense(8, activation='relu'))
    model.add(Dense(1, activation='sigmoid'))
    # Compile model
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    tensorboard =TensorBoard(log_dir='./logs', histogram_freq=1, batch_size=1, write_graph=True, write_grads=True, write_images=False, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None)
    
    # Fit the model
    model.fit(X, Y, epochs=150, batch_size=10, validation_split=0.2, verbose=1, callbacks=[tensorboard])
    # evaluate the model
    scores = model.evaluate(X, Y)
    print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
    

    tensorboard 的状态没有变化window。因此,我重新启动了 tensorboard 服务器并完成了上面概述的类似过程。但是 tensorboard window 在 firefox 中仍然显示相同的状态日志:

    No dashboards are active for the current data set.
    
    Probable causes:
    
        You haven’t written any data to your event files.
        TensorBoard can’t find your event files. 
    
    If you’re new to using TensorBoard, and want to find out how to add data and set up your event files, check out the README and perhaps the TensorBoard tutorial.
    
    If you think TensorBoard is configured properly, please see the section of the README devoted to missing data problems and consider filing an issue on GitHub.
    
    Last reload: Tue Jan 16 2018 18:46:45 GMT+0530 (IST)
    
    Log directory: =/home/tharun/Desktop/logs  
    

终端中的以下行成功了!!当前位于 /home/tharun/Desktop/ 目录

tensorboard --logdir=./
/home/tharun/anaconda2/lib/python2.7/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
TensorBoard 0.4.0rc3 at http://sidh-pc:6006 (Press CTRL+C to quit)
W0117 22:24:07.582409 Reloader plugin_event_accumulator.py:303] Found more than one graph event per run, or there was a metagraph containing a graph_def, as well as one or more graph events.  Overwriting the graph with the newest event.
W0117 22:24:42.635272 Reloader plugin_event_accumulator.py:311] Found more than one metagraph event per run. Overwriting the metagraph with the newest event.