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. linux查看进程开始时间

    ps -eo pid,lstart|grep [pid] 命令解释: -e Select all processes. -o,o Specify user-defined format. pid a ...

  2. centos 升级 python

    1. 下载python 2.7编译安装 $wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz $tar zxvf Python-. ...

  3. (转)MongoDB入门分享-笔记整理精选

    原文地址:http://www.cnblogs.com/Kummy/p/3372729.html 最近在学习MongoDB,怕以后忘记,自己做了一个整理,给不知道的小伙伴一起分享学习一下. 第一步&g ...

  4. Java实现四则运算 谢雅淇 袁杏仪

    GitHub链接:https://github.com/3216004716/four-operations.git 项目相关要求 使用 -n 参数控制生成题目的个数,例如 Myapp.exe -n ...

  5. MongoDB整理笔记の安装及配置

    1.官网下载 地址:http://www.mongodb.org/downloads mongodb-linux-x86_64-2.4.9.tgz (目前为止,64位最新版本) 2.解压 切换到下载目 ...

  6. 挂载ISO 和 KILL 掉占用进程

    mount -t iso9660 -o loop,user VMware-tools-linux-8.6.0-425873.iso /mnt/cdrom fuser -m -v -i -k  /mnt ...

  7. 并发编程学习笔记之Java存储模型(十三)

    概述 Java存储模型(JMM),安全发布.规约,同步策略等等的安全性得益于JMM,在你理解了为什么这些机制会如此工作后,可以更容易有效地使用它们. 1. 什么是存储模型,要它何用. 如果缺少同步,就 ...

  8. day5学python 基础+装饰器内容

    基础+装饰器内容 递归特性# 1.必须有一个明确的结束条件# 2.每次进入更深一层递归时,问题规模相比上次递归应有所减少# 3.递归效率不高 def run(n): print(n) if int(n ...

  9. winform datagridview某一列设为自动宽度

    如果用displayedcells只会使看见的数据自动列宽,滚动条往下发现后面的没有自动列宽,所以要用allcells就不会出现这个问题

  10. AttributeError: module 'yagmail' has no attribute 'SMTP',关于使用yagmail发邮件报错的解决方法

    想用yagmail,发送自动化测试结果邮件,发现运行的时候报错.最后发现是自己的脚本名称用的yagmail.py,更改成另一个就好,换了my_yagmail.py 再运行OK啦!!!!