代码语言:javascript复制对应 tensorflow 1.15版本
log_dir = "./logdir"
metadata_path = os.path.join(log_dir, 'metadata.csv')
names = ["苹果", "香蕉"]
embs = [[1.0, 2.0, 0.1, 0.4], [1.0, 1.0, 0.1, 0.4]]
# 1. 写 metadata.csv. 这里存的是对应的 embedding的 文本
with open(metadata_path, "w") as f:
for name in names:
f.write("{}n".format(name))
# 2. 将embs 存为 checkpoint
embedding = tf.Variable(embs, dtype=tf.float32)
ckpt = tf.train.Checkpoint(embedding=embedding)
with tf.Session() as sess:
tf.global_variables_initializer().run()
ckpt.save(os.path.join(log_dir, "embedding.ckpt"))
# 3. 写summary
from tensorboard.plugins import projector
config = projector.ProjectorConfig()
cfg_emb = config.embeddings.add()
# 这个名字是个默认值
cfg_emb.tensor_name = "embedding/.ATTRIBUTES/VARIABLE_VALUE"
cfg_emb.metadata_path = 'metadata.csv'
projector.visualize_embeddings(tf.summary.FileWriter(log_dir), config)
# 然后命令行执行 tensorboard --logdir=logdir