Пример #1
0
def _log_variable(variable):
  if isinstance(variable, list):
    for var in variable:
      if fc._is_variable(variable):  # pylint: disable=protected-access
        logging.info('Created variable %s, with device=%s', var.name,
                     var.device)
  elif fc._is_variable(variable):  # pylint: disable=protected-access
    logging.info('Created variable %s, with device=%s', variable.name,
                 variable.device)
Пример #2
0
def _log_variable(variable):
    if isinstance(variable, list):
        for var in variable:
            if fc._is_variable(variable):  # pylint: disable=protected-access
                logging.info('Created variable %s, with device=%s', var.name,
                             var.device)
    elif fc._is_variable(variable):  # pylint: disable=protected-access
        logging.info('Created variable %s, with device=%s', variable.name,
                     variable.device)
Пример #3
0
def _create_joint_embedding_lookup(columns_to_tensors,
                                   embedding_lookup_arguments,
                                   num_outputs,
                                   trainable,
                                   weight_collections):
  """Creates an embedding lookup for all columns sharing a single weight."""
  for arg in embedding_lookup_arguments:
    assert arg.weight_tensor is None, (
        'Joint sums for weighted sparse columns are not supported. '
        'Please use weighted_sum_from_feature_columns instead.')
    assert arg.combiner == 'sum', (
        'Combiners other than sum are not supported for joint sums. '
        'Please use weighted_sum_from_feature_columns instead.')
  assert len(embedding_lookup_arguments) >= 1, (
      'At least one column must be in the model.')
  prev_size = 0
  sparse_tensors = []
  for a in embedding_lookup_arguments:
    t = a.input_tensor
    values = t.values + prev_size
    prev_size += a.vocab_size
    sparse_tensors.append(
        sparse_tensor_py.SparseTensor(t.indices,
                                      values,
                                      t.dense_shape))
  sparse_tensor = sparse_ops.sparse_concat(1, sparse_tensors)
  with variable_scope.variable_scope(
      None, default_name='linear_weights', values=columns_to_tensors.values()):
    variable = contrib_variables.model_variable(
        name='weights',
        shape=[prev_size, num_outputs],
        dtype=dtypes.float32,
        initializer=init_ops.zeros_initializer(),
        trainable=trainable,
        collections=weight_collections)
    if fc._is_variable(variable):  # pylint: disable=protected-access
      variable = [variable]
    else:
      variable = variable._get_variable_list()  # pylint: disable=protected-access
    predictions = embedding_ops.safe_embedding_lookup_sparse(
        variable,
        sparse_tensor,
        sparse_weights=None,
        combiner='sum',
        name='_weights')
    return variable, predictions
Пример #4
0
def _create_joint_embedding_lookup(columns_to_tensors,
                                   embedding_lookup_arguments,
                                   num_outputs,
                                   trainable,
                                   weight_collections):
  """Creates an embedding lookup for all columns sharing a single weight."""
  for arg in embedding_lookup_arguments:
    assert arg.weight_tensor is None, (
        'Joint sums for weighted sparse columns are not supported. '
        'Please use weighted_sum_from_feature_columns instead.')
    assert arg.combiner == 'sum', (
        'Combiners other than sum are not supported for joint sums. '
        'Please use weighted_sum_from_feature_columns instead.')
  assert len(embedding_lookup_arguments) >= 1, (
      'At least one column must be in the model.')
  prev_size = 0
  sparse_tensors = []
  for a in embedding_lookup_arguments:
    t = a.input_tensor
    values = t.values + prev_size
    prev_size += a.vocab_size
    sparse_tensors.append(
        sparse_tensor_py.SparseTensor(t.indices,
                                      values,
                                      t.dense_shape))
  sparse_tensor = sparse_ops.sparse_concat(1, sparse_tensors)
  with variable_scope.variable_scope(
      None, default_name='linear_weights', values=columns_to_tensors.values()):
    variable = contrib_variables.model_variable(
        name='weights',
        shape=[prev_size, num_outputs],
        dtype=dtypes.float32,
        initializer=init_ops.zeros_initializer(),
        trainable=trainable,
        collections=weight_collections)
    if fc._is_variable(variable):  # pylint: disable=protected-access
      variable = [variable]
    else:
      variable = variable._get_variable_list()  # pylint: disable=protected-access
    predictions = embedding_ops.safe_embedding_lookup_sparse(
        variable,
        sparse_tensor,
        sparse_weights=None,
        combiner='sum',
        name='_weights')
    return variable, predictions
