Example #1
0
    def state_placeholders(self):
        """Get the Tensorflow placeholders for the model's states.

    Returns:
      A Namespace tree containing the placeholders.
    """
        return NS.Copy(self._state_placeholders)
Example #2
0
 def __call__(self, x, state, context=None):
     # construct the usual graph without unrolling
     state = NS.Copy(state)
     state.cells = Wayback.transition(state.time,
                                      state.cells,
                                      self.cells,
                                      below=x,
                                      above=context,
                                      hp=self.hp,
                                      symbolic=True)
     state.time += 1
     state.time %= self.period
     return state
Example #3
0
def make_transition_graph(state,
                          transition,
                          x=None,
                          context=None,
                          temperature=1.0,
                          hp=None):
    """Make the graph that processes a single sequence element.

  Args:
    state: `_make_sequence_graph` loop state.
    transition: Model transition function mapping (xelt, model_state,
        context) to (output, new_model_state).
    x: Sequence of integer (categorical) inputs. Axes [time, batch].
    context: Optional Tensor denoting context, shaped [batch, ?].
    temperature: Softmax temperature to use for sampling.
    hp: Model hyperparameters.

  Returns:
    Updated loop state.
  """
    state = NS.Copy(state)

    xelt = tfutil.shaped_one_hot(
        state.xhats.read(state.i) if x is None else x[state.i, :],
        [None, hp.data_dim])
    embedding = tfutil.layers([xelt], sizes=hp.io_sizes, use_bn=hp.use_bn)
    h, state.model = transition(embedding, state.model, context=context)

    # predict the next elt
    with tf.variable_scope("xhat") as scope:
        embedding = tfutil.layers([h], sizes=hp.io_sizes, use_bn=hp.use_bn)
        exhat = tfutil.project(embedding, output_dim=hp.data_dim)
        xhat = tfutil.sample(exhat, temperature)
        state.xhats = state.xhats.write(state.i + LEFTOVER, xhat)

    if x is not None:
        target = tfutil.shaped_one_hot(x[state.i + 1], [None, hp.data_dim])
        state.losses = state.losses.write(
            state.i, tf.nn.softmax_cross_entropy_with_logits(exhat, target))
        state.errors = state.errors.write(
            state.i,
            tf.not_equal(tf.nn.top_k(exhat)[1],
                         tf.nn.top_k(target)[1]))
        state.exhats = state.exhats.write(state.i, exhat)

    state.i += 1
    return state
Example #4
0
 def __call__(self, x, state, context=None):
     state = NS.Copy(state)
     for i, _ in enumerate(self.cells):
         cell_inputs = []
         if i == 0:
             cell_inputs.append(x)
         if context is not None and i == len(self.cells) - 1:
             cell_inputs.append(context)
         if self.hp.vskip:
             # feed in state of all other layers
             cell_inputs.extend(self.cells[j].get_output(state.cells[j])
                                for j in range(len(self.cells)) if j != i)
         else:
             # feed in state of layer below
             if i > 0:
                 cell_inputs.append(self.cells[i - 1].get_output(
                     state.cells[i - 1]))
         state.cells[i] = self.cells[i].transition(cell_inputs,
                                                   state.cells[i],
                                                   scope="cell%i" % i)
     return state
