theano dimshuffle的使用说明

本文详细介绍了如何使用特定模式对张量进行维度变换,包括如何使用‘x’字符来进行广播操作,并提供了多个实例来帮助理解不同模式的效果。

Returns a view of this tensor with permuted dimensions. Typically the pattern will include the integers 0, 1, ... ndim-1, and any number of‘x’ characters in dimensions where this tensor should be broadcasted.

A few examples of patterns and their effect:

  • (‘x’) -> make a 0d (scalar) into a 1d vector
  • (0, 1) -> identity for 2d vectors
  • (1, 0) -> inverts the first and second dimensions
  • (‘x’, 0) -> make a row out of a 1d vector (N to 1xN)
  • (0, ‘x’) -> make a column out of a 1d vector (N to Nx1)
  • (2, 0, 1) -> AxBxC to CxAxB
  • (0, ‘x’, 1) -> AxB to Ax1xB
  • (1, ‘x’, 0) -> AxB to Bx1xA
  • (1,) -> This remove dimensions 0. It must be a broadcastable dimension (1xA to A)
变换的时候数字分别代表维度,x类似于reshpe中的-1参数,最后分配.
“class ResConv1DLayer(object): def __init__(self, rng, input, n_in=0, n_out=0, halfWinSize=0, dilation=1, activation=T.nnet.relu, mask=None): self.input = input self.n_in = n_in self.n_out = n_out self.halfWinSize = halfWinSize windowSize = 2*halfWinSize + 1 self.filter_size = windowSize self.dilation = (1, dilation) in4conv2D = input.dimshuffle(0, 1, 'x', 2) w_shp = (n_out, n_in, 1, windowSize) if activation == T.nnet.relu: W_values = np.asarray(rng.normal(scale=np.sqrt(2. / (n_in*windowSize + n_out)), size = w_shp), dtype=theano.config.floatX ) else: W_values = np.asarray( rng.uniform(low = - np.sqrt(6. / (n_in*windowSize + n_out)), high = np.sqrt(6. / (n_in*windowSize + n_out)), size = w_shp), dtype=theano.config.floatX ) if activation == T.nnet.sigmoid: W_values *= 4 self.W = theano.shared(value=W_values, name='ResConv1d_W', borrow=True) b_shp = (n_out,) self.b = theano.shared(np.asarray( rng.uniform(low=-.0, high=.0, size=b_shp), dtype=input.dtype), name ='ResConv1d_b', borrow=True) if dilation > 1: conv_out = T.nnet.conv2d(in4conv2D, self.W, filter_shape=w_shp, border_mode='half', filter_dilation=(1, dilation) ) else: conv_out = T.nnet.conv2d(in4conv2D, self.W, filter_shape=w_shp, border_mode='half') if activation is not None: conv_out_bias = activation(conv_out + self.b.dimshuffle('x', 0, 'x', 'x')) else: conv_out_bias = (conv_out + self.b.dimshuffle('x', 0, 'x', 'x')) out2 = conv_out_bias.dimshuffle(0, 1, 3, 2)[:, :, :, 0] if mask is not None: out2_sub = out2[:, :, :mask.shape[1] ] mask_new = mask.dimshuffle(0, 'x', 1) self.output = T.set_subtensor(out2_sub, T.mul(out2_sub, mask_new) ) else: self.output = out2 self.params=[self.W, self.b] self.paramL1 = abs(self.W).sum() + abs(self.b).sum() self.paramL2 = (self.W**2).sum() + (self.b**2).sum()”将这段theano代码转成torch的
03-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值