怎样做招聘网站分析,网站建设入门教程视频,有哪些网站可以做青旅义工,重庆介绍ppt制作前言
至此感觉应该可以写出一个logistic回归程序了#xff0c;但是为了达到对theano中张量的更灵活的使用, 还是先看一下thenao.tensor对变量都提供了哪些操作#xff0c;最全的文档戳这里或者这里, 这里就稍微摘取一点自我感觉以后可能用得多的函数
基本张量函数
创建张量…前言
至此感觉应该可以写出一个logistic回归程序了但是为了达到对theano中张量的更灵活的使用, 还是先看一下thenao.tensor对变量都提供了哪些操作最全的文档戳这里或者这里, 这里就稍微摘取一点自我感觉以后可能用得多的函数
基本张量函数
创建张量
以下三条语句都是创建一个张量实例, 代表一个0维的整型阵列, 名字是myvar
xT.scalar(myvar,dtypeint32)
xT.iscalar(myvar)
xT.TensorType(dtypeint32,broadcastable())(myvar)
print x.type
#TensorType(int32, scalar)
还有其它可选的创建方法, 比如下面分别创建从0维到5维浮点型张量
xT.scalar(myvar,dtypetheano.config.floatX)#创建0维阵列
xT.vector(myvar,dtypetheano.config.floatX)#创建以为阵列
xT.row(myvar,dtypetheano.config.floatX)#创建二维阵列,行数为1
xT.col(myvar,dtypetheano.config.floatX)#创建二维阵列,列数为1
xT.matrix(myvar,dtypetheano.config.floatX)#创建二维矩阵
xT.tensor3(myvar,dtypetheano.config.floatX)#创建三维张量
xT.tensor4(myvar,dtypetheano.config.floatX)#创建四维张量
xT.tensor5(myvar,dtypetheano.config.floatX)#创建五维张量
x.ndim#输出维度看看
#5
一次性创建多个张量方法 标量: iscalars, lscalars, fscalars, dscalars
向量: ivectors, lvectors, fvectors, dvectors
行向量: irows, lrows, frows, drows
列向量:icols, lcols, fcols, dcols
矩阵: imatrices, lmatrices, fmatrices, dmatricesx,y,zT.dmatrices(3)#没有名字的三个矩阵
x,y,zT.dmatrices(x,y,z)#有名字的三个矩阵
自己定义一个更高维的阵列, 比如创建一个6维阵列
dtensor6T.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, ndimNone) x是要被重新reshape的张量 newshape是x被reshape以后的大小 ndim是指定新的shape的长度, 如果是None, 此函数会自动推导其值同样是重组张量大小, 将(2,3,4,5)大小的四维向量重组为(2,60)大小, 就是用 theano.tensor.flatten(x, outdim2) outdim就是输出的向量的引导维度的大小循环移位函数 theano.tensor.roll(x, shift, axisNone) 将输入张量x的第axis的维度循环移动shift维, 跟numpy.roll一样的效果张量左右或者指定维度填充padding theano.tensor.shape_padleft(x, n_ones1)#左边padding n_ones个1
theano.tensor.shape_padright(x, n_ones1)#右边padding n_ones个1
theano.tensor.shape_padaxis(t, axis)#在axis插入一个维度 tensor theano.tensor.tensor3()
theano.tensor.shape_padaxis(tensor, axis0)#InplaceDimShuffle{x,0,1,2}.0theano.tensor.shape_padaxis(tensor, axis1)#InplaceDimShuffle{0,x,1,2}.0填充张量
可以用某个值(0,1或者其它值)填充指定大小的张量
theano.tensor.zeros_like(x, dtypeNone)#填充一个与x相同大小的全1矩阵, 默认类型是x.dtype
theano.tensor.ones_like(x)#填充一个与x相同大小的全1矩阵, 默认类型是x.dtype
theano.tensor.zeros(shape,dtypeNone)#填充一个大小为shape的全0矩阵, 默认类型floaX
theano.tensor.ones(shape, dtypeNone)##填充一个大小为shape的全1矩阵, 默认类型floaX
theano.tensor.fill(a, b)#用标量b填充与a同样大小的矩阵
theano.tensor.alloc(value, *shape)#用value填充一个shape大小的张量
theano.tensor.eye(n, mNone, k0, dtypetheano.config.floatX)#输出的行数n和列数m,主对角线是0,正数是朝上移动负数是朝下移动的对角线,创建的是一个除了第k个对角线上值为1以外的全0矩阵
theano.tensor.identity_like(x)#与x大小相同的矩阵,但是主对角线值为1,其它元素值为0
theano.tensor.stack(tensors, axis0)#按照axis轴去堆叠多个张量
theano中给出了最后一个用于堆叠张量的函数的使用方法
#堆叠张量
a,b,cT.scalars(3)
a.ndim#如果是标量维度就是0
xT.stack([a,b,c])
x.ndim#堆叠成一个向量, 所以维度是1a,b,cT.tensor4s(3)
xT.stack([a,b,c])
x.ndim
#5
内部操作(最大最小值,方差,均值之类的)
theano.tensor.max(x, axisNone, keepdimsFalse)#返回axis轴上的最大值
theano.tensor.argmax(x, axisNone, keepdimsFalse)#返回axis轴上最大值的索引
theano.tensor.max_and_argmax(x, axisNone, keepdimsFalse)#返回axis轴上最大值及其索引
theano.tensor.min(x, axisNone, keepdimsFalse)#返回axis轴上最小值
theano.tensor.argmin(x, axisNone, keepdimsFalse)#返回axis轴上最小值的索引
theano.tensor.sum(x, axisNone, dtypeNone, keepdimsFalse, acc_dtypeNone)#按照axis轴加和
theano.tensor.prod(x, axisNone, dtypeNone, keepdimsFalse, acc_dtypeNone, no_zeros_in_inputFalse)#沿着axis轴元素乘积
theano.tensor.mean(x, axisNone, dtypeNone, keepdimsFalse, acc_dtypeNone)#axis轴的均值
theano.tensor.var(x, axisNone, keepdimsFalse)#axis轴的方差
theano.tensor.std(x, axisNone, keepdimsFalse)#axis轴的标准差
索引
比如找矩阵中大于某个数的所有数值, 需要注意的是, theano并没有提供布尔类型, 所以需要这样找 tT.arange(9).reshape((3,3))#不要使用t[t 4].eval()t[(t4).nonzero()].eval()#array([5, 6, 7, 8], dtypeint64)
重置张量某部分值, 比如用5取代索引[10:]处也就是10及其以后所有索引的值 r T.ivector()
new_r T.set_subtensor(r[10:], 5)将张量某部分值加上一个值, 比如实现r[10:] 5
#theano.tensor.inc_subtensor(x, y, inplaceFalse, set_instead_of_incFalse, tolerate_inplace_aliasingFalse)r T.ivector()
new_r T.inc_subtensor(r[10:], 5)
转换类型、复数操作
#theano.tensor.cast(x, dtype)#把x转换成一个具有相同大小的不同数据类型的张量
import theano.tensor as T
x 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)#ab是否相等
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)#Switch
theano.tensor.where(cond, ift, iff)#Switch的别名
theano.tensor.clip(x, min, max)#如果xmin就取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)#返回-a
theano.tensor.inv(a)#返回导数, 也就是1.0/a
theano.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, modehalf_away_from_zero)#四舍五入
theano.tensor.iround(a, modehalf_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, axes2)#比如(2,3,4)和(5,6,4,3)的张量乘积,得到的是(2,5,6)大小的张量,这三个维度就是没有被加和起来的维度,具体实现可以看官方文档
梯度
theano.gradient.grad(cost, wrt, consider_constantNone, disconnected_inputsraise, add_namesTrue, known_gradsNone, return_disconnectedzero, null_gradientsraise)
暂时只要知道cost就是损失函数表达式, param可以是一个数组或者矩阵, 就是被求导的变量
∂cost∂wrt\frac{\partial cost}{\partial wrt}可以参考前面的一篇博客:
【theano-windows】学习笔记三——theano中的导数【PS】遗憾的时候目前只知道用途并不知道具体到实际操作中的用法不过嘛只有先知道有这个东东才能继续探索怎么用嘛。接下来继续上一篇博客【theano-windows】学习笔记四——theano中的条件语句的后续学习, 按照官方教程, 应该是学习循环函数scan的使用了
本博文的代码链接: https://pan.baidu.com/s/1hstJVEk 密码: eqhj