Theano 学习笔记 Basics:Baby Steps - Algebra

这篇博客介绍了Theano库的基础知识,包括如何添加两个标量和矩阵。通过定义符号变量并编译成C代码来创建函数。讨论了Theano变量的类型,如dscalar,并展示了如何使用eval()进行计算。此外,还提到了矩阵相加的实现,以及Theano支持的不同数据类型。最后,给出了一个练习,要求修改代码以计算表达式a^2 + b^2 + 2*a*b。
摘要由CSDN通过智能技术生成

教程地址:http://deeplearning.net/software/theano/tutorial/adding.html

Adding two Scalars

>>> 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)

先定义了两个 symbols (Variables);函数 f 的输出为 a numpy.ndarray with zero dimensions。

当执行 f = function([x, y], z) 这句时,明显停滞了,这个时候 f 正被编译成 C 代码

这样就创建了 f 函数,使用它:

>>> f(2, 3)
array(5.0)
>>> numpy.allclose(f(16.3, 12.1), 28.4)
True

 

Step 1

>>> x = T.dscalar('x')
>>> y = T.dscalar('y')

T.dscalar 的变量类类型是 “0-dimensional arrays (scalar) of doubles (d)”. It is a Theano Type

dscalar 不是一个类。 因此,x,y 都不是 dscalar 的实例。 它们是 TensorVariable 的实例。但是把 theano Type dscalar 赋给了x,y 的 type 属性:

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


当 T.dscalar 带字符串参数时,字符串是这个变量的名字;名字不是必须的,只是方便调试程序。

 

Step 2

>>> z = x + y

可以使用 pp function to pretty-print 出:

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

 


Step 3

最后一步是创建 x,y 为输入,z 为输出的函数:

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

function的第一个参数为输入变量组成的 list ,第二个参数为单个变量或者变量的 list,为函数的输出。

 

Note:我们可以使用变量的eval方法。它虽然不想function()一样灵活,但是已经足够应付教程中所有需求。这样就不需要 import function()了。

>>> 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

eval() 接收的 {x : 16.3, y : 12.1} 为一个字典,返回表达式的数值结果。

eval()第一次对变量使用时会比较慢,因为它会调用 function() 来编译表达式。后续对相同变量调用 eval() 就会变快了,因为变量缓存了编译好的函数。

 

Adding two Matrices

唯一的改变是实例化 x,y 时使用 matrix Types:

>>> x = T.dmatrix('x')
>>> y = T.dmatrix('y')
>>> z = x + y
>>> f = function([x, y], z)


以2D arrays为输入使用函数:

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


以 NumPy array 为输入:

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


也可以进行 scalars to matrices, vectors to matrices, scalars to vectors 的相加,详见 broadcasting ,类似 matlab 中 repmat 后相加。

 

如下变量类型可用:

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

a guide to all types compatible with NumPy arrays may be found here: tensor creation

 

Note:需要根据电脑情况人工选择 32- or 64-bit integers (i prefix vs. the l prefix) and floats (f prefix vs. the d prefix).

 

Exercise

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]))

 

[    0.     2.  1026.]

Modify and execute this code to compute this expression: a ** 2 + b ** 2 + 2 * a * b.

 

答案:

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.]


 


 

Stkcd [股票代码] ShortName [股票简称] Accper [统计截止日期] Typrep [报表类型编码] Indcd [行业代码] Indnme [行业名称] Source [公告来源] F060101B [净利润现金净含量] F060101C [净利润现金净含量TTM] F060201B [营业收入现金含量] F060201C [营业收入现金含量TTM] F060301B [营业收入现金净含量] F060301C [营业收入现金净含量TTM] F060401B [营业利润现金净含量] F060401C [营业利润现金净含量TTM] F060901B [筹资活动债权人现金净流量] F060901C [筹资活动债权人现金净流量TTM] F061001B [筹资活动股东现金净流量] F061001C [筹资活动股东现金净流量TTM] F061201B [折旧摊销] F061201C [折旧摊销TTM] F061301B [公司现金流1] F061302B [公司现金流2] F061301C [公司现金流TTM1] F061302C [公司现金流TTM2] F061401B [股权现金流1] F061402B [股权现金流2] F061401C [股权现金流TTM1] F061402C [股权现金流TTM2] F061501B [公司自由现金流(原有)] F061601B [股权自由现金流(原有)] F061701B [全部现金回收率] F061801B [营运指数] F061901B [资本支出与折旧摊销比] F062001B [现金适合比率] F062101B [现金再投资比率] F062201B [现金满足投资比率] F062301B [股权自由现金流] F062401B [企业自由现金流] Indcd1 [行业代码1] Indnme1 [行业名称1] 季度数据,所有沪深北上市公司的 分别包含excel、dta数据文件格式及其说明,便于不同软件工具对数据的分析应用 数据来源:基于上市公司年报及公告数据整理,或相关证券交易所、各部委、省、市数据 数据范围:基于沪深北证上市公司 A股(主板、中小企业板、创业板、科创板等)数据整理计算
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值