This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| In [1]: import numpy as np | |
| ...: import jax.numpy as jnp | |
| ...: from jax import random, jit | |
| In [2]: def slow_f(x): | |
| ...: # Element-wise ops see a large benefit from fusion | |
| ...: return x * x + x * 2.0 | |
| ...: | |
| ...: # use XLA to compile the function | |
| ...: fast_f = jit(slow_f) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Compute covariance matrix | |
| C = np.dot(X.T, X) / (n-1) | |
| # Eigen decomposition | |
| eigen_vals, eigen_vecs = np.linalg.eig(C) | |
| # SVD | |
| U, Sigma, Vh = np.linalg.svd(X, | |
| full_matrices=False, | |
| compute_uv=True) | |
| # Relationship between singular values and eigen values: | |
| print(np.allclose(np.square(Sigma) / (n - 1), eigen_vals)) # True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def svd(X): | |
| # Data matrix X, X doesn't need to be 0-centered | |
| n, m = X.shape | |
| # Compute full SVD | |
| U, Sigma, Vh = np.linalg.svd(X, | |
| full_matrices=False, # It's not necessary to compute the full matrix of U or V | |
| compute_uv=True) | |
| # Transform X with SVD components | |
| X_svd = np.dot(U, np.diag(Sigma)) | |
| return X_svd |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def pca(X): | |
| # Data matrix X, assumes 0-centered | |
| n, m = X.shape | |
| assert np.allclose(X.mean(axis=0), np.zeros(m)) | |
| # Compute covariance matrix | |
| C = np.dot(X.T, X) / (n-1) | |
| # Eigen decomposition | |
| eigen_vals, eigen_vecs = np.linalg.eig(C) | |
| # Project X onto PC space | |
| X_pca = np.dot(X, eigen_vecs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def interpolate_from_a_to_b_for_c(model, X, labels, a=None, b=None, x_c=None, alpha=0.): | |
| '''Perform interpolation between two classes a and b for any sample x_c. | |
| model: a trained generative model | |
| X: data in the original space with shape: (n_samples, n_features) | |
| labels: array of class labels (n_samples, ) | |
| a, b: class labels a and b | |
| x_c: input sample to manipulate (1, n_features) | |
| alpha: scalar for the magnitude and direction of the interpolation | |
| ''' | |
| # Encode samples to the latent space |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| __author__ = "lewis.r.liu@gmail.com" | |
| __copyright__ = "Copyright 2018, github.com/ruxi" | |
| __license__ = "MIT" | |
| __version__ = 0.0.1 | |
| # update: Mar 5 , 2018 | |
| # created: Feb 19, 2018 | |
| # desc: seaborn jointplot with 'hue' | |
| # prepared for issue: https://github.com/mwaskom/seaborn/issues/365 | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Compiled source # | |
| ################### | |
| *.com | |
| *.class | |
| *.dll | |
| *.exe | |
| *.o | |
| *.so | |
| # Packages # |