Пример #5
0
def _create_embedding_lookup(column,
                             columns_to_tensors,
                             embedding_lookup_arguments,
                             num_outputs,
                             trainable,
                             weight_collections):
  """Creates variables and returns predictions for linear weights in a model.

  Args:
   column: the column we're working on.
   columns_to_tensors: a map from column name to tensors.
   embedding_lookup_arguments: arguments for embedding lookup.
   num_outputs: how many outputs.
   trainable: whether the variable we create is trainable.
   weight_collections: weights will be placed here.

  Returns:
  variables: the created embeddings.
  predictions: the computed predictions.
  """
  with variable_scope.variable_scope(
      None, default_name=column.name, values=columns_to_tensors.values()):
    variable = contrib_variables.model_variable(
        name='weights',
        shape=[embedding_lookup_arguments.vocab_size, num_outputs],
        dtype=dtypes.float32,
        initializer=embedding_lookup_arguments.initializer,
        trainable=trainable,
        collections=weight_collections)
    if fc._is_variable(variable):  # pylint: disable=protected-access
      variable = [variable]
    else:
      variable = variable._get_variable_list()  # pylint: disable=protected-access
    predictions = embedding_ops.safe_embedding_lookup_sparse(
        variable,
        embedding_lookup_arguments.input_tensor,
        sparse_weights=embedding_lookup_arguments.weight_tensor,
        combiner=embedding_lookup_arguments.combiner,
        name=column.name + '_weights')
    return variable, predictions
Пример #6
0
def _create_embedding_lookup(column,
                             columns_to_tensors,
                             embedding_lookup_arguments,
                             num_outputs,
                             trainable,
                             weight_collections):
  """Creates variables and returns predictions for linear weights in a model.

  Args:
   column: the column we're working on.
   columns_to_tensors: a map from column name to tensors.
   embedding_lookup_arguments: arguments for embedding lookup.
   num_outputs: how many outputs.
   trainable: whether the variable we create is trainable.
   weight_collections: weights will be placed here.

  Returns:
  variables: the created embeddings.
  predictions: the computed predictions.
  """
  with variable_scope.variable_scope(
      None, default_name=column.name, values=columns_to_tensors.values()):
    variable = contrib_variables.model_variable(
        name='weights',
        shape=[embedding_lookup_arguments.vocab_size, num_outputs],
        dtype=dtypes.float32,
        initializer=embedding_lookup_arguments.initializer,
        trainable=trainable,
        collections=weight_collections)
    if fc._is_variable(variable):  # pylint: disable=protected-access
      variable = [variable]
    else:
      variable = variable._get_variable_list()  # pylint: disable=protected-access
    predictions = embedding_ops.safe_embedding_lookup_sparse(
        variable,
        embedding_lookup_arguments.input_tensor,
        sparse_weights=embedding_lookup_arguments.weight_tensor,
        combiner=embedding_lookup_arguments.combiner,
        name=column.name + '_weights')
    return variable, predictions
