Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update get_cost in z_encoder #61

Open
hndgzkn opened this issue Jan 10, 2022 · 0 comments
Open

Update get_cost in z_encoder #61

hndgzkn opened this issue Jan 10, 2022 · 0 comments

Comments

@hndgzkn
Copy link
Collaborator

hndgzkn commented Jan 10, 2022

Update get_cost function in _z_encoder to call compute_objective in _z_encoder.

As ztz and ztX are not initialized prior to calling compute_z, this requires some changes. Two possible solutions:

  • get_cost function can return the value of cost function when z_hat=0 (i.e. 0.5 * np.linalg.norm(self.X[0]) ** 2) if ztz and ztX are not set.
    However test_get_cost fails with the following message (final cost after running compute_z is greater than the initial cost):
solver = 'l-bfgs'
X = array([[[ 0.49671415, -0.1382643 ,  0.64768854,  1.52302986,
         -0.23415337, -0.23413696,  1.57921282,  0.767434...     1.06667469,  1.16929559,  1.38215899,  0.64870989,
         -0.16711808,  0.14671369,  1.20650897, -0.81693567]]])
D_hat = array([[ 0.85771824,  0.1945646 ,  0.47588238,  0.69484882,  0.57908626,
        -0.30303112,  0.2946003 , -0.04693235...1623,  0.71909456,  0.68934122,  0.06912736,  0.16871127,
        -0.39607166, -0.88370123, -0.15521554,  0.06975264]])
requires_dicodile = None

    @pytest.mark.parametrize('solver, n_trials, rank1',
                             [('l-bfgs', 3, True),
                              ('dicodile', 1, False)])
    def test_get_cost(solver, X, D_hat, requires_dicodile):
        """Test for valid values."""
    
        with get_z_encoder_for(solver=solver,
                               X=X,
                               D_hat=D_hat,
                               n_atoms=N_ATOMS,
                               n_times_atom=N_TIMES_ATOM,
                               n_jobs=2) as z_encoder:
            initial_cost = z_encoder.get_cost()
    
            z_encoder.compute_z()
            z_hat = z_encoder.get_z_hat()
            final_cost = z_encoder.get_cost()
    
>           assert final_cost < initial_cost
E           assert 393.7852276977734 < 144.81588197885912

D_hat      = array([[ 0.85771824,  0.1945646 ,  0.47588238,  0.69484882,  0.57908626,
        -0.30303112,  0.2946003 , -0.04693235...1623,  0.71909456,  0.68934122,  0.06912736,  0.16871127,
        -0.39607166, -0.88370123, -0.15521554,  0.06975264]])
X          = array([[[ 0.49671415, -0.1382643 ,  0.64768854,  1.52302986,
         -0.23415337, -0.23413696,  1.57921282,  0.767434...     1.06667469,  1.16929559,  1.38215899,  0.64870989,
         -0.16711808,  0.14671369,  1.20650897, -0.81693567]]])
final_cost = 393.7852276977734
initial_cost = 144.81588197885912
requires_dicodile = None
solver     = 'l-bfgs'
z_encoder  = <alphacsc._z_encoder.AlphaCSCEncoder object at 0x7fef89941eb0>
z_hat      = array([[[0.03148913, 0.        , 0.11964971, ..., 0.        ,
         0.02470083, 0.        ],
        [0.        , 0...0967279, 0.        ],
        [0.12684568, 0.12219768, 0.02416528, ..., 0.        ,
         0.        , 0.        ]]])

  • ztz and ztX should be set to 0 upon initialization as
        self.ztz = np.zeros((self.n_atoms, self.n_atoms,
                             2 * self.n_times_atom - 1))
        self.ztX = np.zeros((self.n_atoms, self.n_channels,
                             self. n_times_atom))

However test_get_cost fails with the following message (the cost computed in the test is not close to final_cost computed after compute_z is called. ):

solver = 'l-bfgs'
X = array([[[ 0.49671415, -0.1382643 ,  0.64768854,  1.52302986,
         -0.23415337, -0.23413696,  1.57921282,  0.767434...     1.06667469,  1.16929559,  1.38215899,  0.64870989,
         -0.16711808,  0.14671369,  1.20650897, -0.81693567]]])
D_hat = array([[ 0.85771824,  0.1945646 ,  0.47588238,  0.69484882,  0.57908626,
        -0.30303112,  0.2946003 , -0.04693235...1623,  0.71909456,  0.68934122,  0.06912736,  0.16871127,
        -0.39607166, -0.88370123, -0.15521554,  0.06975264]])
requires_dicodile = None

    @pytest.mark.parametrize('solver, n_trials, rank1',
                             [('l-bfgs', 3, True),
                              ('dicodile', 1, False)])
    def test_get_cost(solver, X, D_hat, requires_dicodile):
        """Test for valid values."""
    
        with get_z_encoder_for(solver=solver,
                               X=X,
                               D_hat=D_hat,
                               n_atoms=N_ATOMS,
                               n_times_atom=N_TIMES_ATOM,
                               n_jobs=2) as z_encoder:
            initial_cost = z_encoder.get_cost()
    
            z_encoder.compute_z()
            z_hat = z_encoder.get_z_hat()
            final_cost = z_encoder.get_cost()
    
            assert final_cost < initial_cost
    
            X_hat = construct_X_multi(z_hat, D_hat, n_channels=N_CHANNELS)
            cost = compute_objective(X=X, X_hat=X_hat, z_hat=z_hat, reg=0.1,
                                     D=D_hat)
    
>           assert np.isclose(cost, final_cost)
E           assert False
E            +  where False = <function isclose at 0x7f9aa010ac10>(396.9063356164568, 393.7852276977734)
E            +    where <function isclose at 0x7f9aa010ac10> = np.isclose

D_hat      = array([[ 0.85771824,  0.1945646 ,  0.47588238,  0.69484882,  0.57908626,
        -0.30303112,  0.2946003 , -0.04693235...1623,  0.71909456,  0.68934122,  0.06912736,  0.16871127,
        -0.39607166, -0.88370123, -0.15521554,  0.06975264]])
X          = array([[[ 0.49671415, -0.1382643 ,  0.64768854,  1.52302986,
         -0.23415337, -0.23413696,  1.57921282,  0.767434...     1.06667469,  1.16929559,  1.38215899,  0.64870989,
         -0.16711808,  0.14671369,  1.20650897, -0.81693567]]])
X_hat      = array([[[ 1.87670366e-02,  1.58087090e-02,  7.03257398e-02,
          1.36799839e-01,  3.19383914e-02,  5.76223312e-02...09410227e-01,  8.94328662e-02,
          6.66489290e-02,  4.55550879e-02,  1.00303005e-01,
         -1.48259646e-02]]])
cost       = 396.9063356164568
final_cost = 393.7852276977734
initial_cost = 432.6504303161992
requires_dicodile = None
solver     = 'l-bfgs'
z_encoder  = <alphacsc._z_encoder.AlphaCSCEncoder object at 0x7f9a7bbf7e50>
z_hat      = array([[[0.03148913, 0.        , 0.11964971, ..., 0.        ,
         0.02470083, 0.        ],
        [0.        , 0...0967279, 0.        ],
        [0.12684568, 0.12219768, 0.02416528, ..., 0.        ,
         0.        , 0.        ]]])

alphacsc/tests/test_z_encoder.py:274: AssertionError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant