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. ...
随机推荐
- 115、TensorFlow变量的使用
# To use the value of a tf.Variable in a Tesnorflow graph , simply treat it like a normal tf.Tensor ...
- python中列表元素连接方法join用法实例
python中列表元素连接方法join用法实例 这篇文章主要介绍了python中列表元素连接方法join用法,实例分析了Python中join方法的使用技巧,非常具有实用价值,分享给大家供大家参考. ...
- flysql 里两种传参的方式
传参的方式,两个标清楚: for lists_bx_goods in out_list: sql = XDO().get_update_sql('init_goods_test', { "一 ...
- JQuery判断radio(单选框)是否选中和获取选中值
一.设置选中方法 代码如下: $("input[name='名字']").get(0).checked=true; $("input[name='名字']"). ...
- 转 使用Python的logging.config.fileConfig配置日志
Python的logging.config.fileConfig方式配置日志,通过解析conf配置文件实现.文件 logglogging.conf 配置如下: [loggers]keys=root,f ...
- java反射(三)--反射与操作类
一.反射与操作类 在反射机制的处理过程之中不仅仅只是一个实例化对象的处理操作,更多的情况下还有类的组成的操作,任何一个类的基本组成结构:父类(父接口),包,属性,方法(构造方法,普通方法)--获取类的 ...
- for语句基础求和练习
结构 for(初始化表达式;条件表达式;循环后的操作表达式) { 循环体; } 1.求出1-10之间数据之和: class Hello2 { public static void main(Strin ...
- SpringBoot-技术专区-实战方案-应用监控线程池
背景 废话不多说,做这个监控的背景很简单,我们的项目都是以spring boot框架为基础开发的,代码里所有的异步线程都是通过@Async标签标注的,并且标注的时候都是指定对应线程池的,如果不知@As ...
- django-celery beat报错 error pid
最近在用django-celery添加定时任务,测试时启动过一次Beat,beat按理说是只能启动一个的但是不服务器都重启过了还是提示已有进程在运行. ERROR: Pidfile (celerybe ...
- Hadoop伪分布式环境安装
一.环境准备 阿里云ECS(Centos7).已预装JDK8 Hadoop安装包 hadoop-2.7.7.tar.gz 二. 安装步骤 1.确认JDK环境的安装位置 命令 echo $JAVA_HO ...