Example #1
0
    def make_inputs(self, features, training=None):
        inputs = features["char_ids"]
        flat_inputs = tf.reshape(inputs, [-1, tf.shape(inputs)[-1]])
        embeddings = self._embed(flat_inputs, training)
        sequence_length = tf.count_nonzero(flat_inputs, axis=1)

        cell = build_cell(1,
                          self.num_units,
                          tf.estimator.ModeKeys.TRAIN if training else None,
                          dropout=self.dropout,
                          cell_class=self.cell_class)
        rnn_outputs, rnn_state = tf.nn.dynamic_rnn(
            cell,
            embeddings,
            sequence_length=sequence_length,
            dtype=embeddings.dtype)

        if self.encoding == "average":
            encoding = tf.reduce_mean(rnn_outputs, axis=1)
        elif self.encoding == "last":
            encoding = last_encoding_from_state(rnn_state)

        outputs = tf.reshape(encoding,
                             [-1, tf.shape(inputs)[1], self.num_units])
        return outputs
  def _build(self, features, labels, params, mode, config=None):
    with tf.variable_scope("encoder"):
      inputs = self.features_inputter.transform_data(
          features,
          mode=mode,
          log_dir=config.model_dir if config is not None else None)

      encoder_outputs, encoder_state, _ = self.encoder.encode(
          inputs,
          sequence_length=self.features_inputter.get_length(features),
          mode=mode)

    if self.encoding == "average":
      encoding = tf.reduce_mean(encoder_outputs, axis=1)
    elif self.encoding == "last":
      encoding = last_encoding_from_state(encoder_state)

    with tf.variable_scope("generator"):
      logits = tf.layers.dense(
          encoding,
          self.labels_inputter.vocabulary_size)

    if mode != tf.estimator.ModeKeys.TRAIN:
      labels_vocab_rev = self.labels_inputter.vocabulary_lookup_reverse()
      classes_prob = tf.nn.softmax(logits)
      classes_id = tf.argmax(classes_prob, axis=1)
      predictions = {
          "classes": labels_vocab_rev.lookup(classes_id)
      }
    else:
      predictions = None

    return logits, predictions
Example #3
0
    def _call(self, features, labels, params, mode):
        training = mode == tf.estimator.ModeKeys.TRAIN
        if self.features_inputter.has_word():
            with tf.variable_scope("encoder"):
                inputs = self.features_inputter.get_word(features,
                                                         training=training)

                to_concat = [inputs]

                if self.features_inputter.has_lm():
                    lm_layers = self.features_inputter.get_lm(features)
                    lm_layer_weights = tf.nn.softmax(tf.Variable(
                        tf.random.uniform([lm_layers.shape[-2].value],
                                          maxval=1)),
                                                     axis=0)
                    w = tf.tensordot(lm_layers,
                                     lm_layer_weights,
                                     axes=[[2], [0]])
                    to_concat.append(w)

                inputs = tf.concat(to_concat, axis=-1)

                encoder_outputs, encoder_state, _ = self.encoder.encode(
                    inputs,
                    sequence_length=self.features_inputter.get_length(
                        features),
                    mode=mode)

            if self.encoding == "average":
                encoding = tf.reduce_mean(encoder_outputs, axis=1)
            elif self.encoding == "last":
                encoding = last_encoding_from_state(encoder_state)

        if self.features_inputter.has_global():
            global_features = self.features_inputter.get_global(features)
            print(global_features)

            encoding = tf.concat([encoding, global_features],
                                 axis=-1) if self.features_inputter.has_word(
                                 ) else global_features
            encoding = tf.layers.dense(encoding, 256, activation='relu')

        with tf.variable_scope("generator"):
            logits = tf.layers.dense(encoding, self.number_of_labels)

        if mode != tf.estimator.ModeKeys.TRAIN:
            prob = tf.math.sigmoid(logits)
            predictions = {"probabilities": prob, "labels": tf.round(prob)}
        else:
            predictions = None

        return logits, predictions
Example #4
0
    def transform(self, inputs, mode):
        flat_inputs = tf.reshape(inputs, [-1, tf.shape(inputs)[-1]])
        embeddings = self._embed(flat_inputs, mode)
        sequence_length = tf.count_nonzero(flat_inputs, axis=1)

        cell = build_cell(1,
                          self.num_units,
                          mode,
                          dropout=self.dropout,
                          cell_class=self.cell_class)
        rnn_outputs, rnn_state = tf.nn.dynamic_rnn(
            cell,
            embeddings,
            sequence_length=sequence_length,
            dtype=embeddings.dtype)

        if self.encoding == "average":
            encoding = tf.reduce_mean(rnn_outputs, axis=1)
        elif self.encoding == "last":
            encoding = last_encoding_from_state(rnn_state)

        outputs = tf.reshape(encoding,
                             [-1, tf.shape(inputs)[1], self.num_units])
        return outputs