-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneural.py
202 lines (169 loc) · 7.29 KB
/
neural.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# coding=utf-8
# -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
#
# File Name : neural.py
#
# Purpose : Implement the algorithm
#
# Creation Date : 02-05-2017
#
# Last Modified : Tue 2 May 2017
#
# Created By : Yunfei Chu ([email protected])
#
# _._._._._._._._._._._._._._._._._._._._._.
from six.moves import cPickle as pickle
import numpy as np
import tensorflow as tf
import matplotlib
import random
class NMFM:
def __init__(self, config):
self.is_Init = False
self.sess = tf.Session()
self.batch_size = config.batch_size
############ define variables #################
########## MODE: 0 -- FM, 1--NFM, 2--MFM, 3--NMFM
# MODE-0
self.mode = config.mode
self.struct = config.struct
struct = self.struct
self.h = tf.Variable(tf.ones([struct['output_dim'], 1]), trainable=False, dtype=tf.float32)
self.V = tf.Variable(tf.random_uniform([struct['input_dim'], struct['output_dim']], -1.0, 1.0), dtype=tf.float32)
self.w = tf.Variable(tf.random_normal([struct['input_dim'], 1]), dtype=tf.float32)
self.w0 = tf.Variable(tf.constant(0.0), dtype=tf.float32)
if self.mode == (1 or 3): # neural
print('neural')
self.layers = len(config.struct['layers'])
self.W = {}
self.b = {}
for i in range(self.layers - 1):
name = i
self.W[name] = tf.Variable(tf.random_normal([struct[i], struct[i + 1]]), name=name)
self.b[name] = tf.Variable(tf.zeros([struct[i + 1]]), name=name)
if self.mode == (2 or 3): # textual
print('textual')
self.H = tf.Variable(tf.random_normal([struct['text_dim'], struct['output_dim']]))
###############################################
############## define input ###################
# these variables are for sparse_dot
self.X_sp_indices = tf.placeholder(tf.int64, shape=[None, 2], name='raw_indices')
self.X_sp_val = tf.placeholder(tf.float32, shape=[None], name='raw_data')
self.X_sp_shape = tf.placeholder(tf.int64, shape=[2], name='raw_shape')
self.X_sp_val_ids = tf.placeholder(tf.int64, shape=[None, 2], name='val_sp_indices')
self.X_val_shape = tf.placeholder(tf.int64, shape=[2], name='val_shape')
self.X_sp = tf.SparseTensor(self.X_sp_indices, self.X_sp_val, self.X_sp_shape) # shape:[none,input_dim]
self.X_val_sp = tf.SparseTensor(self.X_sp_val_ids, self.X_sp_val**2, self.X_val_shape) # shape:[none, len_v]
self.X_indices = tf.placeholder(tf.int64, shape=[None], name='val_indices')
if self.mode == (2 or 3): # textual
# Tfidf input
self.T = tf.placeholder("float", [None, struct['text_dim']])
###############################################
self.loss = self.__make_loss(config)
self.optimizer = tf.train.GradientDescentOptimizer(config.learning_rate).minimize(self.loss)
print("######### Finish! ########")
def __FM(self): # Finished!
# factorization machine
# normalized_embeddings = self.V / norm
norm = tf.sqrt(tf.reduce_sum(tf.square(self.V), 1, keep_dims=True))
contribution_linear = \
tf.sparse_tensor_dense_matmul(self.X_sp, self.w)+self.w0 # x[samples, input_dim] w[input_dim, 1]
self.V = self.V/norm
a = \
(tf.sparse_tensor_dense_matmul(self.X_sp, self.V))**2 # x[samples, input_dim] V[input_dim, rank]
V_records = tf.nn.embedding_lookup(self.V, self.X_indices)
b = \
tf.sparse_tensor_dense_matmul(self.X_val_sp, V_records**2) # shape [samples,rank]
f_v = (a-b)*0.5
contribution_interplay = tf.matmul(f_v, self.h) # [samples,rank],[rank,1] =[samples,1]
output = contribution_linear + contribution_interplay #[samples,1]
return a, b, output
def __NFM(self):
# neural factorization machine
pass
def __MFM(self):
# modified factorization machine
pass
def __NMFM(self):
# neural modified factorization machine
pass
def __make_compute_graph(self):
pass
# todo
def __make_loss(self, config):
if config.mode == 0:
output = self.__FM()[2]
elif config.mode == 1:
output = self.__NFM()
elif config.mode == 2:
output = self.__MFM()
elif config.mode == 3:
output = self.__NMFM()
else:
output = 0
print("Warning: set the wrong mode!")
loss = tf.pow(output-1.0, 2)
return loss
# todo
def save_model(self, path):
saver = tf.train.Saver()
saver.save(self.sess, path)
def restore_model(self, path):
saver = tf.train.Saver()
saver.restore(self.sess, path)
def do_variables_init(self):
init = tf.global_variables_initializer()
norm = tf.sqrt(tf.reduce_sum(tf.square(self.V), 1, keep_dims=True))
self.V = self.V/norm
self.sess.run(init)
self.is_Init = True
print("############ init!! ##########")
def __get_feed_dict(self, data): # Finished!
X_ind = np.array(data.X_sp_indices)
X_val = np.array(data.X_sp_val)
X_shape = np.array(data.X_sp_shape)
X_val_shape = np.array(data.X_val_shape)
X_val_ids = np.array(data.X_val_ids)
X_indices = np.array(data.X_indices)
return {self.X_sp_indices: X_ind, self.X_sp_shape: X_shape, self.X_sp_val: X_val,
self.X_sp_val_ids:X_val_ids, self.X_val_shape: X_val_shape,self.X_indices:X_indices}
def fit(self, data):
if (not self.is_Init):
print("Warning: the model isn't initialized, and will be initialized randomly")
self.do_variables_init()
feed_dict = self.__get_feed_dict(data)
_ = self.sess.run(self.optimizer, feed_dict=feed_dict)
def get_loss(self, data):
if (not self.is_Init):
print
"Warning: the model isn't initialized, and will be initialized randomly"
self.do_variables_init()
feed_dict = self.__get_feed_dict(data)
return self.sess.run(tf.reduce_mean(self.loss), feed_dict=feed_dict)
def get_output(self, data):
if (not self.is_Init):
print
"Warning: the model isn't initialized, and will be initialized randomly"
self.do_variables_init()
feed_dict = self.__get_feed_dict(data)
return self.sess.run(self.__FM()[2],feed_dict=feed_dict)
def get_embedding(self, data):
return self.sess.run(self.V, feed_dict=self.__get_feed_dict(data))
def get_a(self,data):
feed_dict = self.__get_feed_dict(data)
return self.sess.run(self.__FM()[0], feed_dict=feed_dict)
def get_b(self,data):
feed_dict = self.__get_feed_dict(data)
return self.sess.run(self.__FM()[1], feed_dict=feed_dict)
def get_w0(self, data):
feed_dict = self.__get_feed_dict(data)
return self.sess.run(self.w0, feed_dict=feed_dict)
def get_w(self, data):
feed_dict = self.__get_feed_dict(data)
return self.sess.run(self.w, feed_dict=feed_dict)
def get_W(self):
return self.sess.run(self.W)
def get_B(self):
return self.sess.run(self.B)
def close(self):
self.sess.close()