keras系列二(补充)theano的tensor库

来源:互联网 发布:sql 选取第三大 编辑:程序博客网 时间:2024/05/21 16:55

今天看keras的模块,都是调用的theano的方法,感觉有必要补一下tensor相关知识,下面将学习一下tensor的一般用法和自己的理解。

tensor是在numpy 基础上更加适用于神经网络的张量操作的库, tensor所有的操作都是基于符号的,所有就有T.dscalar(), T.dmatrix(), T.dvector()等操作。


下面的demo参考自: http://www.th7.cn/Program/Python/201408/270636.shtml


第一个问题就是: 何谓张量呢? 学过高等物理,貌似有点印象,但是早已不记得了

  1. Tensors are sometimes defined as multidimensional arrays, in the same way that a matrix is a two-dimensional array. From this point of view, a matrix is certainly a special case of a tensor.

  2. In differential geometry and physics, "tensor" refers to a certain kind of object that can be described at a point on a manifold (though the word "tensor" is often used to refer to a tensor field, in which one tensor is chosen for every point). From this point of view, a matrix can be used to describe a rank-two tensor in local coordinates, but a rank-two tensor is not itself a matrix.

  3. In linear algebra, "tensor" sometimes refers to an element of a tensor product, and sometimes refers to a certain kind of multilinear map. Again, neither of these is a generalization of "matrix", though you can get a matrix from a rank-two tensor if you choose a basis for your vector space.   这是从stack overflow 上摘得一个比较好的 答案,说一下我的理解, tensor就是一个多维的数组

demo1,  标量的相加

import theano.tensor as Tfrom theano import functionx = T.dscalar('x')y = T.dscalar('y')z = x + yf = function([x, y], z)a = f(1,2)print aprint type(a)print type(f)print type(x)

<span style="font-family: Arial, Helvetica, sans-serif;">打印结果是 : 3.0 </span>


demo2, 矩阵相乘

import theano.tensor as Timport numpy as npfrom theano import functionx = T.dmatrix('x')y = T.dmatrix('y')z = x + yf = function([x, y], z)
#注意是矩阵相乘,必须为矩阵的形式,如果是list形式,报错信息为:
#TypeError: ('Bad input argument to theano function with name "test02.py:7"  at index 0(0-based)', 'Wrong number of dimensions: expected 2, got 1 with shape (3,).')c = f([[1,2,3]], [[1,2,3]]) print cprint type(c)

demo3, 多输入,多输出

import theano.tensor as Tfrom theano import functiona, b = T.dmatrices('a', 'b')diff = a - babs_diff = abs(diff)diff_squared = diff**2f = function([a, b], [diff, abs_diff,diff_squared])print f([[1, 1], [1, 1]], [[0, 1], [2,3]]) #a=<span style="font-family: Arial, Helvetica, sans-serif;">[[1, 1], [1, 1]] , b = </span><span style="font-family: Arial, Helvetica, sans-serif;">[[0, 1], [2,3]]</span>
输出:

[array([[ 1.,  0.],    [-1., -2.]]), array([[ 1.,  0.],   [ 1.,  2.]]), array([[ 1.,  0.],    [ 1.,  4.]])]


demo4. 共享变量,累加器

共享变量是比较重要的, 主要是用于gpu中,提高性能

import theano.tensor as Tfrom theano import functionfrom theano import sharedstate = shared(0) #make state be shared variableinc = T.iscalar('inc') #defination a scalaraccumulator = function([inc], state, updates=[(state, state+inc)]) #state = state + incprint state.get_value() accumulator(1) #传一个inc进去print state.get_value()accumulator(300) #传一个300进去print state.get_value()state.set_value(-1) #将state的设置为-1print state.get_value()
print accumulator(3)print state.get_value()
输出是:

0

1

301

-1

2

demo5 , 保留之前的state不改变,记录当前的结果,用given参数
import theano.tensor as Tfrom theano import functionfrom theano import sharedinc = T.iscalar('inc')state = shared(0)fn_of_state = state * 2 + incfoo = T.scalar(dtype=state.dtype)
print "foo is :", fooskip_shared = function([inc, foo], fn_of_state,  givens=[(state,foo)])print skip_shared(1, 3)print state.get_value()
输出是:

7

0





0 0
原创粉丝点击