Skip to content

Instantly share code, notes, and snippets.

View rmsander's full-sized avatar
🙂
Life is good.

Ryan Sander rmsander

🙂
Life is good.
View GitHub Profile
@rmsander
rmsander / covariance_stability_metrics.py
Created September 6, 2021 19:11
Function to compute stability metrics for covariance matrices, namely log determinants and condition numbers.
"""Function to compute stability metrics for covariance matrices,
namely log determinants and condition numbers."""
# Use torch
import torch
def compute_covariance_metrics(Kxx, x_train, writer):
"""Helper function to compute covariance metrics.
Parameters:
Kxx (torch.Tensor): Tensor object corresponding to a covariance matrix.
@rmsander
rmsander / composite_prod_norm_sklearn.py
Created September 6, 2021 19:02
Function for implementing custom distance metrics for use in sklearn neighbor search algorithms.
"""Function for implementing custom distance metrics for use in sklearn neighbor
search algorithms.
Here, we will use a composite product norm composed of a product of norms over
given (contiguous) subsets of states. The norms over these subsets are taken
to be L2 norms.
If x = [s a]^T, where s and a are the contiguous subsets of x that this
product norm is decomposed into, then this product norm can be written as:
@rmsander
rmsander / set_mujoco_env_state.py
Created September 6, 2021 13:51
Allows for dynamically changing the state of MuJoCo environments.
"""Module for setting a Gym MuJoCo environment's state to a stored state using
qpos and qvel inputs, which are used for state setting in the MuJoCo simulator.
An example is carried out with the HalfCheetah environment."""
from gym.envs.mujoco.half_cheetah_v3 import HalfCheetahEnv
import numpy as np
def main():
# Initialize and reset the environment to extract initial state
"""Adapted from the Keras VAE guide: https://keras.io/examples/generative/vae/."""
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
def make_encoder():
"""Function for making the encoder."""
latent_dim = 10
import numpy as np
import tensorflow as tf
def vector_same_class_sample_from_file(Y, y_sample, base_paths_by_class=None, X=None, num_classes=2):
"""Function for generating equal-class Mixup pairs in a vectorized fashion.
Essentially, what we'll do is separate X into different sets according to the
class of the corresponding index in Y. Then, given y_sample, a set of classes
of the first set of images/vectors that we use for Mixup, we pair the same classes
together appropriately by only sampling corresponding same-class images/vectors
from X using appropriate indexing.
@rmsander
rmsander / uniform_same_class_selection.py
Last active September 6, 2021 14:06
Module for a TF/Keras function for selecting samples of the same class to interpolate (e.g. via Mixup interpolation).
"""Module for a TF/Keras function for selecting samples of the same class to interpolate (e.g. via
Mixup interpolation)."""
import numpy as np
import tensorflow as tf
def vector_same_class_sample(X, Y, y_sample, num_classes=2):
"""Function for generating equal-class Mixup pairs in a vectorized fashion.
Essentially, what we'll do is separate X into different sets according to the
class of the corresponding index in Y. Then, given y_sample, a set of classes
@rmsander
rmsander / mixup_tensorflow.py
Last active August 1, 2021 01:11
MIxup TensorFlow
import tensorflow as tf
import numpy as np
def mixup_tensorflow(X1, X2, alpha=1.0):
"""Function for implementing Mixup using TensorFlow.
Mixup interpolation occurs between corresponding indices
of X1[i] and X2[i].
Parameters:

If you're using mujoco-py (likely through the MuJoCo suite in gym) on MIT Supercloud, you may encounter issues with POSIX locking if you try to install and setup mujoco-py on the standard shared file system.

To get around this, you can install mujoco-py on a different section (the permanent storage section) of the Supercloud server. Below is a bash script I used to get around issue (you may have to modify some of the commands, such as the conda environment you use):

#!/bin/bash

#SBATCH -c 10
#SBATCH -n 1
#SBATCH --exclusive
@rmsander
rmsander / mixup_general.py
Last active July 31, 2021 17:21
Implements Mixup for generalized vector/matrix/tensor combinations using TensorFlow.
import tensorflow as tf
import numpy as np
def mixup_tensorflow(X1, X2, alpha=1.0):
"""Function for implementing Mixup using TensorFlow.
Mixup interpolation occurs between corresponding indices
of X1[i] and X2[i].
Parameters:
import gpytorch
import numpy as np
import matplotlib.pyplot as plt
import torch
# Create features
X = torch.tensor(np.arange(100)).float()
# Create targets as noisy function of features
Y = torch.tensor(np.add(np.sin(X / 10), np.random.normal(loc=0, scale=0.5, size=100))).float()