keras能够使用keras.callbacks.TensorBoard
以张量板可联合格式导出部分训练数据但是,它并不支持张量板中的embedding visualisation。
有解决方法吗?
答案 0 :(得分:9)
找到解决方案:
import os
import keras
import tensorflow
ROOT_DIR = '/tmp/tfboard'
os.makedirs(ROOT_DIR, exist_ok=True)
OUTPUT_MODEL_FILE_NAME = os.path.join(ROOT_DIR,'tf.ckpt')
# get the keras model
model = get_model()
# get the tensor name from the embedding layer
tensor_name = next(filter(lambda x: x.name == 'embedding', model.layers)).W.name
# the vocabulary
metadata_file_name = os.path.join(ROOT_DIR,tensor_name)
embedding_df = get_embedding()
embedding_df.to_csv(metadata_file_name, header=False, columns=[])
saver = tensorflow.train.Saver()
saver.save(keras.backend.get_session(), OUTPUT_MODEL_FILE_NAME)
summary_writer = tensorflow.train.SummaryWriter(ROOT_DIR)
config = tensorflow.contrib.tensorboard.plugins.projector.ProjectorConfig()
embedding = config.embeddings.add()
embedding.tensor_name = tensor_name
embedding.metadata_path = metadata_file_name
tensorflow.contrib.tensorboard.plugins.projector.visualize_embeddings(summary_writer, config)
答案 1 :(得分:5)
此功能有拉取请求 - https://github.com/fchollet/keras/pull/5247回调已扩展,可为特定嵌入图层创建可视化。
答案 2 :(得分:0)
现在可以直接通过keras.callbacks.TensorBoard
回调来实现:
from keras import callbacks
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=10,
callbacks=[
callbacks.TensorBoard(batch_size=batch_size,
embeddings_freq=3, # Store embeddings every 3 epochs (this can be time consuming)
embeddings_layer_names=['fc1', 'fc2'], # Embeddings are taken from layers with names fc1 and fc2
embeddings_metadata='metadata.tsv', # This file will describe the embeddings data (see below)
embeddings_data=x_test), # Data used for the embeddings
],
)
# Use this metadata.tsv file before you have a trained model:
with open("metadata.tsv", 'w') as f:
f.write("label\tidx\n")
f.write('\n'.join(["{}\t{}".format(class_names[int(y.argmax())], i)
for i, y in enumerate(y_test)]))
# After the model is trained, you can update the metadata file to include more information, such as the predicted labels and the mistakes:
y_pred = model.predict(x_test)
with open("metadata.tsv", 'w') as f:
f.write("label\tidx\tpredicted\tcorrect\n")
f.write('\n'.join(["{}\t{}\t{}\t{}".format(class_names[int(y.argmax())],
i,
class_names[int(y_pred[i].argmax())],
class_names[int(y.argmax())]==class_names[int(y_pred[i].argmax())])
for i, y in enumerate(y_test)]))
注意:Tensorboard通常会在metadata.tsv
目录中查找您的logs
。如果找不到,它将告诉您它在寻找的路径,您可以在其中复制并刷新张量板。