14
14
from scipy .sparse import lil_matrix , csr_matrix , save_npz , load_npz
15
15
from datetime import datetime
16
16
17
+ if tf .__version__ .startswith ('2' ):
18
+ tf .compat .v1 .disable_eager_execution ()
19
+
17
20
18
21
# is it possible to one-hot encode the data prior to feeding it
19
22
# into the neural network, so that we don't have to do it on the fly?
@@ -84,13 +87,13 @@ def __init__(self, D, M, K):
84
87
85
88
def build (self , D , M , K ):
86
89
# params
87
- self .W = tf .Variable (tf .random_normal (shape = (D , K , M )) * np .sqrt (2.0 / M ))
90
+ self .W = tf .Variable (tf .random . normal (shape = (D , K , M )) * np .sqrt (2.0 / M ))
88
91
self .c = tf .Variable (np .zeros (M ).astype (np .float32 ))
89
92
self .b = tf .Variable (np .zeros ((D , K )).astype (np .float32 ))
90
93
91
94
# data
92
- self .X_in = tf .placeholder (tf .float32 , shape = (None , D , K ))
93
- self .mask = tf .placeholder (tf .float32 , shape = (None , D , K ))
95
+ self .X_in = tf .compat . v1 . placeholder (tf .float32 , shape = (None , D , K ))
96
+ self .mask = tf .compat . v1 . placeholder (tf .float32 , shape = (None , D , K ))
94
97
95
98
# conditional probabilities
96
99
# NOTE: tf.contrib.distributions.Bernoulli API has changed in Tensorflow v1.2
@@ -99,39 +102,39 @@ def build(self, D, M, K):
99
102
self .p_h_given_v = p_h_given_v # save for later
100
103
101
104
# draw a sample from p(h | v)
102
- r = tf .random_uniform (shape = tf .shape (p_h_given_v ))
103
- H = tf .to_float (r < p_h_given_v )
105
+ r = tf .random . uniform (shape = tf .shape (input = p_h_given_v ))
106
+ H = tf .cast (r < p_h_given_v , dtype = tf . float32 )
104
107
105
108
# draw a sample from p(v | h)
106
109
# note: we don't have to actually do the softmax
107
110
logits = dot2 (H , self .W ) + self .b
108
- cdist = tf .distributions .Categorical (logits = logits )
111
+ cdist = tf .compat . v1 . distributions .Categorical (logits = logits )
109
112
X_sample = cdist .sample () # shape is (N, D)
110
113
X_sample = tf .one_hot (X_sample , depth = K ) # turn it into (N, D, K)
111
114
X_sample = X_sample * self .mask # missing ratings shouldn't contribute to objective
112
115
113
116
114
117
# build the objective
115
- objective = tf .reduce_mean (self .free_energy (self .X_in )) - tf .reduce_mean (self .free_energy (X_sample ))
116
- self .train_op = tf .train .AdamOptimizer (1e-2 ).minimize (objective )
118
+ objective = tf .reduce_mean (input_tensor = self .free_energy (self .X_in )) - tf .reduce_mean (input_tensor = self .free_energy (X_sample ))
119
+ self .train_op = tf .compat . v1 . train .AdamOptimizer (1e-2 ).minimize (objective )
117
120
# self.train_op = tf.train.GradientDescentOptimizer(1e-3).minimize(objective)
118
121
119
122
# build the cost
120
123
# we won't use this to optimize the model parameters
121
124
# just to observe what happens during training
122
125
logits = self .forward_logits (self .X_in )
123
126
self .cost = tf .reduce_mean (
124
- tf .nn .softmax_cross_entropy_with_logits (
125
- labels = self .X_in ,
127
+ input_tensor = tf .nn .softmax_cross_entropy_with_logits (
128
+ labels = tf . stop_gradient ( self .X_in ) ,
126
129
logits = logits ,
127
130
)
128
131
)
129
132
130
133
# to get the output
131
134
self .output_visible = self .forward_output (self .X_in )
132
135
133
- initop = tf .global_variables_initializer ()
134
- self .session = tf .Session ()
136
+ initop = tf .compat . v1 . global_variables_initializer ()
137
+ self .session = tf .compat . v1 . Session ()
135
138
self .session .run (initop )
136
139
137
140
def fit (self , X , mask , X_test , mask_test , epochs = 10 , batch_sz = 256 , show_fig = True ):
@@ -202,10 +205,10 @@ def fit(self, X, mask, X_test, mask_test, epochs=10, batch_sz=256, show_fig=True
202
205
plt .show ()
203
206
204
207
def free_energy (self , V ):
205
- first_term = - tf .reduce_sum (dot1 (V , self .b ))
208
+ first_term = - tf .reduce_sum (input_tensor = dot1 (V , self .b ))
206
209
second_term = - tf .reduce_sum (
207
210
# tf.log(1 + tf.exp(tf.matmul(V, self.W) + self.c)),
208
- tf .nn .softplus (dot1 (V , self .W ) + self .c ),
211
+ input_tensor = tf .nn .softplus (dot1 (V , self .W ) + self .c ),
209
212
axis = 1
210
213
)
211
214
return first_term + second_term
0 commit comments