Theano快速突击(一)

这个怎么读呢?第一次碰到时很自然的发音是 /θi.ˈæ.noʊ/,不过如果看一些视频可能也有发/te.ˈaː.no/的。这两种都有,比较官方的说法可能是这个

I think I say roughly /θi.ˈæ.noʊ/ (using the international phonetic alphabet), or /te.ˈaː.no/ when speaking Dutch, which is my native language. I guess the latter is actually closer to the original Greek pronunciation :)

维基发音:

Theano (/θɪˈænoʊ/; Greek: Θεανώ; fl. 6th-century BC), or Theano of Crotone,[1] is the name given to perhaps two Pythagorean philosophers.

1、简介

Theano是一个Python库,它可以让你定义,优化以及对数学表达式求值,尤其是多维数组(numpy的ndarray)的表达式的求值。对于解决大量数据的问题,使用Theano可能获得与手工用C实现差不多的性能。另外通过利用GPU,它能获得比CPU上的C实现快很多数量级。Theano把计算机代数系统(CAS)和优化的编译器结合在一起。 它也可以对许多数学操作生成自定义的c代码。这种CAS和优化编译的组合对于有复杂数学表达式重复的被求值并且求值速度很关键的问题是非常有用的。对于许多不同的表达式只求值一次的场景,Theano也能最小化编译/分析的次数,但是仍然可以提供诸如自动差分这样的符号计算的特性。

2、安装

这个比较好安装,网上教程也很多,这里按照官网安装,不多解释。

http://www.deeplearning.net/software/theano/install.html#install

3、使用

首先导入

from theano import *
import theano.tensor as T
然后就可以使用了。

例一:原文比较好

标量相加
Adding two Scalars
==================

To get us started with Theano 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:

>>> import numpy
>>> import theano.tensor as T
>>> from theano import function
>>> 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)
>>> numpy.allclose(f(16.3, 12.1), 28.4)
True

Let's break this down into several steps. The first step is to define
two symbols (*Variables*) representing the quantities that you want
to add. Note that from now on, we will use the term
*Variable* to mean "symbol" (in other words,
*x*, *y*, *z* are all *Variable* objects). The output of the function
*f* is a ``numpy.ndarray`` with zero dimensions.

If you are following along and typing into an interpreter, you may have
noticed that there was a slight delay in executing the ``function``
instruction. Behind the scene, *f* was being compiled into C code.


.. note:

  A *Variable* is the main data structure you work with when
  using Theano. The symbolic inputs that you operate on are
  *Variables* and what you get from applying various operations to
  these inputs are also *Variables*. For example, when I type

  >>> x = theano.tensor.ivector()
  >>> y = -x

  *x* and *y* are both Variables, i.e. instances of the
  ``theano.gof.graph.Variable`` class. The
  type of both *x* and *y* is ``theano.tensor.ivector``.


**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 (`scalar`) of doubles
(`d`)". It is a Theano :ref:`type`.

``dscalar`` is not a class. Therefore, neither *x* nor *y*
are actually instances of ``dscalar``. They are instances of
:class:`TensorVariable`. *x* and *y*
are, however, assigned the theano Type ``dscalar`` in their ``type``
field, as you can see here:

>>> type(x)
<class 'theano.tensor.var.TensorVariable'>
>>> x.type
TensorType(float64, scalar)
>>> T.dscalar
TensorType(float64, scalar)
>>> x.type is T.dscalar
True

By calling ``T.dscalar`` with a string argument, you create a
*Variable* representing a floating-point scalar quantity with the
given name. If you provide no argument, the symbol will be unnamed. Names
are not required, but they can help debugging.

More will be said in a moment regarding Theano's inner structure. You
could also learn more by looking into :ref:`graphstructures`.


**Step 2**

The second step is to combine *x* and *y* into their sum *z*:

>>> z = x + y

*z* is yet another *Variable* which represents the addition of
*x* and *y*. You can use the :ref:`pp <libdoc_printing>`
function to pretty-print out the computation associated to *z*.

>>> from theano import pp
>>> print(pp(z))
(x + y)


**Step 3**

The last step is to create a function taking *x* and *y* as inputs
and giving *z* as output:

>>> f = function([x, y], z)

The first argument to :func:`function <function.function>` is a list of Variables
that will be provided as inputs to the function. The second argument
is a single Variable *or* a list of Variables. For either case, the second
argument is what we want to see as output when we apply the function. *f* may
then be used like a normal Python function.

.. note::

    As a shortcut, you can skip step 3, and just use a variable's
    :func:`eval <theano.gof.graph.Variable.eval>` method.
    The :func:`eval` method is not as flexible
    as :func:`function` but it can do everything we've covered in
    the tutorial so far. It has the added benefit of not requiring
    you to import :func:`function` . Here is how :func:`eval` works:

    >>> import numpy
    >>> import theano.tensor as T
    >>> x = T.dscalar('x')
    >>> y = T.dscalar('y')
    >>> z = x + y
    >>> numpy.allclose(z.eval({x : 16.3, y : 12.1}), 28.4)
    True

    We passed :func:`eval` a dictionary mapping symbolic theano
    variables to the values to substitute for them, and it returned
    the numerical value of the expression.

    :func:`eval` will be slow the first time you call it on a variable --
    it needs to call :func:`function` to compile the expression behind
    the scenes. Subsequent calls to :func:`eval` on that same variable
    will be fast, because the variable caches the compiled 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. 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 variable is a NumPy array. We can also use NumPy arrays directly as
