当前位置:首页 >> 技术栈专业化分层 >> 【theano-windows】学习笔记五——theano中张量部分函数,汽车电脑维修(张量shape)

【theano-windows】学习笔记五——theano中张量部分函数,汽车电脑维修(张量shape)

cpugpu芯片开发光刻机 技术栈专业化分层 2
文件名:【theano-windows】学习笔记五——theano中张量部分函数,汽车电脑维修 【theano-windows】学习笔记五——theano中张量部分函数 前言

至此感觉应该可以写出一个logistic回归程序了,但是为了达到对theano中张量的更灵活的使用, 还是先看一下thenao.tensor对变量都提供了哪些操作,最全的文档戳这里或者这里, 这里就稍微摘取一点自我感觉以后可能用得多的函数

基本张量函数 创建张量

以下三条语句都是创建一个张量实例, 代表一个0维的整型阵列, 名字是myvar

x=T.scalar('myvar',dtype='int32')x=T.iscalar('myvar')x=T.TensorType(dtype='int32',broadcastable=())('myvar')print x.type#TensorType(int32, scalar)

还有其它可选的创建方法, 比如下面分别创建从0维到5维浮点型张量

x=T.scalar('myvar',dtype=theano.config.floatX)#创建0维阵列x=T.vector('myvar',dtype=theano.config.floatX)#创建以为阵列x=T.row('myvar',dtype=theano.config.floatX)#创建二维阵列,行数为1x=T.col('myvar',dtype=theano.config.floatX)#创建二维阵列,列数为1x=T.matrix('myvar',dtype=theano.config.floatX)#创建二维矩阵x=T.tensor3('myvar',dtype=theano.config.floatX)#创建三维张量x=T.tensor4('myvar',dtype=theano.config.floatX)#创建四维张量x=T.tensor5('myvar',dtype=theano.config.floatX)#创建五维张量x.ndim#输出维度看看#5

一次性创建多个张量方法

'''标量: iscalars, lscalars, fscalars, dscalars向量: ivectors, lvectors, fvectors, dvectors行向量: irows, lrows, frows, drows列向量:icols, lcols, fcols, dcols矩阵: imatrices, lmatrices, fmatrices, dmatrices'''x,y,z=T.dmatrices(3)#没有名字的三个矩阵x,y,z=T.dmatrices('x','y','z')#有名字的三个矩阵

自己定义一个更高维的阵列, 比如创建一个6维阵列

dtensor6=T.TensorType('float64',(False,)*6)#自定义6维阵列

重组

获取张量的shape

theano.tensor.shape(x)

【PS】很奇怪为什么用这句话去获取上面创建的0-6维阵列, 大小都是0, 难道是因为只声明没赋值?回头赋值看看. 但是用x.ndim能够得到张量的维度

将一个张量x按照要求重组成指定大小的张量, 比如1*9的变成3*3的

theano.tensor.reshape(x, newshape, ndim=None)

x是要被重新reshape的张量 newshape是x被reshape以后的大小

ndim是指定新的shape的长度, 如果是None, 此函数会自动推导其值

同样是重组张量大小, 将(2,3,4,5)大小的四维向量重组为(2,60)大小, 就是用

theano.tensor.flatten(x, outdim=2)

outdim就是输出的向量的引导维度的大小

循环移位函数

theano.tensor.roll(x, shift, axis=None)

将输入张量x的第axis的维度循环移动shift维, 跟numpy.roll一样的效果

张量左右或者指定维度填充padding

theano.tensor.shape_padleft(x, n_ones=1)#左边padding n_ones个1theano.tensor.shape_padright(x, n_ones=1)#右边padding n_ones个1theano.tensor.shape_padaxis(t, axis)#在axis插入一个维度 tensor = theano.tensor.tensor3()theano.tensor.shape_padaxis(tensor, axis=0)#InplaceDimShuffle{x,0,1,2}.0theano.tensor.shape_padaxis(tensor, axis=1)#InplaceDimShuffle{0,x,1,2}.0 填充张量

可以用某个值(0,1或者其它值)填充指定大小的张量

theano.tensor.zeros_like(x, dtype=None)#填充一个与x相同大小的全1矩阵, 默认类型是x.dtypetheano.tensor.ones_like(x)#填充一个与x相同大小的全1矩阵, 默认类型是x.dtypetheano.tensor.zeros(shape,dtype=None)#填充一个大小为shape的全0矩阵, 默认类型floaXtheano.tensor.ones(shape, dtype=None)##填充一个大小为shape的全1矩阵, 默认类型floaXtheano.tensor.fill(a, b)#用标量b填充与a同样大小的矩阵theano.tensor.alloc(value, *shape)#用value填充一个shape大小的张量theano.tensor.eye(n, m=None, k=0, dtype=theano.config.floatX)#输出的行数n和列数m,主对角线是0,正数是朝上移动,负数是朝下移动的对角线,创建的是一个除了第k个对角线上值为1以外的全0矩阵theano.tensor.identity_like(x)#与x大小相同的矩阵,但是主对角线值为1,其它元素值为0theano.tensor.stack(tensors, axis=0)#按照`axis`轴去堆叠多个张量

theano中给出了最后一个用于堆叠张量的函数的使用方法

