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
第一个问题就是: 何谓张量呢? 学过高等物理,貌似有点印象,但是早已不记得了
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.
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.
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>
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
- keras系列二(补充)theano的tensor库
- Theano学习系列(3):符号变量的方法之creat new tensor
- Theano学习系列(4):符号变量的方法之Tensor Reduction
- theano学习初步(二) 基础Tensor函数
- theano学习初步(二) 基础Tensor函数
- 基于theano的keras安装
- keras系列二 keras的大体结构介绍
- Keras - 一个基于 Theano 的深度学习 Python 库
- Keras: 基于Theano和TensorFlow的快速深度学习库
- Keras默认使用的后端模式(Theano/TensorFlow)设置
- 【Tensorflow keras】Keras:基于Theano和TensorFlow的深度学习库
- theano.tensor模块安排
- keras的GPU模式安装- theano 后端
- keras的backend 设置 tensorflow,theano
- keras的backend 设置 tensorflow,theano
- Keras 切换后端(Theano和TensorFlow)
- 【Keras】切换后端(Theano和TensorFlow)
- Keras 切换后端(Theano和TensorFlow)
- Linux命令大全----常用文件操作命令
- bat中调用另外一个bat
- java.lang.IllegalStateException此种错误为springmvc的requestmapping在一个controller中重复的原因
- iOS开发多线程篇—GCD介绍
- HashMap 原理浅析
- keras系列二(补充)theano的tensor库
- 剑指offer-面试题36-数组中的逆序对(O(n)复杂度)
- Leetcode - Palindrome Linked List
- Linux IP代理筛选系统(shell+proxy)
- html img Src base64 图片显示
- XP+IIS5.1+MVC4 发布网站
- 数学竖式排版中不为人知的技巧
- [从头学数学] 第203节 坐标系与参数方程
- FreeRTOS高级篇2---FreeRTOS任务创建分析