100、TensorFlow实现FFM Field-awared FM模型
'''
Created on 2017年11月15日 @author: weizhen
'''
import tensorflow as tf
import pandas as pd
import numpy as np
import os
input_x_size = 80;
field_size = 8;
vector_dimension = 3;
total_plan_train_steps = 1000;
MODEL_SAVE_PATH = "TFModel"
MODEL_NAME = "FFM"
BATCH_SIZE = 1; def createTwoDimensionWeight(input_x_size,
field_size,
vector_dimension):
weights = tf.truncated_normal([int(input_x_size * (input_x_size + 1) / 2),
field_size,
vector_dimension
])
tf_weights = tf.Variable(weights);
return tf_weights;
def createOneDimensionWeight(input_x_size):
weights = tf.truncated_normal([input_x_size])
tf_weights = tf.Variable(weights)
return tf_weights;
def createZeroDimensionWeight():
weights = tf.truncated_normal([1])
tf_weights = tf.Variable(weights)
return tf_weights;
def inference(input_x, input_x_field):
"""计算回归模型输出的值"""
zeroWeights = createZeroDimensionWeight(); # 随机初始化常数项的权重
oneDimWeights = createOneDimensionWeight(input_x_size); # 随机初始化一次项的权重 secondValue = tf.reduce_sum(tf.multiply(oneDimWeights, input_x, name="secondVale")); # 计算一次项的权重和x的点积,和点积后的和
firstTwoValue = tf.add(zeroWeights, secondValue, name="firstTwoValue"); # 常数项和一次项的值 thirdWeight = createTwoDimensionWeight(input_x_size, # 创建二次项的权重变量
field_size,
vector_dimension); thirdValue = tf.Variable(0.0, dtype=tf.float32); # 初始化二次项的和为0
input_shape = input_x_size; # 得到输入训练数据的大小
for i in range(input_shape):
featureIndex1 = i; # 第一个特征的索引编号
fieldIndex1 = int(input_x_field[i]); # 第一个特征所在域的索引编号
for j in range(i + 1, input_shape):
featureIndex2 = j; # 第二个特征的索引编号
fieldIndex2 = int(input_x_field[j]); # 第二个特征的所在域索引编号
vectorLeft = tf.convert_to_tensor([[featureIndex1, fieldIndex2, 0], [featureIndex1, fieldIndex2, 1], [featureIndex1, fieldIndex2, 2]])
weightLeft = tf.gather_nd(thirdWeight, vectorLeft)
weightLeftAfterCut = tf.squeeze(weightLeft) vectorRight = tf.convert_to_tensor([[featureIndex2, fieldIndex1, 0], [featureIndex2, fieldIndex1, 1], [featureIndex2, fieldIndex1, 2]])
weightRight = tf.gather_nd(thirdWeight, vectorRight)
weightRightAfterCut = tf.squeeze(weightRight)
tempValue = tf.reduce_sum(tf.multiply(weightLeftAfterCut, weightRightAfterCut)) indices2 = [i]
indices3 = [j] xi = tf.squeeze(tf.gather_nd(input_x, indices2));
xj = tf.squeeze(tf.gather_nd(input_x, indices3)); product = tf.reduce_sum(tf.multiply(xi, xj)); secondItemVal = tf.multiply(tempValue, product) tf.assign(thirdValue, tf.add(thirdValue, secondItemVal)) fowardY = tf.add(firstTwoValue, thirdValue) return fowardY;
def read_csv():
f = open('train_sales_data.csv')
df = pd.read_csv(f)
y = np.array(df['UNIT_SALES'])
x1 = np.array(df['ITEM_NBR'])
x2 = np.array(df['STORE_NBR'])
x3 = np.array(df['CITY_GUAYAQUIL'])
x4 = np.array(df['CITY_BABAHOY'])
x5 = np.array(df['CITY_PLAYAS'])
x6 = np.array(df['CITY_LOJA'])
x7 = np.array(df['CITY_EL_CARMEN'])
x8 = np.array(df['CITY_LATACUNGA'])
x9 = np.array(df['CITY_GUARAN'])
x10 = np.array(df['CITY_CUENC'])
x11 = np.array(df['CITY_ESMERALDAS'])
x12 = np.array(df['CITY_QUITO'])
x13 = np.array(df['CITY_CAYAMBE'])
x14 = np.array(df['CITY_SALINAS'])
x15 = np.array(df['CITY_RIOBAMBA'])
x16 = np.array(df['CITY_SANTO_DOMINGO'])
x17 = np.array(df['CITY_DAULE'])
x18 = np.array(df['CITY_MACHALA'])
x19 = np.array(df['CITY_MACHALA_1'])
x20 = np.array(df['CITY_QUEVEDO'])
x21 = np.array(df['STATE_AZUAY'])
x22 = np.array(df['STATE_BOLIVAR'])
x23 = np.array(df['STATE_CHIMBORAZO'])
x24 = np.array(df['STATE_COTOPAXI'])
x25 = np.array(df['STATE_EL_ORO'])
x26 = np.array(df['STATE_ESMERALDAS'])
x27 = np.array(df['STATE_GUAYAS'])
x28 = np.array(df['STATE_IMBABURA'])
x29 = np.array(df['STATE_LOJA'])
x30 = np.array(df['STATE_LOS_RIOS'])
x31 = np.array(df['STATE_MANABI'])
x32 = np.array(df['STATE_PICHINCHA'])
x33 = np.array(df['STATE_SANTA_ELENA'])
x34 = np.array(df['STATE_SANTO_DOMINGO_DE_LOS'])
x35 = np.array(df['STATE_TUNGURAHUA'])
x36 = np.array(df['N_CLUSTER_1'])
x37 = np.array(df['N_CLUSTER_2'])
x38 = np.array(df['N_CLUSTER_3'])
x39 = np.array(df['N_CLUSTER_4'])
x40 = np.array(df['N_CLUSTER_5'])
x41 = np.array(df['N_CLUSTER_6'])
x42 = np.array(df['N_CLUSTER_7'])
x43 = np.array(df['N_CLUSTER_8'])
x44 = np.array(df['N_CLUSTER_9'])
x45 = np.array(df['N_CLUSTER_10'])
x46 = np.array(df['N_CLUSTER_11'])
x47 = np.array(df['N_CLUSTER_12'])
x48 = np.array(df['N_CLUSTER_13'])
x49 = np.array(df['N_CLUSTER_14'])
x50 = np.array(df['N_CLUSTER_15'])
x51 = np.array(df['N_CLUSTER_16'])
x52 = np.array(df['N_CLUSTER_17'])
x53 = np.array(df['FAMILY_CLEANING'])
x54 = np.array(df['FAMILY_BREAD_BAKERY'])
x55 = np.array(df['FAMILY_LIQUOR_WINE_BEER'])
x56 = np.array(df['FAMILY_PREPARED_FOODS'])
x57 = np.array(df['FAMILY_MEATS'])
x58 = np.array(df['FAMILY_BEAUTY'])
x59 = np.array(df['FAMILY_HARDWARE'])
x60 = np.array(df['FAMILY_BEVERAGES'])
x61 = np.array(df['FAMILY_DAIRY'])
x62 = np.array(df['FAMILY_GROCERY_II'])
x63 = np.array(df['FAMILY_POULTRY'])
x64 = np.array(df['FAMILY_SEAFOOD'])
x65 = np.array(df['FAMILY_LAWN_AND_GARDEN'])
x66 = np.array(df['FAMILY_EGGS'])
x67 = np.array(df['FAMILY_DELI'])
x68 = np.array(df['FAMILY_LINGERIE'])
x69 = np.array(df['FAMILY_FROZEN_FOODS'])
x70 = np.array(df['FAMILY_AUTOMOTIVE'])
x71 = np.array(df['FAMILY_GROCERY_I'])
x72 = np.array(df['FAMILY_PERSONAL_CARE'])
x73 = np.array(df['PERISHABLE_TRUE'])
x74 = np.array(df['TYPE_HOLIDAY'])
x75 = np.array(df['TYPE_WORK_DAY'])
x76 = np.array(df['LOCALE_NATIONAL'])
x77 = np.array(df['LOCALE_NAME_ECUADOR'])
x78 = np.array(df['LOCALE_PRIMER_DIA_DEL_ANO'])
x79 = np.array(df['LOCALE_RECUPERO_PUENTE_NAVIDAD'])
x80 = np.array(df['LOCALE_RECUPERO_PUENTE'])
x81 = np.array(df["FIELD_CATEGORY"]) train_x, train_y, train_x_field = [], [], []
for j in range(80):
train_x_field.append(x81[j])
print(x81[j]) for i in range(y.shape[0]):
train_x_temp = []
train_y_temp = [] train_x_temp.append(x1[i])
train_x_temp.append(x2[i])
train_x_temp.append(x3[i])
train_x_temp.append(x4[i]) train_x_temp.append(x5[i])
train_x_temp.append(x6[i])
train_x_temp.append(x7[i])
train_x_temp.append(x8[i]) train_x_temp.append(x9[i])
train_x_temp.append(x10[i])
train_x_temp.append(x11[i])
train_x_temp.append(x12[i]) train_x_temp.append(x13[i])
train_x_temp.append(x14[i])
train_x_temp.append(x15[i])
train_x_temp.append(x16[i]) train_x_temp.append(x17[i])
train_x_temp.append(x18[i])
train_x_temp.append(x19[i])
train_x_temp.append(x20[i]) train_x_temp.append(x21[i])
train_x_temp.append(x22[i])
train_x_temp.append(x23[i])
train_x_temp.append(x24[i]) train_x_temp.append(x25[i])
train_x_temp.append(x26[i])
train_x_temp.append(x27[i])
train_x_temp.append(x28[i]) train_x_temp.append(x29[i])
train_x_temp.append(x30[i])
train_x_temp.append(x31[i])
train_x_temp.append(x32[i]) train_x_temp.append(x33[i])
train_x_temp.append(x34[i])
train_x_temp.append(x35[i])
train_x_temp.append(x36[i]) train_x_temp.append(x37[i])
train_x_temp.append(x38[i])
train_x_temp.append(x39[i])
train_x_temp.append(x40[i]) train_x_temp.append(x41[i])
train_x_temp.append(x42[i])
train_x_temp.append(x43[i])
train_x_temp.append(x44[i]) train_x_temp.append(x45[i])
train_x_temp.append(x46[i])
train_x_temp.append(x47[i])
train_x_temp.append(x48[i]) train_x_temp.append(x49[i])
train_x_temp.append(x50[i])
train_x_temp.append(x51[i])
train_x_temp.append(x52[i]) train_x_temp.append(x53[i])
train_x_temp.append(x54[i])
train_x_temp.append(x55[i])
train_x_temp.append(x56[i]) train_x_temp.append(x57[i])
train_x_temp.append(x58[i])
train_x_temp.append(x59[i])
train_x_temp.append(x60[i]) train_x_temp.append(x61[i])
train_x_temp.append(x62[i])
train_x_temp.append(x63[i])
train_x_temp.append(x64[i]) train_x_temp.append(x65[i])
train_x_temp.append(x66[i])
train_x_temp.append(x67[i])
train_x_temp.append(x68[i]) train_x_temp.append(x69[i])
train_x_temp.append(x70[i])
train_x_temp.append(x71[i])
train_x_temp.append(x72[i]) train_x_temp.append(x73[i])
train_x_temp.append(x74[i])
train_x_temp.append(x75[i])
train_x_temp.append(x76[i]) train_x_temp.append(x77[i])
train_x_temp.append(x78[i])
train_x_temp.append(x79[i])
train_x_temp.append(x80[i]) train_y_temp.append(y[i]) train_x.append(train_x_temp);
train_y.append(train_y_temp);
f.close();
return (train_x, train_y, train_x_field)
if __name__ == "__main__":
global_step = tf.Variable(0, trainable=False)
(train_x, train_y, train_x_field) = read_csv();
input_x = tf.placeholder(tf.float32, [None, 80])
input_y = tf.placeholder(tf.float32, [None, 1])
y_ = inference(input_x, train_x_field)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=y_, labels=input_y);
train_step = tf.train.GradientDescentOptimizer(0.001, name="GradientDescentOptimizer").minimize(cross_entropy, global_step=global_step); saver = tf.train.Saver();
with tf.Session() as sess:
tf.global_variables_initializer().run()
for i in range(total_plan_train_steps):
input_x_batch = train_x[int(i * BATCH_SIZE):int((i + 1) * BATCH_SIZE)]
input_y_batch = train_y[int(i * BATCH_SIZE):int((i + 1) * BATCH_SIZE)] predict_loss , steps = sess.run([train_step, global_step], feed_dict={input_x:input_x_batch, input_y:input_y_batch})
if (i + 1) % 2 == 0:
print("After {step} training step(s) , loss on training batch is {predict_loss} "
.format(step=steps, predict_loss=predict_loss)) saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=steps)
writer = tf.summary.FileWriter(os.path.join(MODEL_SAVE_PATH, MODEL_NAME), tf.get_default_graph())
writer.close()
100、TensorFlow实现FFM Field-awared FM模型的更多相关文章
- field方法属于模型的连贯操作方法之一
field方法属于模型的连贯操作方法之一,主要目的是标识要返回或者操作的字段,可以用于查询和写入操作. 1.用于查询 指定字段 在查询操作中field方法是使用最频繁的. $Model->fie ...
- 三分钟快速上手TensorFlow 2.0 (下)——模型的部署 、大规模训练、加速
前文:三分钟快速上手TensorFlow 2.0 (中)——常用模块和模型的部署 TensorFlow 模型导出 使用 SavedModel 完整导出模型 不仅包含参数的权值,还包含计算的流程(即计算 ...
- Tensorflow Mask-RCNN训练识别箱子的模型运行结果(练习)
Tensorflow Mask-RCNN训练识别箱子的模型
- 聊聊推荐系统,FM模型效果好在哪里?
本文始发于公众号:Coder梁 大家好,我们今天继续来聊聊推荐系统. 在上一回当中我们讨论了LR模型对于推荐系统的应用,以及它为什么适合推荐系统,并且对它的优点以及缺点进行了分析.最后我们得出了结论, ...
- 深度学习Tensorflow生产环境部署(下·模型部署篇)
前一篇讲过环境的部署篇,这一次就讲讲从代码角度如何导出pb模型,如何进行服务调用. 1 hello world篇 部署完docker后,如果是cpu环境,可以直接拉取tensorflow/servin ...
- TensorFlow使用记录 (九): 模型保存与恢复
模型文件 tensorflow 训练保存的模型注意包含两个部分:网络结构和参数值. .meta .meta 文件以 “protocol buffer”格式保存了整个模型的结构图,模型上定义的操作等信息 ...
- ubuntu16.04 使用tensorflow object detection训练自己的模型
一.构建自己的数据集 1.格式必须为jpg.jpeg或png. 2.在models/research/object_detection文件夹下创建images文件夹,在images文件夹下创建trai ...
- 吴裕雄--天生自然TensorFlow高层封装:Estimator-自定义模型
# 1. 自定义模型并训练. import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist i ...
- TensorFlow文本与序列的深度模型
TensorFlow深度学习笔记 文本与序列的深度模型 Deep Models for Text and Sequence 转载请注明作者:梦里风林Github工程地址:https://github. ...
随机推荐
- soj#552 449E Jzzhu and Squares
分析 https://www.cnblogs.com/Memory-of-winter/p/11209128.html 代码 #include<bits/stdc++.h> using n ...
- jmeter添加自定义扩展函数之String---base64加密
1,打开eclipse,新建maven工程,在pom中引用jmeter核心jar包,具体请看---https://www.cnblogs.com/guanyf/p/10863033.html---,这 ...
- MVC路由解析---IgnoreRoute
MVC路由解析---IgnoreRoute 文章引导 MVC路由解析---IgnoreRoute MVC路由解析---MapRoute MVC路由解析---UrlRoutingModule Are ...
- 读取 appsettings.json
Appsettings.json 配置: 个配置文件就是一个json文件,并且是严格的json文件,所有的属性都需要添加“”引号.下图是一个常规的代码示例: {"UrlString" ...
- Linux查看硬件配置
1.查看机器所有硬件信息:dmidecode |moredmesg |more 这2个命令出来的信息都非常多,所以建议后面使用"|more"便于查看 2.查看CPU信息 方法一: ...
- appium常见问题11_小米手机初次启动app,报错255“Requires permission android.permission.WRITE_SECURE_SETTINGS”
问题: 新申请的测试机到啦,申请机型是小米9.打开开发者模式.USB调试后,连接电脑,准备跑一下自动化脚本.但是在pycharm中点击run后,出现报错,报错code:255,提示“Requires ...
- 在controller的action内, 得到用户发过来的请求地址和参数url
class PController extends Controller{ public function Log() { echo $_SERVER["HTTP_HOST"] . ...
- Python中sys模块
Python的sys模块提供访问解释器使用或维护的变量,和与解释器进行交互的函数.通俗来讲,sys模块负责程序与python解释器的交互,提供了一系列的函数和变量,用于操控python运行时的环境. ...
- java当中的Timer定时器的4种使用方式
import java.util.Calendar; import java.util.Date; import java.util.Timer; import java.util.TimerTask ...
- Oracle之子查询:Top-N问题
学习了SQL子查询,遇到个Top-N问题,即:加入有张工资表(这里使用Oracle SCOTT用户的emp表),需要查找工资最高的3个员工信息,以下列格式输出: 乍眼一看,这很简单啊,对sal进行排序 ...