예제 #1
0
 def backward(self, grad):
     gk = np.zeros(self._kshape)
     c_gradk(self._x, gk, grad, *self._args)
     xshp = nxshape(grad, self._xshape)
     gx = np.zeros(xshp)
     c_gradx(gx, self._k, grad, *self._args)
     return gx, gk
예제 #2
0
    def backward(self, grad):
        gread = np.zeros(nxshape(grad, self._read_size))
        gmem = np.zeros(nxshape(grad, self._mem_shape))
        gh = np.zeros(nxshape(grad, self._h_shape))
        gr = np.zeros(nxshape(grad, self._w_shape))
        gc = np.zeros(gh.shape)
        gw = np.zeros(gr.shape)

        gradx = list()
        for t in range(grad.shape[1] - 1, -1, -1):
            grad_t = grad[:, t, :]
            gc, gh, gx_t, gr, gw, gread, gmem = \
                self._step.backward(
                    gc, gh, gr, gw, gread, gmem, grad_t)
            gradx = [gx_t] + gradx

        return np.stack(gradx, 1)
예제 #3
0
    def forward(self, x):
        self._step.flush()
        memory = np.array([self._mem] * x.shape[0])
        h = np.zeros(nxshape(x, self._h_shape))
        w_read = np.zeros(nxshape(x, self._w_shape))
        w_write = np.zeros(nxshape(x, self._w_shape))
        w_read[:, 0] = np.ones(w_read.shape[0])
        w_write[:, 0] = np.ones(w_write.shape[0])
        mem_read = memory[:, 0, :]
        c = np.zeros(h.shape)

        # Loop through time
        result = list()
        for t in range(x.shape[1]):
            x_t = x[:, t, :]
            c, h_new, w_read, w_write, \
            mem_read, memory, readout = \
            self._step.forward(
                c, h, x[:, t, :], w_read,
                w_write, mem_read, memory)
            result.append(readout)

        result = np.stack(result, 1)
        return result
예제 #4
0
파일: lstm.py 프로젝트: thtrieu/essence
    def forward(self, x, lens=None):
        # create mask for lens
        self._step.flush()
        full_len = x.shape[1]
        if lens is not None:
            lens = np.array(lens) - 1.0
        elif lens is None:
            lens = np.ones(full_len)
            lens = lens * full_len
        lens = lens[:, None]

        mask = (lens >= range(full_len))
        mask = mask.astype(np.float64)
        self._mask = mask[:, :, None]
        out_shape = nxshape(x, self._step._out_shape)
        # Loop through time steps
        h = np.zeros(out_shape)
        c = np.zeros(out_shape)
        result = list()  # return this
        for t in range(full_len):
            c, h = self._step.forward(c, h, x[:, t, :])
            result.append(h)
        return np.stack(result, 1) * self._mask
예제 #5
0
 def forward(self, x):
     pooled = np.zeros(nxshape(x, self._out_shape))
     self._mark = np.zeros(nxshape(x, self._out_shape))
     self._mark = self._mark.astype(np.int32)
     c_xpool2(x, self._mark, pooled, *self._inp_shape)
     return pooled
예제 #6
0
 def forward(self, x, k):
     self._x = x; self._k = k
     shp = nxshape(x, self._out_shape)
     conved = np.zeros(shp)
     c_conv2d(x, k, conved, *self._args)
     return conved
예제 #7
0
파일: shaping.py 프로젝트: thtrieu/essence
 def backward(self, grad):
     real_shape = nxshape(grad, self._inp_shape)
     base_grad = np.zeros(real_shape)
     base_grad[self._indices] = grad
     return base_grad
예제 #8
0
파일: shaping.py 프로젝트: thtrieu/essence
 def forward(self, x):
     new_shape = self._out_shape
     if not self._over_batch:
         new_shape = nxshape(x, new_shape)
     self._x_shape = x.shape
     return x.reshape(new_shape)
예제 #9
0
파일: shaping.py 프로젝트: thtrieu/essence
 def backward(self, grad):
     g = np.zeros(nxshape(grad, self._inp_shape))
     g[self._item] = grad
     return g