Example #5
0
    def _make_sequence_graph_with_unroll(self,
                                         model_state=None,
                                         x=None,
                                         initial_xelt=None,
                                         context=None,
                                         length=None,
                                         temperature=1.0,
                                         hp=None,
                                         back_prop=False):
        """Create a sequence graph by unrolling upper layers.

    This method is similar to `_make_sequence_graph`, except that `length` must be provided. The
    resulting graph behaves in the same way as that constructed by `_make_sequence_graph`, except
    that the upper layers are outside of the while loop and so the gradient can actually be
    truncated between runs of lower layers.

    If `x` is given, the graph processes the sequence `x` one element at a time.  At step `i`, the
    model receives the `i`th element as input, and its output is used to predict the `i + 1`th
    element.

    The last element is not processed, as there would be no further element available to compare
    against and compute loss. To ensure all data is processed during TBPTT, segments `x` fed into
    successive computations of the graph should overlap by 1.

    If `x` is not given, `initial_xelt` must be given as the first input to the model.  Further
    elements are constructed from the model's predictions.

    Args:
      model_state: initial state of the model.
      x: Sequence of integer (categorical) inputs. Not needed if sampling.
          Axes [time, batch].
      initial_xelt: When sampling, x is not given; initial_xelt specifies
          the input x[0] to the first timestep.
      context: a `Tensor` denoting context, e.g. for conditioning.
          Axes [batch, features].
      length: Optional length of sequence. Inferred from `x` if possible.
      temperature: Softmax temperature to use for sampling.
      hp: Model hyperparameters.
      back_prop: Whether the graph will be backpropagated through.

    Raises:
      ValueError: if `length` is not an int.

    Returns:
      Namespace containing relevant symbolic variables.
    """
        if length is None or not isinstance(length, int):
            raise ValueError(
                "For partial unrolling, length must be known at graph construction time."
            )

        if model_state is None:
            model_state = self.state_placeholders()

        state = NS(model=model_state,
                   inner_initial_xelt=initial_xelt,
                   xhats=[],
                   losses=[],
                   errors=[])

        # i suspect ugly gradient biases may occur if gradients are truncated
        # somewhere halfway through the cycle. ensure we start at a cycle boundary.
        state.model.time = tfutil.assertion(state.model.time,
                                            tf.equal(state.model.time, 0),
                                            [state.model.time],
                                            name="outer_alignment_assertion")
        # ensure we end at a cycle boundary too.
        assert (length - LEFTOVER) % self.period == 0

        inner_period = int(np.prod(hp.periods[:self.outer_indices[0] + 1]))

        # hp.boundaries specifies truncation boundaries relative to the end of the sequence and in terms
        # of each layer's own steps; translate this to be relative to the beginning of the sequence and
        # in terms of sequence elements. note that due to the dynamic unrolling of the inner graph, the
        # inner layers necessarily get truncated at the topmost inner layer's boundary.
        boundaries = [
            length - 1 - hp.boundaries[i] * int(np.prod(hp.periods[:i + 1]))
            for i in range(len(hp.periods))
        ]
        assert all(0 <= boundary and boundary < length - LEFTOVER
                   for boundary in boundaries)
        assert boundaries == list(reversed(sorted(boundaries)))

        print "length %s periods %s boundaries %s %s inner period %s" % (
            length, hp.periods, hp.boundaries, boundaries, inner_period)

        outer_step_count = length // inner_period
        for outer_time in range(outer_step_count):
            if outer_time > 0:
                tf.get_variable_scope().reuse_variables()

            # update outer layers (wrap in seq scope to be consistent with the fully
            # symbolic version of this graph)
            with tf.variable_scope("seq"):
                # truncate gradient (only effective on outer layers)
                for i in range(len(self.cells)):
                    if outer_time * inner_period <= boundaries[i]:
                        state.model.cells[i] = list(
                            map(tf.stop_gradient, state.model.cells[i]))

                state.model.cells = Wayback.transition(
                    outer_time * inner_period,
                    state.model.cells,
                    self.cells,
                    below=None,
                    above=context,
                    subset=self.outer_indices,
                    hp=hp,
                    symbolic=False)

            # run inner layers on subsequence
            if x is None:
                inner_x = None
            else:
                start = inner_period * outer_time
                stop = inner_period * (outer_time + 1) + LEFTOVER
                inner_x = x[start:stop, :]

            # grab a copy of the outer states. they will not be updated in the inner
            # loop, so we can put back the copy after the inner loop completes.
            # this avoids the gradient truncation due to calling `while_loop` with
            # `back_prop=False`.
            outer_cell_states = NS.Copy(state.model.cells[self.outer_slice])

            def _inner_transition(input_, state, context=None):
                assert not context
                state.cells = Wayback.transition(state.time,
                                                 state.cells,
                                                 self.cells,
                                                 below=input_,
                                                 above=None,
                                                 subset=self.inner_indices,
                                                 hp=hp,
                                                 symbolic=True)
                state.time += 1
                state.time %= self.period
                h = self.get_output(state)
                return h, state

            inner_back_prop = back_prop and outer_time * inner_period >= boundaries[
                self.inner_indices[-1]]
            inner_ts = _make_sequence_graph(
                transition=_inner_transition,
                model_state=state.model,
                x=inner_x,
                initial_xelt=state.inner_initial_xelt,
                temperature=temperature,
                hp=hp,
                back_prop=inner_back_prop)

            state.model = inner_ts.final_state.model
            state.inner_initial_xelt = inner_ts.final_xelt if x is not None else inner_ts.final_xhatelt
            state.final_xhatelt = inner_ts.final_xhatelt
            if x is not None:
                state.final_x = inner_x
                state.final_xelt = inner_ts.final_xelt
                # track only losses and errors after the boundary to avoid bypassing the truncation boundary.
                if inner_back_prop:
                    state.losses.append(inner_ts.loss)
                    state.errors.append(inner_ts.error)
            state.xhats.append(inner_ts.xhat)

            # restore static outer states
            state.model.cells[self.outer_slice] = outer_cell_states

            # double check alignment to be safe
            state.model.time = tfutil.assertion(
                state.model.time,
                tf.equal(state.model.time % inner_period, 0),
                [state.model.time, tf.shape(inner_x)],
                name="inner_alignment_assertion")

        ts = NS()
        ts.xhat = tf.concat(0, state.xhats)
        ts.final_xhatelt = state.final_xhatelt
        ts.final_state = state
        if x is not None:
            ts.final_x = state.final_x
            ts.final_xelt = state.final_xelt
            # inner means are all on the same sample size, so taking their mean is valid
            ts.loss = tf.reduce_mean(state.losses)
            ts.error = tf.reduce_mean(state.errors)
        return ts
