コード例 #1
0
ファイル: cae.py プロジェクト: poolio/cae.py
    def f_df(self, theta, x):
        """
        Compute objective and gradient of the CAE objective using the
        examples {\bf x}.
        
        Parameters
        ----------
        x: array-like, shape (n_examples, n_inputs)
        
        Parameters
        ----------
        loss: array-like, shape (n_examples,)
            Value of the loss function for each example before the step.
        """
        self.set_params(theta)
        h = self.encode(x)
        r = self.decode(h)
        def _contraction_jacobian():
            """
            Compute the gradient of the contraction cost w.r.t parameters.
            """
            a = 2*(h * (1 - h))**2 
            d = ((1 - 2 * h) * a * (self.W**2).sum(0)[None, :])
            b = np.dot(x.T / x.shape[0], d)
            c = a.mean(0) * self.W
            return (b + c), d.mean(0)
        
        def _reconstruction_jacobian():
            """                                                                 
            Compute the gradient of the reconstruction cost w.r.t parameters.      
            """
            dr = (r - x) / x.shape[0]
            dr *= r * (1-r)
            dd = np.dot(dr.T, h)
            dh = np.dot(dr, self.W) * h * (1. - h)
            de = np.dot(x.T, dh)
            return (dd + de), dr.sum(0), dh.sum(0)

        W_rec, c_rec, b_rec = _reconstruction_jacobian()
        W_con, b_con = _contraction_jacobian()
        dW = W_rec + self.jacobi_penalty * W_con
        dhidbias = b_rec + self.jacobi_penalty * b_con
        dvisbias = c_rec 
        return self.loss(x, h, r), vector_from_args([dW, dhidbias, dvisbias]);
コード例 #2
0
ファイル: model_gradient.py プロジェクト: poolio/cae.py
 def get_params(self):
     return vector_from_args(self.params)
コード例 #3
0
ファイル: cae.py プロジェクト: poolio/cae.py
 def get_params(self):
     return vector_from_args([self.W, self.hidbias, self.visbias])