Skip to content

Instantly share code, notes, and snippets.

@bhpfelix
bhpfelix / backward_error_example.py
Created April 4, 2020 23:25
RuntimeError: Trying to backward through the graph a second time
import torch
import numpy as np
# This will be share by both iterations and will make the second backward fail !
a = torch.ones(3, requires_grad=True) * 4
# Instead, do
# a = torch.tensor(np.ones(3) * 4, requires_grad=True)
for i in range(10):
@bhpfelix
bhpfelix / setup.md
Created November 28, 2019 07:28
Setup Steps for Habitat

conda create -n habitat_headless python=3.6 cmake=3.14.0

conda activate habitat_headless

cd ~/github/habitat-sim/

pip install -r requirements.txt

conda activate habitat_headless

@bhpfelix
bhpfelix / rotation.py
Created October 15, 2019 04:42
Rotation preserves probability
import numpy as np
from numpy.linalg import norm
from scipy.special import erf
def get_rotation_matrix(x):
"""
Get rotation matrix for the space that aligns vector x with y = [1, 0, 0, 0, 0, ..., 0]
See: https://math.stackexchange.com/questions/598750/finding-the-rotation-matrix-in-n-dimensions
"""
@bhpfelix
bhpfelix / convert.py
Last active October 27, 2020 04:10
Code snippet for porting TensorFlow trained model to PyTorch
import numpy as np
from PIL import Image
np.random.seed(2)
import torchvision
import torch
# torch.manual_seed(0)
import torch.nn as nn
import torch.nn.functional as F
from torchvision import transforms
import tensorflow as tf
@bhpfelix
bhpfelix / tf_vs_pt.py
Created June 25, 2019 10:52
Comparing TensorFlow and PyTorch Operation (AvgPool, Conv2d)
import numpy as np
np.random.seed(0)
import torch
import torch.nn as nn
import tensorflow as tf
import matplotlib.pyplot as plt
slim = tf.contrib.slim
@bhpfelix
bhpfelix / visualization.py
Created May 24, 2019 09:06
Recreating Graph Visualization in Randomly Wired Neural Network
import networkx as nx
def to_directed(graph):
G = nx.DiGraph(source=[], sink=[])
for node in range(graph.number_of_nodes()):
neighbors = list(graph.neighbors(node))
neighbors.sort()
if node < neighbors[0]: # input nodes
G.graph['source'].append(node)