Example #6
0
 def testCopy(self):
     before = NS(v=2, w=NS(x=1, y=NS(z=0)))
     after = NS.Copy(before)
     self.assertEqual(before, after)
     self.assertTrue(
         all(a is b for a, b in zip(NS.Flatten(after), NS.Flatten(before))))
Example #7
0
def make_transition_graph(state,
                          transition,
                          x=None,
                          context=None,
                          temperature=1.0,
                          hp=None):
    """Make the graph that processes a single sequence element.

  Args:
    state: `_make_sequence_graph` loop state.
    transition: Model transition function mapping (xchunk, model_state,
        context) to (output, new_model_state).
    x: Sequence of integer (categorical) inputs. Axes [time, batch].
    context: Optional Tensor denoting context, shaped [batch, ?].
    temperature: Softmax temperature to use for sampling.
    hp: Model hyperparameters.

  Returns:
    Updated loop state.
  """
    state = NS.Copy(state)

    xchunk = _get_flat_chunk(state.xhats if x is None else x,
                             state.i * hp.chunk_size,
                             hp.chunk_size,
                             depth=hp.data_dim)
    embedding = tfutil.layers([xchunk], sizes=hp.io_sizes, use_bn=hp.use_bn)
    h, state.model = transition(embedding, state.model, context=context)

    # predict the next chunk
    exhats = []
    with tf.variable_scope("xhat") as scope:
        for j in range(hp.chunk_size):
            if j > 0:
                scope.reuse_variables()

            xchunk = _get_flat_chunk(state.xhats if x is None else x,
                                     state.i * hp.chunk_size + j,
                                     hp.chunk_size,
                                     depth=hp.data_dim)
            embedding = tfutil.layers([h, xchunk],
                                      sizes=hp.io_sizes,
                                      use_bn=hp.use_bn)
            exhat = tfutil.project(embedding, output_dim=hp.data_dim)
            exhats.append(exhat)

            state.xhats = state.xhats.write((state.i + 1) * hp.chunk_size + j,
                                            tfutil.sample(exhat, temperature))

    if x is not None:
        targets = tf.unpack(_get_1hot_chunk(x, (state.i + 1) * hp.chunk_size,
                                            hp.chunk_size,
                                            depth=hp.data_dim),
                            num=hp.chunk_size,
                            axis=1)
        state.losses = _put_chunk(state.losses, state.i * hp.chunk_size, [
            tf.nn.softmax_cross_entropy_with_logits(exhat, target)
            for exhat, target in util.equizip(exhats, targets)
        ])
        state.errors = _put_chunk(state.errors, state.i * hp.chunk_size, [
            tf.not_equal(tf.nn.top_k(exhat)[1],
                         tf.nn.top_k(target)[1])
            for exhat, target in util.equizip(exhats, targets)
        ])
        state.exhats = _put_chunk(state.exhats, state.i * hp.chunk_size,
                                  exhats)

    state.i += 1
    return state