コード例 #1
0
    def forward(self, inputs, weights):
        del weights
        x1, x2 = inputs

        x1_split = np.split(x1, self._n_sections, self._axis)
        x2_split = np.split(x2, self._n_sections, self._axis)

        res = [np.concatenate(ys, -1) for ys in zip(x1_split, x2_split)]
        return tuple(res)
コード例 #2
0
    def forward(self, inputs, weights):
        x, lstm_state = inputs

        # LSTM state consists of c and h.
        c, h = np.split(lstm_state, 2, axis=-1)

        # Dense layer on the concatenation of x and h.
        w, b = weights
        y = np.dot(np.concatenate([x, h], axis=-1), w) + b

        # i = input_gate, j = new_input, f = forget_gate, o = output_gate
        i, j, f, o = np.split(y, 4, axis=-1)

        new_c = c * backend.sigmoid(f) + backend.sigmoid(i) * np.tanh(j)
        new_h = np.tanh(new_c) * backend.sigmoid(o)
        return new_h, np.concatenate([new_c, new_h], axis=-1)
コード例 #3
0
def dataset_to_stream(dataset, input_name, n_chunks=0):
  """Takes a tf.Dataset and creates a numpy stream of ready batches."""
  for example in backend.dataset_as_numpy(dataset):
    features = example[0]
    inp, out = features[input_name], example[1]
    mask = features['mask'] if 'mask' in features else None
    # All input-pipeline processing should be on CPU.
    with tf.device('cpu:0'):
      # Some accelerators don't handle uint8 well, cast to int.
      if isinstance(inp, np.uint8):
        inp = inp.astype(np.int32)
      if isinstance(out, np.uint8):
        out = out.astype(np.int32)
      if len(out.shape) > 1 and out.shape[-1] == 1:
        out = np.squeeze(out, axis=-1)
      if n_chunks > 0:
        inp = tuple(np.split(inp, n_chunks, axis=1))
        out = tuple(np.split(out, n_chunks, axis=1))
    yield (inp, out) if mask is None else (inp, out, mask)
コード例 #4
0
    def reverse(self, output, weights=(), state=(), new_state=(), **kwargs):
        del weights, kwargs

        x1_split = []
        x2_split = []
        for y in output:
            y1, y2 = np.split(y, 2, -1)
            x1_split.append(y1)
            x2_split.append(y2)

        x1 = np.concatenate(x1_split, self._axis)
        x2 = np.concatenate(x2_split, self._axis)

        return (x1, x2)
コード例 #5
0
    def forward(self, inputs, weights):
        x, gru_state = inputs

        # Dense layer on the concatenation of x and h.
        w1, b1, w2, b2 = weights
        y = np.dot(np.concatenate([x, gru_state], axis=-1), w1) + b1

        # Update and reset gates.
        u, r = np.split(backend.sigmoid(y), 2, axis=-1)

        # Candidate.
        c = np.dot(np.concatenate([x, r * gru_state], axis=-1), w2) + b2

        new_gru_state = u * gru_state + (1 - u) * np.tanh(c)
        return new_gru_state, new_gru_state