将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
将libFM模型变换成tensorflow可serving的形式的更多相关文章
- 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 ...
- javascript将毫秒转换成hh:mm:ss的形式
function formatMilliseconds(value) { var second = parseInt(value) / 1000; // second var minute = 0; ...
- sql数值显示成千分位分隔符的形式
), )--带小数点 ), ),'.00','')--不带小数点
- 将百度坐标转换的javascript api官方示例改写成传统的回调函数形式
改写前: 百度地图中坐标转换的JavaScript API示例官方示例如下: var points = [new BMap.Point(116.3786889372559,39.90762965106 ...
- http://xx.xxx.xxx.xx:8080/把路径设置成http服务访问的形式
1.官网下载python安装包(eg:python-3.6.3-embed-win32),并解压文件 2.配置环境变量 3.cmd里查看python版本并设置服务路径 4. 访问查看
- 【C/C++】任意大于1的整数分解成素数因子乘积的形式
// #include<stdio.h> #include<math.h> #include<malloc.h> int isprime(long n); void ...
- 【python 数据结构】相同某个字段值的所有数据(整理成数组包字典的形式)
class MonitoredKeywordMore(APIView): def post(self, request): try: # 设置原生命令并且请求数据 parents_asin = str ...
- 21个项目玩转深度学习:基于TensorFlow的实践详解02—CIFAR10图像识别
cifar10数据集 CIFAR-10 是由 Hinton 的学生 Alex Krizhevsky 和 Ilya Sutskever 整理的一个用于识别普适物体的小型数据集.一共包含 10 个类别的 ...
- 从锅炉工到AI专家(9)
无监督学习 前面已经说过了无监督学习的概念.无监督学习在实际的工作中应用还是比较多见的. 从典型的应用上说,监督学习比较多用在"分类"上,利用给定的数据,做出一个决策,这个决策在有 ...
随机推荐
- 自定义UINavigationBar的背景【转】
from:http://cocoa.venj.me/blog/custom-navbar-background/ 为了让我们的应用程序更加美观,我们往往希望对iPhone自带的控件进行一点自定义.比如 ...
- ios PNG Crush error (PNG图片错误)
我是这么解决的: I had the same problem. How to fix : Open up image with Preview -> File > Export > ...
- Android Studio2.3更换默认的ConstraintLayout布局
1.在as安装目录\plugins\Android\lib\templates\activities\common\root\res\layout下,找到simple.xml.ftl文件 2.用以下布 ...
- WinForm 中使用 Action 子线程对主线程 控制进行访问
/// <summary> /// 开启新线程执行 /// </summary> /// <param name="sender"></p ...
- Mysql避免重复插入记录方法
一.mysql replace用法 1.replace into replace into table (id,name) values('1','aa'),('2','bb') 此语句的作用是向 ...
- iOS11.0后APP的图标和启动图
随着Xcode9的更新,APP的图标和启动图也发生了略微变化,下面介绍下图标和启动图的设置. 1.APP图标: 这些是系统默认你开发的项目支持iPad.Spotlight等,其实真正我们的项目只要支持 ...
- win8.1 开启企业模式
1,Win+R Gpedit.msc 2, 3, 4,F12 多了一项企业模式
- Qt 学习之路 2(65):访问网络(1)
Home / Qt 学习之路 2 / Qt 学习之路 2(65):访问网络(1) Qt 学习之路 2(65):访问网络(1) 豆子 2013年10月11日 Qt 学习之路 2 18条评论 现在 ...
- php 对象的自定义遍历
php对象的自定义遍历 对手册中的案例进行分析 更好的理解foreach() 的遍历步骤 class myIterator implements Iterator { private $positio ...
- Java过滤器详细文档,简介,实例,应用
简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 ...