廣告點擊率預估之FM模型的學習和TF代碼實現

一步步完善中,求各位大神指導

目前這份代碼還是別人的實現:

# -*- coding: utf-8 -*-"""Created on Sun Apr 16 10:40:12 2017@author: hg"""import tensorflow as tffrom sklearn.datasets import load_irisfrom sklearn.cross_validation import train_test_splitn=4k=3#data loadiris=load_iris()x=iris["data"]y=iris["target"]#0, 1, 2#print "x.shape:", x.shape#150 * 4#delete data whose label = 2x,y=x[y!=2],y[y!=2]for i in range(len(y)): if y[i]==0: y[i]=-1x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=9415)#write fm algorithmw0=tf.Variable(0.1, dtype=tf.float64)w1=tf.Variable(tf.truncated_normal([n], dtype=tf.float64))#print "w1:", w1.asnumpy()v =tf.Variable(tf.truncated_normal([n,k], dtype=tf.float64))x_=tf.placeholder(tf.float64,[None,n])y_=tf.placeholder(tf.float64,[None])batch=tf.placeholder(tf.int32)x_square =tf.square(x)#sigma(v[i][f] * x[i]) i:1-n#print "x.shape:", x.shape#print "v.shape:", v.shapeq=tf.square(tf.reduce_sum(tf.matmul(x, v),axis=1))h=tf.reduce_sum(tf.matmul(tf.square(x), tf.square(v)),axis=1)#print "q.shape:", q.shape#print "h.shape:", h.shapeone_order = tf.reduce_sum(tf.multiply(x_,w1),axis=1)y_fm = w0 + one_order#print "y_fm.shape:", y_fm.shapetwo_order = 1/2*tf.reduce_sum(q-h, 0)y_fm = y_fm + two_ordercost=tf.reduce_sum(0.5*tf.square(y_fm-y_))# +tf.contrib.layers.l2_regularizer(0.1)(w0)#+tf.contrib.layers.l2_regularizer(0.1)(w1)+tf.contrib.layers.l2_regularizer(0.1)(w2)batch_fl=tf.cast(batch,tf.float64)accury=(batch_fl+tf.reduce_sum(tf.sign(tf.multiply(y_fm,y_))))/(batch_fl*2)train_op=tf.train.AdamOptimizer(learning_rate=0.1).minimize(cost)with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(2000): #for i in range(20): sess.run(train_op,feed_dict={x_:x_train, y_:y_train, batch:70}) print "loss:", sess.run(cost,feed_dict={x_:x_train, y_:y_train, batch:70}) #print "w2 shape:", w2.shape #print "w2_new shape:", w2_new.shape #print "board_x shape:", board_x.shape print sess.run(accury,feed_dict={x_:x_test,y_:y_test,batch:70})

未完待續。。。


推薦閱讀:

計算廣告基礎知識普及

TAG:機器學習 | 點擊率 | 計算廣告學 |