Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
e1d92296
提交
e1d92296
authored
2月 26, 2009
作者:
Olivier Breuleux
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added basic tutorial
上级
63b77263
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
283 行增加
和
0 行删除
+283
-0
adding.txt
doc/tutorial/adding.txt
+147
-0
examples.txt
doc/tutorial/examples.txt
+76
-0
index.txt
doc/tutorial/index.txt
+53
-0
module.txt
doc/tutorial/module.txt
+7
-0
没有找到文件。
doc/tutorial/adding.txt
0 → 100644
浏览文件 @
e1d92296
===========================
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
doc/tutorial/examples.txt
0 → 100644
浏览文件 @
e1d92296
.. _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
doc/tutorial/index.txt
0 → 100644
浏览文件 @
e1d92296
.. _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
doc/tutorial/module.txt
0 → 100644
浏览文件 @
e1d92296
============
Using Module
============
WRITEME
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论