Пример #7
0
    def _to_dnn_input_layer(self,
                            transformed_input_tensor,
                            weight_collections=None,
                            trainable=True,
                            output_rank=2):
        """Returns a Tensor as an input to the first layer of neural network.
        Args:
            transformed_input_tensor: A tensor that has undergone the transformations
            in `insert_transformed_feature`. Rank should be >= `output_rank`.
            unused_weight_collections: Unused. One hot encodings are not variable.
            unused_trainable: Unused. One hot encodings are not trainable.
            output_rank: the desired rank of the output `Tensor`.

        Returns:
            A outputs Tensor of RNN to be fed into the first layer of neural network.

        Raises:
        """
        sparse_id_column = self.sparse_id_column.id_tensor(transformed_input_tensor)
        # pylint: disable=protected-access
        sparse_id_column = layers._inner_flatten(sparse_id_column, output_rank)

        batch_size = sparse_id_column.dense_shape[0]
        dense_id_tensor = sparse_ops.sparse_to_dense(sparse_id_column.indices,
                                                     [batch_size, 
                                                      self.max_sequence_length],
                                                     sparse_id_column.values,
                                                     default_value=0)
       # dense_id_tensor = gen_array_ops.reshape(dense_id_tensor, [-1, self.max_sequence_length])

        if self.shared_embedding_name is not None:
            shared_embedding_collection_name = (
                "SHARED_EMBEDDING_COLLECTION_" + self.shared_embedding_name.upper())
            graph = ops.get_default_graph()
            shared_embedding_collection = (
                graph.get_collection_ref(shared_embedding_collection_name))
            shape = [self.length, self.embedding_dimension]
            if shared_embedding_collection:
                if len(shared_embedding_collection) > 1:
                    raise ValueError(
                        "Collection %s can only contain one "
                        "(partitioned) variable." % shared_embedding_collection_name)
                else:
                    embeddings = shared_embedding_collection[0]
                    if embeddings.get_shape() != shape:
                        raise ValueError(
                            "The embedding variable with name {} already "
                            "exists, but its shape does not match required "
                            "embedding shape here. Please make sure to use "
                            "different shared_embedding_name for different "
                            "shared embeddings.".format(args.shared_embedding_name))
            else:
                embeddings = contrib_variables.model_variable(
                    name=self.shared_embedding_name,
                    shape=shape,
                    dtype=dtypes.float32,
                    initializer=self.initializer,
                    trainable=(trainable and self.trainable),
                    collections=weight_collections)
                graph.add_to_collection(shared_embedding_collection_name, embeddings)
        else:
            embeddings = contrib_variables.model_variable(
                name="weights",
                shape=[self.length, self.embedding_dimension],
                dtype=dtypes.float32,
                initializer=self.initializer,
                trainable=(trainable and self.trainable),
                collections=weight_collections)

        if _is_variable(embeddings):
            embeddings = [embeddings]
        else:
            embeddings = embeddings._get_variable_list()  # pylint: disable=protected-access
       
        embedding_inputs = embedding_lookup(
            embeddings,
            dense_id_tensor,
            max_norm=self.max_norm)
        
        dropout = (self.dropout_keep_probabilities
                   if self.mode == model_fn.ModeKeys.TRAIN
                   else None)

        sequence_length =  self._sequence_length(dense_id_tensor)
        if bidirectional_rnn:
            cell_fw = rnn_common.construct_rnn_cell(self.num_units, self.cell_type, dropout)
            cell_bw = rnn_common.construct_rnn_cell(self.num_units, self.cell_type, dropout)
            _rnn_outputs, _ = rnn.bidirectional_dynamic_rnn(cell_fw,
                                                            cell_bw,
                                                            embedding_inputs,
                                                            sequence_length=sequence_length,
                                                            dtype=dtypes.float32)
            rnn_outputs = array_ops.concat(_rnn_outputs, axis=2)
        else:
            cell = rnn_common.construct_rnn_cell(self.num_units, self.cell_type, dropout)
            rnn_outputs, _ = rnn.dynamic_rnn(cell,
                                             embedding_inputs,
                                             sequence_length=sequence_length,
                                             dtype=dtypes.float32)
        
        return self._extract_last_relevent(rnn_outputs, sequence_length)