本文整理汇总了Python中theano.sandbox.cuda.basic_ops.gpu_contiguous函数的典型用法代码示例。如果您正苦于以下问题:Python gpu_contiguous函数的具体用法?Python gpu_contiguous怎么用?Python gpu_contiguous使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了gpu_contiguous函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _deconv2d
def _deconv2d(X, w, subsample=(1, 1), border_mode=(0, 0), conv_mode='conv'):
"""
from Alec (https://github.com/Newmu/dcgan_code/blob/master/lib/ops.py)
sets up dummy convolutional forward pass and uses its grad as deconv
currently only tested/working with same padding
"""
img = gpu_contiguous(X)
kerns = gpu_contiguous(w)
out = gpu_alloc_empty(
img.shape[0],
kerns.shape[1],
img.shape[2]*subsample[0],
img.shape[3]*subsample[1]
)
desc = GpuDnnConvDesc(border_mode=border_mode, subsample=subsample,
conv_mode=conv_mode)
desc = desc(
out.shape,
kerns.shape
)
d_img = GpuDnnConvGradI()(kerns, img, out, desc)
return d_img
示例2: run_gradweight
def run_gradweight(self, inputs_shape, filters_shape, dCdH_shape,
subsample=(1, 1, 1)):
inputs_val = numpy.random.random(inputs_shape).astype('float32')
dCdH_val = numpy.random.random(dCdH_shape).astype('float32')
inputs = shared(inputs_val)
dCdH = shared(dCdH_val)
conv = theano.tensor.nnet.convGrad3D(V=inputs, dCdH=dCdH,
WShape=filters_shape,
d=subsample)
img = gpu_contiguous(inputs.dimshuffle(0, 4, 1, 2, 3))
topgrad = gpu_contiguous(dCdH.dimshuffle(0, 4, 1, 2, 3))
if (subsample == (1, 1, 1)):
conv_gemm = GpuCorr3dMM_gradWeights(subsample=subsample)(img,
topgrad)
else:
conv_gemm = GpuCorr3dMM_gradWeights(subsample=subsample)(
img, topgrad, shape=filters_shape[1:4])
conv_gemm = conv_gemm.dimshuffle(0, 2, 3, 4, 1)
f_ref = theano.function([], conv)
f = theano.function([], conv_gemm, mode=mode_with_gpu)
res_ref = f_ref()
res = f()
utt.assert_allclose(res_ref, res)
示例3: __init__
def __init__(self, numpy_rng, input, input_shape, filter_shape, poolsize, activation,
W=None, b=None, border_mode = 'valid', use_fast = False):
assert input_shape[1] == filter_shape[1]
self.input = input#.reshape(input_shape)
self.input_shape = input_shape
self.filter_shape = filter_shape
self.poolsize = poolsize
self.activation = activation
fan_in = numpy.prod(filter_shape[1:])
fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) / numpy.prod(poolsize))
if W is None: #initialize weights with random weights in range (-w_bound,w_bound)
W_bound = numpy.sqrt(6. / (fan_in + fan_out))
initial_W = numpy.asarray(
numpy_rng.uniform(low=-W_bound, high=W_bound,size=filter_shape),
dtype=theano.config.floatX)
if activation == T.nnet.sigmoid:
initial_W *= 4
W = theano.shared(value = initial_W, name = 'W')
self.W = W
if b is None: # the bias is a 1D tensor -- one bias per output feature map
b_values = numpy.zeros((filter_shape[0],), dtype=theano.config.floatX)
b = theano.shared(value=b_values, name='b')
self.b = b
#Will be used for computing momentum
self.delta_W = theano.shared(value = numpy.zeros(filter_shape,dtype=theano.config.floatX), name='delta_W')
self.delta_b = theano.shared(value = numpy.zeros_like(self.b.get_value(borrow=True),dtype=theano.config.floatX), name='delta_b')
if use_fast: #uses pylearn2 modules but it has got lot of limitations
input_shuffled = self.input.dimshuffle(1, 2, 3, 0) #rotating axes towards right
filters_shuffled = self.W.dimshuffle(1, 2, 3, 0) #rotating axes towards right
conv_op = FilterActs()
contiguous_input = gpu_contiguous(input_shuffled)
contiguous_filters = gpu_contiguous(filters_shuffled)
conv_out_shuffled = conv_op(contiguous_input, contiguous_filters)
y_out_shuffled = activation(conv_out_shuffled + self.b.dimshuffle(0, 'x', 'x', 'x'))
pool_op = MaxPool(ds=poolsize[0], stride=poolsize[0]) #only supports square window for pooling
pooled_out = pool_op(y_out_shuffled).dimshuffle(3, 0, 1, 2) # roating axes back
else: #uses theano modules, border_mode - ?
conv_out = conv.conv2d(input=self.input, filters=self.W,filter_shape=filter_shape,
image_shape=input_shape,border_mode = border_mode)
y_out = activation(conv_out + self.b.dimshuffle('x', 0, 'x', 'x'))
# downsample each feature map individually, using maxpooling
pooled_out = downsample.max_pool_2d(input=y_out, ds=poolsize, ignore_border=True)
self.output = pooled_out
#if flatten: #if final convolution layer we need to flatten
# self.output = self.output.flatten(2)
self.params = [self.W, self.b]
self.delta_params = [self.delta_W, self.delta_b]
示例4: local_gpu_fft_conv
def local_gpu_fft_conv(node):
"""
gpu_conv -> gpu_fft_conv_op
"""
if not isinstance(node.op, GpuConv):
return
if (node.op.border_mode=='full' and
node.op.subsample==(1,1)):
img, kern = node.inputs
img = gpu_contiguous(img)
kern = gpu_contiguous(kern)
gpu_fft_conv = GpuFFTConvOp(node.op.border_mode, check=node.op.verbose)
return [gpu_fft_conv(img,kern)]
if (config.GpuFFTConvOp.valid and
node.op.border_mode=='valid' and
node.op.subsample==(1,1) and
node.op.kshp and node.op.imshp):
kshp = node.op.kshp
ishp = node.op.imshp[1:]
pad_up = kshp[0]-1
pad_left = kshp[1]-1
size_height = ishp[0]-kshp[0]+1
size_width = ishp[1]-kshp[1]+1
img = gpu_contiguous(node.inputs[0])
kern = gpu_contiguous(node.inputs[1])
gpu_fft_conv = GpuFFTConvOp("full", check=node.op.verbose)(img,kern)[:,:,pad_up:pad_up+size_height,pad_left:pad_left+size_width]
gpu_fft_conv = cuda.gpu_from_host(gpu_fft_conv)
return [gpu_fft_conv]
示例5: __init__
def __init__(self, numpy_rng=None, input = None, filter_shape=(2, 1, 5, 5),
poolsize=(1, 1), activation=T.nnet.sigmoid,
flatten = False, use_fast = False):
self.type = 'conv'
self.input = input
self.filter_shape = filter_shape
self.poolsize = poolsize
self.activation = activation
self.flatten = flatten
fan_in = numpy.prod(filter_shape[1:])
fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) /
numpy.prod(poolsize))
# initialize weights with random weights
W_bound = numpy.sqrt(6. / (fan_in + fan_out))
initial_W = numpy.asarray( numpy_rng.uniform(
low=-W_bound, high=W_bound,
size=filter_shape),
dtype=theano.config.floatX)
if activation == T.nnet.sigmoid:
initial_W *= 4
W = theano.shared(value = initial_W, name = 'W')
self.W = W
# the bias is a 1D tensor -- one bias per output feature map
b_values = numpy.zeros((filter_shape[0],), dtype=theano.config.floatX)
self.b = theano.shared(value=b_values, name='b')
# convolve input feature maps with filters
if use_fast:
from theano.sandbox.cuda.basic_ops import gpu_contiguous
from pylearn2.sandbox.cuda_convnet.filter_acts import FilterActs
from pylearn2.sandbox.cuda_convnet.pool import MaxPool
input_shuffled = self.input.dimshuffle(1, 2, 3, 0) # bc01 to c01b
filters_shuffled = self.W.dimshuffle(1, 2, 3, 0) # bc01 to c01b
conv_op = FilterActs()
contiguous_input = gpu_contiguous(input_shuffled)
contiguous_filters = gpu_contiguous(filters_shuffled)
conv_out_shuffled = conv_op(contiguous_input, contiguous_filters)
y_out_shuffled = activation(conv_out_shuffled + self.b.dimshuffle(0, 'x', 'x', 'x'))
pool_op = MaxPool(ds=poolsize[0], stride=poolsize[0])
self.output = pool_op(y_out_shuffled).dimshuffle(3, 0, 1, 2)
else:
conv_out = conv.conv2d(input=self.input, filters=self.W,
filter_shape=filter_shape)
y_out = activation(conv_out + self.b.dimshuffle('x', 0, 'x', 'x'))
# downsample each feature map individually, using maxpooling
self.output = downsample.max_pool_2d(input=y_out,
ds=poolsize, ignore_border=True)
if self.flatten:
self.output = self.output.flatten(2)
self.params = [self.W, self.b]
示例6: get_output_for
def get_output_for(self, input, *args, **kwargs):
if self.dimshuffle:
filters = self.W.dimshuffle(1, 2, 3, 0) # bc01 to c01b
input = input.dimshuffle(1, 2, 3, 0) # bc01 to c01b
else:
filters = self.W
if self.flip_filters:
filters = filters[:, ::-1, ::-1, :] # flip width, height
contiguous_filters = gpu_contiguous(filters)
contiguous_input = gpu_contiguous(input)
conved = self.filter_acts_op(contiguous_input, contiguous_filters)
if self.b is not None:
if self.untie_biases:
biases = self.b.dimshuffle(0, 1, 2, 'x') # c01 to c01b
else:
biases = self.b.dimshuffle(0, 'x', 'x', 'x') # c to c01b
conved += biases
conved = self.nonlinearity(conved)
if self.dimshuffle:
return conved.dimshuffle(3, 0, 1, 2) # c01b to bc01
else:
return conved
示例7: output
def output(self, input=None, dropout_active=True, *args, **kwargs):
if input == None:
input = self.input_layer.output(dropout_active=dropout_active, *args, **kwargs)
if dropout_active and (self.dropout > 0.):
retain_prob = 1 - self.dropout
mask = layers.srng.binomial(input.shape, p=retain_prob, dtype='int32').astype('float32')
# apply the input mask and rescale the input accordingly. By doing this it's no longer necessary to rescale the weights at test time.
input = input / retain_prob * mask
# pad input so the valid convolution amounts to a circular one.
# we need to copy (filter_size - stride) values from one side to the other
input_padded = T.zeros((input.shape[0], input.shape[1] + self.filter_size - self.stride, input.shape[2], input.shape[3]))
input_padded = T.set_subtensor(input_padded[:, :input.shape[1], :, :], input)
input_padded = T.set_subtensor(input_padded[:, input.shape[1]:, :, :], input[:, :self.filter_size - self.stride, :, :])
contiguous_input = gpu_contiguous(input_padded)
contiguous_filters = gpu_contiguous(self.W)
conved = self.filter_acts_op(contiguous_input, contiguous_filters)
if self.untie_biases:
conved += self.b.dimshuffle(0, 1, 2, 'x')
else:
conved += self.b.dimshuffle(0, 'x', 'x', 'x')
return self.nonlinearity(conved)
示例8: fprop
def fprop(self, input):
# we reduce the precision of parameters for the computations
self.w_comp = apply_format(self.format, self.W, self.comp_precision, self.w_range)
self.b_comp = apply_format(self.format, self.b, self.comp_precision, self.b_range)
input = input.reshape(self.image_shape)
# convolution
input_shuffled = input.dimshuffle(1, 2, 3, 0) # bc01 to c01b
filters_shuffled = self.w_comp.dimshuffle(1, 2, 3, 0) *self.scale # bc01 to c01b
conv_op = FilterActs(stride=self.filter_stride, partial_sum=self.partial_sum,pad = self.zero_pad)
contiguous_input = gpu_contiguous(input_shuffled)
contiguous_filters = gpu_contiguous(filters_shuffled)
conv_out_shuffled = conv_op(contiguous_input, contiguous_filters)
# downsample each feature map individually, using maxpooling
# pooled_out = downsample.max_pool_2d(input=conv_out,
# ds=poolsize, ignore_border=True)
pool_op = MaxPool(ds=self.pool_shape, stride=self.pool_stride)
pooled_out_shuffled = pool_op(conv_out_shuffled)
pooled_out = pooled_out_shuffled.dimshuffle(3, 0, 1, 2) # c01b to bc01
# bias
pooled_out = apply_format(self.format, pooled_out + self.b_comp.dimshuffle('x', 0, 'x', 'x')*self.scale, self.comp_precision, self.z_range)
# activation
pooled_out = self.activation(pooled_out)
pooled_out = apply_format(self.format, pooled_out.flatten(2), self.comp_precision, self.y_range)
return pooled_out
示例9: build_graph
def build_graph(self, state_below):
filters = self.filters
nfilters = self.nfilters
b = self.b
border_mode = self.border_mode
# activ = self.activ
batch_size = state_below.shape[0]
out_size = DeConvNet.infer_size(state_below.shape[1:3],
filters.shape[2:], self.stride,
self.border_mode)
out_shape = [batch_size, nfilters, out_size[0], out_size[1]]
state_below = state_below.dimshuffle(0, 3, 1, 2)
filters = gpu_contiguous(filters)
state_below = gpu_contiguous(state_below)
out_shape = tensor.stack(out_shape)
desc = GpuDnnConvDesc(border_mode=border_mode, subsample=self.stride,
conv_mode='conv')(out_shape, filters.shape)
pred = GpuDnnConvGradI()(
filters, state_below, gpu_alloc_empty(*out_shape), desc)
pred += b.dimshuffle('x', 0, 'x', 'x')
pred = pred.dimshuffle(0, 2, 3, 1)
return eval(self.activ)(pred)
示例10: f_conv
def f_conv(self, x, spec, in_dim, weight_name):
layer_type, dims = spec
num_filters = dims[0]
filter_size = (dims[1], dims[1])
stride = (dims[2], dims[2])
bm = 'full' if 'convf' in layer_type else 'valid'
num_channels = in_dim[0]
W = self.weight(self.rand_init_conv(
(num_filters, num_channels) + filter_size), weight_name)
if stride != (1, 1):
f = GpuCorrMM(subsample=stride, border_mode=bm, pad=(0, 0))
y = f(gpu_contiguous(x), gpu_contiguous(W))
else:
assert self.p.batch_size == self.p.valid_batch_size
y = conv2d(x, W, image_shape=(2*self.p.batch_size, ) + in_dim,
filter_shape=((num_filters, num_channels) +
filter_size), border_mode=bm)
output_size = ((num_filters,) +
ConvOp.getOutputShape(in_dim[1:], filter_size,
stride, bm))
return y, output_size
示例11: __init__
def __init__(self, rngs, input_layer, Lshape, traits, activation):
super(ConvLayer, self).__init__(input_layer, traits, "Conv")
self.rng = rngs[0]
self.l2decay = traits['l2decay']
filter_shape = Lshape[1]
# The number of input channels must match number of filter channels
assert Lshape[0][1] == filter_shape[1]
self.pad = traits['padding']
self.W = NNl.gen_weights(self.rng, filter_shape, 0, traits['initW'])
# convolve input feature maps with filters
# Using Alex K.'s fast CUDA conv, courtesy of S. Dieleman
self.x = self.input_layer.output(False)
conv_op = FilterActs(pad=self.pad, partial_sum=1)
input_shuffled = (self.x).dimshuffle(1, 2, 3, 0) # bc01 to c01b
filters_shuffled = (self.W).dimshuffle(1, 2, 3, 0) # bc01 to c01b
contiguous_input = gpu_contiguous(input_shuffled)
contiguous_filters = gpu_contiguous(filters_shuffled)
out_shuffled = conv_op(contiguous_input, contiguous_filters)
self.conv_out = out_shuffled.dimshuffle(3, 0, 1, 2) # c01b to bc01
# store parameters of this layer
self.params = [self.W]
示例12: run_gradinput
def run_gradinput(self, inputs_shape, filters_shape,
subsample=(1, 1, 1)):
inputs_val = numpy.random.random(inputs_shape).astype('float32')
filters_val = numpy.random.random(filters_shape).astype('float32')
inputs = shared(inputs_val)
filters = shared(filters_val)
bias = shared(numpy.zeros(filters_shape[4]).astype('float32'))
conv = theano.tensor.nnet.convTransp3D(W=filters,
b=bias,
d=subsample,
H=inputs)
f_ref = theano.function([], conv)
res_ref = f_ref()
# Get bottom shape using convTransp3D
bottom_shape = res_ref.shape
bottom_val = numpy.random.random(bottom_shape).astype('float32')
bottom = shared(bottom_val)
weight = gpu_contiguous(filters.dimshuffle(0, 4, 1, 2, 3))
top = gpu_contiguous(inputs.dimshuffle(0, 4, 1, 2, 3))
if (subsample == (1, 1, 1)):
conv_gemm = GpuCorr3dMM_gradInputs(subsample=subsample)(
kern=weight, topgrad=top)
else:
conv_gemm = GpuCorr3dMM_gradInputs(subsample=subsample)(
kern=weight, topgrad=top,
shape=bottom.shape[1:4])
conv_gemm = conv_gemm.dimshuffle(0, 2, 3, 4, 1)
f = theano.function([], conv_gemm, mode=mode_with_gpu)
res = f()
utt.assert_allclose(res_ref, res)
示例13: get_output_for
def get_output_for(self, input, **kwargs):
if self.dimshuffle:
filters = self.W.dimshuffle(1, 2, 3, 0) # bc01 to c01b
input = input.dimshuffle(1, 2, 3, 0) # bc01 to c01b
else:
filters = self.W
if self.flip_filters:
filters = filters[:, ::-1, ::-1, :] # flip top-down, left-right
contiguous_filters = gpu_contiguous(filters)
contiguous_input = gpu_contiguous(input)
conved = self.filter_acts_op(contiguous_input, contiguous_filters)
if self.stride != 1:
# cuda-convnet calculates a non-standard strided output shape,
# so we need to truncate the output in this case
pad = self.pad if isinstance(self.pad, tuple) else (self.pad,) * 2
true_rows = conv_output_length(input.shape[1], self.filter_size[0], self.stride[0], pad[0])
true_columns = conv_output_length(input.shape[2], self.filter_size[1], self.stride[1], pad[1])
conved = conved[:, :true_rows, :true_columns, :]
if self.b is not None:
if self.untie_biases:
biases = self.b.dimshuffle(0, 1, 2, "x") # c01 to c01b
else:
biases = self.b.dimshuffle(0, "x", "x", "x") # c to c01b
conved += biases
conved = self.nonlinearity(conved)
if self.dimshuffle:
return conved.dimshuffle(3, 0, 1, 2) # c01b to bc01
else:
return conved
示例14: output
def output(self, input=None, dropout_active=True, *args, **kwargs):
if input == None:
print "if input is none we do sth in cc_layers"
input = self.input_layer.output(dropout_active=dropout_active, *args, **kwargs)
print type(input)
if dropout_active and (self.dropout > 0.):
print "input is not none"
retain_prob = 1 - self.dropout
mask = layers.srng.binomial(input.shape, p=retain_prob, dtype='int32').astype('float32')
# apply the input mask and rescale the input accordingly. By doing this it's no longer necessary to rescale the weights at test time.
input = input / retain_prob * mask
#print "input is",input
print type(input)
print "we got to contigous input thingy"
contiguous_input = gpu_contiguous(input)
contiguous_filters = gpu_contiguous(self.W)
conved = self.filter_acts_op(contiguous_input, contiguous_filters)
print "jsghksfjhgjfldgjhdflghj"
if self.untie_biases:
conved += self.b.dimshuffle(0, 1, 2, 'x')
else:
conved += self.b.dimshuffle(0, 'x', 'x', 'x')
return self.nonlinearity(conved)
示例15: deconv
def deconv(self, X, subsample=(2, 2), border_mode=(2, 2), conv_mode='conv', atype='sigmoid'):
"""
sets up dummy convolutional forward pass and uses its grad as deconv
currently only tested/working with same padding
"""
#Always return a c contiguous output.
#Copy the input only if it is not already c contiguous.
img = gpu_contiguous(X)
kerns = gpu_contiguous(self.W)
#Implement Alloc on the gpu, but without initializing memory.
gpu_alloc_img_shape = gpu_alloc_empty(img.shape[0], kerns.shape[1], \
img.shape[2]*subsample[0], img.shape[3]*subsample[1]).shape
#This Op builds a convolution descriptor for use in the other convolution operations.
desc = GpuDnnConvDesc(border_mode=border_mode, subsample=subsample,
conv_mode=conv_mode)(gpu_alloc_img_shape, kerns.shape)
out = gpu_alloc_empty(img.shape[0], kerns.shape[1], img.shape[2]*subsample[0],\
img.shape[3]*subsample[1])
#The convolution gradient with respect to the inputs.
d_img = GpuDnnConvGradI()(kerns, img, out, desc)
return activation_fn_th(d_img + self.b.dimshuffle('x', 0, 'x', 'x'), atype=atype)