Exemplo n.º 1
0
    def bottle_neck(self, hidden_state, lstm_cells, x_data, train):
        x = chainer.Variable(x_data, volatile=not train)

        a1 = self.l1_a(F.concat((x, hidden_state["h1"])))
        x1 = self.l1_x(F.concat((x, hidden_state["h1"], lstm_cells["c1"])))
        c1, h1 = peephole_lstm(lstm_cells["c1"], a1, x1)
        h1 = gradient_clip(h1, 10.0)

        a2 = self.l2_a(F.concat((x, hidden_state["h2"], h1)))
        x2 = self.l2_x(F.concat((x, hidden_state["h2"], h1, lstm_cells["c2"])))
        c2, h2 = peephole_lstm(lstm_cells["c2"], a2, x2)
        h2 = gradient_clip(h2, 10.0)

        a3 = self.l2_a(F.concat((x, hidden_state["h3"], h2)))
        x3 = self.l2_x(F.concat((x, hidden_state["h3"], h2, lstm_cells["c3"])))
        c3, h3 = peephole_lstm(lstm_cells["c3"], a3, x3)
        h3 = gradient_clip(h3, 10.0)

        y = self.l4(F.concat((h1, h2, h3)))
        y = gradient_clip(y, 100.0)

        n = int((y.data.shape[1] - 1) / 6)
        gi, b = gauss_bernoulli_params(n, y)

        hidden_state = {"h1": h1, "h2": h2, "h3": h3}
        if train:
            lstm_cells = {"c1": c1, "c2": c2, "c3": c3}

        return gi, b, hidden_state, lstm_cells
Exemplo n.º 2
0
    def check_backward(self, f):
        x = chainer.Variable(f(self.x))

        y = gradient_clip(x, self.a, self.b)
        y.creator.forward((x.data,))
        y.grad = f(self.y_grad)
        y.backward()
        assert_allclose(y.data, x.data)
        assert_allclose(x.grad, f(self.x_grad_ab))

        y = gradient_clip(x, self.a)
        y.creator.forward((x.data,))
        y.grad = f(self.y_grad)
        y.backward()
        assert_allclose(y.data, x.data)
        assert_allclose(x.grad, f(self.x_grad_a))
    def bottle_neck(self, hidden_state, lstm_cells, x_data, train):
        x = chainer.Variable(x_data, volatile=not train)

        h1_in = self.l1_first(x) + self.l1_recur(hidden_state['h1'])
        c1, h1 = F.lstm(lstm_cells['c1'], h1_in)
        h1 = gradient_clip(h1, 10.0)
        h2_in = self.l2_first(x) + self.l2_recur(hidden_state['h2']) + self.l2_input(h1)
        c2, h2 = F.lstm(lstm_cells['c2'], h2_in)
        h2 = gradient_clip(h2, 10.0)
        h3_in = self.l3_first(x) + self.l3_recur(hidden_state['h3']) + self.l3_input(h2)
        c3, h3 = F.lstm(lstm_cells['c3'], h3_in)
        h3 = gradient_clip(h3, 10.0)

        y = self.l4(F.concat((h1, h2, h3)))
        y = gradient_clip(y, 100.0)

        n = int((y.data.shape[1] - 1) / 6)
        gi, b = gauss_bernoulli_params(n, y)

        hidden_state = {'h1': h1, 'h2': h2, 'h3': h3}
        if train:
            lstm_cells = {'c1': c1, 'c2': c2, 'c3': c3}

        return gi, b, hidden_state, lstm_cells