我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用theano.Apply()。
def make_node(self, state, time): """Creates an Apply node representing the application of the op on the inputs provided. Parameters ---------- state : array_like The state to transform into feature space time : int The current time being processed Returns ------- theano.Apply [description] """ state = T.as_tensor_variable(state) time = T.as_tensor_variable(time) return theano.Apply(self, [state, time], [T.bmatrix()])
def perform(self, node, inputs_storage, output_storage): """Peform the transformation from output to feature space. Defines the Python implementation of the op. It is in charge of doing the processing to go from output space (statematrix) to feature space. Parameters ---------- node : Reference to an Apply node which was previously obtained via the Op‘s make_node() method. inputs_storage : array_like A list of references to data which can be operated on using non-symbolic statements output_storage : array_like A list of storage cells where the output is to be stored """ state, time = inputs_storage output_storage[0][0] = np.array(self.d.f.note_state_single_to_input_form(state, time), dtype='int8')
def make_node(self, inp, out, out_grad, ws, stride, pad): ctx_name = infer_context_name(inp, out, out_grad) inp = as_gpuarray_variable(inp, ctx_name) assert (inp.ndim in [4, 5]) out_grad = as_gpuarray_variable(out_grad, ctx_name) assert (out_grad.ndim in [4, 5]) out = as_gpuarray_variable(out, ctx_name) assert(out.ndim in [4, 5]) assert (out_grad.ndim == inp.ndim) assert (inp.ndim == out.ndim) ws = tensor.as_tensor_variable(ws) stride = tensor.as_tensor_variable(stride) pad = tensor.as_tensor_variable(pad) assert ws.type.ndim == stride.type.ndim and ws.type.ndim == pad.type.ndim assert ws.type.ndim == 1 return Apply(self, [inp, out, out_grad, ws, stride, pad], [inp.type()])
def make_node(self, desc, x, y, dy, dhy, dcy, w, hx, cx, reserve): # We trust the callers here xshp = as_scalar(x.shape[2]).astype('uint64') inputs = [desc, xshp, y, dy, w, hx, reserve] outputs = [reserve.type(), x.type(), hx.type()] if self.rnn_mode == 'lstm': inputs.append(cx) outputs.append(cx.type()) if self.grad_h: inputs.append(dhy) if self.grad_c: inputs.append(dcy) return Apply(self, inputs, outputs) # We have special requirements so this is hooking into COp
def make_node(self, img, topgrad, shape=None): ctx_name = infer_context_name(img, topgrad) img = as_gpuarray_variable(img, ctx_name) topgrad = as_gpuarray_variable(topgrad, ctx_name) 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 = [shape[0], shape[1]] assert shape[0].ndim == 0 assert shape[1].ndim == 0 else: height_width = [] broadcastable = [topgrad.type.broadcastable[1], img.type.broadcastable[1], False, False] return Apply(self, [img, topgrad] + height_width, [GpuArrayType(dtype=img.dtype, context_name=ctx_name, broadcastable=broadcastable)()])
def make_node(self, kern, topgrad, shape=None): ctx_name = infer_context_name(kern, topgrad) kern = as_gpuarray_variable(kern, ctx_name) topgrad = as_gpuarray_variable(topgrad, ctx_name) 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 self.subsample != (1, 1) and shape is None: raise ValueError('shape must be given if subsample != (1, 1)') height_width = [shape[0], shape[1]] if self.subsample != (1, 1) else [] if height_width: 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, [GpuArrayType(dtype=topgrad.dtype, context_name=ctx_name, broadcastable=broadcastable)()])
def make_node(self, img, topgrad, shape=None): ctx_name = infer_context_name(img, topgrad) img = as_gpuarray_variable(img, ctx_name) topgrad = as_gpuarray_variable(topgrad, ctx_name) if img.type.ndim != 5: raise TypeError('img must be 5D tensor') if topgrad.type.ndim != 5: raise TypeError('topgrad must be 5D tensor') if self.subsample != (1, 1, 1) or self.border_mode == "half": if shape is None: raise ValueError('shape must be given if subsample != (1, 1, 1)' ' or border_mode == "half"') height_width_depth = [shape[0], shape[1], shape[2]] assert shape[0].ndim == 0 assert shape[1].ndim == 0 assert shape[2].ndim == 0 else: height_width_depth = [] broadcastable = [topgrad.type.broadcastable[1], img.type.broadcastable[1], False, False, False] return Apply(self, [img, topgrad] + height_width_depth, [GpuArrayType(dtype=img.dtype, context_name=ctx_name, broadcastable=broadcastable)()])
def make_node(self, kern, topgrad, shape=None): ctx_name = infer_context_name(kern, topgrad) kern = as_gpuarray_variable(kern, ctx_name) topgrad = as_gpuarray_variable(topgrad, ctx_name) 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 self.subsample != (1, 1, 1) and shape is None: raise ValueError('shape must be given if subsample != (1, 1, 1)') height_width_depth = [shape[0], shape[1], shape[2]] if self.subsample != (1, 1, 1) else [] if height_width_depth: assert shape[0].ndim == 0 assert shape[1].ndim == 0 assert shape[2].ndim == 0 broadcastable = [topgrad.type.broadcastable[0], kern.type.broadcastable[1], False, False, False] return Apply(self, [kern, topgrad] + height_width_depth, [GpuArrayType(dtype=topgrad.dtype, context_name=ctx_name, broadcastable=broadcastable)()])
def c_code_cache_version_apply(self, node): version = [18] # the version corresponding to the c code in this Op # now we insert versions for the ops on which we depend... scalar_node = Apply( self.scalar_op, [Scalar(dtype=input.type.dtype)() for input in node.inputs], [Scalar(dtype=output.type.dtype)() for output in node.outputs]) version.extend(self.scalar_op.c_code_cache_version_apply(scalar_node)) for i in node.inputs + node.outputs: version.extend(Scalar(dtype=i.type.dtype).c_code_cache_version()) version.extend(self.kernel_version(node)) if all(version): return tuple(version) else: return ()
def make_node(self, input): ctx_name = infer_context_name(input) res = CAReduceDtype.make_node(self, input) input = as_gpuarray_variable(input, ctx_name) otype = GpuArrayType(dtype=res.outputs[0].dtype, broadcastable=res.outputs[0].broadcastable, context_name=ctx_name) if res.op.axis is not None: redux = [] for i in range(len(input.type.broadcastable)): redux.append(i in res.op.axis) # since redux is just another way to describe what is in axis # it doesn't need to be compared in __eq__ or __hash__ res.op.redux = redux return Apply(res.op, [input], [otype()])
def make_node(self, pvals, unis): assert pvals.dtype == 'float32' assert unis.dtype == 'float32' 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, pvals, unis, n): assert pvals.dtype == 'float32' assert unis.dtype == 'float32' 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 = 'int64' else: odtype = self.odtype assert odtype == 'int64', odtype br = (pvals.broadcastable[1], pvals.broadcastable[0]) out = GpuArrayType(broadcastable=br, dtype=odtype, context_name=ctx_name)() return Apply(self, [pvals, unis, as_scalar(n)], [out])
def make_node(self, o, W, h, inputIdx, outputIdx): ctx = infer_context_name(o, W, h) o = as_gpuarray_variable(o, ctx) W = as_gpuarray_variable(W, ctx) h = as_gpuarray_variable(h, ctx) inputIdx = as_tensor_variable(inputIdx) outputIdx = as_tensor_variable(outputIdx) assert o.ndim == 3 assert W.ndim == 4 assert h.ndim == 3 assert inputIdx.ndim == 2 assert outputIdx.ndim == 2 assert inputIdx.type.dtype in discrete_dtypes assert outputIdx.type.dtype in discrete_dtypes return Apply(self, [o, W, h, inputIdx, outputIdx], [o.type()])
def test_retNone1(self): """Test that it is not ok to return None from op.grad()""" class retNone(gof.op.Op): __props__ = () def make_node(self): inputs = [theano.tensor.vector()] outputs = [theano.tensor.vector()] return gof.Apply(self, inputs, outputs) def grad(self, inp, grads): x, = inp gz, = grads pass a = retNone().make_node() self.assertRaises(TypeError, grad_sources_inputs, [(a.out, one)], None)
def test_wrong_rval_len1(self): """Test that it is not ok to return the wrong number of gradient terms """ class retOne(gof.op.Op): __props__ = () def make_node(self, *inputs): outputs = [theano.tensor.vector()] return gof.Apply(self, inputs, outputs) def grad(self, inputs, grads): return [inputs[0].zeros_like()] i = theano.tensor.vector() j = theano.tensor.vector() a1 = retOne().make_node(i) grad_sources_inputs([(a1.out, one)], None) a2 = retOne().make_node(i, j) self.assertRaises(ValueError, grad_sources_inputs, [(a2.out, one)], None)
def test_1in_1out(self): """Test grad is called correctly for a 1-to-1 op""" gval = theano.tensor.matrix() class O(gof.op.Op): __props__ = () def make_node(self): inputs = [theano.tensor.matrix()] outputs = [theano.tensor.matrix()] return gof.Apply(self, inputs, outputs) def grad(self, inp, grads): return gval, a1 = O().make_node() g = grad_sources_inputs([(a1.outputs[0], one)], None) self.assertTrue(g[a1.inputs[0]] is gval)
def test_Nin_1out(self): """Test grad is called correctly for a many-to-1 op""" gval0 = theano.tensor.scalar() gval1 = theano.tensor.scalar() class O(gof.op.Op): __props__ = () def make_node(self): inputs = [theano.tensor.scalar(), theano.tensor.scalar()] outputs = [theano.tensor.matrix()] return gof.Apply(self, inputs, outputs) def grad(self, inp, grads): x0, x1 = inp gz, = grads return (gval0, gval1) a1 = O().make_node() g = grad_sources_inputs([(a1.outputs[0], one)], None) self.assertTrue(g[a1.inputs[0]] is gval0) self.assertTrue(g[a1.inputs[1]] is gval1)
def test_Nin_Nout(self): """Test grad is called correctly for a many-to-many op""" gval0 = theano.tensor.matrix() gval1 = theano.tensor.matrix() class O(gof.op.Op): __props__ = () def make_node(self): inputs = [theano.tensor.matrix(), theano.tensor.matrix()] outputs = [theano.tensor.matrix(), theano.tensor.matrix()] return gof.Apply(self, inputs, outputs) def grad(self, inp, grads): return gval0, gval1 a1 = O().make_node() g = grad_sources_inputs([(a1.outputs[0], one)], None) self.assertTrue(g[a1.inputs[0]] is gval0) self.assertTrue(g[a1.inputs[1]] is gval1)
def test_unimplemented_grad_grad(self): # tests that unimplemented grads are caught in the grad method class DummyOp(gof.Op): __props__ = () def make_node(self, x): return gof.Apply(self, [x], [x.type()]) def grad(self, inputs, output_grads): return [theano.gradient.grad_not_implemented(self, 0, inputs[0])] a = theano.tensor.scalar() b = DummyOp()(a) self.assertRaises(TypeError, theano.gradient.grad, b, a)
def test_undefined_grad_grad(self): # tests that undefined grads are caught in the grad method class DummyOp(gof.Op): __props__ = () def make_node(self, x): return gof.Apply(self, [x], [x.type()]) def grad(self, inputs, output_grads): return [theano.gradient.grad_undefined(self, 0, inputs[0])] a = theano.tensor.scalar() b = DummyOp()(a) self.assertRaises(TypeError, theano.gradient.grad, b, a)
def make_node(self, x, v, sorter=None): x = basic.as_tensor(x, ndim=1) v = basic.as_tensor(v) out_type = v.type.clone(dtype='int64') if sorter is None: return theano.Apply(self, [x, v], [out_type()]) else: sorter = basic.as_tensor(sorter, ndim=1) if (theano.configdefaults.python_int_bitwidth() == 32 and sorter.dtype == 'int64'): raise TypeError( "numpy.searchsorted with Python 32bit do not support a" " sorter of int64.") if sorter.type not in basic.int_vector_types: raise TypeError('sorter must be an integer vector', sorter.type) return theano.Apply(self, [x, v, sorter], [out_type()])
def make_node(self, a, val, offset): a = tensor.as_tensor_variable(a) val = tensor.as_tensor_variable(val) offset = tensor.as_tensor_variable(offset) if a.ndim != 2: raise TypeError('%s: first parameter must have exactly' ' two dimensions' % self.__class__.__name__) elif val.ndim != 0: raise TypeError('%s: second parameter must be a scalar' % self.__class__.__name__) elif offset.ndim != 0: raise TypeError('%s: third parameter must be a scalar' % self.__class__.__name__) val = tensor.cast(val, dtype=scalar.upcast(a.dtype, val.dtype)) if val.dtype != a.dtype: raise TypeError('%s: type of second parameter must be the same' ' as the first\'s' % self.__class__.__name__) elif offset.dtype[:3] != 'int': raise TypeError('%s: type of third parameter must be as integer' ' use theano.tensor.cast( input, \'int32/int64\')' % self.__class__.__name__) return gof.Apply(self, [a, val, offset], [a.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 self.subsample != (1, 1, 1) and shape is None: raise ValueError('shape must be given if subsample != (1, 1, 1)') if self.subsample != (1, 1, 1): height_width_depth = [as_tensor_variable(shape[0]).astype('int64'), as_tensor_variable(shape[1]).astype('int64'), as_tensor_variable(shape[2]).astype('int64')] else: height_width_depth = [] 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, 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 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, 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 self.subsample != (1, 1) and shape is None: raise ValueError('shape must be given if subsample != (1, 1)') height_width = [as_tensor_variable(shape[0]).astype('int64'), as_tensor_variable(shape[1]).astype('int64')] if self.subsample != (1, 1) else [] 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 c_init_code_struct(self, node, name, sub): """ Optional: return a code string specific to the apply to be inserted in the struct initialization code. Parameters ---------- node : an Apply instance in the graph being compiled name : str A unique name to distinguish variables from those of other nodes. sub A dictionary of values to substitute in the code. Most notably it contains a 'fail' entry that you should place in your code after setting a python exception to indicate an error. Raises ------ MethodNotDefined The subclass does not override this method. """ raise utils.MethodNotDefined("c_init_code_apply", type(self), self.__class__.__name__)
def c_support_code_struct(self, node, name): """ Optional: return utility code for use by an `Op` that will be inserted at struct scope, that can be specialized for the support of a particular `Apply` node. Parameters ---------- node : an Apply instance in the graph being compiled name : str A unique name to distinguish you variables from those of other nodes. Raises ------ MethodNotDefined Subclass does not implement this method. """ raise utils.MethodNotDefined("c_support_code_struct", type(self), self.__class__.__name__)
def c_cleanup_code_struct(self, node, name): """ Optional: return a code string specific to the apply to be inserted in the struct cleanup code. Parameters ---------- node : an Apply instance in the graph being compiled name : str A unique name to distinguish variables from those of other nodes. Raises ------ MethodNotDefined The subclass does not override this method. """ raise utils.MethodNotDefined("c_cleanup_code_struct", type(self), self.__class__.__name__)
def make_node(self, *inputs): """ Create a "apply" nodes for the inputs in that order. """ if not hasattr(self, 'itypes'): raise NotImplementedError("You can either define itypes and otypes,\ or implement make_node") if not hasattr(self, 'otypes'): raise NotImplementedError("You can either define itypes and otypes,\ or implement make_node") if len(inputs) != len(self.itypes): raise ValueError("We expected %d inputs but got %d." % (len(self.itypes), len(inputs))) if not all(inp.type == it for inp, it in zip(inputs, self.itypes)): raise TypeError( "We expected inputs of types '%s' but got types '%s' " % (str(self.itypes), str([inp.type for inp in inputs]))) return theano.Apply(self, inputs, [o() for o in self.otypes])
def make_node(self, img_shape, kern_shape): if img_shape.type.ndim != 1 or img_shape.type.dtype != 'int64': raise TypeError('img must be 1D shape tensor') if kern_shape.type.ndim != 1 or kern_shape.type.dtype != 'int64': raise TypeError('kern must be 1D shape tensor') node = Apply(self, [img_shape, kern_shape], [CDataType("cudnnConvolutionDescriptor_t", freefunc="cudnnDestroyConvolutionDescriptor")()]) # DebugMode cannot compare the values of CDataType variables, so by # default it returns False all the time. To prevent DebugMode from # complaining because of the MergeOptimizer, we make this variable # always compare to True. out = node.outputs[0] out.tag.values_eq_approx = tensor.type.values_eq_approx_always_true return node
def make_node(self, img, kern, output, desc, alpha=None, beta=None): img = as_cuda_ndarray_variable(img) kern = as_cuda_ndarray_variable(kern) output = as_cuda_ndarray_variable(output) if img.type.ndim != 4: raise TypeError('img must be 4D tensor') if kern.type.ndim != 4: raise TypeError('kern must be 4D tensor') if output.type.ndim != 4: raise TypeError('output must be a 4D 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, [img, kern, output, desc, alpha, beta], [output.type()])
def make_node(self, img, kern, output, desc, alpha=None, beta=None): img = as_cuda_ndarray_variable(img) kern = as_cuda_ndarray_variable(kern) output = as_cuda_ndarray_variable(output) if img.type.ndim != 5: raise TypeError('img must be 5D tensor') if kern.type.ndim != 5: raise TypeError('kern must be 5D tensor') if output.type.ndim != 5: raise TypeError('output must be a 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, [img, kern, output, desc, alpha, beta], [output.type()])
def make_node(self, img, topgrad, output, desc, alpha=None, beta=None): img = as_cuda_ndarray_variable(img) topgrad = as_cuda_ndarray_variable(topgrad) output = as_cuda_ndarray_variable(output) 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 output.type.ndim != 4: raise TypeError('output must be 4D 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, [img, topgrad, output, desc, alpha, beta], [output.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 != 4: raise TypeError('kern must be 4D tensor') if topgrad.type.ndim != 4: raise TypeError('topgrad must be 4D tensor') if output.type.ndim != 4: raise TypeError('output must be 4D 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, 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, inp, out, inp_grad, ws, stride, pad): inp = as_cuda_ndarray_variable(inp) assert (inp.ndim in [4, 5]) inp_grad = as_cuda_ndarray_variable(inp_grad) assert (inp_grad.ndim in [4, 5]) out = as_cuda_ndarray_variable(out) assert(out.ndim in [4, 5]) assert (inp_grad.ndim == inp.ndim) assert (inp.ndim == out.ndim) ws = tensor.as_tensor_variable(ws) stride = tensor.as_tensor_variable(stride) pad = tensor.as_tensor_variable(pad) assert ws.type.ndim == stride.type.ndim and ws.type.ndim == pad.type.ndim assert ws.type.ndim == 1 return Apply(self, [inp, out, inp_grad, ws, stride, pad], [inp.type()])
def make_node(self, input): ib = tuple(input.type.broadcastable) if not ib == self.input_broadcastable: if len(ib) != len(self.input_broadcastable): raise TypeError(( "The number of dimensions of the " "input is incorrect for this op. Expected %s, got %s." % (self.input_broadcastable, ib))) for expected, b in zip(self.input_broadcastable, ib): if expected is True and b is False: raise TypeError(( "The broadcastable pattern of the " "input is incorrect for this op. Expected %s, got %s." % (self.input_broadcastable, ib))) # else, expected == b or expected is False and b is True # Both case are good. ob = [] if not isinstance(input.type, CudaNdarrayType): input = as_cuda_ndarray_variable(input) for value in self.new_order: if value == 'x': ob.append(True) else: ob.append(ib[value]) return Apply(self, [input], [CudaNdarrayType(broadcastable=ob)()])
def c_code_cache_version_apply(self, node): version = [15] # the version corresponding to the c code in this Op # now we insert versions for the ops on which we depend... Apply(self.scalar_op, [Scalar( dtype=input.type.dtype)() for input in node.inputs], [Scalar( dtype=output.type.dtype)() for output in node.outputs]) version.extend(self.scalar_op.c_code_cache_version()) for i in node.inputs + node.outputs: version.extend( Scalar(dtype=i.type.dtype).c_code_cache_version()) if all(version): return tuple(version) else: return ()
def make_node(self, x, ilist): x_ = as_cuda_ndarray_variable(x) ilist_ = tensor.as_tensor_variable(ilist) if ilist_.type.dtype[:3] not in ('int', 'uin'): raise TypeError('index must be integers') if ilist_.type.ndim != 1: raise TypeError('index must be vector') if x_.type.ndim == 0: raise TypeError('cannot index into a scalar') # c code suppose it is int64 if x.ndim in [1, 2, 3] and ilist_.dtype in [ 'int8', 'int16', 'int32', 'uint8', 'uint16', 'uint32']: ilist_ = tensor.cast(ilist_, 'int64') bcast = (ilist_.broadcastable[0],) + x_.broadcastable[1:] return Apply(self, [x_, ilist_], [CudaNdarrayType(dtype=x.dtype, broadcastable=bcast)()])
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 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 = [shape[0], shape[1]] assert shape[0].ndim == 0 assert shape[1].ndim == 0 else: height_width = [] broadcastable = [topgrad.type.broadcastable[1], img.type.broadcastable[1], False, False] return Apply(self, [img, topgrad] + height_width, [CudaNdarrayType(broadcastable)()])
def make_node(self, img, topgrad, shape=None): img = as_cuda_ndarray_variable(img) topgrad = as_cuda_ndarray_variable(topgrad) if shape is not None: shape = as_tensor_variable(shape) if img.type.ndim != 5: raise TypeError('img must be 5D tensor') if topgrad.type.ndim != 5: raise TypeError('topgrad must be 5D tensor') if self.subsample != (1, 1, 1) or self.border_mode == "half": if shape is None: raise ValueError('shape must be given if subsample != (1, 1, 1)' ' or border_mode == "half"') height_width_depth = [shape[0], shape[1], shape[2]] assert shape[0].ndim == 0 assert shape[1].ndim == 0 assert shape[2].ndim == 0 else: height_width_depth = [] broadcastable = [topgrad.type.broadcastable[1], img.type.broadcastable[1], False, False, False] return Apply(self, [img, topgrad] + height_width_depth, [CudaNdarrayType(broadcastable)()])
def make_node(self, kern, topgrad, shape=None): kern = as_cuda_ndarray_variable(kern) topgrad = as_cuda_ndarray_variable(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 self.subsample != (1, 1, 1) and shape is None: raise ValueError('shape must be given if subsample != (1, 1, 1)') height_width_depth = [shape[0], shape[1], shape[2]] if self.subsample != (1, 1, 1) else [] if height_width_depth: assert shape[0].ndim == 0 assert shape[1].ndim == 0 assert shape[2].ndim == 0 broadcastable = [topgrad.type.broadcastable[0], kern.type.broadcastable[1], False, False, False] return Apply(self, [kern, topgrad] + height_width_depth, [CudaNdarrayType(broadcastable)()])
def make_node(self, V, W, b, d): """ Parameters ---------- V Visible unit, input. W Weights, filter. b Bias. d Strides when moving the filter over the input. """ V_ = as_cuda_ndarray_variable(V) W_ = as_cuda_ndarray_variable(W) b_ = as_cuda_ndarray_variable(b) d_ = T.as_tensor_variable(d) broad = (V_.broadcastable[0], W_.broadcastable[0], False, False, False) return theano.Apply(self, inputs=[V_, W_, b_, d_], outputs=[CudaNdarrayType(dtype=V_.dtype, broadcastable=broad)()])
def make_node(self, V, d, WShape, dCdH): """ Parameters ---------- V Visible. d Strides. WShape Shapes of the weights -> shape of this op output. dCdH Other input with what V will be convolved. """ V_ = as_cuda_ndarray_variable(V) d_ = T.as_tensor_variable(d) WShape_ = T.as_tensor_variable(WShape) dCdH_ = as_cuda_ndarray_variable(dCdH) broad = (False,) * 5 return theano.Apply(self, inputs=[V_, d_, WShape_, dCdH_], outputs=[CudaNdarrayType(dtype=V_.dtype, broadcastable=broad)()])
def make_node(self, s_x_): if s_x_.type.dtype != 'float32': raise ValueError('Only float32 is allowed') ctx_name = infer_context_name(s_x_) s_x = as_gpuarray_variable(s_x_, ctx_name) return th.Apply(self, [s_x], [s_x.type(), s_x.type()])
def make_node(self, s_x_): if s_x_.type.dtype != 'float32': raise ValueError('Only float32 is allowed') ctx_name = infer_context_name(s_x_) s_x = as_gpuarray_variable(s_x_, ctx_name) return th.Apply(self, [s_x], [s_x.type()])
def make_node(self, input_): input_ = tensor.as_tensor_variable(input_) output_type = tensor.TensorType( input_.dtype, input_.broadcastable[:-1]) return theano.Apply(self, [input_], [output_type()])
def make_node(self, input_): defs_type = tensor.TensorType('int64', [False, False]) def_mask_type = tensor.TensorType('float32', [False, False]) # both type happened to be the same, but this is just a coincidence def_map_type = defs_type return theano.Apply( self, [input_], [defs_type(), def_mask_type(), def_map_type()])
def make_node(self, x): x = as_gpuarray_variable(x, self.context_name) x_arg = pygpu.elemwise.arg('x', 'float32', read=True) c_arg = pygpu.elemwise.arg('c', 'float32', read=True, write=True) self.my_op = pygpu.elemwise.GpuElemwise(get_context(self.context_name), "c = " + str(self.a) + " * x + " + str(self.b), [x_arg, c_arg], convert_f16=True) return Apply(self, [x], [x.type()])