MCPcopy Create free account
hub / github.com/alibaba/euler / ShallowEncoder

Class ShallowEncoder

tf_euler/python/utils/encoders.py:32–171  ·  view source on GitHub ↗

Basic encoder combining embedding of node id and dense feature.

Source from the content-addressed store, hash-verified

30
31
32class ShallowEncoder(layers.Layer):
33 """
34 Basic encoder combining embedding of node id and dense feature.
35 """
36
37 def __init__(self, dim=None, feature_idx='f1', feature_dim=0, max_id=-1,
38 sparse_feature_idx=-1, sparse_feature_max_id=-1,
39 embedding_dim=16, use_hash_embedding=False, combiner='concat',
40 **kwargs):
41 super(ShallowEncoder, self).__init__(**kwargs)
42
43 if combiner not in ['add', 'concat']:
44 raise ValueError('combiner must be \'add\' or \'concat\'.')
45 if combiner == 'add' and dim is None:
46 raise ValueError('add must be used with dim provided.')
47
48 use_feature = feature_idx != -1
49 use_id = max_id != -1
50 use_sparse_feature = sparse_feature_idx != -1
51
52 if not isinstance(feature_idx, list) and use_feature:
53 feature_idx = [feature_idx]
54 if isinstance(feature_dim, int) and use_feature:
55 feature_dim = [feature_dim]
56 if use_feature and len(feature_idx) != len(feature_dim):
57 raise ValueError('feature_dim must be the same length as feature'
58 '_idx.idx:%s, dim:%s' % (str(feature_idx),
59 str(feature_dim)))
60
61 if isinstance(sparse_feature_idx, int) and use_sparse_feature:
62 sparse_feature_idx = [sparse_feature_idx]
63 if isinstance(sparse_feature_max_id, int) and use_sparse_feature:
64 sparse_feature_max_id = [sparse_feature_max_id]
65 if use_sparse_feature and \
66 len(sparse_feature_idx) != len(sparse_feature_max_id):
67
68 raise ValueError('sparse_feature_idx must be the same length as'
69 'sparse_feature_max_id.')
70
71 embedding_num = (1 if use_id else 0) + \
72 (len(sparse_feature_idx) if use_sparse_feature else 0)
73
74 if combiner == 'add':
75 embedding_dim = dim
76 if isinstance(embedding_dim, int) and embedding_num:
77 embedding_dim = [embedding_dim] * embedding_num
78 if embedding_num and len(embedding_dim) != embedding_num:
79 raise ValueError('length of embedding_num must be int(use_id) + '
80 'len(sparse_feature_idx)')
81
82 if isinstance(use_hash_embedding, bool) and embedding_num:
83 use_hash_embedding = [use_hash_embedding] * embedding_num
84 if embedding_num and len(use_hash_embedding) != embedding_num:
85 raise ValueError('length of use_hash_embedding must be int(use_id)'
86 ' + len(sparse_feature_idx)')
87
88 # model architechture
89 self.dim = dim

Callers 5

__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected