| 491 | |
| 492 | # generate feature columns |
| 493 | def build_feature_columns(data_location=None): |
| 494 | # uid_file |
| 495 | uid_file = os.path.join(data_location, 'uid_voc.txt') |
| 496 | mid_file = os.path.join(data_location, 'mid_voc.txt') |
| 497 | cat_file = os.path.join(data_location, 'cat_voc.txt') |
| 498 | if (not os.path.exists(uid_file)) or (not os.path.exists(mid_file)) or ( |
| 499 | not os.path.exists(cat_file)): |
| 500 | print( |
| 501 | "uid_voc.txt, mid_voc.txt or cat_voc does not exist in data file.") |
| 502 | sys.exit() |
| 503 | # uid |
| 504 | uid_cate_column = tf.feature_column.categorical_column_with_vocabulary_file( |
| 505 | 'UID', uid_file, default_value=0) |
| 506 | ev_opt = None |
| 507 | if args.group_embedding and not args.tf: |
| 508 | context = tf.feature_column.group_embedding_column_scope(name="categorical") |
| 509 | else: |
| 510 | context = contextlib.nullcontext() |
| 511 | with context: |
| 512 | |
| 513 | if not args.tf: |
| 514 | '''Feature Elimination of EmbeddingVariable Feature''' |
| 515 | if args.ev_elimination == 'gstep': |
| 516 | # Feature elimination based on global steps |
| 517 | evict_opt = tf.GlobalStepEvict(steps_to_live=4000) |
| 518 | elif args.ev_elimination == 'l2': |
| 519 | # Feature elimination based on l2 weight |
| 520 | evict_opt = tf.L2WeightEvict(l2_weight_threshold=1.0) |
| 521 | else: |
| 522 | evict_opt = None |
| 523 | '''Feature Filter of EmbeddingVariable Feature''' |
| 524 | if args.ev_filter == 'cbf': |
| 525 | # CBF-based feature filter |
| 526 | filter_option = tf.CBFFilter(filter_freq=3, |
| 527 | max_element_size=2**30, |
| 528 | false_positive_probability=0.01, |
| 529 | counter_type=tf.int64) |
| 530 | elif args.ev_filter == 'counter': |
| 531 | # Counter-based feature filter |
| 532 | filter_option = tf.CounterFilter(filter_freq=3) |
| 533 | else: |
| 534 | filter_option = None |
| 535 | ev_opt = tf.EmbeddingVariableOption(evict_option=evict_opt, |
| 536 | filter_option=filter_option) |
| 537 | |
| 538 | if args.ev: |
| 539 | '''Embedding Variable Feature with feature_column API''' |
| 540 | uid_cate_column = tf.feature_column.categorical_column_with_embedding( |
| 541 | 'UID', dtype=tf.string, ev_option=ev_opt) |
| 542 | elif args.adaptive_emb: |
| 543 | ''' Adaptive Embedding Feature Part 2 of 2 |
| 544 | Expcet the follow code, a dict, 'adaptive_mask_tensors', is need as the input of |
| 545 | 'tf.feature_column.input_layer(adaptive_mask_tensors=adaptive_mask_tensors)'. |
| 546 | For column 'COL_NAME',the value of adaptive_mask_tensors['$COL_NAME'] is a int32 |
| 547 | tensor with shape [batch_size]. |
| 548 | ''' |
| 549 | uid_cate_column = tf.feature_column.categorical_column_with_adaptive_embedding( |
| 550 | 'UID', |