deepfm tensorflow 模型导出

  1. 添加name

    
    with tf.name_scope("output"):
                self.out = tf.add(tf.matmul(concat_input, self.weights["concat_projection"]), self.weights["concat_bias"])
                if self.loss_type == "logloss":
                    self.out = tf.nn.sigmoid(self.out, name="predictlabel")
  2. 训练模型,得到模型文件

deepfm tensorflow 模型导出

  1. 导出pd,新建model.py(跟模型在同一文件夹下)

from tensorflow.python import pywrap_tensorflow
import tensorflow as tf
from tensorflow.python.framework import graph_util

def getAllNodes(checkpoint_path):
    reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path)
    var_to_shape_map = reader.get_variable_to_shape_map()
    # Print tensor name and values
    for key in var_to_shape_map:
        print("tensor_name: ", key)
        #print(reader.get_tensor(key))

def freeze_graph(ckpt, output_graph):
    output_node_names = 'output/predictlabel'

    # saver = tf.train.import_meta_graph(ckpt+'.meta', clear_devices=True)
    saver = tf.compat.v1.train.import_meta_graph(ckpt+".meta", clear_devices=True)
    graph = tf.get_default_graph()
    input_graph_def = graph.as_graph_def()

    with tf.Session() as sess:
        saver.restore(sess, ckpt)
        output_graph_def = graph_util.convert_variables_to_constants(
            sess=sess,
            input_graph_def=input_graph_def,
            output_node_names=output_node_names.split(',')
        )
        with tf.gfile.GFile(output_graph, 'wb') as fw:
            fw.write(output_graph_def.SerializeToString())
        print('{} ops in the final graph.'.format(len(output_graph_def.node)))

if __name__ == '__main__':
    ckpt_path = 'model'

    getAllNodes(ckpt_path)

    output_graph_path = 'res.pb'
    freeze_graph(ckpt_path, output_graph_path)

有两个地方注意:
a: ckpt_path=“model” 是前缀,见图片。 b: output_node_names = 'output/predictlabel' 跟第一步设置的一样。

  1. 运行此python文件,得到pd文件。
上一篇:2020-12-13


下一篇:模型权重记录与恢复