entry_attr.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. __all__ = []
  15. class EntryAttr:
  16. """
  17. Entry Config for paddle.static.nn.sparse_embedding with Parameter Server.
  18. Examples:
  19. .. code-block:: python
  20. >>> import paddle
  21. >>> paddle.enable_static()
  22. >>> sparse_feature_dim = 1024
  23. >>> embedding_size = 64
  24. >>> entry = paddle.distributed.ProbabilityEntry(0.1)
  25. >>> input = paddle.static.data(name='ins', shape=[1], dtype='int64')
  26. >>> emb = paddle.static.nn.sparse_embedding(
  27. ... input=input,
  28. ... size=[sparse_feature_dim, embedding_size],
  29. ... is_test=False,
  30. ... entry=entry,
  31. ... param_attr=paddle.ParamAttr(
  32. ... name="SparseFeatFactors",
  33. ... initializer=paddle.nn.initializer.Uniform()
  34. ... )
  35. ... )
  36. """
  37. def __init__(self):
  38. self._name = None
  39. def _to_attr(self):
  40. """
  41. Returns the attributes of this parameter.
  42. Returns:
  43. Parameter attributes(map): The attributes of this parameter.
  44. """
  45. raise NotImplementedError("EntryAttr is base class")
  46. class ProbabilityEntry(EntryAttr):
  47. """
  48. Examples:
  49. .. code-block:: python
  50. >>> import paddle
  51. >>> paddle.enable_static()
  52. >>> sparse_feature_dim = 1024
  53. >>> embedding_size = 64
  54. >>> entry = paddle.distributed.ProbabilityEntry(0.1)
  55. >>> input = paddle.static.data(name='ins', shape=[1], dtype='int64')
  56. >>> emb = paddle.static.nn.sparse_embedding(
  57. ... input=input,
  58. ... size=[sparse_feature_dim, embedding_size],
  59. ... is_test=False,
  60. ... entry=entry,
  61. ... param_attr=paddle.ParamAttr(
  62. ... name="SparseFeatFactors",
  63. ... initializer=paddle.nn.initializer.Uniform()
  64. ... )
  65. ... )
  66. """
  67. def __init__(self, probability):
  68. super().__init__()
  69. if not isinstance(probability, float):
  70. raise ValueError("probability must be a float in (0,1)")
  71. if probability <= 0 or probability >= 1:
  72. raise ValueError("probability must be a float in (0,1)")
  73. self._name = "probability_entry"
  74. self._probability = probability
  75. def _to_attr(self):
  76. return ":".join([self._name, str(self._probability)])
  77. class CountFilterEntry(EntryAttr):
  78. """
  79. Examples:
  80. .. code-block:: python
  81. >>> import paddle
  82. >>> paddle.enable_static()
  83. >>> sparse_feature_dim = 1024
  84. >>> embedding_size = 64
  85. >>> entry = paddle.distributed.CountFilterEntry(10)
  86. >>> input = paddle.static.data(name='ins', shape=[1], dtype='int64')
  87. >>> emb = paddle.static.nn.sparse_embedding(
  88. ... input=input,
  89. ... size=[sparse_feature_dim, embedding_size],
  90. ... is_test=False,
  91. ... entry=entry,
  92. ... param_attr=paddle.ParamAttr(
  93. ... name="SparseFeatFactors",
  94. ... initializer=paddle.nn.initializer.Uniform()
  95. ... )
  96. ... )
  97. """
  98. def __init__(self, count_filter):
  99. super().__init__()
  100. if not isinstance(count_filter, int):
  101. raise ValueError(
  102. "count_filter must be a valid integer greater than 0"
  103. )
  104. if count_filter < 0:
  105. raise ValueError(
  106. "count_filter must be a valid integer greater or equal than 0"
  107. )
  108. self._name = "count_filter_entry"
  109. self._count_filter = count_filter
  110. def _to_attr(self):
  111. return ":".join([self._name, str(self._count_filter)])
  112. class ShowClickEntry(EntryAttr):
  113. """
  114. Examples:
  115. .. code-block:: python
  116. >>> import paddle
  117. >>> paddle.enable_static()
  118. >>> sparse_feature_dim = 1024
  119. >>> embedding_size = 64
  120. >>> shows = paddle.static.data(name='show', shape=[1], dtype='int64')
  121. >>> clicks = paddle.static.data(name='click', shape=[1], dtype='int64')
  122. >>> input = paddle.static.data(name='ins', shape=[1], dtype='int64')
  123. >>> entry = paddle.distributed.ShowClickEntry("show", "click")
  124. >>> emb = paddle.static.nn.sparse_embedding(
  125. ... input=input,
  126. ... size=[sparse_feature_dim, embedding_size],
  127. ... is_test=False,
  128. ... entry=entry,
  129. ... param_attr=paddle.ParamAttr(
  130. ... name="SparseFeatFactors",
  131. ... initializer=paddle.nn.initializer.Uniform()
  132. ... )
  133. ... )
  134. """
  135. def __init__(self, show_name, click_name):
  136. super().__init__()
  137. if not isinstance(show_name, str) or not isinstance(click_name, str):
  138. raise ValueError("show_name click_name must be a str")
  139. self._name = "show_click_entry"
  140. self._show_name = show_name
  141. self._click_name = click_name
  142. def _to_attr(self):
  143. return ":".join([self._name, self._show_name, self._click_name])