easy-cs-rec-custommodel 0.8.6__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of easy-cs-rec-custommodel might be problematic. Click here for more details.

Files changed (336) hide show
  1. easy_cs_rec_custommodel-0.8.6.dist-info/LICENSE +203 -0
  2. easy_cs_rec_custommodel-0.8.6.dist-info/METADATA +48 -0
  3. easy_cs_rec_custommodel-0.8.6.dist-info/RECORD +336 -0
  4. easy_cs_rec_custommodel-0.8.6.dist-info/WHEEL +6 -0
  5. easy_cs_rec_custommodel-0.8.6.dist-info/top_level.txt +2 -0
  6. easy_rec/__init__.py +114 -0
  7. easy_rec/python/__init__.py +0 -0
  8. easy_rec/python/builders/__init__.py +0 -0
  9. easy_rec/python/builders/hyperparams_builder.py +78 -0
  10. easy_rec/python/builders/loss_builder.py +333 -0
  11. easy_rec/python/builders/optimizer_builder.py +211 -0
  12. easy_rec/python/builders/strategy_builder.py +44 -0
  13. easy_rec/python/compat/__init__.py +0 -0
  14. easy_rec/python/compat/adam_s.py +245 -0
  15. easy_rec/python/compat/array_ops.py +229 -0
  16. easy_rec/python/compat/dynamic_variable.py +542 -0
  17. easy_rec/python/compat/early_stopping.py +653 -0
  18. easy_rec/python/compat/embedding_ops.py +162 -0
  19. easy_rec/python/compat/embedding_parallel_saver.py +316 -0
  20. easy_rec/python/compat/estimator_train.py +116 -0
  21. easy_rec/python/compat/exporter.py +473 -0
  22. easy_rec/python/compat/feature_column/__init__.py +0 -0
  23. easy_rec/python/compat/feature_column/feature_column.py +3675 -0
  24. easy_rec/python/compat/feature_column/feature_column_v2.py +5233 -0
  25. easy_rec/python/compat/feature_column/sequence_feature_column.py +648 -0
  26. easy_rec/python/compat/feature_column/utils.py +154 -0
  27. easy_rec/python/compat/layers.py +329 -0
  28. easy_rec/python/compat/ops.py +14 -0
  29. easy_rec/python/compat/optimizers.py +619 -0
  30. easy_rec/python/compat/queues.py +311 -0
  31. easy_rec/python/compat/regularizers.py +208 -0
  32. easy_rec/python/compat/sok_optimizer.py +440 -0
  33. easy_rec/python/compat/sync_replicas_optimizer.py +528 -0
  34. easy_rec/python/compat/weight_decay_optimizers.py +475 -0
  35. easy_rec/python/core/__init__.py +0 -0
  36. easy_rec/python/core/easyrec_metrics/__init__.py +24 -0
  37. easy_rec/python/core/easyrec_metrics/distribute_metrics_impl_pai.py +3702 -0
  38. easy_rec/python/core/easyrec_metrics/distribute_metrics_impl_tf.py +3768 -0
  39. easy_rec/python/core/learning_schedules.py +228 -0
  40. easy_rec/python/core/metrics.py +402 -0
  41. easy_rec/python/core/sampler.py +844 -0
  42. easy_rec/python/eval.py +102 -0
  43. easy_rec/python/export.py +150 -0
  44. easy_rec/python/feature_column/__init__.py +0 -0
  45. easy_rec/python/feature_column/feature_column.py +664 -0
  46. easy_rec/python/feature_column/feature_group.py +89 -0
  47. easy_rec/python/hpo/__init__.py +0 -0
  48. easy_rec/python/hpo/emr_hpo.py +140 -0
  49. easy_rec/python/hpo/generate_hpo_sql.py +71 -0
  50. easy_rec/python/hpo/pai_hpo.py +297 -0
  51. easy_rec/python/inference/__init__.py +0 -0
  52. easy_rec/python/inference/csv_predictor.py +189 -0
  53. easy_rec/python/inference/hive_parquet_predictor.py +200 -0
  54. easy_rec/python/inference/hive_predictor.py +166 -0
  55. easy_rec/python/inference/odps_predictor.py +70 -0
  56. easy_rec/python/inference/parquet_predictor.py +147 -0
  57. easy_rec/python/inference/parquet_predictor_v2.py +147 -0
  58. easy_rec/python/inference/predictor.py +621 -0
  59. easy_rec/python/inference/processor/__init__.py +0 -0
  60. easy_rec/python/inference/processor/test.py +170 -0
  61. easy_rec/python/inference/vector_retrieve.py +124 -0
  62. easy_rec/python/input/__init__.py +0 -0
  63. easy_rec/python/input/batch_tfrecord_input.py +117 -0
  64. easy_rec/python/input/criteo_binary_reader.py +259 -0
  65. easy_rec/python/input/criteo_input.py +107 -0
  66. easy_rec/python/input/csv_input.py +175 -0
  67. easy_rec/python/input/csv_input_ex.py +72 -0
  68. easy_rec/python/input/csv_input_v2.py +68 -0
  69. easy_rec/python/input/datahub_input.py +320 -0
  70. easy_rec/python/input/dummy_input.py +58 -0
  71. easy_rec/python/input/hive_input.py +123 -0
  72. easy_rec/python/input/hive_parquet_input.py +140 -0
  73. easy_rec/python/input/hive_rtp_input.py +174 -0
  74. easy_rec/python/input/input.py +1064 -0
  75. easy_rec/python/input/kafka_dataset.py +144 -0
  76. easy_rec/python/input/kafka_input.py +235 -0
  77. easy_rec/python/input/load_parquet.py +317 -0
  78. easy_rec/python/input/odps_input.py +101 -0
  79. easy_rec/python/input/odps_input_v2.py +110 -0
  80. easy_rec/python/input/odps_input_v3.py +132 -0
  81. easy_rec/python/input/odps_rtp_input.py +187 -0
  82. easy_rec/python/input/odps_rtp_input_v2.py +104 -0
  83. easy_rec/python/input/parquet_input.py +397 -0
  84. easy_rec/python/input/parquet_input_v2.py +180 -0
  85. easy_rec/python/input/parquet_input_v3.py +203 -0
  86. easy_rec/python/input/rtp_input.py +225 -0
  87. easy_rec/python/input/rtp_input_v2.py +145 -0
  88. easy_rec/python/input/tfrecord_input.py +100 -0
  89. easy_rec/python/layers/__init__.py +0 -0
  90. easy_rec/python/layers/backbone.py +571 -0
  91. easy_rec/python/layers/capsule_layer.py +176 -0
  92. easy_rec/python/layers/cmbf.py +390 -0
  93. easy_rec/python/layers/common_layers.py +192 -0
  94. easy_rec/python/layers/dnn.py +87 -0
  95. easy_rec/python/layers/embed_input_layer.py +25 -0
  96. easy_rec/python/layers/fm.py +26 -0
  97. easy_rec/python/layers/input_layer.py +396 -0
  98. easy_rec/python/layers/keras/__init__.py +34 -0
  99. easy_rec/python/layers/keras/activation.py +114 -0
  100. easy_rec/python/layers/keras/attention.py +267 -0
  101. easy_rec/python/layers/keras/auxiliary_loss.py +47 -0
  102. easy_rec/python/layers/keras/blocks.py +262 -0
  103. easy_rec/python/layers/keras/bst.py +119 -0
  104. easy_rec/python/layers/keras/custom_ops.py +250 -0
  105. easy_rec/python/layers/keras/data_augment.py +133 -0
  106. easy_rec/python/layers/keras/din.py +67 -0
  107. easy_rec/python/layers/keras/einsum_dense.py +598 -0
  108. easy_rec/python/layers/keras/embedding.py +81 -0
  109. easy_rec/python/layers/keras/fibinet.py +251 -0
  110. easy_rec/python/layers/keras/interaction.py +416 -0
  111. easy_rec/python/layers/keras/layer_norm.py +364 -0
  112. easy_rec/python/layers/keras/mask_net.py +166 -0
  113. easy_rec/python/layers/keras/multi_head_attention.py +717 -0
  114. easy_rec/python/layers/keras/multi_task.py +125 -0
  115. easy_rec/python/layers/keras/numerical_embedding.py +376 -0
  116. easy_rec/python/layers/keras/ppnet.py +194 -0
  117. easy_rec/python/layers/keras/transformer.py +192 -0
  118. easy_rec/python/layers/layer_norm.py +51 -0
  119. easy_rec/python/layers/mmoe.py +83 -0
  120. easy_rec/python/layers/multihead_attention.py +162 -0
  121. easy_rec/python/layers/multihead_cross_attention.py +749 -0
  122. easy_rec/python/layers/senet.py +73 -0
  123. easy_rec/python/layers/seq_input_layer.py +134 -0
  124. easy_rec/python/layers/sequence_feature_layer.py +249 -0
  125. easy_rec/python/layers/uniter.py +301 -0
  126. easy_rec/python/layers/utils.py +248 -0
  127. easy_rec/python/layers/variational_dropout_layer.py +130 -0
  128. easy_rec/python/loss/__init__.py +0 -0
  129. easy_rec/python/loss/circle_loss.py +82 -0
  130. easy_rec/python/loss/contrastive_loss.py +79 -0
  131. easy_rec/python/loss/f1_reweight_loss.py +38 -0
  132. easy_rec/python/loss/focal_loss.py +93 -0
  133. easy_rec/python/loss/jrc_loss.py +128 -0
  134. easy_rec/python/loss/listwise_loss.py +161 -0
  135. easy_rec/python/loss/multi_similarity.py +68 -0
  136. easy_rec/python/loss/pairwise_loss.py +307 -0
  137. easy_rec/python/loss/softmax_loss_with_negative_mining.py +110 -0
  138. easy_rec/python/loss/zero_inflated_lognormal.py +76 -0
  139. easy_rec/python/main.py +878 -0
  140. easy_rec/python/model/__init__.py +0 -0
  141. easy_rec/python/model/autoint.py +73 -0
  142. easy_rec/python/model/cmbf.py +47 -0
  143. easy_rec/python/model/collaborative_metric_learning.py +182 -0
  144. easy_rec/python/model/custom_model.py +323 -0
  145. easy_rec/python/model/dat.py +138 -0
  146. easy_rec/python/model/dbmtl.py +116 -0
  147. easy_rec/python/model/dcn.py +70 -0
  148. easy_rec/python/model/deepfm.py +106 -0
  149. easy_rec/python/model/dlrm.py +73 -0
  150. easy_rec/python/model/dropoutnet.py +207 -0
  151. easy_rec/python/model/dssm.py +154 -0
  152. easy_rec/python/model/dssm_senet.py +143 -0
  153. easy_rec/python/model/dummy_model.py +48 -0
  154. easy_rec/python/model/easy_rec_estimator.py +739 -0
  155. easy_rec/python/model/easy_rec_model.py +467 -0
  156. easy_rec/python/model/esmm.py +242 -0
  157. easy_rec/python/model/fm.py +63 -0
  158. easy_rec/python/model/match_model.py +357 -0
  159. easy_rec/python/model/mind.py +445 -0
  160. easy_rec/python/model/mmoe.py +70 -0
  161. easy_rec/python/model/multi_task_model.py +303 -0
  162. easy_rec/python/model/multi_tower.py +62 -0
  163. easy_rec/python/model/multi_tower_bst.py +190 -0
  164. easy_rec/python/model/multi_tower_din.py +130 -0
  165. easy_rec/python/model/multi_tower_recall.py +68 -0
  166. easy_rec/python/model/pdn.py +203 -0
  167. easy_rec/python/model/ple.py +120 -0
  168. easy_rec/python/model/rank_model.py +485 -0
  169. easy_rec/python/model/rocket_launching.py +203 -0
  170. easy_rec/python/model/simple_multi_task.py +54 -0
  171. easy_rec/python/model/uniter.py +46 -0
  172. easy_rec/python/model/wide_and_deep.py +121 -0
  173. easy_rec/python/ops/1.12/incr_record.so +0 -0
  174. easy_rec/python/ops/1.12/kafka.so +0 -0
  175. easy_rec/python/ops/1.12/libcustom_ops.so +0 -0
  176. easy_rec/python/ops/1.12/libembed_op.so +0 -0
  177. easy_rec/python/ops/1.12/libhiredis.so.1.0.0 +0 -0
  178. easy_rec/python/ops/1.12/librdkafka++.so.1 +0 -0
  179. easy_rec/python/ops/1.12/librdkafka.so.1 +0 -0
  180. easy_rec/python/ops/1.12/libredis++.so +0 -0
  181. easy_rec/python/ops/1.12/libredis++.so.1 +0 -0
  182. easy_rec/python/ops/1.12/libredis++.so.1.2.3 +0 -0
  183. easy_rec/python/ops/1.12/libstr_avx_op.so +0 -0
  184. easy_rec/python/ops/1.12/libwrite_sparse_kv.so +0 -0
  185. easy_rec/python/ops/1.15/incr_record.so +0 -0
  186. easy_rec/python/ops/1.15/kafka.so +0 -0
  187. easy_rec/python/ops/1.15/libcustom_ops.so +0 -0
  188. easy_rec/python/ops/1.15/libembed_op.so +0 -0
  189. easy_rec/python/ops/1.15/libhiredis.so.1.0.0 +0 -0
  190. easy_rec/python/ops/1.15/librdkafka++.so +0 -0
  191. easy_rec/python/ops/1.15/librdkafka++.so.1 +0 -0
  192. easy_rec/python/ops/1.15/librdkafka.so +0 -0
  193. easy_rec/python/ops/1.15/librdkafka.so.1 +0 -0
  194. easy_rec/python/ops/1.15/libredis++.so.1 +0 -0
  195. easy_rec/python/ops/1.15/libstr_avx_op.so +0 -0
  196. easy_rec/python/ops/2.12/libcustom_ops.so +0 -0
  197. easy_rec/python/ops/2.12/libload_embed.so +0 -0
  198. easy_rec/python/ops/2.12/libstr_avx_op.so +0 -0
  199. easy_rec/python/ops/__init__.py +0 -0
  200. easy_rec/python/ops/gen_kafka_ops.py +193 -0
  201. easy_rec/python/ops/gen_str_avx_op.py +28 -0
  202. easy_rec/python/ops/incr_record.py +30 -0
  203. easy_rec/python/predict.py +170 -0
  204. easy_rec/python/protos/__init__.py +0 -0
  205. easy_rec/python/protos/autoint_pb2.py +122 -0
  206. easy_rec/python/protos/backbone_pb2.py +1416 -0
  207. easy_rec/python/protos/cmbf_pb2.py +435 -0
  208. easy_rec/python/protos/collaborative_metric_learning_pb2.py +252 -0
  209. easy_rec/python/protos/custom_model_pb2.py +57 -0
  210. easy_rec/python/protos/dat_pb2.py +262 -0
  211. easy_rec/python/protos/data_source_pb2.py +422 -0
  212. easy_rec/python/protos/dataset_pb2.py +1920 -0
  213. easy_rec/python/protos/dbmtl_pb2.py +191 -0
  214. easy_rec/python/protos/dcn_pb2.py +197 -0
  215. easy_rec/python/protos/deepfm_pb2.py +163 -0
  216. easy_rec/python/protos/dlrm_pb2.py +163 -0
  217. easy_rec/python/protos/dnn_pb2.py +329 -0
  218. easy_rec/python/protos/dropoutnet_pb2.py +239 -0
  219. easy_rec/python/protos/dssm_pb2.py +262 -0
  220. easy_rec/python/protos/dssm_senet_pb2.py +282 -0
  221. easy_rec/python/protos/easy_rec_model_pb2.py +1672 -0
  222. easy_rec/python/protos/esmm_pb2.py +133 -0
  223. easy_rec/python/protos/eval_pb2.py +930 -0
  224. easy_rec/python/protos/export_pb2.py +379 -0
  225. easy_rec/python/protos/feature_config_pb2.py +1359 -0
  226. easy_rec/python/protos/fm_pb2.py +90 -0
  227. easy_rec/python/protos/hive_config_pb2.py +138 -0
  228. easy_rec/python/protos/hyperparams_pb2.py +624 -0
  229. easy_rec/python/protos/keras_layer_pb2.py +692 -0
  230. easy_rec/python/protos/layer_pb2.py +1936 -0
  231. easy_rec/python/protos/loss_pb2.py +1713 -0
  232. easy_rec/python/protos/mind_pb2.py +497 -0
  233. easy_rec/python/protos/mmoe_pb2.py +215 -0
  234. easy_rec/python/protos/multi_tower_pb2.py +295 -0
  235. easy_rec/python/protos/multi_tower_recall_pb2.py +198 -0
  236. easy_rec/python/protos/optimizer_pb2.py +2017 -0
  237. easy_rec/python/protos/pdn_pb2.py +293 -0
  238. easy_rec/python/protos/pipeline_pb2.py +516 -0
  239. easy_rec/python/protos/ple_pb2.py +231 -0
  240. easy_rec/python/protos/predict_pb2.py +1140 -0
  241. easy_rec/python/protos/rocket_launching_pb2.py +169 -0
  242. easy_rec/python/protos/seq_encoder_pb2.py +1084 -0
  243. easy_rec/python/protos/simi_pb2.py +54 -0
  244. easy_rec/python/protos/simple_multi_task_pb2.py +97 -0
  245. easy_rec/python/protos/tf_predict_pb2.py +630 -0
  246. easy_rec/python/protos/tower_pb2.py +661 -0
  247. easy_rec/python/protos/train_pb2.py +1197 -0
  248. easy_rec/python/protos/uniter_pb2.py +307 -0
  249. easy_rec/python/protos/variational_dropout_pb2.py +91 -0
  250. easy_rec/python/protos/wide_and_deep_pb2.py +131 -0
  251. easy_rec/python/test/__init__.py +0 -0
  252. easy_rec/python/test/csv_input_test.py +340 -0
  253. easy_rec/python/test/custom_early_stop_func.py +19 -0
  254. easy_rec/python/test/dh_local_run.py +104 -0
  255. easy_rec/python/test/embed_test.py +155 -0
  256. easy_rec/python/test/emr_run.py +119 -0
  257. easy_rec/python/test/eval_metric_test.py +107 -0
  258. easy_rec/python/test/excel_convert_test.py +64 -0
  259. easy_rec/python/test/export_test.py +513 -0
  260. easy_rec/python/test/fg_test.py +70 -0
  261. easy_rec/python/test/hive_input_test.py +311 -0
  262. easy_rec/python/test/hpo_test.py +235 -0
  263. easy_rec/python/test/kafka_test.py +373 -0
  264. easy_rec/python/test/local_incr_test.py +122 -0
  265. easy_rec/python/test/loss_test.py +110 -0
  266. easy_rec/python/test/odps_command.py +61 -0
  267. easy_rec/python/test/odps_local_run.py +86 -0
  268. easy_rec/python/test/odps_run.py +254 -0
  269. easy_rec/python/test/odps_test_cls.py +39 -0
  270. easy_rec/python/test/odps_test_prepare.py +198 -0
  271. easy_rec/python/test/odps_test_util.py +237 -0
  272. easy_rec/python/test/pre_check_test.py +54 -0
  273. easy_rec/python/test/predictor_test.py +394 -0
  274. easy_rec/python/test/rtp_convert_test.py +133 -0
  275. easy_rec/python/test/run.py +138 -0
  276. easy_rec/python/test/train_eval_test.py +1299 -0
  277. easy_rec/python/test/util_test.py +85 -0
  278. easy_rec/python/test/zero_inflated_lognormal_test.py +53 -0
  279. easy_rec/python/tools/__init__.py +0 -0
  280. easy_rec/python/tools/add_boundaries_to_config.py +67 -0
  281. easy_rec/python/tools/add_feature_info_to_config.py +145 -0
  282. easy_rec/python/tools/convert_config_format.py +48 -0
  283. easy_rec/python/tools/convert_rtp_data.py +79 -0
  284. easy_rec/python/tools/convert_rtp_fg.py +106 -0
  285. easy_rec/python/tools/create_config_from_excel.py +427 -0
  286. easy_rec/python/tools/criteo/__init__.py +0 -0
  287. easy_rec/python/tools/criteo/convert_data.py +157 -0
  288. easy_rec/python/tools/edit_lookup_graph.py +134 -0
  289. easy_rec/python/tools/faiss_index_pai.py +116 -0
  290. easy_rec/python/tools/feature_selection.py +316 -0
  291. easy_rec/python/tools/hit_rate_ds.py +223 -0
  292. easy_rec/python/tools/hit_rate_pai.py +138 -0
  293. easy_rec/python/tools/pre_check.py +120 -0
  294. easy_rec/python/tools/predict_and_chk.py +111 -0
  295. easy_rec/python/tools/read_kafka.py +55 -0
  296. easy_rec/python/tools/split_model_pai.py +286 -0
  297. easy_rec/python/tools/split_pdn_model_pai.py +272 -0
  298. easy_rec/python/tools/test_saved_model.py +80 -0
  299. easy_rec/python/tools/view_saved_model.py +39 -0
  300. easy_rec/python/tools/write_kafka.py +65 -0
  301. easy_rec/python/train_eval.py +325 -0
  302. easy_rec/python/utils/__init__.py +15 -0
  303. easy_rec/python/utils/activation.py +120 -0
  304. easy_rec/python/utils/check_utils.py +87 -0
  305. easy_rec/python/utils/compat.py +14 -0
  306. easy_rec/python/utils/config_util.py +652 -0
  307. easy_rec/python/utils/constant.py +43 -0
  308. easy_rec/python/utils/convert_rtp_fg.py +616 -0
  309. easy_rec/python/utils/dag.py +192 -0
  310. easy_rec/python/utils/distribution_utils.py +268 -0
  311. easy_rec/python/utils/ds_util.py +65 -0
  312. easy_rec/python/utils/embedding_utils.py +73 -0
  313. easy_rec/python/utils/estimator_utils.py +1036 -0
  314. easy_rec/python/utils/export_big_model.py +630 -0
  315. easy_rec/python/utils/expr_util.py +118 -0
  316. easy_rec/python/utils/fg_util.py +53 -0
  317. easy_rec/python/utils/hit_rate_utils.py +220 -0
  318. easy_rec/python/utils/hive_utils.py +183 -0
  319. easy_rec/python/utils/hpo_util.py +137 -0
  320. easy_rec/python/utils/hvd_utils.py +56 -0
  321. easy_rec/python/utils/input_utils.py +108 -0
  322. easy_rec/python/utils/io_util.py +282 -0
  323. easy_rec/python/utils/load_class.py +249 -0
  324. easy_rec/python/utils/meta_graph_editor.py +941 -0
  325. easy_rec/python/utils/multi_optimizer.py +62 -0
  326. easy_rec/python/utils/numpy_utils.py +18 -0
  327. easy_rec/python/utils/odps_util.py +79 -0
  328. easy_rec/python/utils/pai_util.py +86 -0
  329. easy_rec/python/utils/proto_util.py +90 -0
  330. easy_rec/python/utils/restore_filter.py +89 -0
  331. easy_rec/python/utils/shape_utils.py +432 -0
  332. easy_rec/python/utils/static_shape.py +71 -0
  333. easy_rec/python/utils/test_utils.py +866 -0
  334. easy_rec/python/utils/tf_utils.py +56 -0
  335. easy_rec/version.py +4 -0
  336. test/__init__.py +0 -0
