Пример #1
0
def _input_from_feature_columns(columns_to_tensors,
                                feature_columns,
                                weight_collections,
                                trainable,
                                scope,
                                output_rank,
                                default_name,
                                cols_to_outs=None):
  """Implementation of `input_from(_sequence)_feature_columns`."""
  columns_to_tensors = columns_to_tensors.copy()
  check_feature_columns(feature_columns)
  if cols_to_outs is not None and not isinstance(cols_to_outs, dict):
    raise ValueError('cols_to_outs must be a dict unless None')
  with variable_scope.variable_scope(scope,
                                     default_name=default_name,
                                     values=columns_to_tensors.values()):
    output_tensors = []
    transformer = _Transformer(columns_to_tensors)
    if weight_collections:
      weight_collections = list(set(list(weight_collections) +
                                    [ops.GraphKeys.GLOBAL_VARIABLES]))

    for column in sorted(set(feature_columns), key=lambda x: x.key):
      with variable_scope.variable_scope(None,
                                         default_name=column.name,
                                         values=columns_to_tensors.values()):
        transformed_tensor = transformer.transform(column)
        if output_rank == 3:
          transformed_tensor = nest.map_structure(
              functools.partial(
                  _maybe_reshape_input_tensor,
                  column_name=column.name,
                  output_rank=output_rank), transformed_tensor)
        try:
          # pylint: disable=protected-access
          arguments = column._deep_embedding_lookup_arguments(
              transformed_tensor)
          output_tensors.append(
              fc._embeddings_from_arguments(  # pylint: disable=protected-access
                  column,
                  arguments,
                  weight_collections,
                  trainable,
                  output_rank=output_rank))

        except NotImplementedError as ee:
          try:
            # pylint: disable=protected-access
            output_tensors.append(column._to_dnn_input_layer(
                transformed_tensor,
                weight_collections,
                trainable,
                output_rank=output_rank))
          except ValueError as e:
            raise ValueError('Error creating input layer for column: {}.\n'
                             '{}, {}'.format(column.name, e, ee))
        if cols_to_outs is not None:
          cols_to_outs[column] = output_tensors[-1]
    return array_ops.concat(output_tensors, output_rank - 1)
Пример #2
0
def _input_from_feature_columns(columns_to_tensors,
                                feature_columns,
                                weight_collections,
                                trainable,
                                scope,
                                output_rank,
                                default_name,
                                cols_to_outs=None):
  """Implementation of `input_from(_sequence)_feature_columns`."""
  columns_to_tensors = columns_to_tensors.copy()
  check_feature_columns(feature_columns)
  if cols_to_outs is not None and not isinstance(cols_to_outs, dict):
    raise ValueError('cols_to_outs must be a dict unless None')
  with variable_scope.variable_scope(scope,
                                     default_name=default_name,
                                     values=columns_to_tensors.values()):
    output_tensors = []
    transformer = _Transformer(columns_to_tensors)
    if weight_collections:
      weight_collections = list(set(list(weight_collections) +
                                    [ops.GraphKeys.GLOBAL_VARIABLES]))

    for column in sorted(set(feature_columns), key=lambda x: x.key):
      with variable_scope.variable_scope(None,
                                         default_name=column.name,
                                         values=columns_to_tensors.values()):
        transformed_tensor = transformer.transform(column)
        if output_rank == 3:
          transformed_tensor = nest.map_structure(
              functools.partial(
                  _maybe_reshape_input_tensor,
                  column_name=column.name,
                  output_rank=output_rank), transformed_tensor)
        try:
          # pylint: disable=protected-access
          arguments = column._deep_embedding_lookup_arguments(
              transformed_tensor)
          output_tensors.append(
              fc._embeddings_from_arguments(  # pylint: disable=protected-access
                  column,
                  arguments,
                  weight_collections,
                  trainable,
                  output_rank=output_rank))

        except NotImplementedError as ee:
          try:
            # pylint: disable=protected-access
            output_tensors.append(column._to_dnn_input_layer(
                transformed_tensor,
                weight_collections,
                trainable,
                output_rank=output_rank))
          except ValueError as e:
            raise ValueError('Error creating input layer for column: {}.\n'
                             '{}, {}'.format(column.name, e, ee))
        if cols_to_outs is not None:
          cols_to_outs[column] = output_tensors[-1]
    return array_ops.concat(output_tensors, output_rank - 1)
