Python中的 Theano
Theano是一个Python库,它允许我们如此高效地评估包括多维数组在内的数学运算。它主要用于构建深度学习项目。它在图形处理单元 (GPU) 上比在 CPU 上运行得更快。 Theano 获得了高速,这给 C 实现在涉及大量数据的问题上带来了激烈的竞争。它可以利用 GPU,在某些特定情况下,它在 CPU 上的性能比 C 语言要好相当多的数量级。
它知道如何获取结构并将它们转换为使用 numpy 和一些本机库的非常有效的代码。它主要用于处理深度学习中使用的大型神经网络算法所需的计算类型。这就是为什么它是深度学习领域非常流行的库。
如何安装 Theano:
pip install theano
我们需要使用的几个符号位于 Theano 的张量子包中。我们经常用一个方便的名字导入这样的包,比如说,T。
from theano import *
import theano.tensor as T
为什么选择 Theano Python库:
Theano 是 numpy 和 sympy 之间的一种混合体,试图将两者结合成一个强大的库。 theano 的一些优点如下:
- 稳定性优化: Theano 可以找出一些不稳定的表达式,并可以使用更稳定的手段来评估它们
- 执行速度优化:如前所述,theano 可以利用最近的 GPU 并在您的 CPU 或 GPU 中执行部分表达式,使其比Python快得多
- 符号微分: Theano 足够智能,可以自动创建用于计算梯度的符号图
Theano 的基础知识:
Theano 是一个Python库,可让您高效地定义、优化和评估涉及多维数组的数学表达式。一些 Theano 实现如下。
减去两个标量:
Python
# Python program showing
# subtraction of two scalars
import theano
from theano import tensor
# Declaring variables
a = tensor.dscalar()
b = tensor.dscalar()
# Subtracting
res = a - b
# Converting it to a callable object
# so that it takes matrix as parameters
func = theano.function([a, b], res)
# Calling function
assert 20.0 == func(30.5, 10.5)
Python
# Python program showing
# addition of two scalars
# Addition of two scalars
import numpy
import theano.tensor as T
from theano import function
# Declaring two variables
x = T.dscalar('x')
y = T.dscalar('y')
# Summing up the two numbers
z = x + y
# Converting it to a callable object
# so that it takes matrix as parameters
f = function([x, y], z)
f(5, 7)
Python
# Python program showing
# addition of two matrices
# Adding two matrices
import numpy
import theano.tensor as T
from theano import function
x = T.dmatrix('x')
y = T.dmatrix('y')
z = x + y
f = function([x, y], z)
f([[30, 50], [2, 3]], [[60, 70], [3, 4]])
Python
# Python program to illustrate logistic
# sigmoid function using theano
# Load theano library
import theano
from theano import tensor
# Declaring variable
a = tensor.dmatrix('a')
# Sigmoid function
sig = 1 / (1 + tensor.exp(-a))
# Now it takes matrix as parameters
log = theano.function([a], sig)
# Calling function
print(log([[0, 1], [-1, -2]]))
它不会提供任何输出,因为两个数字的断言与给定的数字匹配,因此它会产生一个真值。
添加两个标量:
Python
# Python program showing
# addition of two scalars
# Addition of two scalars
import numpy
import theano.tensor as T
from theano import function
# Declaring two variables
x = T.dscalar('x')
y = T.dscalar('y')
# Summing up the two numbers
z = x + y
# Converting it to a callable object
# so that it takes matrix as parameters
f = function([x, y], z)
f(5, 7)
输出:
array(12.0)
添加两个矩阵:
Python
# Python program showing
# addition of two matrices
# Adding two matrices
import numpy
import theano.tensor as T
from theano import function
x = T.dmatrix('x')
y = T.dmatrix('y')
z = x + y
f = function([x, y], z)
f([[30, 50], [2, 3]], [[60, 70], [3, 4]])
输出:
array([[ 90., 120.],
[ 5., 7.]])
使用 theano 的逻辑函数:
让我们尝试计算逻辑曲线,由下式给出:
物流函数:
Python
# Python program to illustrate logistic
# sigmoid function using theano
# Load theano library
import theano
from theano import tensor
# Declaring variable
a = tensor.dmatrix('a')
# Sigmoid function
sig = 1 / (1 + tensor.exp(-a))
# Now it takes matrix as parameters
log = theano.function([a], sig)
# Calling function
print(log([[0, 1], [-1, -2]]))
输出 :
[[0.5 0.73105858
0.26894142 0.11920292]]
Theano 是一个基础库,主要用于深度学习研发,可以直接创建深度学习模型,也可以通过 Keras 等方便的库。它支持卷积网络和循环网络,以及两者的组合。