Skip to content

Instantly share code, notes, and snippets.

A Tour of PyTorch Internals (Part I)

The fundamental unit in PyTorch is the Tensor. This post will serve as an overview for how we implement Tensors in PyTorch, such that the user can interact with it from the Python shell. In particular, we want to answer four main questions:

  1. How does PyTorch extend the Python interpreter to define a Tensor type that can be manipulated from Python code?
  2. How does PyTorch wrap the C libraries that actually define the Tensor's properties and methods?
  3. How does PyTorch cwrap work to generate code for Tensor methods?
  4. How does PyTorch's build system take all of these components to compile and generate a workable application?

Extending the Python Interpreter

PyTorch defines a new package torch. In this post we will consider the ._C module. This module is known as an "extension module" - a Python module written in C. Such modules allow us to define new built-in object types (e.g. the Tensor) and to call C/C++ functions.

@kingjr
kingjr / plotting_style.py
Last active August 27, 2016 11:20
plotting_style
import numpy as np
import matplotlib.pyplot as plt
from jr.plot import pretty_plot, plot_eb # available @ http://github.com/kingjr/jr-tools
# make up data
x = np.linspace(1., 5., 100)
all_data = dict(AG=np.sin(x), V1=np.cos(x), IPS=np.cos(x + 1.5))
# choose color manually
colors = ['r', [.1, 1., .2, 1.], 'b']
@SNagappan
SNagappan / README.md
Last active January 8, 2021 15:43
bAbI

##Model

This is an implementation of Facebook's baseline GRU/LSTM model on the bAbI dataset Weston et al. 2015. It includes an interactive demo.

The bAbI dataset contains 20 different question answering tasks.

Model script

The model training script train.py and demo script demo.py are included below.

Instructions

@wangruohui
wangruohui / Caffe Ubuntu 15.10.md
Last active February 28, 2023 09:36
Compile and run Caffe on Ubuntu 15.10

Ubuntu 15.10 have been released for a couple of days. It is a bleeding-edge system coming with Linux kernel 4.2 and GCC 5. However, compiling and running Caffe on this new system is no longer as smooth as on earlier versions. I have done some research related to this issue and finally find a way out. I summarize it here in this short tutorial and I hope more people and enjoy this new system without breaking their works.

Install NVIDIA Driver

The latest NVIDIA driver is officially included in Ubuntu 15.10 repositories. One can install it directly via apt-get.

sudo apt-get install nvidia-352-updates nvidia-modprobe

The nvidia-modprobe utility is used to load NVIDIA kernel modules and create NVIDIA character device files automatically everytime your machine boots up.

Reboot your machine and verify everything works by issuing nvidia-smi or running deviceQuery in CUDA samples.

@vsoch
vsoch / joblib_vs_pickle.py
Created April 24, 2015 03:44
Joblib vs Pickle
from sklearn.externals import joblib
import time
import numpy
import pickle
bigarray = numpy.zeros([190,91,190])
bigarray = bigarray.flatten()
### Saving
@imjasonh
imjasonh / markdown.css
Last active February 12, 2024 17:18
Render Markdown as unrendered Markdown (see http://jsbin.com/huwosomawo)
* {
font-size: 12pt;
font-family: monospace;
font-weight: normal;
font-style: normal;
text-decoration: none;
color: black;
cursor: default;
}
@yassersouri
yassersouri / How to visualize the ILSVRC mean image.md
Last active September 6, 2016 20:57
How to visualize the ILSVRC mean image

I had an issue with how to visualize the ILSVRC mean image. I just wanted to look at it and see how much does it differ from using pixel-wise mean subtraction instead of image-wise mean subtraction.

I assume that you have already downloaded the CaffeNet pretrained and model definition files.

The trick is to initialize two networks, one with mean file set (called net_mean) and the other one without mean file (called net). Then create a fake all 1 image. Use the net_mean to preprocess the fake image for data layer and save the result as fake_pre. Then use the net to deprocess fake_pre for data layer and save it as fake_re. If the two networks net and net_mean were the same then fake_re would be equal to fake, but since we have not set any mean file for net then we can visualize the mean image using 1 - fake_re. Take a look at the code.

The result looks like this:

![ILSVRC mean image](https://gist.github.com/yassersouri/f617bf7eff9172290b4f/raw/863971c47470204234017b91196b5e94a6fe

@staltz
staltz / introrx.md
Last active April 20, 2024 14:15
The introduction to Reactive Programming you've been missing
@yassersouri
yassersouri / Assignments Latex Template.md
Last active November 8, 2021 03:46
Assignments Latex template.

##Assignments Latex Template

###V 0.1

I always wanted some latex template that I could use for assignments. But none of the templates I found online had all the features I wanted. So the natural next step for me was to create one.

###Notes:

  • Use with XeLaTeX
@mblondel
mblondel / kernel_kmeans.py
Last active January 4, 2024 11:45
Kernel K-means.
"""Kernel K-means"""
# Author: Mathieu Blondel <mathieu@mblondel.org>
# License: BSD 3 clause
import numpy as np
from sklearn.base import BaseEstimator, ClusterMixin
from sklearn.metrics.pairwise import pairwise_kernels
from sklearn.utils import check_random_state