theano中的dimshuffle函数用于对张量的维度进行操作,可以增加维度,也可以交换维度,删除维度。

注意的是只有shared才能调用dimshuffle()

'x'表示增加一维,从0d scalar到1d vector

(0, 1)表示一个与原先相同的2D向量

(1, 0)表示将2D向量的两维交换

(‘x’, 0) 表示将一个1d vector变为一个1xN矩阵

(0, ‘x’)将一个1d vector变为一个Nx1矩阵

(2, 0, 1) -> AxBxC to CxAxB (2表示第三维也就是C,0表示第一维A,1表示第二维B)

(0, ‘x’, 1) -> AxB to Ax1xB 表示A,B顺序不变在中间增加一维

(1, ‘x’, 0) -> AxB to Bx1xA 同理自己理解一下

(1,) -> 删除维度0,(1xA to A)

写了个小程序来验证猜想

from __future__ import print_function
import theano
import numpy as np
def print_hline(file):
print('------------------------------------------',file=file,end='\r\n')
write_file=open('G:\data\dimshuffle_output.txt','wb')
v = theano.shared(np.arange(3))
# v.shape is a symbol expression, need theano.function or eval to compile it
print_hline(write_file)
v_disp = v.dimshuffle(0)
print('v.dimshuffle(0):',v_disp.eval(),file=write_file,end='\r\n')
print('v.dimshuffle(0).shape:',v_disp.shape.eval(),file=write_file,end='\r\n')
print_hline(write_file)
v_disp = v.dimshuffle('x', 0)
print("v.dimshuffle('x',0):",v_disp.eval(),file=write_file,end='\r\n')
print("v.dimshuffle('x',0).shape:",v_disp.shape.eval(),file=write_file,end='\r\n')
print_hline(write_file)
v_disp = v.dimshuffle(0,'x')
print("v.dimshuffle(0,'x'):",v_disp.eval(),file=write_file,end='\r\n')
print("v.dimshuffle(0,'x').shape:",v_disp.shape.eval(),file=write_file,end='\r\n')
print_hline(write_file)
v_disp = v.dimshuffle(0,'x','x')
print("v.dimshuffle(0,'x','x'):",v_disp.eval(),file=write_file,end='\r\n')
print("v.dimshuffle(0,'x','x').shape:",v_disp.shape.eval(),file=write_file,end='\r\n')
print_hline(write_file)
v_disp = v.dimshuffle('x',0,'x')
print("v.dimshuffle('x',0,'x'):",v_disp.eval(),file=write_file,end='\r\n')
print("v.dimshuffle('x',0,'x').shape:",v_disp.shape.eval(),file=write_file,end='\r\n')
print_hline(write_file)
v_disp = v.dimshuffle('x','x',0)
print("v.dimshuffle('x','x',0):",v_disp.eval(),file=write_file,end='\r\n')
print("v.dimshuffle('x','x',0).shape:",v_disp.shape.eval(),file=write_file,end='\r\n')
print_hline(write_file)
m = theano.shared(np.arange(6).reshape(2,3))
print("m:",m.eval(),file=write_file,end='\r\n')
print("m.shape:",m.shape.eval(),file=write_file,end='\r\n')
print_hline(write_file)
m_disp = m.dimshuffle(0,'x',1)
print("m.dimshuffle(0,'x',1):",m_disp.eval(),file=write_file,end='\r\n')
print("m.dimshuffle(0,'x',1).shape:",m_disp.shape.eval(),file=write_file,end='\r\n')
print_hline(write_file)
m_disp = m.dimshuffle('x',0,1)
print("m.dimshuffle('x',0,1):",m_disp.eval(),file=write_file,end='\r\n')
print("m.dimshuffle('x',0,1).shape:",m_disp.shape.eval(),file=write_file,end='\r\n')
print_hline(write_file)
m_disp = m.dimshuffle(0,1,'x')
print("m.dimshuffle(0,1,'x'):",m_disp.eval(),file=write_file,end='\r\n')
print("m.dimshuffle(0,1,'x').shape:",m_disp.shape.eval(),file=write_file,end='\r\n')
print_hline(write_file)
# amount to transpose
m_disp = m.dimshuffle(1,'x',0)
print("m.dimshuffle(1,'x',0):",m_disp.eval(),file=write_file,end='\r\n')
print("m.dimshuffle(1,'x',0).shape:",m_disp.shape.eval(),file=write_file,end='\r\n')
write_file.close()

首先定义了一个[0 1 2]的1D vector:v,v.dimshuffle(0)中的0表示第一维:3,也只有一维,所以不变。因为是1D的,所以shape只有(3,)

v.dimshuffle(0): [0 1 2]
v.dimshuffle(0).shape: [3]

v.dimshuffle('x',0)表示在第一维前加入一维,只要记住加了'x'就加了一维,所以大小变成了1x3

v.dimshuffle('x',0): [[0 1 2]]
v.dimshuffle('x',0).shape: [1 3]

剩下的同理可理解

v.dimshuffle(0,'x'): [[0]
[1]
[2]]
v.dimshuffle(0,'x').shape: [3 1]
v.dimshuffle(0,'x','x'): [[[0]]

 [[1]]

 [[2]]]
v.dimshuffle(0,'x','x').shape: [3 1 1]
v.dimshuffle('x',0,'x'): [[[0]
[1]
[2]]]
v.dimshuffle('x',0,'x').shape: [1 3 1]
v.dimshuffle('x','x',0): [[[0 1 2]]]
v.dimshuffle('x','x',0).shape: [1 1 3]

