
正文
将libFM模型变换成tensorflow可serving的形式
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
fm_model是libFM生成的模型
model.ckpt是可以tensorflow serving的模型结构
亲测输出正确。
代码:
import tensorflow as tf # libFM model
def load_fm_model(file_name):
state = ''
fid = 0
max_fid = 0
w0 = 0.0
wj = {}
v = {}
k = 0
with open(file_name) as f:
for line in f:
line = line.rstrip()
if 'global bias W0' in line:
state = 'w0'
fid = 0
continue
elif 'unary interactions Wj' in line:
state = 'wj'
fid = 0
continue
elif 'pairwise interactions Vj,f' in line:
state = 'v'
fid = 0
continue if state == 'w0':
fv = float(line)
w0 = fv
elif state == 'wj':
fv = float(line)
if fv != 0:
wj[fid] = fv
fid += 1
max_fid = max(max_fid, fid)
elif state == 'v':
fv = [float(_v) for _v in line.split(' ')]
k = len(fv)
if any([_v!=0 for _v in fv]):
v[fid] = fv
fid += 1
max_fid = max(max_fid, fid)
return w0, wj, v, k, max_fid _w0, _wj, _v, _k, _max_fid = load_fm_model('libfm_model_file') # max feature_id
n = _max_fid
print 'n', n # vector dimension
k = _k
print 'k', k # write fm algorithm
w0 = tf.constant(_w0)
w1c = tf.constant([_wj.get(fid, 0) for fid in xrange(n)], shape=[n])
w1 = tf.Variable(w1c)
#print 'w1', w1 vec = []
for fid in xrange(n):
vec.append(_v.get(fid, [0]*k))
w2c = tf.constant(vec, shape=[n,k])
w2 = tf.Variable(w2c)
print 'w2', w2 # inputs
x = tf.placeholder(tf.string, [None])
batch = tf.shape(x)[0]
x_s = tf.string_split(x)
inds = tf.stack([tf.cast(x_s.indices[:,0], tf.int64), tf.string_to_number(x_s.values, tf.int64)], axis=1)
x_sparse = tf.sparse.SparseTensor(indices=inds, values=tf.ones([tf.shape(inds)[0]]), dense_shape=[batch,n])
x_ = tf.sparse.to_dense(x_sparse) w2_rep = tf.reshape(tf.tile(w2, [batch,1]), [-1,n,k])
print 'w2_rep', w2_rep x_rep = tf.reshape(tf.tile(tf.reshape(x_, [batch*n, 1]), [1,k]), [-1,n,k])
print 'x_rep', x_rep
x_rep2 = tf.square(x_rep) #print tf.multiply(w2_rep,x_rep)
#print tf.reduce_sum(tf.multiply(w2_rep,x_rep), axis=1)
q = tf.square(tf.reduce_sum(tf.multiply(w2_rep, x_rep), axis=1))
h = tf.reduce_sum(tf.multiply(tf.square(w2_rep), x_rep2), axis=1) y = w0 + tf.reduce_sum(tf.multiply(x_, w1), axis=1) +\
1.0/2 * tf.reduce_sum(q-h, axis=1) saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
#a = sess.run(y, feed_dict={x_:x_train,y_:y_train,batch:70})
#print a
save_path = "./model.ckpt"
tf.saved_model.simple_save(sess, save_path, inputs={"x": x}, outputs={"y": y})
参考:
https://blog.csdn.net/u010159842/article/details/78789355 (开头借鉴此文,但其有不少细节错误)
https://www.tensorflow.org/guide/saved_model
http://nowave.it/factorization-machines-with-tensorflow.html





