deepfm tensorflow 模型导出及java使用

接上篇

python导出

from tensorflow.python import pywrap_tensorflow
import tensorflow as tf
from tensorflow0 ( S 7 l 7 m.python.framework import gr( 4 ( ? R T j 5aph_util
def getAllNodes(checkpoint_path):
reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_pK 4 O 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_n| n G ` K *ame: ", key)
#t n H N xprint(b X  } Nreader.get_tensor(key))
def freeze_graph(ckpt, output_graph):
output_node_names = 'feat_index,feat_value,label,dropout_keep_fm,dropout_keep. w n_deep,train_phase,output/predictlabel'
# saver = tf.train.import_meta_graph(ckpt+'.mw & q 8 T &etD o l aa', clear_devices=True)
saver = tf.compat.v1.train.import_meta_graph(ckpt+".meta", clear_devices=True)
grX z p ] z X B M Zaph = tf.get_default_graph()
input_graph_def = graph.as_graph_def()
with tf.Session() as sess:6 2 z & 2 9 b B
saver.restore(sess, ckpt)
for node in input_graphm = A K ; i #_def.node:
if node.op == 'RefSwitch'. * g ^ : t:
node.op = 'Switch'
for index in range(len(node.input)):
if 'C ! N /  t H Rmoving_' in node.input[index]:
n4 o e l Eode.input[indeu R K t * A (x] =D ( / node.input[index] + '/read'
elif node.op == 'AU I T 3 { CssignSub':
nodeH A 5 %.op = 'Sub'
if 'use_locking' in node.attr: del node.attr['use_locking']
output8 ) P_graph_def = graph_util.convert_variables_to_constants& j | 4 J(
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.SerializeTe j f A k ( H  xoString())
print(T h H'{} 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)

java加载


private final static Lo- g e %gger logger = LoggerFactory.getLogger(Test.class);
private Graph graph;
privater D Z Y Session sess;
public Test(String pbFile) {
try {
graph =Y ? ) v y X | new Graph(s K i O w = ();
byte[] graphBytes = IOUtils.toByteArray(new FileInputStream(pbFile));
graph.importGraphDef(graphs p 3 bBytes);
sess = new Session(graph);Z = b ^ 7 _ y
} catch (java.io.IOException e) {
logger.error("",e);
}
}
public float[][] predict(int[][] fX L $eat_index, float[][] feat_value, float[][]label, fl= V j i 5 A O d Roat[]dropout_keep_fm,
float[]dropout_keep_deep, bot v $olean train_phase) {
Tensor indexTensor = Te& b ] _ r / ynsor.create(feat_iT K i c D = dndex);
Tensor vav } .lueTensor = Tensor.create(feat_value);
Tensor labelTensorX ` 7 = TeQ u ` Z 6 xnsor.create(label);
Tensor dropoutKeepFmTensor = Tensor.create(dropout_keep_fm);
Tensor dropoutkee6 K C 7 }pDS $ $ % V , Z UeepTensor = Tensor.createz ^ m(dropout_keep_deep);
Tensor trainPhaseTensor = Tensor.create(train_phase);
Tensor rlt = sess.runner().feed("feat_index", indexT8 d  a 6ensor).feed("feat_vp ] ) balue", valueTensor)
.feed("label", labelTensor).feed(= Y = D W | v"dropout_keep_fm", dropoutKeepFmTensor)
.feed("4 l ` adropou? 6 a c M + t st_keep_deep", dropoutkeepDeepTensor( n &).feed("traY Y : ~ 3 Pin_phase", trainPhas~ k z ; J k seTensor)
.fetch("output/predictlabel").run().get(; D S ) R F0);
float[][] finalRlt =Q { s X M new float[feat_index.length][1]x x `  i  ] V &;
rlt.copyTo(finalRlt);
retu8 ^ z _ ; &rn finalRlt;
}
public static void main( String[] args ) throwsc D J L = K K P IOException
{
Test t=new Test("C? B _ p v Y:\\res. w C J Mpb");
float[][] res=t.pre( e p _ Q : M z ]dic{ 9 n ; x t(new int[][] {{1,12,16,21,330,1065,1078,b e | a o1087,1089,1092,1093,1095,1098,1099,t r K n q N ` } s1101,1104,1105,1107,1125,2639,20838,20857,21171,21997,22109,23049,23550,24190,24217,29147,43920,43922,43924,43926,43930,43941,44055,44995,45496,46136,/ @ s + | I46163,51046,65866,65868,65870,65872,65876,65887,2 Z g ^66001,6& n , ]6941,67442,68082,68109,72991,87812,87814,87817,87818,87822,87833}}, new floc y J z ? S 9 ;at[][]{{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,o { O S1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}}, new float[][]{{1}},
new float[]{1.0f,1.0f},
new float[]{0.8f, 0.8f, 0.8f} , false);
System.ou7 t 8 jt.println(JSON.toJSONString(res));
}