inputs:

>>> import numpy
>>> f(numpy.array([[1, 2], [3, 4]]), numpy.array([[10, 20], [30, 40]]))
array([[ 11.,  22.],
       [ 33.,  44.]])

It is possible to add scalars to matrices, vectors to matrices,
scalars to vectors, etc. The behavior of these operations is defined
by :ref:`broadcasting <libdoc_tensor_broadcastable>`.
重点:
The following types are available:

* **byte**: ``bscalar, bvector, bmatrix, brow, bcol, btensor3, btensor4, btensor5``
* **16-bit integers**: ``wscalar, wvector, wmatrix, wrow, wcol, wtensor3, wtensor4, wtensor5``
* **32-bit integers**: ``iscalar, ivector, imatrix, irow, icol, itensor3, itensor4, itensor5``
* **64-bit integers**: ``lscalar, lvector, lmatrix, lrow, lcol, ltensor3, ltensor4, ltensor5``
* **float**: ``fscalar, fvector, fmatrix, frow, fcol, ftensor3, ftensor4, ftensor5``
* **double**: ``dscalar, dvector, dmatrix, drow, dcol, dtensor3, dtensor4, dtensor5``
* **complex**: ``cscalar, cvector, cmatrix, crow, ccol, ctensor3, ctensor4, ctensor5``

The previous list is not exhaustive and a guide to all types compatible
with NumPy arrays may be found here: :ref:`tensor creation<libdoc_tensor_creation>`.

.. note::

   You, the user---not the system architecture---have to choose whether your
   program will use 32- or 64-bit integers (``i`` prefix vs. the ``l`` prefix)
   and floats (``f`` prefix vs. the ``d`` prefix).



Exercise
========

.. testcode::

   import theano
   a = theano.tensor.vector() # declare variable
   out = a + a ** 10               # build symbolic expression
   f = theano.function([a], out)   # compile function
   print(f([0, 1, 2]))

.. testoutput::

   [    0.     2.  1026.]

例二:

compute this expression: a ** 2 + b ** 2 + 2 * a * b.
#!/usr/bin/env python
# Theano tutorial
# Solution to Exercise in section 'Baby Steps - Algebra' 
from __future__ import absolute_import, print_function, division
import theano
a = theano.tensor.vector() # declare variable
b = theano.tensor.vector() # declare variable
out = a ** 2 + b ** 2 + 2 * a * b # build symbolic expression
f = theano.function([a, b], out) # compile function
print(f([1, 2], [4, 5])) # prints [ 25. 49.]

参考:

http://www.deeplearning.net/software/theano/tutorial/index.html

http://www.deeplearning.net/software/theano/tutorial/adding.html#adding-two-matrices


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自适应dropout算法是一种能够自动调整每层神经元dropout概率的算法,它可以有效地避免过拟合问题。下面是一个使用theano库实现的简单自适应dropout算法的代码示例: ```python import theano import theano.tensor as T import numpy as np def adaptive_dropout(input, p): # 计算每层神经元的dropout概率 keep_prob = T.minimum(1., T.exp(-p * input.mean(axis=0)) + 0.5) # 生成掩码矩阵 mask = theano.sandbox.rng_mrg.MRG_RandomStreams().binomial(n=1, p=keep_prob, size=input.shape, dtype=theano.config.floatX) # 对输入进行dropout操作 output = input * mask return output # 定义输入数据和dropout概率 x = T.matrix('x') p = 0.5 # 构建神经网络 layer1 = adaptive_dropout(x, p) layer2 = adaptive_dropout(layer1, p) layer3 = adaptive_dropout(layer2, p) output = T.nnet.softmax(layer3) # 定义损失函数和优化器 y = T.ivector('y') loss = T.nnet.categorical_crossentropy(output, y).mean() params = [param for param in theano.tensor.get_all_params(output) if param.name not in {'beta', 'gamma'}] updates = theano.updates.adam(loss, params) # 编译模型 train_fn = theano.function(inputs=[x, y], outputs=loss, updates=updates) predict_fn = theano.function(inputs=[x], outputs=output.argmax(axis=-1)) # 训练模型 X_train = np.random.rand(1000, 10) y_train = np.random.randint(0, 2, 1000) for epoch in range(10): loss = train_fn(X_train, y_train) print('Epoch {}: Loss = {}'.format(epoch + 1, loss)) ``` 在这个示例中,我们定义了一个`adaptive_dropout`函数来实现自适应dropout操作。该函数计算每一层神经元的dropout概率,并使用theano的随机数生成器生成掩码矩阵。然后将该掩码矩阵应用于输入数据来执行dropout操作。 最后,我们定义了一个简单的三层全连接神经网络,并使用自适应dropout算法来避免过拟合。我们还使用theano的Adam优化器来训练模型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值