#堆叠张量a,b,c=T.scalars(3)a.ndim#如果是标量,维度就是0x=T.stack([a,b,c])x.ndim#堆叠成一个向量, 所以维度是1a,b,c=T.tensor4s(3)x=T.stack([a,b,c])x.ndim#5 内部操作(最大最小值,方差,均值之类的) theano.tensor.max(x, axis=None, keepdims=False)#返回axis轴上的最大值theano.tensor.argmax(x, axis=None, keepdims=False)#返回axis轴上最大值的索引theano.tensor.max_and_argmax(x, axis=None, keepdims=False)#返回axis轴上最大值及其索引theano.tensor.min(x, axis=None, keepdims=False)#返回axis轴上最小值theano.tensor.argmin(x, axis=None, keepdims=False)#返回axis轴上最小值的索引theano.tensor.sum(x, axis=None, dtype=None, keepdims=False, acc_dtype=None)#按照axis轴加和theano.tensor.prod(x, axis=None, dtype=None, keepdims=False, acc_dtype=None, no_zeros_in_input=False)#沿着axis轴元素乘积theano.tensor.mean(x, axis=None, dtype=None, keepdims=False, acc_dtype=None)#axis轴的均值theano.tensor.var(x, axis=None, keepdims=False)#axis轴的方差theano.tensor.std(x, axis=None, keepdims=False)#axis轴的标准差 索引

比如找矩阵中大于某个数的所有数值, 需要注意的是, theano并没有提供布尔类型, 所以需要这样找

t=T.arange(9).reshape((3,3))#不要使用t[t > 4].eval()t[(t>4).nonzero()].eval()#array([5, 6, 7, 8], dtype=int64)

重置张量某部分值, 比如用5取代索引[10:]处也就是10及其以后所有索引的值

r = T.ivector()new_r = T.set_subtensor(r[10:], 5)

将张量某部分值加上一个值, 比如实现r[10:] += 5

#theano.tensor.inc_subtensor(x, y, inplace=False, set_instead_of_inc=False, tolerate_inplace_aliasing=False)r = T.ivector()new_r = T.inc_subtensor(r[10:], 5) 转换类型、复数操作 #theano.tensor.cast(x, dtype)#把x转换成一个具有相同大小的不同数据类型的张量import theano.tensor as Tx = T.matrix()x_as_int = T.cast(x, 'int32')#注意如果x是复数会报错 theano.tensor.real(x)#返回复数的实数域部分theano.tensor.imag(x)#返回复数的复数域部分 比较大小 theano.tensor.lt(a, b)#a < b大于theano.tensor.gt(a, b)#a > b小于theano.tensor.le(a, b)#a <= b小于等于theano.tensor.ge(a, b)#a >= b大于等于theano.tensor.eq(a, b)#a==b是否相等theano.tensor.neq(a, b)#a!=b是否不相等theano.tensor.isnan(a)#numpy.isnan是否为非数1/0之类的theano.tensor.isinf(a)#numpy.isinf是否为无穷 条件 theano.tensor.switch(cond, ift, iff)#Switchtheano.tensor.where(cond, ift, iff)#Switch的别名theano.tensor.clip(x, min, max)#如果x<min就取min, 大于max就取max 数学操作(较为常用) theano.tensor.abs_(a)#取绝对值theano.tensor.angle(a)#complex-valued张量的角度分量theano.tensor.exp(a)#指数运算theano.tensor.maximum(a, b)#返回a和b较大的一个theano.tensor.minimum(a, b)#返回a和b较小的一个theano.tensor.neg(a)#返回-atheano.tensor.inv(a)#返回导数, 也就是1.0/atheano.tensor.log(a), log2(a), log10(a)#返回以e,2,10为底的对数值theano.tensor.sgn(a)#返回a的符号theano.tensor.ceil(a)#向上取整theano.tensor.floor(a)#向下取整theano.tensor.round(a, mode="half_away_from_zero")#四舍五入theano.tensor.iround(a, mode="half_away_from_zero")#cast(round(a, mode),’int64’)的简写theano.tensor.sqr(a)#平方theano.tensor.sqrt(a)#开根号theano.tensor.cos(a), sin(a), tan(a)#返回a的三角函数值theano.tensor.cosh(a), sinh(a), tanh(a)#返回a的反三角函数值 线性代数 theano.tensor.dot(X, Y)#矩阵乘法,或者是向量内积theano.tensor.outer(X, Y)#向量外积theano.tensor.tensordot(a, b, axes=2)#比如(2,3,4)和(5,6,4,3)的张量乘积,得到的是(2,5,6)大小的张量,这三个维度就是没有被加和起来的维度,具体实现可以看官方文档 梯度 theano.gradient.grad(cost, wrt, consider_constant=None, disconnected_inputs='raise', add_names=True, known_grads=None, return_disconnected='zero', null_gradients='raise')

暂时只要知道cost就是损失函数表达式, param可以是一个数组或者矩阵, 就是被求导的变量

costwrt 可以参考前面的一篇博客: 【theano-windows】学习笔记三——theano中的导数

【PS】遗憾的时候目前只知道用途,并不知道具体到实际操作中的用法,不过嘛,只有先知道有这个东东才能继续探索怎么用嘛。接下来继续上一篇博客【theano-windows】学习笔记四——theano中的条件语句的后续学习, 按照官方教程, 应该是学习循环函数scan的使用了

本博文的代码:链接: https://pan.baidu.com/s/1hstJVEk 密码: eqhj

协助本站SEO优化一下,谢谢!
关键词不能为空
同类推荐
«    2025年12月    »
1234567
891011121314
15161718192021
22232425262728
293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接