Skip to content

Instantly share code, notes, and snippets.

View fsodogandji's full-sized avatar

Fabrice Sodogandji fsodogandji

View GitHub Profile
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

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.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mrdrozdov
mrdrozdov / example.py
Last active December 28, 2018 22:10
Logging in Tensorflow
from tf_logger import TFLogger
""" Example of using TFLogger to save train & dev statistics. To visualize
in tensorboard simply do:
tensorboard --logdir /path/to/summaries
This code does depend on Tensorflow, but does not require that your model
is built using Tensorflow. For instance, could build a model in Chainer, then
@nlm
nlm / async_loop.py
Created October 18, 2016 08:18
python async loop demo to understand asyncio basics
from functools import wraps
from types import FunctionType, GeneratorType
import logging
import time
def coroutine(f):
@wraps(f)
def wrapper(*args, **kwargs):
logging.debug('coroutine starting: {}'.format(f.__name__))
return f(*args, **kwargs)
@stared
stared / README.md
Last active January 18, 2017 13:28
Sequential model in Keras -> ASCII
@npyoung
npyoung / pymc3_statespace_model.ipynb
Last active July 30, 2022 05:41
A state space model distribution for pymc3
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#!/usr/local/bin/python3
import glob
import h5py
import matplotlib.animation as animation
import matplotlib.pyplot as plot
import numpy as np
import tensorflow as tf
@fsodogandji
fsodogandji / elastic_transform.py
Created August 11, 2016 10:54 — forked from chsasank/elastic_transform.py
Elastic transformation of an image in Python
import numpy as np
from scipy.ndimage.interpolation import map_coordinates
from scipy.ndimage.filters import gaussian_filter
def elastic_transform(image, alpha, sigma, random_state=None):
"""Elastic deformation of images as described in [Simard2003]_.
.. [Simard2003] Simard, Steinkraus and Platt, "Best Practices for
Convolutional Neural Networks applied to Visual Document Analysis", in
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.