【问题标题】:TensorBoard Embedding Example?TensorBoard 嵌入示例?
【发布时间】:2017-05-06 14:50:02
【问题描述】:

我正在寻找一个张量板嵌入示例,其中包含虹膜数据,例如嵌入投影仪http://projector.tensorflow.org/

但不幸的是,我找不到。只需在https://www.tensorflow.org/how_tos/embedding_viz/中提供一些有关如何操作的信息@

有人知道这个功能的基本教程吗?

基础知识:

1) 设置一个二维张量变量来保存您的嵌入。

embedding_var = tf.Variable(....)

2) 定期将嵌入保存在 LOG_DIR 中。

3) 将元数据与您的嵌入相关联。

【问题讨论】:

    标签: tensorflow embedding tensorboard


    【解决方案1】:

    我已将 FastText's pre-trained word vectors 与 TensorBoard 一起使用。

    import os
    import tensorflow as tf
    import numpy as np
    import fasttext
    from tensorflow.contrib.tensorboard.plugins import projector
    
    # load model
    word2vec = fasttext.load_model('wiki.en.bin')
    
    # create a list of vectors
    embedding = np.empty((len(word2vec.words), word2vec.dim), dtype=np.float32)
    for i, word in enumerate(word2vec.words):
        embedding[i] = word2vec[word]
    
    # setup a TensorFlow session
    tf.reset_default_graph()
    sess = tf.InteractiveSession()
    X = tf.Variable([0.0], name='embedding')
    place = tf.placeholder(tf.float32, shape=embedding.shape)
    set_x = tf.assign(X, place, validate_shape=False)
    sess.run(tf.global_variables_initializer())
    sess.run(set_x, feed_dict={place: embedding})
    
    # write labels
    with open('log/metadata.tsv', 'w') as f:
        for word in word2vec.words:
            f.write(word + '\n')
    
    # create a TensorFlow summary writer
    summary_writer = tf.summary.FileWriter('log', sess.graph)
    config = projector.ProjectorConfig()
    embedding_conf = config.embeddings.add()
    embedding_conf.tensor_name = 'embedding:0'
    embedding_conf.metadata_path = os.path.join('log', 'metadata.tsv')
    projector.visualize_embeddings(summary_writer, config)
    
    # save the model
    saver = tf.train.Saver()
    saver.save(sess, os.path.join('log', "model.ckpt"))
    

    然后在你的终端中运行这个命令:

    tensorboard --logdir=log
    

    【讨论】:

    • 要完成旅程,请安装 jupyter-tensorboard 以直接从 Jupyter Notebook 调用 tensorboard。
    【解决方案2】:

    听起来您想获得在 TensorBoard 上运行 t-SNE 的可视化部分。正如您所描述的,Tensorflow 的 API 仅在 how-to document 中提供了最基本的命令。

    我已将我的工作解决方案与 MNIST 数据集上传到 my GitHub repo

    是的,一般分为三个步骤:

    1. 为每个维度创建元数据。
    2. 将图像与每个维度相关联。
    3. 将数据加载到 TensorFlow 中并将嵌入保存在 LOG_DIR 中。

    TensorFlow r0.12 版本仅包含通用细节。我知道官方源代码中没有完整的代码示例。

    我发现其中涉及的两个任务未在操作方法中记录。

    1. 从源准备数据
    2. 将数据加载到tf.Variable

    虽然 TensorFlow 是为使用 GPU 而设计的,但在这种情况下,我选择使用 CPU 生成 t-SNE 可视化,因为该过程占用的内存比我的 MacBookPro GPU 所能访问的更多。 TensorFlow 包含对 MNIST 数据集的 API 访问,所以我使用了它。 MNIST 数据是一个结构化的 numpy 数组。使用tf.stack 函数可以将此数据集堆叠到张量列表中,张量列表可以嵌入到可视化中。以下代码包含我如何提取数据并设置 TensorFlow 嵌入变量。

    with tf.device("/cpu:0"):
        embedding = tf.Variable(tf.stack(mnist.test.images[:FLAGS.max_steps], axis=0), trainable=False, name='embedding')
    

    创建元数据文件是通过对 numpy 数组进行切片来执行的。

    def save_metadata(file):
        with open(file, 'w') as f:
            for i in range(FLAGS.max_steps):
                c = np.nonzero(mnist.test.labels[::1])[1:][0][i]
                f.write('{}\n'.format(c))
    

    要关联的图像文件如操作指南中所述。我已将前 10,000 张 MNIST 图像的 png 文件上传到 my GitHub

    到目前为止,TensorFlow 对我来说非常好用,它的计算速度很快,有据可查,而且对于我目前要做的任何事情,API 似乎功能完整。我期待在来年使用自​​定义数据集生成更多可视化。这篇文章是从my blog 编辑的。祝你好运,请告诉我进展如何。 :)

    【讨论】:

    • 谢谢@norman_h,我会检查你的代码并回来:)。我不使用图像,而是使用 csv 文本进行数据分类。
    • @Patrick 那么我猜你会省略处理精灵的行并稍微不同地构建你的metadata.tsv
    • 当我尝试使用您生成的模型、元数据等运行 tensorboard 时,GUI 中没有显示任何内容。它只是空白。我正在使用 TF 0.12.0-rc1。您是否缺少projector_config.pbtxt 文件中的model_checkpoint_path
    • 升级到 TensorFlow 1.0 或尝试使用 tf0.12.0 github.com/normanheckscher/mnist-tensorboard-embeddings/tree/…的旧提交
    • 图片在那里。链接不会 404。
    【解决方案3】:

    查看“Hands-on TensorBoard (TensorFlow Dev Summit 2017)”讲座https://www.youtube.com/watch?v=eBbEDRsCmv4 它演示了 TensorBoard 在 MNIST 数据集上的嵌入。

    演讲的示例代码和幻灯片可以在这里找到https://github.com/mamcgrath/TensorBoard-TF-Dev-Summit-Tutorial

    【讨论】:

    • 能否提供官方教程的网址?
    • 以上链接中没有代码.. 一些要点...我正在寻找一个使用 t-sne/PCA 的 Tensorboard 嵌入可视化的工作示例,该示例目前可与 TF 1.0 一起使用没有运气..
    • 已更新源代码链接以使用 github。应该更容易导航。
    【解决方案4】:

    TensorFlow to GitHub 存储库中出现了一个问题:No real code example for using the tensorboard embedding tab #6322 (mirror)。

    它包含一些有趣的指针。


    如果有兴趣,一些使用 TensorBoard 嵌入来显示字符和单词嵌入的代码: https://github.com/Franck-Dernoncourt/NeuroNER

    例子:

    仅供参考:How can I select which checkpoint to view in TensorBoard's embeddings tab?

    【讨论】:

    【解决方案5】:

    采用预训练嵌入并将其可视化在 tensorboard 上。

    嵌入 -> 训练嵌入

    metadata.tsv -> 元数据信息

    max_size -> 嵌入.shape[0]

    import tensorflow as tf
    from tensorflow.contrib.tensorboard.plugins import projector
    
    sess = tf.InteractiveSession()
    
    with tf.device("/cpu:0"):
        tf_embedding = tf.Variable(embedding, trainable = False, name = "embedding")
    
    tf.global_variables_initializer().run()
    path = "tensorboard"
    saver = tf.train.Saver()
    writer = tf.summary.FileWriter(path, sess.graph)
    config = projector.ProjectorConfig()
    embed = config.embeddings.add()
    embed.tensor_name = "embedding"
    embed.metadata_path = "metadata.tsv"
    projector.visualize_embeddings(writer, config)
    saver.save(sess, path+'/model.ckpt' , global_step=max_size )
    

    $ tensorboard --logdir="tensorboard" --port=8080

    【讨论】:

      【解决方案6】:

      接受的答案对理解一般顺序非常有帮助:

      1. 为每个向量(样本)创建元数据
      2. 将图像(精灵)与每个矢量相关联
      3. 将数据加载到 TensorFlow 中并使用检查点和摘要编写器保存嵌入(请注意,路径在整个过程中是一致的)。

      对我来说,基于 MNIST 的示例仍然过于依赖预训练数据和预生成的 sprite 和元数据文件。为了填补这个空白,我自己创建了一个这样的例子,并决定在这里分享给任何感兴趣的人——代码在GitHub

      【讨论】:

        【解决方案7】:

        这是官方指南的链接。

        https://www.tensorflow.org/versions/r1.1/get_started/embedding_viz

        它说它最后一次更新是 2017 年 6 月。

        【讨论】:

          猜你喜欢
          • 2017-05-18
          • 2018-05-25
          • 2018-02-26
          • 2017-09-15
          • 1970-01-01
          • 2017-10-27
          • 1970-01-01
          • 1970-01-01
          • 2018-11-02
          相关资源
          最近更新 更多