tensorboard使用及tensorflow各层权重系数输出
环境Python3.7.5,tensorflow、tensorboard均为1.14.0
首先,读取meta文件,ckpt文件夹内含有以下文件:
读取代码如下:(ckpt路径需要对应,本例中meta文件分为model.ckpt-0.meta及model.ckpt-7425.meta两组文件,ckpt路径分别到model.ckpt-0及model.ckpt-7425)
import tensorflow as tf from tensorflow.python.platform import gfile graph = tf.get_default_graph() graphdef = graph.as_graph_def() _ = tf.train.import_meta_graph("Desktop/ww/model.ckpt-7086.meta") summary_write = tf.summary.FileWriter("Desktop/ww/log" , graph) summary_write.close()
随后,在终端使用tensorboard提取日志内容:
tensorboard --logdir=Desktop/ww/log/ --host=127.0.0.1 --port=6006
查看
并可进一步查看相关结构,可以在代码中插入tf.summary.scalar来监听系数变化,参考:https://blog.csdn.net/sinat_33761963/article/details/62433234
打印权重系数代码:
from tensorflow.python import pywrap_tensorflow reader = pywrap_tensorflow.NewCheckpointReader("Desktop/ww/model.ckpt-7086") var_to_shape_map = reader.get_variable_to_shape_map() for key in var_to_shape_map: print("tensor_name: ", key) print(reader.get_tensor(key))
更新:
使用TensorFlow高阶API Estimator时,会自动生成详细的tensorboard日志,不需要读取模型,直接:
tensorboard --logdir=Desktop/model_ckpt20200106 --host=127.0.0.1 --port=6006
即可查看