第二个例子,m是一个2x3矩阵

m: [[0 1 2]
[3 4 5]]
m.shape: [2 3]

先确定0,'x',1的维数,0对应第一维(2),1表示第二维(3),'x'表示新加入的维度(1)

所以结果维度是2x1x3

加括号的顺序按照从左到右(外->内)的顺序

1.先加最内层3,3表示括号内有3个数,因此是[0 1 2]和[3 4 5]

2.再加中间层1,1表示括号内只有一个匹配的"[]",因此是[[0 1 2]],[[3 4 5]]

3.最后加最外层2,2表示括号内有两个匹配的"[]"(只算最外层的匹配),于是最后结果是

[[[0 1 2]]

[[3 4 5]]]

m.dimshuffle(0,'x',1): [[[0 1 2]]

 [[3 4 5]]]
m.dimshuffle(0,'x',1).shape: [2 1 3]

剩下的同理可以理解

m.dimshuffle('x',0,1): [[[0 1 2]
[3 4 5]]]
m.dimshuffle('x',0,1).shape: [1 2 3]
m.dimshuffle(0,1,'x'): [[[0]
[1]
[2]] [[3]
[4]
[5]]]
m.dimshuffle(0,1,'x').shape: [2 3 1]
m.dimshuffle(1,'x',0): [[[0 3]]

 [[1 4]]

 [[2 5]]]
m.dimshuffle(1,'x',0).shape: [3 1 2]

theano中的dimshuffle的更多相关文章

  1. Theano入门笔记1:Theano中的Graph Structure

    译自:http://deeplearning.net/software/theano/extending/graphstructures.html#graphstructures 理解Theano计算 ...

  2. theano中的scan用法

    scan函数是theano中的循环函数,相当于for loop.在读别人的代码时第一次看到,有点迷糊,不知道输入.输出怎么定义,网上也很少有example,大多数都是相互转载同一篇.所以,还是要看官方 ...

  3. Theano中的导数

    计算梯度 现在让我们使用Theano来完成一个稍微复杂的任务:创建一个函数,该函数计算相对于其参数x的某个表达式y的导数.为此,我们将使用宏T.grad.例如,我们可以计算相对于的梯度 import ...

  4. theano中对图像进行convolution 运算

    (1) 定义计算过程中需要的symbolic expression """ 定义相关的symbolic experssion """ # c ...

  5. theano中的concolutional_mlp.py学习

    (1) evaluate _lenet5中的导入数据部分 # 导入数据集,该函数定义在logistic_sgd中,返回的是一个list datasets = load_data(dataset) # ...

  6. Theano2.1.21-基础知识之theano中多核的支持

    来自:http://deeplearning.net/software/theano/tutorial/multi_cores.html Multi cores support in Theano 一 ...

  7. theano中的logisticregression代码学习

    1 class LogisticRegression (object): 2 def __int__(self,...): 3 4 #定义一些与逻辑回归相关的各种函数 5 6 def method1( ...

  8. theano中tensor的构造方法

    import theano.tensor as T x = T.scalar('myvar') myvar = 256 print type(x),x,myvar 运行结果: <class 't ...

  9. Theano入门笔记2:scan函数等

    1.Theano中的scan函数 目前先弱弱的认为:相当于symbolic的for循环吧,或者说计算图上的for循环,也可以用来替代repeat-until. 与scan相比,scan_checkpo ...

随机推荐

  1. 自己用C语言写PIC32 serial bootloader

    了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 从15年12月份以来我 ...

  2. flex+java+blazeds 多通道好文

    http://www.cnblogs.com/noam/archive/2010/08/05/1793504.html blazeds, spring3整合实现RPC服务和消息服务 环境: MyEcl ...

  3. Programming Entity Framework 翻译(1)-目录

    1. Introducing the ADO.NET Entity Framework ado.net entity framework 介绍 1 The Entity Relationship Mo ...

  4. .NET (上传的)文件下载

    string strPath = “//UploadFiles/Equipment/QQ截图.png” // 文件有效地址 FileInfo fileInfo = new FileInfo(strPa ...

  5. CSS3基础03(3D②) 求粉丝

    3 D (3.1)rotateY 围绕着Y轴进行旋转 (1)正数是(站在右边推),负数是(站在左边推) (2.1)定义元素背过去是否可见 backface-visibility: visible|hi ...

  6. MIME Sniffing

    Abstract: The web.config file does not include the required header to mitigate MIME sniffing attacks ...

  7. 使用用Generic.xaml加载默认的主题资源

    把Resource嵌入到Generic.xaml文件中,并把该文件放到应用程序的Themes主题文件夹下面,这们Generic.xaml文件中的资源就可以被系统识别为默认主题一部分,从而进行使用. 为 ...

  8. js浏览器对象的属性和方法

    1.window对象 /*1.计算浏览器窗口大小*/ //不算滚动条: var width = window.innerWidth; var height = window.innerHeight; ...

  9. Eclipse取消设置项目默认空间

    分享一个小经验:       如果,在启动Eclipse时选中了Use this as the default and do not ask again 下次要启动时不会再次显示修改工作空间的选择, ...

  10. c#绘制表格

    //绘制表格 //定义绘制表格的参数 Pen _Pen = new Pen(Brushes.Black); // VBConversions Note: Initial value cannot be ...