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

[bug report] GTC seems incorrect. #49

Open
ncble opened this issue Jun 3, 2019 · 1 comment
Open

[bug report] GTC seems incorrect. #49

ncble opened this issue Jun 3, 2019 · 1 comment

Comments

@ncble
Copy link
Collaborator

ncble commented Jun 3, 2019

Describe the bug
In current master branch, we calculate the GTC by calling srl_zoo/plotting/representation_plot.py. In particular, in the function plotCorrelation, we call numpy to compute the correlation.

corr = np.corrcoef(x=x + eps, y=states_rewards["states"] + eps, rowvar=0)

However in the numpy.corrcoef doc, they spefify that numpy.corrcoef(x, y=None, rowvar=True, bias=<no value>, ddof=<no value>) y should have the same shape as x. In fact, if we replace this line of code by

corr = np.corrcoef(x=x + eps, y=np.random.rand(x.shape[0], 200) + eps, rowvar=0) ## where 200 is the state dim

and execute N times, the results will be almost the same (absolute difference is less than 1e-5).

Code example
Replace the predcited state by a random matrix as described above and execute the following command line under the folde srl_zoo/ :

$ python -m plotting.representation_plot -i "./logs/mobile2D_random_tar_seed_0/19-05-28_18h28_22_custom_cnn_ST_DIM200_autoencoder/states_rewards.npz" --correlation --data-folder "mobile2D_random_tar_seed_0" --print-corr

p.s. replace the folder name 19-05-28_18h28_22_custom_cnn_ST_DIM200_autoencoder according to yours, of course.

@ncble
Copy link
Collaborator Author

ncble commented Jun 4, 2019

I have just written a function that computes the GTC (ground truth correlation) for any dimension of states. It also takes care the case where the ground truth vectors are identical (e.g. fixed target position) and return 0 in this case. The results are coherent with the np.corrcoef and are different from the current origin/master code. This means that the previous calculation of GTC might be wrong. I hope that I didn't misunderstand the GTC...

def compute_GTC(state_pred, ground_truth, epsilon=1e-8):
    """
    :param state_pred (np.array) shape (N, state_dim)
    :param ground_truth (np.array) shape (N, dim), usually dim = 2
    return GTC (np.array): max of correlation coefficients, shape (dim, )
    """
    assert len(state_pred.shape) == len(ground_truth.shape) == 2, "Input should be 2D array"
    std_sp = np.std(state_pred, axis=0) # shape (state_dim, )
    std_gt = np.std(ground_truth, axis=0) # shape (dim, )
    mean_sp = np.mean(state_pred, axis=0)
    mean_gt = np.mean(ground_truth, axis=0)
    
    # scalar product
    A = (state_pred-mean_sp)[..., None] * (ground_truth-mean_gt)[:, None, :] # shape (N, state_dim, dim)
    corr = np.mean(A, axis=0) # shape (state_dim, dim)
    std = std_sp[:, None] * std_gt[None, :]
    corr = corr / (std+epsilon)
    gtc = np.max(corr, axis=0)
    for ind, std in enumerate(std_gt):
        if std < epsilon:
            gtc[ind] = 0
    return gtc

@ncble ncble changed the title [bug report (unsolved)] GTC seems incorrect. [bug report] GTC seems incorrect. Jun 14, 2019
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

Successfully merging a pull request may close this issue.

1 participant