11
11
except :
12
12
from tensorflow .keras .optimizers import Adam
13
13
14
+ import horovod .tensorflow as hvd
15
+
14
16
os .environ ["TF_FORCE_GPU_ALLOW_GROWTH" ] = "true" #VERY IMPORTANT!
15
17
16
18
os .environ ["TF_GPU_THREAD_MODE" ] = "gpu_private"
29
31
30
32
# optimal performance
31
33
os .environ ['TF_XLA_FLAGS' ] = '--tf_xla_auto_jit=2 --tf_xla_cpu_global_jit'
32
- tf .config .experimental .set_synchronous_execution (False )
33
34
34
35
flags .DEFINE_string ('mode' , 'train' , 'Select the running mode: train or test.' )
35
36
flags .DEFINE_string ('model_dir' , 'model_dir' ,
@@ -181,7 +182,7 @@ def embedding_out_split(embedding_out_concat, input_split_dims):
181
182
return embedding_out
182
183
183
184
184
- class ChannelEmbeddingLayers ():
185
+ class ChannelEmbeddingLayers (tf . keras . layers . Layer ):
185
186
186
187
def __init__ (self ,
187
188
name = '' ,
@@ -191,6 +192,8 @@ def __init__(self,
191
192
mpi_size = 1 ,
192
193
mpi_rank = 0 ):
193
194
195
+ super (ChannelEmbeddingLayers , self ).__init__ ()
196
+
194
197
self .gpu_device = ["GPU:0" ]
195
198
self .cpu_device = ["CPU:0" ]
196
199
@@ -227,6 +230,9 @@ def __init__(self,
227
230
kernel_initializer = tf .keras .initializers .RandomNormal (0.0 , 0.1 ),
228
231
bias_initializer = tf .keras .initializers .RandomNormal (0.0 , 0.1 ))
229
232
233
+ def build (self , input_shape ):
234
+ super (ChannelEmbeddingLayers , self ).build (input_shape )
235
+
230
236
def __call__ (self , features_info ):
231
237
dense_inputs = []
232
238
dense_input_dims = []
@@ -301,14 +307,14 @@ def __init__(self,
301
307
self .user_embedding = ChannelEmbeddingLayers (
302
308
name = 'user' ,
303
309
dense_embedding_size = user_embedding_size ,
304
- user_embedding_size = user_embedding_size * 2 ,
310
+ sparse_embedding_size = user_embedding_size * 2 ,
305
311
embedding_initializer = embedding_initializer ,
306
312
mpi_size = mpi_size ,
307
313
mpi_rank = mpi_rank )
308
314
self .movie_embedding = ChannelEmbeddingLayers (
309
315
name = 'movie' ,
310
316
dense_embedding_size = movie_embedding_size ,
311
- user_embedding_size = movie_embedding_size * 2 ,
317
+ sparse_embedding_size = movie_embedding_size * 2 ,
312
318
embedding_initializer = embedding_initializer ,
313
319
mpi_size = mpi_size ,
314
320
mpi_rank = mpi_rank )
@@ -339,13 +345,14 @@ def call(self, features):
339
345
# Construct input layers
340
346
for fea_name in features .keys ():
341
347
fea_info = feature_info_spec [fea_name ]
342
- input_tensor = tf .keras .layers .Input (shape = (fea_info ['dim' ],),
343
- dtype = fea_info ['dtype' ],
344
- name = fea_name )
348
+ input_tensor = features [fea_name ]
349
+ input_tensor = tf .keras .layers .Lambda (lambda x : x ,
350
+ name = fea_name )(input_tensor )
351
+ input_tensor = tf .reshape (input_tensor , (- 1 , fea_info ['dim' ]))
345
352
fea_info ['input_tensor' ] = input_tensor
346
353
if fea_info .__contains__ ('boundaries' ):
347
- input_tensor = tf . raw_ops . Bucketize (input = input_tensor ,
348
- boundaries = fea_info ['boundaries' ])
354
+ input_tensor = Bucketize (
355
+ boundaries = fea_info ['boundaries' ])( input_tensor )
349
356
# To prepare for GPU table combined queries, use a prefix to distinguish different features in a table.
350
357
if fea_info ['ptype' ] == 'normal_gpu' :
351
358
if fea_info ['dtype' ] == tf .int64 :
@@ -361,13 +368,15 @@ def call(self, features):
361
368
fea_info ['pretreated_tensor' ] = input_tensor
362
369
363
370
user_fea = ['user_id' , 'user_gender' , 'user_occupation_label' ]
371
+ user_fea = [i for i in features .keys () if i in user_fea ]
364
372
user_fea_info = {
365
373
key : value
366
374
for key , value in feature_info_spec .items ()
367
375
if key in user_fea
368
376
}
369
377
user_latent = self .user_embedding (user_fea_info )
370
378
movie_fea = ['movie_id' , 'movie_genres' , 'user_occupation_label' ]
379
+ movie_fea = [i for i in features .keys () if i in movie_fea ]
371
380
movie_fea_info = {
372
381
key : value
373
382
for key , value in feature_info_spec .items ()
@@ -382,7 +391,8 @@ def call(self, features):
382
391
383
392
bias = self .bias_net (latent )
384
393
x = 0.2 * x + 0.8 * bias
385
- return x
394
+ user_rating = tf .keras .layers .Lambda (lambda x : x , name = 'user_rating' )(x )
395
+ return {'user_rating' : user_rating }
386
396
387
397
388
398
def get_dataset (batch_size = 1 ):
@@ -408,7 +418,10 @@ def get_dataset(batch_size=1):
408
418
tf .cast (x ["timestamp" ] - 880000000 , tf .int32 ),
409
419
})
410
420
411
- ratings = ds .map (lambda x : {"user_rating" : x ["user_rating" ]})
421
+ ratings = ds .map (lambda x : {
422
+ "user_rating" :
423
+ tf .one_hot (tf .cast (x ["user_rating" ] - 1 , dtype = tf .int64 ), 5 )
424
+ })
412
425
dataset = tf .data .Dataset .zip ((features , ratings ))
413
426
shuffled = dataset .shuffle (1_000_000 ,
414
427
seed = 2021 ,
@@ -551,8 +564,8 @@ def train():
551
564
auc ,
552
565
])
553
566
554
- if os .path .exists (FLAGS .model_dir ):
555
- model .load_weights (FLAGS .model_dir )
567
+ if os .path .exists (FLAGS .model_dir + '/variables' ):
568
+ model .load_weights (FLAGS .model_dir + '/variables/variables' )
556
569
557
570
tensorboard_callback = tf .keras .callbacks .TensorBoard (log_dir = FLAGS .model_dir )
558
571
save_options = tf .saved_model .SaveOptions (namespace_whitelist = ['TFRA' ])
0 commit comments