def make_node(self, kern, topgrad, shape=None): kern = as_cuda_ndarray_variable(kern) topgrad = as_cuda_ndarray_variable(topgrad) if kern.type.ndim != 4: raise TypeError('kern must be 4D tensor') if topgrad.type.ndim != 4: raise TypeError('topgrad must be 4D tensor') if shape is None: if self.subsample != (1, 1): raise ValueError('shape must be given if subsample != (1, 1)') height_width = [] else: height_width = [shape[0], shape[1]] assert shape[0].ndim == 0 assert shape[1].ndim == 0 broadcastable = [topgrad.type.broadcastable[0], kern.type.broadcastable[1], False, False] return Apply(self, [kern, topgrad] + height_width, [CudaNdarrayType(broadcastable)()])
def make_node(self, dCdy, x, a, b, l, s): for input, ndim in ((dCdy, 2 + len(self.patch_shape)), (x, 2 + len(self.patch_shape)), (a, 2), (b, 2), (l, 2), (s, 2)): if not input.type.ndim == ndim: raise TypeError() dCdy, x, a, b, l, s = tuple(map(gpu_contiguous, (dCdy, x, a, b, l, s))) inputs = list(map(as_cuda_ndarray_variable, (dCdy, x, a, b, l, s))) # we could return the much smaller dCdl, dCds but that # gives us very little room to parallelize (e.g. with batch # size 100 and 3 spatial dimensions we have only 600 # independently computable output elements). output_type = CudaNdarrayType( broadcastable=list(inputs[0].type.broadcastable) + [False], dtype=inputs[0].type.dtype) dydl = output_type() dyds = output_type() return Apply(self, inputs, [dydl, dyds])
def make_node(self, pvals, unis): ctx_name = infer_context_name(pvals, unis) pvals = as_gpuarray_variable(pvals, ctx_name) unis = as_gpuarray_variable(unis, ctx_name) assert pvals.dtype in ["float32", "float16", "float64"] assert unis.dtype in ["float32", "float16", "float64"] if pvals.ndim != 2: raise NotImplementedError("pvals ndim should be 2", pvals.ndim) if unis.ndim != 1: raise NotImplementedError("unis ndim should be 1", unis.ndim) if self.odtype == "auto": odtype = pvals.dtype else: odtype = self.odtype br = (pvals.broadcastable[1], pvals.broadcastable[0]) out = GpuArrayType(broadcastable=br, dtype=odtype, context_name=ctx_name)() return Apply(self, [pvals, unis], [out])
def make_node(self, ten4, neib_shape, neib_step=None): ten4 = as_gpuarray_variable(ten4, infer_context_name(ten4)) neib_shape = T.as_tensor_variable(neib_shape) if neib_step is None: neib_step = neib_shape else: neib_step = T.as_tensor_variable(neib_step) assert ten4.ndim == 4 assert neib_shape.ndim == 1 assert neib_step.ndim == 1 assert neib_shape.dtype in T.integer_dtypes assert neib_step.dtype in T.integer_dtypes return Apply(self, [ten4, neib_shape, neib_step], [ GpuArrayType(broadcastable=(False, False), dtype=ten4.type.dtype, context_name=ten4.type.context_name)() ])
def make_node(self, inp, inp_grad, out, desc): inp = as_cuda_ndarray_variable(inp) if inp.type.ndim != 4: raise TypeError('inp must be 4D tensor') inp_grad = as_cuda_ndarray_variable(inp_grad) if inp_grad.type.ndim != 4: raise TypeError('inp_grad must be 4D tensor') out = as_cuda_ndarray_variable(out) if out.type.ndim != 4: raise TypeError('out must be 4D tensor') if not isinstance(desc.type, CDataType) \ or desc.type.ctype != 'cudnnPoolingDescriptor_t': raise TypeError('desc must be cudnnPoolingDescriptor_t') return Apply(self, [inp, inp_grad, out, desc], [inp.type()])
def make_node(self, kern, topgrad, output, desc, alpha=None, beta=None): kern = as_cuda_ndarray_variable(kern) topgrad = as_cuda_ndarray_variable(topgrad) output = as_cuda_ndarray_variable(output) if kern.type.ndim != 5: raise TypeError('kern must be 5D tensor') if topgrad.type.ndim != 5: raise TypeError('topgrad must be 5D tensor') if output.type.ndim != 5: raise TypeError('output must be 5D tensor') if not isinstance(desc.type, CDataType) \ or desc.type.ctype != 'cudnnConvolutionDescriptor_t': raise TypeError('desc must be cudnnConvolutionDescriptor_t') alpha = ensure_float(alpha, _one, 'alpha') beta = ensure_float(beta, _zero, 'beta') return Apply(self, [kern, topgrad, output, desc, alpha, beta], [output.type()])
def make_node(self, img, topgrad, shape=None): img = as_cuda_ndarray_variable(img) topgrad = as_cuda_ndarray_variable(topgrad) if img.type.ndim != 4: raise TypeError('img must be 4D tensor') if topgrad.type.ndim != 4: raise TypeError('topgrad must be 4D tensor') if shape is None: if self.subsample != (1, 1) or self.border_mode == "half": raise ValueError('shape must be given if subsample != (1, 1)' ' or border_mode == "half"') height_width = [] else: height_width = [shape[0], shape[1]] assert shape[0].ndim == 0 assert shape[1].ndim == 0 broadcastable = [topgrad.type.broadcastable[1], img.type.broadcastable[1], False, False] return Apply(self, [img, topgrad] + height_width, [CudaNdarrayType(broadcastable)()])
def make_node(self, ten4, neib_shape, neib_step=None): """ :param neib_step: (dx,dy) where dx is the number of rows to skip between patch and dy is the number of columns. When None, this is the same as neib_shape(patch are disjoint) """ ten4 = T.as_tensor_variable(ten4) neib_shape = T.as_tensor_variable(neib_shape) if neib_step is None: neib_step = neib_shape else: neib_step = T.as_tensor_variable(neib_step) assert ten4.ndim == 4 assert neib_shape.ndim == 1 assert neib_step.ndim == 1 return Apply(self, [ten4, neib_shape, neib_step], [T.matrix(dtype=ten4.type.dtype)])
def make_node(self, img, topgrad, shape=None): img = as_tensor_variable(img) topgrad = as_tensor_variable(topgrad) if img.type.ndim != 4: raise TypeError('img must be 4D tensor') if topgrad.type.ndim != 4: raise TypeError('topgrad must be 4D tensor') if self.subsample != (1, 1) or self.border_mode == "half": if shape is None: raise ValueError('shape must be given if subsample != (1, 1)' ' or border_mode == "half"') height_width = [as_tensor_variable(shape[0]).astype('int64'), as_tensor_variable(shape[1]).astype('int64')] else: height_width = [] broadcastable = [topgrad.type.broadcastable[1], img.type.broadcastable[1], False, False] dtype = img.type.dtype return Apply(self, [img, topgrad] + height_width, [TensorType(dtype, broadcastable)()])
def make_node( self, x, scale, bias, epsilon=1e-4, running_average_factor=0.1, running_mean=None, running_var=None, ): x = as_tensor_variable(x) scale = as_tensor_variable(scale) bias = as_tensor_variable(bias) epsilon = as_tensor_variable(epsilon) running_average_factor = as_tensor_variable(running_average_factor) if running_mean is not None: running_mean = as_tensor_variable(running_mean) if running_var is not None: running_var = as_tensor_variable(running_var) assert x.ndim == scale.ndim == bias.ndim assert (running_mean is None and running_var is None) or ( running_mean is not None and running_var is not None ) assert running_mean is None or running_mean.ndim == x.ndim assert running_var is None or running_var.ndim == x.ndim # Upcast to common dtype on the non-scalar # Keep as is dtype of scalar (epsilon and running_average_factor) if running_mean: x, scale, bias, running_mean, running_var = as_common_dtype( x, scale, bias, running_mean, running_var ) else: x, scale, bias = as_common_dtype(x, scale, bias) inputs = [x, scale, bias, epsilon, running_average_factor] output_types = [x.type(), scale.type(), scale.type()] if running_mean is not None and running_var is not None: inputs.append(running_mean) inputs.append(running_var) output_types.append(scale.type()) output_types.append(scale.type()) return Apply(self, inputs, output_types)
def make_node(self, ten4, neib_shape, neib_step=None): """ :param ten4: a list of lists of images ten4 is of shape (list 1 dim, list 2 dim, row, col) :param neib_shape: (r,c) where r is the height of the neighborhood in rows and c is the width of the neighborhood in columns :param neib_step: (dr,dc) where dr is the number of rows to skip between patch and dc is the number of columns. When None, this is the same as neib_shape(patch are disjoint) output: a 2D matrix, written using the following pattern idx = 0 for i in xrange(list 1 dim) for j in xrange(list 2 dim) for k in <image column coordinates> for l in <image row coordinates> output[idx,:] = flattened version of ten4[i,j,l:l+r,k:k+c] idx += 1 (note: the op isn't necessarily implemented internally with these for loops, they're just the easiest way to describe the output pattern) """ ten4 = T.as_tensor_variable(ten4) neib_shape = T.as_tensor_variable(neib_shape) if neib_step is None: neib_step = neib_shape else: neib_step = T.as_tensor_variable(neib_step) assert ten4.ndim == 4 assert neib_shape.ndim == 1 assert neib_step.ndim == 1 return Apply(self, [ten4, neib_shape, neib_step], [T.matrix(dtype=ten4.type.dtype)])
def make_node(self, img, topgrad, shape=None): img = as_tensor_variable(img) topgrad = as_tensor_variable(topgrad) img, topgrad = self.as_common_dtype(img, topgrad) if img.type.ndim != 4: raise TypeError("img must be 4D tensor") if topgrad.type.ndim != 4: raise TypeError("topgrad must be 4D tensor") if shape is None: if self.subsample != (1, 1) or self.border_mode == "half": raise ValueError( "shape must be given if subsample != (1, 1)" ' or border_mode == "half"' ) height_width = [] else: height_width = [ as_tensor_variable(shape[0]).astype("int64"), as_tensor_variable(shape[1]).astype("int64"), ] if self.unshared is True: broadcastable = [ topgrad.type.broadcastable[1], False, False, img.type.broadcastable[1], False, False, ] else: broadcastable = [ topgrad.type.broadcastable[1], img.type.broadcastable[1], False, False, ] dtype = img.type.dtype return Apply( self, [img, topgrad] + height_width, [TensorType(dtype, broadcastable)()] )
def make_node(self, pvals, unis): assert unis.dtype == pvals.dtype assert pvals.dtype in ['float32', 'float16', 'float64'] ctx_name = infer_context_name(pvals, unis) pvals = as_gpuarray_variable(pvals, ctx_name) unis = as_gpuarray_variable(unis, ctx_name) if pvals.ndim != 2: raise NotImplementedError('pvals ndim should be 2', pvals.ndim) if unis.ndim != 1: raise NotImplementedError('unis ndim should be 1', unis.ndim) if self.odtype == 'auto': odtype = pvals.dtype else: odtype = self.odtype br = (pvals.broadcastable[1], pvals.broadcastable[0]) out = GpuArrayType(broadcastable=br, dtype=odtype, context_name=ctx_name)() return Apply(self, [pvals, unis], [out])
def make_node(self, img, kern): img = as_tensor_variable(img) kern = as_tensor_variable(kern) img, kern = self.as_common_dtype(img, kern) if img.type.ndim != 4: raise TypeError("img must be 4D tensor") if self.unshared is True: if kern.type.ndim != 6: raise TypeError("kern must be 6D tensor") else: if kern.type.ndim != 4: raise TypeError("kern must be 4D tensor") broadcastable = [ img.type.broadcastable[0], kern.type.broadcastable[0], False, False, ] dtype = img.type.dtype return Apply(self, [img, kern], [TensorType(dtype, broadcastable)()])
def make_node(self, kern, topgrad, desc, h, w): kern = as_cuda_ndarray_variable(kern) topgrad = as_cuda_ndarray_variable(topgrad) if kern.type.ndim != 4: raise TypeError('kern must be 4D tensor') if topgrad.type.ndim != 4: raise TypeError('topgrad must be 4D tensor') if not isinstance(desc.type, CDataType) \ or desc.type.ctype != 'cudnnConvolutionDescriptor_t': raise TypeError('desc must be cudnnConvolutionDescriptor_t') h = as_scalar(h) w = as_scalar(w) broadcastable = [topgrad.type.broadcastable[0], kern.type.broadcastable[1], False, False] return Apply(self, [kern, topgrad, desc, h, w], [CudaNdarrayType(broadcastable)()])
def make_node(self, kern, topgrad, shape=None): kern = as_tensor_variable(kern) topgrad = as_tensor_variable(topgrad) kern, topgrad = self.as_common_dtype(kern, topgrad) if kern.type.ndim != 4: raise TypeError('kern must be 4D tensor') if topgrad.type.ndim != 4: raise TypeError('topgrad must be 4D tensor') if shape is None: if self.subsample != (1, 1): raise ValueError('shape must be given if subsample != (1, 1)') height_width = [] else: height_width = [as_tensor_variable(shape[0]).astype('int64'), as_tensor_variable(shape[1]).astype('int64')] broadcastable = [topgrad.type.broadcastable[0], kern.type.broadcastable[1], False, False] dtype = kern.type.dtype return Apply(self, [kern, topgrad] + height_width, [TensorType(dtype, broadcastable)()])
def make_node(self, o, x, y, xIdx, yIdx, alpha=None): """ Compute the dot product of the specified pieces of vectors and matrices. Parameters ---------- var: shape, comment o: (xBlocks, yBlocks, xSize, ySize) x: (batch, xWin, xSize) y: (batch, yWin, ySize) xIdx: (batch, iWin), indexes of the x blocks yIdx: (batch, oWin), indexes of the y blocks returns (xBlocks, yBlocks, xSize, ySize), outer(x[i], y[j]) + o[i, j] Notation -------- - `batch` is the number of examples in a minibatch (batch size). - `xBlocks` is the total number of blocks in x. - `xSize` is the size of each of these x blocks. - `xWin` is the number of blocks that will be used as x. Which blocks will be used is specified in `xIdx`. - `yBlocks` is the number or possible y blocks. - `ySize` is the size of each of these y blocks. - `yWin` is the number of y blocks that will actually be computed. Which blocks will be computed is specified in `yIdx`. """ one = tensor.constant(numpy.asarray(1.0, dtype='float32')) o = theano.tensor.as_tensor_variable(o) x = theano.tensor.as_tensor_variable(x) y = theano.tensor.as_tensor_variable(y) if alpha is None: alpha = one output = o.type.__class__(dtype=o.type.dtype, broadcastable=(False, ) * o.ndim)() return Apply(self, [o, x, y, xIdx, yIdx, alpha], [output])
def make_node(self, inp, out, out_grad, desc): inp = as_gpuarray_variable(inp) out_grad = as_gpuarray_variable(out_grad) out = as_gpuarray_variable(out) if desc.owner is not None: nd = desc.owner.op.get_ndim() + 2 if inp.type.ndim != nd: raise TypeError('inp must be %dD tensor' % (nd, )) if out_grad.type.ndim != nd: raise TypeError('out_grad must be %dD tensor' % (nd, )) if out.type.ndim != nd: raise TypeError('out must be %dD tensor' % (nd, )) if (not isinstance(desc.type, CDataType) or desc.type.ctype != 'cudnnPoolingDescriptor_t'): raise TypeError('desc must be cudnnPoolingDescriptor_t') return Apply(self, [inp, out, out_grad, desc], [inp.type()])
def make_node(self, kern, topgrad, shape=None): kern = as_tensor_variable(kern) topgrad = as_tensor_variable(topgrad) kern, topgrad = self.as_common_dtype(kern, topgrad) if kern.type.ndim != 5: raise TypeError("kern must be 5D tensor") if topgrad.type.ndim != 5: raise TypeError("topgrad must be 5D tensor") if shape is None: if self.subsample != (1, 1, 1): raise ValueError( "shape must be given if subsample != (1, 1, 1)") height_width_depth = [] else: height_width_depth = [ as_tensor_variable(shape[0]).astype("int64"), as_tensor_variable(shape[1]).astype("int64"), as_tensor_variable(shape[2]).astype("int64"), ] if self.num_groups > 1: broadcastable = [ topgrad.type.broadcastable[0], False, False, False, False ] else: broadcastable = [ topgrad.type.broadcastable[0], kern.type.broadcastable[1], False, False, False, ] dtype = kern.type.dtype return Apply( self, [kern, topgrad] + height_width_depth, [TensorType(dtype, broadcastable)()], )
def make_node(self, x, scale, bias, estimated_mean, estimated_variance, epsilon=1e-4): x = as_tensor_variable(x) scale = as_tensor_variable(scale) bias = as_tensor_variable(bias) estimated_mean = as_tensor_variable(estimated_mean) estimated_variance = as_tensor_variable(estimated_variance) epsilon = as_tensor_variable(epsilon) # Upcast to common dtype on the non-scalar # Keep as is dtype of scalar (epsilon) x, scale, bias, estimated_mean, estimated_variance = as_common_dtype( x, scale, bias, estimated_mean, estimated_variance) assert x.ndim == scale.ndim == bias.ndim == estimated_mean.ndim == estimated_variance.ndim return Apply( self, [x, scale, bias, estimated_mean, estimated_variance, epsilon], [x.type()])
def make_node(self, inp, eval_point, ws, stride=None, pad=None): ctx_name = infer_context_name(inp) nd = self.ndim inp = as_gpuarray_variable(inp, ctx_name) assert inp.ndim == nd + 2 eval_point = as_gpuarray_variable(eval_point, ctx_name) assert eval_point.ndim == nd + 2 if stride is None: stride = ws if pad is None: pad = (0, ) * nd elif isinstance(pad, (tuple, list)): if max(pad) != 0 and not self.ignore_border: raise ValueError("Padding works only with ignore_border=True") if isinstance(ws, (tuple, list)): if any(pad[i] >= ws[i] for i in range(nd)): raise ValueError("Padding must be smaller than strides") ws = as_tensor_variable(ws) stride = as_tensor_variable(stride) pad = as_tensor_variable(pad) assert ws.ndim == stride.ndim and ws.ndim == pad.ndim assert ws.ndim == 1 if ws.dtype not in theano.tensor.int_dtypes: raise TypeError("Window shape parameters must be ints.") if stride.dtype not in theano.tensor.int_dtypes: raise TypeError("Stride parameters must be ints.") if pad.dtype not in theano.tensor.int_dtypes: raise TypeError("Padding parameters must be ints.") ws = theano.tensor.cast(ws, "int64") stride = theano.tensor.cast(stride, "int64") pad = theano.tensor.cast(pad, "int64") return Apply(self, [inp, eval_point, ws, stride, pad], [eval_point.type()])
def make_node(self, maintainance_op, V, U, UinvT, Q): maintainance_op = as_tensor_variable(maintainance_op) V = as_tensor_variable(V) U = as_tensor_variable(U) UinvT = as_tensor_variable(UinvT) Q = as_tensor_variable(Q) params = [maintainance_op, V, U, UinvT, Q] # make sure parameters are either all of dtype float32 or all of dtype float64 (except for Y_indexes which are integers) elem_type = V.dtype if elem_type != "float32" and elem_type != "float64": raise TypeError( "Parameter V must have dtype of float32 or float64") check_tensor_variables_ndim_and_dtype(0, "int32", ["maintainance_op"], locals()) check_tensor_variables_ndim_and_dtype(2, elem_type, ["V", "U", "UinvT", "Q"], locals()) # Now properly set up outputs to compute outputs = [] return Apply(self, params, outputs)
def make_node(self, x, y): res = Dot22.make_node(self, x, y) x = as_gpuarray_variable(x) y = as_gpuarray_variable(y) assert x.dtype == y.dtype return Apply(self, [x, y], [x.type()])
def make_node(self): return Apply(self, [], [CDataType("cudnnPoolingDescriptor_t")()])
def make_node(self): return Apply(self, [], [Generic()()])
def make_node(self, dy, sm): dy = as_cuda_ndarray_variable(dy) sm = as_cuda_ndarray_variable(sm) assert dy.ndim == 4 assert sm.ndim == 4 return Apply(self, [dy, sm], [sm.type.make_variable()])
def make_node(self, x): x = as_cuda_ndarray_variable(x) assert x.ndim == 4 return Apply(self, [x], [x.type()])
def make_node(self, x, axis, splits): node = Split.make_node(self, x, axis, splits) x = as_gpuarray_variable(x) outs = [GpuArrayType(dtype=o.dtype, broadcastable=o.broadcastable)() for o in node.outputs] return Apply(self, [x] + node.inputs[1:], outs)
def make_node(self, x, shp): x = as_gpuarray_variable(x) res = host_from_gpu(x).reshape(shp, ndim=self.ndim) otype = GpuArrayType(dtype=res.dtype, broadcastable=res.broadcastable) return Apply(self, [x, shp], [otype()])
def make_node(self, input): input = as_gpuarray_variable(input) return Apply(self, [input], [input.type()])