提交 e1d92296 authored 作者: Olivier Breuleux's avatar Olivier Breuleux

added basic tutorial

上级 63b77263
===========================
Adding two numbers together
===========================
Adding two scalars
==================
So, to get us started and get a feel of what we're working with, let's
make a simple function: add two numbers together. Here is how you do
it:
>>> x = T.dscalar('x')
>>> y = T.dscalar('y')
>>> z = x + y
>>> f = function([x, y], z)
And now that we've created our function we can use it:
>>> f(2, 3)
array(5.0)
>>> f(16.3, 12.1)
array(28.4)
Let's break this down into several steps. The first step is to define
two symbols, or Results, representing the quantities that you want to
add. Note that from now on, we will use the term :term:`Result` to
mean "symbol" (in other words, ``x``, ``y``, ``z`` are all Result
objects).
**Step 1**
>>> x = T.dscalar('x')
>>> y = T.dscalar('y')
In Theano, all symbols must be typed. In particular, ``T.dscalar`` is
the type we assign to "0-dimensional arrays of doubles". It is a
Theano :term:`Type`. Therefore, you can guess that by calling
``T.dscalar`` with a string argument, you create a :term:`Result`
representing a floating-point scalar quantity with the given name (if
you provide no argument, the symbol will be unnamed, which can cause
difficulties in debugging).
Note that ``dscalar`` is not a class and that therefore neither ``x``
nor ``y`` are actually instances of ``dscalar``. They are instances of
:api:`TensorResult <theano.tensor.basic.TensorResult>`. It is however
assigned the theano Type ``dscalar`` in its ``type`` field, as you can
see here:
>>> type(x)
<class 'theano.tensor.basic.TensorResult'>
>>> x.type
Tensor(float64, scalar)
>>> T.dscalar
Tensor(float64, scalar)
>>> x.type == T.dscalar
True
Ditto for ``y``. You may learn more about the structures in Theano in
the :ref:`advtutorial` and in :ref:`graphstructures`.
**Step 2**
The second step is to combine ``x`` and ``y`` into their sum ``z``:
>>> z = x + y
``z`` is yet another :term:`Result` which represents the addition of
``x`` and ``y``. You can use the :api:`pp <theano.printing.pp>`
function to print out the computation associated to ``z``.
>>> print pp(z)
x + y
**Step 3**
The last step is to create a function taking ``x`` and ``y`` as inputs
and giving out ``z`` as output:
>>> f = function([x, y], z)
The first argument to ``function`` is a list of :term:`Results
<Result>` that will be provided as inputs to the function. The second
argument is a single Result that we want to see as output *or* a list
of output results.
``f`` may then be used like a normal Python function.
Adding two matrices
===================
You might already have guessed how to do this. Indeed, the only change
from the previous example is that you need to instantiate ``x`` and
``y`` using the matrix Types:
>>> x = T.dmatrix('x')
>>> y = T.dmatrix('y')
>>> z = x + y
>>> f = function([x, y], z)
``dmatrix`` is the Type for matrices of doubles. And then we can use
our new function on 2D arrays:
>>> f([[1, 2], [3, 4]], [[10, 20], [30, 40]])
array([[ 11., 22.],
[ 33., 44.]])
The result is a numpy array. We can also use numpy arrays directly as
inputs:
>>> import numpy
>>> f(numpy.ones((3, 5)), numpy.ones((3, 5)))
array([[ 2., 2., 2., 2., 2.],
[ 2., 2., 2., 2., 2.],
[ 2., 2., 2., 2., 2.]])
It is possible to add scalars to matrices, vectors to matrices,
scalars to vectors, etc. The behavior of these operations is defined
by broadcasting_.
The following types are readily available:
* byte: bscalar, bvector, bmatrix
* 32-bit integers: iscalar, ivector, imatrix
* 64-bit integers: lscalar, lvector, lmatrix
* float: fscalar, fvector, fmatrix
* double: dscalar, dvector, dmatrix
Section
-------
Try to mix and match them and see what happens. A complete list of
types compatible with numpy arrays may be found :ref:`here
<typelist>`.
**Next:** `More examples`_
.. _broadcasting: ../concepts/broadcasting.html
.. _More examples: examples.html
.. _Basic Tutorial - More Examples:
=============
More examples
=============
Logistic function
=================
Let's say that you want to compute the logistic curve, which is given
by:
``s(x) = 1 / (1 + e**(-x))``
You want to compute the function :term:`elementwise` on matrices of
doubles.
>>> x = T.dmatrix('x')
>>> s = 1 / (1 + T.exp(-x))
>>> logistic = function([x], s)
Alternatively:
>>> s = (T.tanh(x) + 1) / 2
>>> logistic = function([x], s)
Computing more than one thing at the same time
==============================================
Theano supports functions with multiple outputs. For example, we can
compute the absolute :term:`elementwise` difference between two
matrices ``x`` and ``y`` and the squared difference at the same time:
>>> x, y = T.dmatrices('xy')
>>> d = x - y
>>> f = function([x, y], [abs(d), d**2])
Theano will make ``f`` in such a way that it will only compute the
difference once. When we use the function, it will return the two
results (reformatted for readability):
>>> f([[1, 1], [1, 1]], [[0, 1], [2, 3]])
[array([[ 1., 0.],
[ 1., 2.]]),
array([[ 1., 0.],
[ 1., 4.]])]
Also note the call to ``dmatrices``. This is a shortcut, use it wisely
;)
Computing gradients
===================
WRITEME
Setting a default value for an argument
=======================================
WRITEME
Making a function with state
============================
WRITEME
**Next:** `Using Module`_
.. _Using Module: module.html
.. _basictutorial:
==============
Basic Tutorial
==============
Before doing anything in this tutorial, make sure that Theano is
installed on your system (see :ref:`install`).
Done? Alright!
Let's start an interactive session and import the package you just
installed:
>>> from theano import *
Many of symbols you will need to use lie in the ``tensor`` subpackage
of theano. Let's import that subpackage under a handy name. I like
``T``.
>>> import theano.tensor as T
From then, here's the tour:
`Adding two numbers together`_
Starting small
`More examples`_
Getting comfortable
`Using Module`_
Getting serious
.. rubric:: Contents
.. toctree::
:maxdepth: 2
adding
examples
module
.. _Adding two numbers together: adding.html
.. _More examples: examples.html
.. _Using Module: module.html
============
Using Module
============
WRITEME
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论