{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\nWhat is PyTorch?\n================\n\nIt\u2019s a Python-based scientific computing package targeted at two sets of\naudiences:\n\n- A replacement for NumPy to use the power of GPUs\n- a deep learning research platform that provides maximum flexibility\n and speed\n\nGetting Started\n---------------\n\nTensors\n^^^^^^^\n\nTensors are similar to NumPy\u2019s ndarrays, with the addition being that\nTensors can also be used on a GPU to accelerate computing.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from __future__ import print_function\nimport torch" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Construct a 5x3 matrix, uninitialized:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = torch.empty(5, 3)\nprint(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Construct a randomly initialized matrix:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = torch.rand(5, 3)\nprint(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Construct a matrix filled zeros and of dtype long:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = torch.zeros(5, 3, dtype=torch.long)\nprint(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Construct a tensor directly from data:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = torch.tensor([5.5, 3])\nprint(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or create a tensor based on an existing tensor. These methods\nwill reuse properties of the input tensor, e.g. dtype, unless\nnew values are provided by user\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = x.new_ones(5, 3, dtype=torch.double) # new_* methods take in sizes\nprint(x)\n\nx = torch.randn_like(x, dtype=torch.float) # override dtype!\nprint(x) # result has the same size" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get its size:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(x.size())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
``torch.Size`` is in fact a tuple, so it supports all tuple operations.
Any operation that mutates a tensor in-place is post-fixed with an ``_``.\n For example: ``x.copy_(y)``, ``x.t_()``, will change ``x``.