16
16
import functools
17
17
18
18
import tensorflow as tf # pylint: disable=unused-import
19
-
20
- HAS_RECOMMENDATION = True
21
- try :
22
- from tensorflow_examples .lite .model_maker .third_party .recommendation .ml .model import recommendation_model as rm # pylint: disable=g-import-not-at-top
23
- except ImportError :
24
- HAS_RECOMMENDATION = False
19
+ from tensorflow_examples .lite .model_maker .core import compat
20
+ from tensorflow_examples .lite .model_maker .third_party .recommendation .ml .model import recommendation_model as _rm
25
21
26
22
27
23
class RecommendationSpec (object ):
28
24
"""Recommendation model spec."""
29
25
26
+ compat_tf_versions = compat .get_compat_tf_versions (2 )
27
+
30
28
def __init__ (self ,
31
29
encoder_type = 'bow' ,
32
30
context_embedding_dim = 128 ,
@@ -36,7 +34,9 @@ def __init__(self,
36
34
hidden_layer_dim_ratios = None ,
37
35
conv_num_filter_ratios = None ,
38
36
conv_kernel_size = None ,
39
- lstm_num_units = None ):
37
+ lstm_num_units = None ,
38
+ eval_top_k = None ,
39
+ batch_size = 16 ):
40
40
"""Initialize spec.
41
41
42
42
Args:
@@ -51,15 +51,23 @@ def __init__(self,
51
51
ratios based on context_embedding_dim.
52
52
conv_kernel_size: int, for 'rnn', Conv1D layers' kernel size.
53
53
lstm_num_units: int, for 'rnn', LSTM layer's unit number.
54
+ eval_top_k: list of int, evaluation metrics for a list of top k.
55
+ batch_size: int, default batch size.
54
56
"""
55
57
hidden_layer_dim_ratios = hidden_layer_dim_ratios or [1.0 , 0.5 , 0.25 ]
56
58
59
+ if encoder_type not in ('bow' , 'cnn' , 'rnn' ):
60
+ raise ValueError ('Not valid encoder_type: {}' .format (encoder_type ))
61
+
57
62
if encoder_type == 'cnn' :
58
63
conv_num_filter_ratios = conv_num_filter_ratios or [2 , 4 ]
59
64
conv_kernel_size = conv_kernel_size or 4
60
65
elif encoder_type == 'rnn' :
61
66
lstm_num_units = lstm_num_units or 16
62
67
68
+ if eval_top_k is None :
69
+ eval_top_k = [1 , 5 , 10 ]
70
+
63
71
self .encoder_type = encoder_type
64
72
self .context_embedding_dim = context_embedding_dim
65
73
self .label_embedding_dim = label_embedding_dim
@@ -69,8 +77,10 @@ def __init__(self,
69
77
self .conv_num_filter_ratios = conv_num_filter_ratios
70
78
self .conv_kernel_size = conv_kernel_size
71
79
self .lstm_num_units = lstm_num_units
80
+ self .eval_top_k = eval_top_k
81
+ self .batch_size = batch_size
72
82
73
- self ._params = {
83
+ self .params = {
74
84
'encoder_type' : encoder_type ,
75
85
'context_embedding_dim' : context_embedding_dim ,
76
86
'label_embedding_dim' : label_embedding_dim ,
@@ -80,13 +90,16 @@ def __init__(self,
80
90
'conv_num_filter_ratios' : conv_num_filter_ratios ,
81
91
'conv_kernel_size' : conv_kernel_size ,
82
92
'lstm_num_units' : lstm_num_units ,
93
+ 'eval_top_k' : eval_top_k ,
83
94
}
84
95
85
96
def create_model (self ):
86
- """Creates recommendation model based on params."""
87
- if not HAS_RECOMMENDATION :
88
- return None
89
- return rm .RecommendationModel (self ._params )
97
+ """Creates recommendation model based on params.
98
+
99
+ Returns:
100
+ Keras model.
101
+ """
102
+ return _rm .RecommendationModel (self .params )
90
103
91
104
92
105
recommendation_bow_spec = functools .partial (
0 commit comments