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.

import torch
import math
import torch.optim
from torch.optim.optimizer import Optimizer, required
class AdamHD(Optimizer):
"""Implements Adam algorithm.
It has been proposed in `Adam: A Method for Stochastic Optimization`_.
Arguments:
params (iterable): iterable of parameters to optimize or dicts defining
parameter groups
@shicai
shicai / vae_simple.py
Created June 12, 2016 03:01 — forked from tencia/vae_simple.py
Variational Auto-Encoder using Lasagne
import sys
import os
import numpy as np
import theano
import theano.tensor as T
import lasagne as nn
import time
from PIL import Image
from scipy.stats import norm
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
@shicai
shicai / preprocessor_fun.h
Created April 21, 2016 15:36 — forked from aras-p/preprocessor_fun.h
Things to commit just before leaving your job
// Just before switching jobs:
// Add one of these.
// Preferably into the same commit where you do a large merge.
//
// This started as a tweet with a joke of "C++ pro-tip: #define private public",
// and then it quickly escalated into more and more evil suggestions.
// I've tried to capture interesting suggestions here.
//
// Contributors: @r2d2rigo, @joeldevahl, @msinilo, @_Humus_,
// @YuriyODonnell, @rygorous, @cmuratori, @mike_acton, @grumpygiant,
@shicai
shicai / CaffeBatchPrediction.cpp
Created March 4, 2016 15:27 — forked from erogol/CaffeBatchPrediction.cpp
Caffe c++ batch based prediction
#include "caffeclassifier.h"
CaffeClassifier::CaffeClassifier(const string& model_file,
const string& trained_file,
const string& mean_file,
const string& label_file,
const bool use_GPU,
const int batch_size) {
if (use_GPU)
Caffe::set_mode(Caffe::GPU);
  1. 对机器学习有基本的理解,最好的材料是Andrew Ng在coursera上的公开课程,看过教程能让你对机器学习有基本的认识,完成课程中的任务会让你对机器学习有更深入的理解
  2. 接下来需要培养对神经网络的感觉,实现一个神经网络,并利用它做点事情
  3. 理解神经网络的原理很重要,但简单的神经网络并不足以解决最有趣的问题。神经网络的变体-卷积神经网络(CNN)非常适合于图像识别类的任务,斯坦福大学的有关资料如下:CS231n 用于图像识别的卷积神经网络(笔记)课件。另外两个参考资料如下CNN 参考资料1CNN 参考资料1
  4. 接下来需要在自己的电脑上运行CNN
    • 买一块GPU,安装CUDA开发工具包
    • 安装Caffe和GUI包Digit
    • 安装Boinc,这个工具对你理解深度学习并没有帮助,但能够让其他研究人员在闲暇时间利用你的GPU做科研
  5. Digit提供了少量算法,比如用于字符识别的lenet算法和用于图像分类的Googlenet算法。要想运行这些算法,需要下载相应的数据集lenet数据集和[Googlenet数据集
@shicai
shicai / forward.py
Created January 9, 2016 14:37 — forked from samim23/forward.py
# "Colorizing B/W Movies with Neural Nets",
# Network/Code Created by Ryan Dahl, hacked by samim.io to work with movies
# BACKGROUND: http://tinyclouds.org/colorize/
# DEMO: https://www.youtube.com/watch?v=_MJU8VK2PI4
# USAGE:
# 1. Download TensorFlow model from: http://tinyclouds.org/colorize/
# 2. Use FFMPEG or such to extract frames from video.
# 3. Make sure your images are 224x224 pixels dimension. You can use imagemagicks "mogrify", here some useful commands:
# mogrify -resize 224x224 *.jpg
# mogrify -gravity center -background black -extent 224x224 *.jpg
@shicai
shicai / springer-free-maths-books.md
Created December 29, 2015 10:49 — forked from bishboria/springer-free-maths-books.md
Springer have made a bunch of books available for free, here are the direct links
@shicai
shicai / iccv2015.md
Created November 26, 2015 05:55 — forked from myungsub/iccv2015.md
upload candidates to awesome-deep-vision

Vision & Language

  • Ask Your Neurons: A Neural-Based Approach to Answering Questions About Images

    • Mateusz Malinowski, Marcus Rohrbach, Mario Fritz
  • Aligning Books and Movies: Towards Story-Like Visual Explanations by Watching Movies and Reading Books

    • Yukun Zhu, Ryan Kiros, Rich Zemel, Ruslan Salakhutdinov, Raquel Urtasun, Antonio Torralba, Sanja Fidler
  • Learning Query and Image Similarities With Ranking Canonical Correlation Analysis

  • Wah Ngo