@@ -0,0 +1,211 @@
1
+ # -*- encoding:utf-8 -*-
2
+ # Copyright 2017 The TensorFlow Authors. All Rights Reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ # ==============================================================================
16
+ """Functions to build training optimizers."""
17
+ import logging
18
+
19
+ import tensorflow as tf
20
+
21
+ from easy_rec.python.compat import weight_decay_optimizers
22
+ from easy_rec.python.core import learning_schedules
23
+
24
+ if tf.__version__ >= '2.0':
25
+ tf = tf.compat.v1
26
+
27
+
28
+ def build(optimizer_config):
29
+ """Create optimizer based on config.
30
+
31
+ Args:
32
+ optimizer_config: A Optimizer proto message.
33
+
34
+ Returns:
35
+ An optimizer and a list of variables for summary.
36
+
37
+ Raises:
38
+ ValueError: when using an unsupported input data type.
39
+ """
40
+ optimizer_type = optimizer_config.WhichOneof('optimizer')
41
+ optimizer = None
42
+
43
+ summary_vars = []
44
+ if optimizer_type == 'rms_prop_optimizer':
45
+ config = optimizer_config.rms_prop_optimizer
46
+ learning_rate = _create_learning_rate(config.learning_rate)
47
+ summary_vars.append(learning_rate)
48
+ optimizer = tf.train.RMSPropOptimizer(
49
+ learning_rate,
50
+ decay=config.decay,
51
+ momentum=config.momentum_optimizer_value,
52
+ epsilon=config.epsilon)
53
+
54
+ if optimizer_type == 'momentum_optimizer':
55
+ config = optimizer_config.momentum_optimizer
56
+ learning_rate = _create_learning_rate(config.learning_rate)
57
+ summary_vars.append(learning_rate)
58
+ optimizer = tf.train.MomentumOptimizer(
59
+ learning_rate, momentum=config.momentum_optimizer_value)
60
+
61
+ if optimizer_type == 'adam_optimizer':
62
+ config = optimizer_config.adam_optimizer
63
+ learning_rate = _create_learning_rate(config.learning_rate)
64
+ summary_vars.append(learning_rate)
65
+ optimizer = tf.train.AdamOptimizer(
66
+ learning_rate, beta1=config.beta1, beta2=config.beta2)
67
+
68
+ if optimizer_type == 'adamw_optimizer':
69
+ config = optimizer_config.adamw_optimizer
70
+ learning_rate = _create_learning_rate(config.learning_rate)
71
+ summary_vars.append(learning_rate)
72
+ logging.info('adamw_optimizer weight_decay = %.8f' % config.weight_decay)
73
+ optimizer = weight_decay_optimizers.AdamWOptimizer(
74
+ weight_decay=config.weight_decay,
75
+ learning_rate=learning_rate,
76
+ beta1=config.beta1,
77
+ beta2=config.beta2)
78
+
79
+ if optimizer_type == 'adam_asyncw_optimizer':
80
+ config = optimizer_config.adam_asyncw_optimizer
81
+ learning_rate = _create_learning_rate(config.learning_rate)
82
+ summary_vars.append(learning_rate)
83
+ logging.info('adam_asyncw_optimizer weight_decay = %.8f' %
84
+ config.weight_decay)
85
+ optimizer = weight_decay_optimizers.AdamAsyncWOptimizer(
86
+ weight_decay=config.weight_decay,
87
+ learning_rate=learning_rate,
88
+ beta1=config.beta1,
89
+ beta2=config.beta2)
90
+
91
+ if optimizer_type == 'lazy_adam_optimizer':
92
+ config = optimizer_config.lazy_adam_optimizer
93
+ learning_rate = _create_learning_rate(config.learning_rate)
94
+ summary_vars.append(learning_rate)
95
+ from easy_rec.python.compat.adam_s import AdamOptimizerS
96
+ optimizer = AdamOptimizerS(
97
+ learning_rate=learning_rate, beta1=config.beta1, beta2=config.beta2)
98
+
99
+ if optimizer_type == 'momentumw_optimizer':
100
+ config = optimizer_config.momentumw_optimizer
101
+ learning_rate = _create_learning_rate(config.learning_rate)
102
+ summary_vars.append(learning_rate)
103
+ logging.info('momentumw_optimizer weight_decay = %.8f' %
104
+ config.weight_decay)
105
+ optimizer = weight_decay_optimizers.MomentumWOptimizer(
106
+ weight_decay=config.weight_decay,
107
+ learning_rate=learning_rate,
108
+ momentum=config.momentum_optimizer_value)
109
+
110
+ if optimizer_type == 'adagrad_optimizer':
111
+ config = optimizer_config.adagrad_optimizer
112
+ learning_rate = _create_learning_rate(config.learning_rate)
113
+ summary_vars.append(learning_rate)
114
+ optimizer = tf.train.AdagradOptimizer(
115
+ learning_rate,
116
+ initial_accumulator_value=config.initial_accumulator_value)
117
+
118
+ if optimizer_type == 'adam_async_optimizer':
119
+ config = optimizer_config.adam_async_optimizer
120
+ learning_rate = _create_learning_rate(config.learning_rate)
121
+ summary_vars.append(learning_rate)
122
+ optimizer = tf.train.AdamAsyncOptimizer(
123
+ learning_rate, beta1=config.beta1, beta2=config.beta2)
124
+
125
+ if optimizer_type == 'ftrl_optimizer':
126
+ config = optimizer_config.ftrl_optimizer
127
+ learning_rate = _create_learning_rate(config.learning_rate)
128
+ summary_vars.append(learning_rate)
129
+ optimizer = tf.train.FtrlOptimizer(
130
+ learning_rate=learning_rate,
131
+ learning_rate_power=config.learning_rate_power,
132
+ initial_accumulator_value=config.initial_accumulator_value,
133
+ l1_regularization_strength=config.l1_reg,
134
+ l2_regularization_strength=config.l2_reg,
135
+ l2_shrinkage_regularization_strength=config.l2_shrinkage_reg)
136
+
137
+ if optimizer is None:
138
+ raise ValueError('Optimizer %s not supported.' % optimizer_type)
139
+
140
+ if optimizer_config.use_moving_average:
141
+ optimizer = tf.contrib.opt.MovingAverageOptimizer(
142
+ optimizer, average_decay=optimizer_config.moving_average_decay)
143
+
144
+ return optimizer, summary_vars
145
+
146
+
147
+ def _create_learning_rate(learning_rate_config):
148
+ """Create optimizer learning rate based on config.
149
+
150
+ Args:
151
+ learning_rate_config: A LearningRate proto message.
152
+
153
+ Returns:
154
+ A learning rate.
155
+
156
+ Raises:
157
+ ValueError: when using an unsupported input data type.
158
+ """
159
+ learning_rate = None
160
+ learning_rate_type = learning_rate_config.WhichOneof('learning_rate')
161
+ if learning_rate_type == 'constant_learning_rate':
162
+ config = learning_rate_config.constant_learning_rate
163
+ learning_rate = tf.constant(
164
+ config.learning_rate, dtype=tf.float32, name='learning_rate')
165
+
166
+ if learning_rate_type == 'exponential_decay_learning_rate':
167
+ config = learning_rate_config.exponential_decay_learning_rate
168
+ learning_rate = learning_schedules.exponential_decay_with_burnin(
169
+ tf.train.get_or_create_global_step(),
170
+ config.initial_learning_rate,
171
+ config.decay_steps,
172
+ config.decay_factor,
173
+ burnin_learning_rate=config.burnin_learning_rate,
174
+ burnin_steps=config.burnin_steps,
175
+ min_learning_rate=config.min_learning_rate,
176
+ staircase=config.staircase)
177
+
178
+ if learning_rate_type == 'manual_step_learning_rate':
179
+ config = learning_rate_config.manual_step_learning_rate
180
+ if not config.schedule:
181
+ raise ValueError('Empty learning rate schedule.')
182
+ learning_rate_step_boundaries = [x.step for x in config.schedule]
183
+ learning_rate_sequence = [config.initial_learning_rate]
184
+ learning_rate_sequence += [x.learning_rate for x in config.schedule]
185
+ learning_rate = learning_schedules.manual_stepping(
186
+ tf.train.get_or_create_global_step(), learning_rate_step_boundaries,
187
+ learning_rate_sequence, config.warmup)
188
+
189
+ if learning_rate_type == 'cosine_decay_learning_rate':
190
+ config = learning_rate_config.cosine_decay_learning_rate
191
+ learning_rate = learning_schedules.cosine_decay_with_warmup(
192
+ tf.train.get_or_create_global_step(), config.learning_rate_base,
193
+ config.total_steps, config.warmup_learning_rate, config.warmup_steps,
194
+ config.hold_base_rate_steps)
195
+
196
+ if learning_rate_type == 'poly_decay_learning_rate':
197
+ config = learning_rate_config.poly_decay_learning_rate
198
+ learning_rate = tf.train.polynomial_decay(
199
+ config.learning_rate_base, tf.train.get_or_create_global_step(),
200
+ config.total_steps, config.end_learning_rate, config.power)
201
+
202
+ if learning_rate_type == 'transformer_learning_rate':
203
+ config = learning_rate_config.transformer_learning_rate
204
+ learning_rate = learning_schedules.transformer_policy(
205
+ tf.train.get_or_create_global_step(), config.learning_rate_base,
206
+ config.hidden_size, config.warmup_steps, config.step_scaling_rate)
207
+
208
+ if learning_rate is None:
209
+ raise ValueError('Learning_rate %s not supported.' % learning_rate_type)
210
+
211
+ return learning_rate
@@ -0,0 +1,44 @@
1
+ # -*- encoding:utf-8 -*-
2
+ # Copyright (c) Alibaba, Inc. and its affiliates.
3
+ import tensorflow as tf
4
+
5
+ from easy_rec.python.protos.train_pb2 import DistributionStrategy
6
+ from easy_rec.python.protos.train_pb2 import TrainConfig
7
+
8
+
9
+ def build(train_config):
10
+ assert isinstance(train_config, TrainConfig)
11
+
12
+ distribution = None
13
+ # single worker multi-gpu strategy
14
+ # currently only works using pai-tf
15
+ if train_config.train_distribute == DistributionStrategy.MirroredStrategy:
16
+ if tf.__version__ <= '1.15':
17
+ distribution = tf.contrib.distribute.MirroredStrategy()
18
+ else:
19
+ distribution = tf.distribute.MirroredStrategy()
20
+ # multi worker multi-gpu strategy
21
+ # works under tf1.15 and tf2.x
22
+ elif train_config.train_distribute == DistributionStrategy.MultiWorkerMirroredStrategy:
23
+ distribution = tf.distribute.experimental.MultiWorkerMirroredStrategy()
24
+ # only works using pai-tf
25
+ elif train_config.train_distribute == DistributionStrategy.ExascaleStrategy:
26
+ import pai
27
+ distribution = pai.distribute.ExascaleStrategy(
28
+ max_splits=10,
29
+ issorted=True,
30
+ optimize_clip_by_global_norm=False,
31
+ enable_sparse_allreduce=False,
32
+ enable_hierarchical_allreduce=True)
33
+ # the older version of MultiWorkerMirroredStrategy
34
+ # works under tf1.12 to tf1.15
35
+ elif train_config.train_distribute == DistributionStrategy.CollectiveAllReduceStrategy:
36
+ distribution = tf.contrib.distribute.CollectiveAllReduceStrategy(
37
+ num_gpus_per_worker=train_config.num_gpus_per_worker)
38
+ # works under tf1.15 and tf2.x
39
+ elif train_config.train_distribute == DistributionStrategy.PSStrategy:
40
+ if tf.__version__ <= '1.15':
41
+ distribution = tf.contrib.distribute.ParameterServerStrategy()
42
+ else:
43
+ distribution = tf.distribute.experimental.ParameterServerStrategy()
44
+ return distribution
File without changes
@@ -0,0 +1,245 @@
1
+ # Copyright 2015 The TensorFlow 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
+ # ==============================================================================
15
+ """Adam for TensorFlow."""
16
+ from __future__ import absolute_import
17
+ from __future__ import division
18
+ from __future__ import print_function
19
+
20
+ from tensorflow.python.eager import context
21
+ from tensorflow.python.framework import ops
22
+ from tensorflow.python.ops import array_ops
23
+ from tensorflow.python.ops import control_flow_ops
24
+ from tensorflow.python.ops import math_ops
25
+ from tensorflow.python.ops import resource_variable_ops
26
+ from tensorflow.python.ops import state_ops
27
+ from tensorflow.python.training import optimizer
28
+ from tensorflow.python.training import training_ops
29
+
30
+
31
+ class AdamOptimizerS(optimizer.Optimizer):
32
+ """Optimizer that implements the Adam algorithm.
33
+
34
+ References:
35
+ Adam - A Method for Stochastic Optimization:
36
+ [Kingma et al., 2015](https://arxiv.org/abs/1412.6980)
37
+ ([pdf](https://arxiv.org/pdf/1412.6980.pdf))
38
+ """
39
+
40
+ def __init__(self,
41
+ learning_rate=0.001,
42
+ beta1=0.9,
43
+ beta2=0.999,
44
+ epsilon=1e-8,
45
+ use_locking=False,
46
+ name='Adam'):
47
+ r"""Construct a new Adam optimizer.
48
+
49
+ Initialization:
50
+
51
+ $$m_0 := 0 \text{(Initialize initial 1st moment vector)}$$
52
+ $$v_0 := 0 \text{(Initialize initial 2nd moment vector)}$$
53
+ $$t := 0 \text{(Initialize timestep)}$$
54
+
55
+ The update rule for `variable` with gradient `g` uses an optimization
56
+ described at the end of section 2 of the paper:
57
+
58
+ $$t := t + 1$$
59
+ $$\text{lr}_t := \mathrm{learning_rate} *
60
+ \sqrt{1 - \beta_2^t} / (1 - \beta_1^t)$$
61
+
62
+ $$m_t := \beta_1 * m_{t-1} + (1 - \beta_1) * g$$
63
+ $$v_t := \beta_2 * v_{t-1} + (1 - \beta_2) * g * g$$
64
+ $$\text{variable} := \text{variable} -
65
+ \text{lr}_t * m_t / (\sqrt{v_t} + \epsilon)$$
66
+
67
+ The default value of 1e-8 for epsilon might not be a good default in
68
+ general. For example, when training an Inception network on ImageNet a
69
+ current good choice is 1.0 or 0.1. Note that since AdamOptimizerS uses the
70
+ formulation just before Section 2.1 of the Kingma and Ba paper rather than
71
+ the formulation in Algorithm 1, the "epsilon" referred to here is "epsilon
72
+ hat" in the paper.
73
+
74
+ The sparse implementation of this algorithm (used when the gradient is an
75
+ IndexedSlices object, typically because of `tf.gather` or an embedding
76
+ lookup in the forward pass) does apply momentum to variable slices even if
77
+ they were not used in the forward pass (meaning they have a gradient equal
78
+ to zero). Momentum decay (beta1) is also applied to the entire momentum
79
+ accumulator. This means that the sparse behavior is equivalent to the dense
80
+ behavior (in contrast to some momentum implementations which ignore momentum
81
+ unless a variable slice was actually used).
82
+
83
+ Args:
84
+ learning_rate: A Tensor or a floating point value. The learning rate.
85
+ beta1: A float value or a constant float tensor. The exponential decay
86
+ rate for the 1st moment estimates.
87
+ beta2: A float value or a constant float tensor. The exponential decay
88
+ rate for the 2nd moment estimates.
89
+ epsilon: A small constant for numerical stability. This epsilon is
90
+ "epsilon hat" in the Kingma and Ba paper (in the formula just before
91
+ Section 2.1), not the epsilon in Algorithm 1 of the paper.
92
+ use_locking: If True use locks for update operations.
93
+ name: Optional name for the operations created when applying gradients.
94
+ Defaults to "Adam".
95
+
96
+ @compatibility(eager)
97
+ When eager execution is enabled, `learning_rate`, `beta1`, `beta2`, and
98
+ `epsilon` can each be a callable that takes no arguments and returns the
99
+ actual value to use. This can be useful for changing these values across
100
+ different invocations of optimizer functions.
101
+ @end_compatibility
102
+ """
103
+ super(AdamOptimizerS, self).__init__(use_locking, name)
104
+ self._lr = learning_rate
105
+ self._beta1 = beta1
106
+ self._beta2 = beta2
107
+ self._epsilon = epsilon
108
+
109
+ # Tensor versions of the constructor arguments, created in _prepare().
110
+ self._lr_t = None
111
+ self._beta1_t = None
112
+ self._beta2_t = None
113
+ self._epsilon_t = None
114
+
115
+ def _get_beta_accumulators(self):
116
+ with ops.init_scope():
117
+ if context.executing_eagerly():
118
+ graph = None
119
+ else:
120
+ graph = ops.get_default_graph()
121
+ return (self._get_non_slot_variable('beta1_power', graph=graph),
122
+ self._get_non_slot_variable('beta2_power', graph=graph))
123
+
124
+ def _create_slots(self, var_list):
125
+ # Create the beta1 and beta2 accumulators on the same device as the first
126
+ # variable. Sort the var_list to make sure this device is consistent across
127
+ # workers (these need to go on the same PS, otherwise some updates are
128
+ # silently ignored).
129
+ first_var = min(var_list, key=lambda x: x.name)
130
+ self._create_non_slot_variable(
131
+ initial_value=self._beta1, name='beta1_power', colocate_with=first_var)
132
+ self._create_non_slot_variable(
133
+ initial_value=self._beta2, name='beta2_power', colocate_with=first_var)
134
+
135
+ # Create slots for the first and second moments.
136
+ for v in var_list:
137
+ self._zeros_slot(v, 'm', self._name)
138
+ self._zeros_slot(v, 'v', self._name)
139
+
140
+ def _prepare(self):
141
+ lr = self._call_if_callable(self._lr)
142
+ beta1 = self._call_if_callable(self._beta1)
143
+ beta2 = self._call_if_callable(self._beta2)
144
+ epsilon = self._call_if_callable(self._epsilon)
145
+
146
+ self._lr_t = ops.convert_to_tensor(lr, name='learning_rate')
147
+ self._beta1_t = ops.convert_to_tensor(beta1, name='beta1')
148
+ self._beta2_t = ops.convert_to_tensor(beta2, name='beta2')
149
+ self._epsilon_t = ops.convert_to_tensor(epsilon, name='epsilon')
150
+
151
+ def _apply_dense(self, grad, var):
152
+ m = self.get_slot(var, 'm')
153
+ v = self.get_slot(var, 'v')
154
+ beta1_power, beta2_power = self._get_beta_accumulators()
155
+ return training_ops.apply_adam(
156
+ var,
157
+ m,
158
+ v,
159
+ math_ops.cast(beta1_power, var.dtype.base_dtype),
160
+ math_ops.cast(beta2_power, var.dtype.base_dtype),
161
+ math_ops.cast(self._lr_t, var.dtype.base_dtype),
162
+ math_ops.cast(self._beta1_t, var.dtype.base_dtype),
163
+ math_ops.cast(self._beta2_t, var.dtype.base_dtype),
164
+ math_ops.cast(self._epsilon_t, var.dtype.base_dtype),
165
+ grad,
166
+ use_locking=self._use_locking).op
167
+
168
+ def _resource_apply_dense(self, grad, var):
169
+ m = self.get_slot(var, 'm')
170
+ v = self.get_slot(var, 'v')
171
+ beta1_power, beta2_power = self._get_beta_accumulators()
172
+ return training_ops.resource_apply_adam(
173
+ var.handle,
174
+ m.handle,
175
+ v.handle,
176
+ math_ops.cast(beta1_power, grad.dtype.base_dtype),
177
+ math_ops.cast(beta2_power, grad.dtype.base_dtype),
178
+ math_ops.cast(self._lr_t, grad.dtype.base_dtype),
179
+ math_ops.cast(self._beta1_t, grad.dtype.base_dtype),
180
+ math_ops.cast(self._beta2_t, grad.dtype.base_dtype),
181
+ math_ops.cast(self._epsilon_t, grad.dtype.base_dtype),
182
+ grad,
183
+ use_locking=self._use_locking)
184
+
185
+ def _apply_sparse_shared(self, grad, var, indices, scatter_add):
186
+ beta1_power, beta2_power = self._get_beta_accumulators()
187
+ beta1_power = math_ops.cast(beta1_power, var.dtype.base_dtype)
188
+ beta2_power = math_ops.cast(beta2_power, var.dtype.base_dtype)
189
+ lr_t = math_ops.cast(self._lr_t, var.dtype.base_dtype)
190
+ beta1_t = math_ops.cast(self._beta1_t, var.dtype.base_dtype)
191
+ beta2_t = math_ops.cast(self._beta2_t, var.dtype.base_dtype)
192
+ epsilon_t = math_ops.cast(self._epsilon_t, var.dtype.base_dtype)
193
+ lr = (lr_t * math_ops.sqrt(1 - beta2_power) / (1 - beta1_power))
194
+ # m_t = beta1 * m + (1 - beta1) * g_t
195
+ m = self.get_slot(var, 'm')
196
+ m_scaled_g_values = grad * (1 - beta1_t)
197
+ # m_t = state_ops.assign(m, m * beta1_t, use_locking=self._use_locking)
198
+ m_decay = array_ops.gather(m, indices) * beta1_t
199
+ m_part_n = m_scaled_g_values + m_decay
200
+ m_t = state_ops.scatter_update(m, indices, m_part_n)
201
+ # v_t = beta2 * v + (1 - beta2) * (g_t * g_t)
202
+ v = self.get_slot(var, 'v')
203
+ v_scaled_g_values = (grad * grad) * (1 - beta2_t)
204
+ v_decay = array_ops.gather(v, indices) * beta2_t
205
+ v_part_n = v_scaled_g_values + v_decay
206
+ v_t = state_ops.scatter_update(v, indices, v_part_n)
207
+ # v_sqrt = math_ops.sqrt(v_t)
208
+ # var_update = state_ops.assign_sub(
209
+ # var, lr * m_t / (v_sqrt + epsilon_t), use_locking=self._use_locking)
210
+ v_part_sqrt = math_ops.sqrt(v_part_n)
211
+ var_update = scatter_add(var, indices,
212
+ -lr * m_part_n / (v_part_sqrt + epsilon_t))
213
+ return control_flow_ops.group(*[var_update, m_t, v_t])
214
+
215
+ def _apply_sparse(self, grad, var):
216
+ return self._apply_sparse_shared(
217
+ grad.values,
218
+ var,
219
+ grad.indices,
220
+ lambda x, i, v: state_ops.scatter_add( # pylint: disable=g-long-lambda
221
+ x,
222
+ i,
223
+ v,
224
+ use_locking=self._use_locking))
225
+
226
+ def _resource_scatter_add(self, x, i, v):
227
+ with ops.control_dependencies(
228
+ [resource_variable_ops.resource_scatter_add(x.handle, i, v)]):
229
+ return x.value()
230
+
231
+ def _resource_apply_sparse(self, grad, var, indices):
232
+ return self._apply_sparse_shared(grad, var, indices,
233
+ self._resource_scatter_add)
234
+
235
+ def _finish(self, update_ops, name_scope):
236
+ # Update the power accumulators.
237
+ with ops.control_dependencies(update_ops):
238
+ beta1_power, beta2_power = self._get_beta_accumulators()
239
+ with ops.colocate_with(beta1_power):
240
+ update_beta1 = beta1_power.assign(
241
+ beta1_power * self._beta1_t, use_locking=self._use_locking)
242
+ update_beta2 = beta2_power.assign(
243
+ beta2_power * self._beta2_t, use_locking=self._use_locking)
244
+ return control_flow_ops.group(
245
+ *update_ops + [update_beta1, update_beta2], name=name_scope)