Пример #3
0
def input_from_feature_columns(columns_to_tensors,
                               feature_columns,
                               weight_collections,
                               scope,
                               trainable=True,
                               output_rank=2,
                               default_name='input_from_feature_columns'):
    columns_to_tensors = columns_to_tensors.copy()
    feature_column_ops.check_feature_columns(feature_columns)
    with variable_scope.variable_scope(scope,
                                       default_name=default_name,
                                       values=columns_to_tensors.values()):
        output_tensors_dict = {}
        transformer = feature_column_ops._Transformer(columns_to_tensors)
        if weight_collections:
            weight_collections = list(set(list(weight_collections) +
                                          [ops.GraphKeys.GLOBAL_VARIABLES]))
        for column in sorted(set(feature_columns), key=lambda x: x.key):
            with variable_scope.variable_scope(None,
                                               default_name=column.name,
                                               values=columns_to_tensors.values()):
                transformed_tensor = transformer.transform(column)
                key = column.key
                try:
                    # pylint: disable=protected-access
                    arguments = column._deep_embedding_lookup_arguments(
                            transformed_tensor)
                    output_tensors_dict[key] = \
                            fc._embeddings_from_arguments(    # pylint: disable=protected-access
                                    column,
                                    arguments,
                                    weight_collections,
                                    trainable,
                                    output_rank=output_rank)
                except NotImplementedError as ee:
                    try:
                        # pylint: disable=protected-access
                        output_tensors_dict[key] = \
                                column._to_dnn_input_layer(
                                        transformed_tensor,
                                        weight_collections,
                                        trainable,
                                        output_rank=output_rank)
                    except ValueError as e:
                        raise ValueError('Error creating input layer for column: {}.\n'
                                                         '{}, {}'.format(column.name, e, ee))
        return output_tensors_dict
Пример #4
0
    def _build_fm_logits(features, fm_feature_columns, output_rank, num_ps_replicas, input_layer_min_slice_size):
        '''
        Function:
            构建fm侧逻辑的接口,返回fm侧的logits
        Args:
            features(tensor_dic):
                包含所有特征的tensor_dict
            fm_feature_columns(feature column set):
                构成fm侧的feature column集合
            output_rank(int):
                期望的输出矩阵的秩
            num_ps_replicas(int):
                PS replicas的数量
            input_layer_min_slice_size(int):
                输入层的最小分片大小
        Returns:
            fm_logits(Tensor):
                构成算法逻辑fm侧的logits
        '''
        if not fm_feature_columns or len(fm_feature_columns) <= 1:
            return None
        else:
            fm_parent_scope = WideAndDeepAlgorithm.FM_SCOPE_NAME
            fm_partitioner = (
                tf.min_max_variable_partitioner(
                    max_partitions=num_ps_replicas))
            with tf.variable_scope(
                fm_parent_scope,
                values=tuple(six.itervalues(features)),
                partitioner=fm_partitioner) as fm_input_scope:

                fm_feature_tensor_list = []
                transformed_tensor_dict = tf.contrib.layers.transform_features(features, fm_feature_columns)

                for column in fm_feature_columns:
                    transformed_tensor = transformed_tensor_dict[column]
                    output_tensor = None
                    with tf.variable_scope(column.name):
                        try:
                            arguments = column._deep_embedding_lookup_arguments(transformed_tensor)
                            output_tensor = feature_column_ops_lib._embeddings_from_arguments(
                                column,
                                arguments,
                                [fm_parent_scope],
                                True,
                                output_rank=output_rank+1)

                        except NotImplementedError as ee:
                            try:
                                output_tensor  = column._to_dnn_input_layer(
                                    transformed_tensor,
                                    [fm_parent_scope],
                                    True,
                                    output_rank=output_rank+1)
                            except ValueError as e:
                                raise ValueError('Error creating input layer for column: {}.\n'
                                               '{}, {}'.format(column.name, e, ee))
                    fm_feature_tensor_list.append(output_tensor)

                fm_logits = 0.0
                num_embs = len(fm_feature_columns)
                for a in range(num_embs):
                    for b in range(a + 1, num_embs):
                        fm_logits += tf.reduce_sum(tf.multiply(fm_feature_tensor_list[a], fm_feature_tensor_list[b]), output_rank, keep_dims=True)
                return fm_logits