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

将libFM模型变换成tensorflow可serving的形式的更多相关文章

  1. object detection模型转换成TensorFlow Lite,在Android应用

    环境 tensorflow = 1.12.0 bazel = 0.18.1 ubuntu = 16.04 python = 3.6.2 安装 bazel (0.18.1) 如果tensorflow是1 ...

  2. javascript将毫秒转换成hh:mm:ss的形式

    function formatMilliseconds(value) { var second = parseInt(value) / 1000; // second var minute = 0; ...

  3. sql数值显示成千分位分隔符的形式

    ), )--带小数点 ), ),'.00','')--不带小数点

  4. 将百度坐标转换的javascript api官方示例改写成传统的回调函数形式

    改写前: 百度地图中坐标转换的JavaScript API示例官方示例如下: var points = [new BMap.Point(116.3786889372559,39.90762965106 ...

  5. http://xx.xxx.xxx.xx:8080/把路径设置成http服务访问的形式

    1.官网下载python安装包(eg:python-3.6.3-embed-win32),并解压文件 2.配置环境变量 3.cmd里查看python版本并设置服务路径 4. 访问查看

  6. 【C/C++】任意大于1的整数分解成素数因子乘积的形式

    // #include<stdio.h> #include<math.h> #include<malloc.h> int isprime(long n); void ...

  7. 【python 数据结构】相同某个字段值的所有数据(整理成数组包字典的形式)

    class MonitoredKeywordMore(APIView): def post(self, request): try: # 设置原生命令并且请求数据 parents_asin = str ...

  8. 21个项目玩转深度学习:基于TensorFlow的实践详解02—CIFAR10图像识别

    cifar10数据集 CIFAR-10 是由 Hinton 的学生 Alex Krizhevsky 和 Ilya Sutskever 整理的一个用于识别普适物体的小型数据集.一共包含 10 个类别的 ...

  9. 从锅炉工到AI专家(9)

    无监督学习 前面已经说过了无监督学习的概念.无监督学习在实际的工作中应用还是比较多见的. 从典型的应用上说,监督学习比较多用在"分类"上,利用给定的数据,做出一个决策,这个决策在有 ...

随机推荐

  1. javascript总结16:数组array12

    1 Array 对象 作用:Array 对象用于在变量中存储多个值. 1.1 数组定义 var ary = new Array();//通过创建对象的方式创建数组 var ary1 = [];// 直 ...

  2. DataType--时间类型

    SQL SERVER 存储时间的方式和存放浮点数的方式类似,存放时按照一定公式算出一个数值,存放到页面,在读取时按照公式求算出时间值,再按照默认或指定的时间格式展示给用户. 如果存放DATETIME数 ...

  3. 20165219 2017-2018-2 《Java程序设计》第5周学习总结

    20165219 2017-2018-2 <Java程序设计>第5周学习总结 课本知识总结 第7章 内部类与异常类 一 1 内部类:类的一种成员 2 外嵌类:包含内部类的类称为内部类的外嵌 ...

  4. B. Spreadsheets(进制转换,数学)

    B. Spreadsheets time limit per test 10 seconds memory limit per test 64 megabytes input standard inp ...

  5. [51nod]1229 序列求和 V2(数学+拉格朗日差值)

    题面 传送门 题解 这种颓柿子的题我可能死活做不出来-- 首先\(r=0\)--算了不说了,\(r=1\)就是个裸的自然数幂次和直接爱怎么搞怎么搞了,所以以下都假设\(r>1\) 设 \[s_p ...

  6. Linux 意外操作后如何进行数据抢救

    Linux 意外操作后如何进行数据抢救 在 GUI 中使用  shift + delete  组合键或是 CLI 下使用 rm -rf 删除选项,这个文件并没有从硬盘(或是其它存储设备)上彻底销毁.当 ...

  7. 八大排序算法的python实现(八)简单选择排序

    代码: #coding:utf-8 #author:徐卜灵 # L = [6, 3, 2, 32, 5, 4] def Select_sort(L): for i in range(0,len(L)) ...

  8. SpringMVC中静态文件的引用

    1.在WebRoot目录下创建 resources文件,里面可以放入css文件 2.在SpringMVC中的配置文件dispatcherServlet-servlet.xml中加入 <!-- 将 ...

  9. 2019.3.7考试T2 离线数论??

    $ \color{#0066ff}{ 题目描述 }$ 一天,olinr 在 luogu.org 刷题,一点提交,等了一分钟之后,又蛙又替. olinr 发动了他的绝招,说:"为啥啊???&q ...

  10. 数学【洛谷P4071】 [SDOI2016]排列计数

    P4071 [SDOI2016]排列计数 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列 ...