Python theano.tensor.ftensor4() Examples
The following are 30
code examples of theano.tensor.ftensor4().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
theano.tensor
, or try the search function
.
Example #1
Source File: check_dependencies.py From FRRN with MIT License | 6 votes |
def check_theano(): """Checks if theano is installed correctly.""" try: import theano import theano.tensor as T # Check float type. if theano.config.floatX != "float32": logging.error("Theano float type must be float32. Add " "floatX=float32 to your .theanorc.") else: logging.info("Theano float is float32.") # Check if cudnn softmax is available. try: from dltools import architectures architectures.FRRNBuilderBase.log_softmax_4d(T.ftensor4()) logging.info("cuDNN spatial softmax found.") except: logging.error("Cannot create cuDNN spatial log softmax. Install " "cuDNN and make sure that theano uses the GPU.") except: logging.error("Cannot import theano.")
Example #2
Source File: test_blocksparse.py From D-VAE with MIT License | 6 votes |
def test_sparseblockouter(self): o = tensor.ftensor4() x = tensor.ftensor3() y = tensor.ftensor3() xIdx = tensor.imatrix() yIdx = tensor.imatrix() out = self.outer_op(o, x, y, xIdx, yIdx) f = theano.function([o, x, y, xIdx, yIdx], out, on_unused_input="warn", mode=self.mode) o_val, x_val, y_val, xIdx_val, yIdx_val = \ BlockSparse_Gemv_and_Outer.outer_data() th_out = f(o_val, x_val, y_val, xIdx_val, yIdx_val) ref_out = BlockSparse_Gemv_and_Outer.outer_numpy( o_val, x_val, y_val, xIdx_val, yIdx_val) utt.assert_allclose(ref_out, th_out)
Example #3
Source File: test_opt.py From D-VAE with MIT License | 6 votes |
def test_blocksparse_inplace_gemv_opt(): b = tensor.fmatrix() W = tensor.ftensor4() h = tensor.ftensor3() iIdx = tensor.lmatrix() oIdx = tensor.lmatrix() o = sparse_block_dot(W, h, iIdx, b, oIdx) f = theano.function([W, h, iIdx, b, oIdx], o) assert hasattr(f.maker.fgraph.outputs[0].tag, 'trace') if theano.config.mode == "FAST_COMPILE": assert not f.maker.fgraph.toposort()[-1].op.inplace else: assert f.maker.fgraph.toposort()[-1].op.inplace
Example #4
Source File: test_blocksparse.py From D-VAE with MIT License | 6 votes |
def test_sparseblockgemv(self): """ Compares the numpy and theano versions of sparseblockgemv. """ b = tensor.fmatrix() W = tensor.ftensor4() h = tensor.ftensor3() iIdx = tensor.imatrix() oIdx = tensor.imatrix() o = self.gemv_op(b.take(oIdx, axis=0), W, h, iIdx, oIdx) f = theano.function([W, h, iIdx, b, oIdx], o, mode=self.mode) W_val, h_val, iIdx_val, b_val, oIdx_val = \ BlockSparse_Gemv_and_Outer.gemv_data() th_out = f(W_val, h_val, iIdx_val, b_val, oIdx_val) ref_out = BlockSparse_Gemv_and_Outer.gemv_numpy( b_val.take(oIdx_val, axis=0), W_val, h_val, iIdx_val, oIdx_val) utt.assert_allclose(ref_out, th_out)
Example #5
Source File: test_blocksparse.py From D-VAE with MIT License | 6 votes |
def test_sparseblockdot(self): """ Compares the numpy version of sparseblockgemv to sparse_block_dot. """ b = tensor.fmatrix() W = tensor.ftensor4() h = tensor.ftensor3() iIdx = tensor.imatrix() oIdx = tensor.imatrix() o = sparse_block_dot(W, h, iIdx, b, oIdx) f = theano.function([W, h, iIdx, b, oIdx], o, mode=self.mode) W_val, h_val, iIdx_val, b_val, oIdx_val = \ BlockSparse_Gemv_and_Outer.gemv_data() th_out = f(W_val, h_val, iIdx_val, b_val, oIdx_val) ref_out = BlockSparse_Gemv_and_Outer.gemv_numpy( b_val.take(oIdx_val, axis=0), W_val, h_val, iIdx_val, oIdx_val) utt.assert_allclose(ref_out, th_out)
Example #6
Source File: test_dnn.py From attention-lvcsr with MIT License | 6 votes |
def test_dnn_conv_merge_broad(): # Make sure that we don't apply output_merge on broadcasted values. if not cuda.dnn.dnn_available(): raise SkipTest(cuda.dnn.dnn_available.msg) img = T.ftensor4() kern = T.ftensor4() conv = dnn.dnn_conv(img, kern) lr = numpy.asarray(0.05, dtype='float32') # this does broadcasting fr = conv + lr f = theano.function([img, kern], [fr]) convs = [n for n in f.maker.fgraph.toposort() if isinstance(n.op, dnn.GpuDnnConv)] assert len(convs) == 1 conv = convs[0] # Assert output was not merged assert isinstance(conv.inputs[2].owner.op, GpuAllocEmpty)
Example #7
Source File: test_opt.py From D-VAE with MIT License | 6 votes |
def test_blocksparse_inplace_outer_opt(): b = tensor.fmatrix() W = tensor.ftensor4() h = tensor.ftensor3() iIdx = tensor.lmatrix() oIdx = tensor.lmatrix() o = sparse_block_dot(W, h, iIdx, b, oIdx) theano.printing.debugprint(tensor.grad(o.sum(), wrt=W)) f = theano.function([W, h, iIdx, b, oIdx], [o, tensor.grad(o.sum(), wrt=W)]) assert hasattr(f.maker.fgraph.outputs[0].tag, 'trace') if theano.config.mode == "FAST_COMPILE": assert not f.maker.fgraph.toposort()[-1].op.inplace else: assert f.maker.fgraph.toposort()[-1].op.inplace
Example #8
Source File: test_dnn.py From attention-lvcsr with MIT License | 6 votes |
def test_dnn_conv_merge_mouts(): # make sure it doesn't attempt to output/alpha merge a convolution # that has multiple clients. if not cuda.dnn.dnn_available(): raise SkipTest(cuda.dnn.dnn_available.msg) img = T.ftensor4() kern = T.ftensor4() out = T.ftensor4() conv = dnn.dnn_conv(img, kern) lr = numpy.asarray(0.05, dtype='float32') if cuda.dnn.version() == -1: # Can't merge alpha with cudnn v1 fr = conv + out else: fr = lr * (conv + out) rr = conv * lr f = theano.function([img, kern, out], [fr, rr], mode=mode_with_gpu) convs = [n for n in f.maker.fgraph.toposort() if isinstance(n.op, dnn.GpuDnnConv)] assert len(convs) == 1
Example #9
Source File: test_conv_cuda_ndarray.py From D-VAE with MIT License | 6 votes |
def test_logical_shapes(self): # Logical shapes are not supported anymore, so we check that it # raises an Exception. for stride in range(1, 4): kshp = (10, 2, 10, 10) featshp = (3, 10, 11, 11) a = tensor.ftensor4() A = tensor.ftensor4() # Need to transpose first two dimensions of kernel, and reverse # index kernel image dims (for correlation) kernel_rotated = tensor.transpose(A, axes=[1, 0, 2, 3]) featshp_logical = (featshp[0], featshp[1], featshp[2] * stride, featshp[3] * stride) kshp_rotated = (kshp[1], kshp[0], kshp[2], kshp[3]) self.assertRaises(ValueError, tensor.nnet.conv2d, a, kernel_rotated, border_mode='full', image_shape=featshp, filter_shape=kshp_rotated, imshp_logical=featshp_logical[1:], kshp_logical=kshp[2:])
Example #10
Source File: test_conv_cuda_ndarray.py From attention-lvcsr with MIT License | 6 votes |
def test_default_conv(): """Just test that we introduce the right GPU convolution version. """ img = theano.tensor.ftensor4() fil = theano.tensor.ftensor4() c = theano.tensor.nnet.conv2d(img, fil) f = theano.function([img, fil], c, mode=theano_mode) if cuda.dnn.dnn_available(): assert any([isinstance(a.op, GpuDnnConv) for a in f.maker.fgraph.apply_nodes]) else: assert any([isinstance(a.op, cuda.blas.GpuCorrMM) for a in f.maker.fgraph.apply_nodes])
Example #11
Source File: test_dnn.py From D-VAE with MIT License | 6 votes |
def test_dnn_conv_merge_mouts(): # make sure it doesn't attempt to output/alpha merge a convolution # that has multiple clients. if not cuda.dnn.dnn_available(): raise SkipTest(cuda.dnn.dnn_available.msg) img = T.ftensor4() kern = T.ftensor4() out = T.ftensor4() conv = dnn.dnn_conv(img, kern) lr = numpy.asarray(0.05, dtype='float32') if cuda.dnn.version() == -1: # Can't merge alpha with cudnn v1 fr = conv + out else: fr = lr * (conv + out) rr = conv * lr f = theano.function([img, kern, out], [fr, rr], mode=mode_with_gpu) convs = [n for n in f.maker.fgraph.toposort() if isinstance(n.op, dnn.GpuDnnConv)] assert len(convs) == 1
Example #12
Source File: test_dnn.py From D-VAE with MIT License | 6 votes |
def test_dnn_conv_merge_broad(): # Make sure that we don't apply output_merge on broadcasted values. if not cuda.dnn.dnn_available(): raise SkipTest(cuda.dnn.dnn_available.msg) img = T.ftensor4() kern = T.ftensor4() conv = dnn.dnn_conv(img, kern) lr = numpy.asarray(0.05, dtype='float32') # this does broadcasting fr = conv + lr f = theano.function([img, kern], [fr]) convs = [n for n in f.maker.fgraph.toposort() if isinstance(n.op, dnn.GpuDnnConv)] assert len(convs) == 1 conv = convs[0] # Assert output was not merged assert isinstance(conv.inputs[2].owner.op, GpuAllocEmpty)
Example #13
Source File: test_blocksparse.py From attention-lvcsr with MIT License | 6 votes |
def test_sparseblockouter(self): o = tensor.ftensor4() x = tensor.ftensor3() y = tensor.ftensor3() xIdx = tensor.imatrix() yIdx = tensor.imatrix() out = self.outer_op(o, x, y, xIdx, yIdx) f = theano.function([o, x, y, xIdx, yIdx], out, on_unused_input="warn", mode=self.mode) o_val, x_val, y_val, xIdx_val, yIdx_val = \ BlockSparse_Gemv_and_Outer.outer_data() th_out = f(o_val, x_val, y_val, xIdx_val, yIdx_val) ref_out = BlockSparse_Gemv_and_Outer.outer_numpy( o_val, x_val, y_val, xIdx_val, yIdx_val) utt.assert_allclose(ref_out, th_out)
Example #14
Source File: test_blocksparse.py From attention-lvcsr with MIT License | 6 votes |
def test_sparseblockgemv(self): """ Compares the numpy and theano versions of sparseblockgemv. """ b = tensor.fmatrix() W = tensor.ftensor4() h = tensor.ftensor3() iIdx = tensor.imatrix() oIdx = tensor.imatrix() o = self.gemv_op(b.take(oIdx, axis=0), W, h, iIdx, oIdx) f = theano.function([W, h, iIdx, b, oIdx], o, mode=self.mode) W_val, h_val, iIdx_val, b_val, oIdx_val = \ BlockSparse_Gemv_and_Outer.gemv_data() th_out = f(W_val, h_val, iIdx_val, b_val, oIdx_val) ref_out = BlockSparse_Gemv_and_Outer.gemv_numpy( b_val.take(oIdx_val, axis=0), W_val, h_val, iIdx_val, oIdx_val) utt.assert_allclose(ref_out, th_out)
Example #15
Source File: test_blocksparse.py From attention-lvcsr with MIT License | 6 votes |
def test_sparseblockdot(self): """ Compares the numpy version of sparseblockgemv to sparse_block_dot. """ b = tensor.fmatrix() W = tensor.ftensor4() h = tensor.ftensor3() iIdx = tensor.imatrix() oIdx = tensor.imatrix() o = sparse_block_dot(W, h, iIdx, b, oIdx) f = theano.function([W, h, iIdx, b, oIdx], o, mode=self.mode) W_val, h_val, iIdx_val, b_val, oIdx_val = \ BlockSparse_Gemv_and_Outer.gemv_data() th_out = f(W_val, h_val, iIdx_val, b_val, oIdx_val) ref_out = BlockSparse_Gemv_and_Outer.gemv_numpy( b_val.take(oIdx_val, axis=0), W_val, h_val, iIdx_val, oIdx_val) utt.assert_allclose(ref_out, th_out)
Example #16
Source File: test_opt.py From attention-lvcsr with MIT License | 6 votes |
def test_blocksparse_inplace_outer_opt(): b = tensor.fmatrix() W = tensor.ftensor4() h = tensor.ftensor3() iIdx = tensor.lmatrix() oIdx = tensor.lmatrix() o = sparse_block_dot(W, h, iIdx, b, oIdx) theano.printing.debugprint(tensor.grad(o.sum(), wrt=W)) f = theano.function([W, h, iIdx, b, oIdx], [o, tensor.grad(o.sum(), wrt=W)]) if theano.config.mode == "FAST_COMPILE": assert not f.maker.fgraph.toposort()[-1].op.inplace else: assert f.maker.fgraph.toposort()[-1].op.inplace
Example #17
Source File: test_dnn.py From attention-lvcsr with MIT License | 5 votes |
def test_conv(self): if not dnn.dnn_available(): raise SkipTest(dnn.dnn_available.msg) img = T.ftensor4('img') kerns = T.ftensor4('kerns') out = T.ftensor4('out') img_val = numpy.asarray( numpy.random.rand(10, 2, 6, 4), dtype='float32' ) kern_vals = numpy.asarray( numpy.random.rand(8, 2, 4, 3), dtype='float32' ) for params in product( ['valid', 'full', 'half'], [(1, 1), (2, 2)], ['conv', 'cross'] ): out_vals = numpy.zeros( dnn.GpuDnnConv.get_out_shape(img_val.shape, kern_vals.shape, border_mode=params[0], subsample=params[1]), dtype='float32') desc = dnn.GpuDnnConvDesc( border_mode=params[0], subsample=params[1], conv_mode=params[2] )(img.shape, kerns.shape) conv = dnn.GpuDnnConv()(img, kerns, out, desc) self._compile_and_check( [img, kerns, out], [conv], [img_val, kern_vals, out_vals], dnn.GpuDnnConv )
Example #18
Source File: test_opt.py From attention-lvcsr with MIT License | 5 votes |
def test_local_lift_abstractconv_gpu_shape(): prev = theano.config.on_opt_error try: theano.config.on_opt_error = 'raise' s = tensor.ivector() a = tensor.ftensor4() b = tensor.ftensor4() c = tensor.nnet.abstract_conv.AbstractConv2d_gradWeights()(a, b, s) theano.function([s, a, b], c, mode=mode_with_gpu) finally: theano.config.on_opt_error = prev
Example #19
Source File: test_dnn.py From attention-lvcsr with MIT License | 5 votes |
def test_softmax(self): if not dnn.dnn_available(): raise SkipTest(dnn.dnn_available.msg) t = T.ftensor4('t') rand_tensor = numpy.asarray( numpy.random.rand(5, 4, 3, 2), dtype='float32' ) self._compile_and_check( [t], [dnn.GpuDnnSoftmax('bc01', 'accurate', 'channel')(t)], [rand_tensor], dnn.GpuDnnSoftmax ) self._compile_and_check( [t], [ T.grad( dnn.GpuDnnSoftmax( 'bc01', 'accurate', 'channel' )(t).mean(), t ) ], [rand_tensor], dnn.GpuDnnSoftmaxGrad )
Example #20
Source File: test_dnn.py From attention-lvcsr with MIT License | 5 votes |
def test_dnn_tag(): """ Test that if cudnn isn't avail we crash and that if it is avail, we use it. """ x = T.ftensor4() old = theano.config.on_opt_error theano.config.on_opt_error = "raise" sio = StringIO() handler = logging.StreamHandler(sio) logging.getLogger('theano.compile.tests.test_dnn').addHandler(handler) # Silence original handler when intentionnally generating warning messages logging.getLogger('theano').removeHandler(theano.logging_default_handler) raised = False try: f = theano.function( [x], pool_2d(x, ds=(2, 2), ignore_border=True), mode=mode_with_gpu.including("cudnn")) except (AssertionError, RuntimeError): assert not dnn.dnn_available(test_ctx_name) raised = True finally: theano.config.on_opt_error = old logging.getLogger( 'theano.compile.tests.test_dnn').removeHandler(handler) logging.getLogger('theano').addHandler(theano.logging_default_handler) if not raised: assert dnn.dnn_available(test_ctx_name) assert any([isinstance(n.op, dnn.GpuDnnPool) for n in f.maker.fgraph.toposort()])
Example #21
Source File: test_dnn.py From attention-lvcsr with MIT License | 5 votes |
def test_log_softmax(self): # This is a test for an optimization that depends on CuDNN v3 or # more recent. Don't test if the CuDNN version is too old. if cuda.dnn.version() < (3000, 3000): raise SkipTest("Log-softmax is only in cudnn v3+") x = T.ftensor4() softmax_out = dnn.GpuDnnSoftmax('bc01', 'accurate', 'channel')(x) log_out = T.log(T.as_tensor_variable(softmax_out)) f = theano.function([x], log_out, mode=mode_with_gpu) # Ensure that the optimization has been applied dnn_softmax_nodes = [n for n in f.maker.fgraph.toposort() if isinstance(n.op, cuda.dnn.GpuDnnSoftmax)] assert len(dnn_softmax_nodes) == 1 assert dnn_softmax_nodes[0].op.algo == "log" # Ensure that the output of the function is valid input_shapes = [(3, 4, 5, 6), (1025, 2, 3, 4), (2, 1025, 3, 4), (2, 3, 1025, 4), (2, 3, 4, 1025), (66000, 2, 3, 4), (2, 66000, 3, 4), (2, 3, 66000, 4), (2, 3, 4, 66000)] for inp_shape in input_shapes: input_val = numpy.random.normal(0, 1, inp_shape).astype("float32") out = f(input_val) expected_out = numpy.log( numpy.exp(input_val) / numpy.exp(input_val).sum(1)[:, None, :, :]) utt.assert_allclose(out, expected_out)
Example #22
Source File: test_dnn.py From attention-lvcsr with MIT License | 5 votes |
def test_softmax(self): if not dnn.dnn_available(test_ctx_name): raise SkipTest(dnn.dnn_available.msg) t = T.ftensor4('t') rand_tensor = numpy.asarray( numpy.random.rand(5, 4, 3, 2), dtype='float32' ) self._compile_and_check( [t], [dnn.GpuDnnSoftmax('accurate', 'channel')(t)], [rand_tensor], dnn.GpuDnnSoftmax ) self._compile_and_check( [t], [ T.grad( dnn.GpuDnnSoftmax( 'accurate', 'channel' )(t).mean(), t ) ], [rand_tensor], dnn.GpuDnnSoftmaxGrad )
Example #23
Source File: test_blocksparse.py From attention-lvcsr with MIT License | 5 votes |
def test_gemv_infershape(self): b = tensor.fmatrix() W = tensor.ftensor4() h = tensor.ftensor3() iIdx = tensor.imatrix() oIdx = tensor.imatrix() self._compile_and_check( [W, h, iIdx, b, oIdx], [self.gemv_op(b.take(oIdx, axis=0), W, h, iIdx, oIdx)], self.gemv_data(), self.gemv_class)
Example #24
Source File: test_blocksparse.py From attention-lvcsr with MIT License | 5 votes |
def test_dot_infershape(self): b = tensor.fmatrix() W = tensor.ftensor4() h = tensor.ftensor3() iIdx = tensor.imatrix() oIdx = tensor.imatrix() self._compile_and_check([W, h, iIdx, b, oIdx], [sparse_block_dot(W, h, iIdx, b, oIdx)], self.gemv_data(), self.gemv_class)
Example #25
Source File: test_blocksparse.py From attention-lvcsr with MIT License | 5 votes |
def test_sparseblockgemvF(self): """ Test the fortan order for W (which can happen in the grad for some graphs). """ b = tensor.fmatrix() W = tensor.ftensor4() h = tensor.ftensor3() iIdx = tensor.imatrix() oIdx = tensor.imatrix() o = self.gemv_op(b.take(oIdx, axis=0), tensor.DimShuffle((False, False, False, False), (0, 1, 3, 2)) (tensor.as_tensor_variable(W)), h, iIdx, oIdx) f = theano.function([W, h, iIdx, b, oIdx], o, mode=self.mode) W_val, h_val, iIdx_val, b_val, oIdx_val = \ BlockSparse_Gemv_and_Outer.gemv_data() th_out = f(numpy.swapaxes(W_val, 2, 3), h_val, iIdx_val, b_val, oIdx_val) ref_out = BlockSparse_Gemv_and_Outer.gemv_numpy( b_val.take(oIdx_val, axis=0), W_val, h_val, iIdx_val, oIdx_val) utt.assert_allclose(ref_out, th_out)
Example #26
Source File: test_opt.py From attention-lvcsr with MIT License | 5 votes |
def test_blocksparse_inplace_gemv_opt(): b = tensor.fmatrix() W = tensor.ftensor4() h = tensor.ftensor3() iIdx = tensor.lmatrix() oIdx = tensor.lmatrix() o = sparse_block_dot(W, h, iIdx, b, oIdx) f = theano.function([W, h, iIdx, b, oIdx], o) if theano.config.mode == "FAST_COMPILE": assert not f.maker.fgraph.toposort()[-1].op.inplace else: assert f.maker.fgraph.toposort()[-1].op.inplace
Example #27
Source File: train.py From FRRN with MIT License | 5 votes |
def compile_validation_function(network, batch_size): """Compiles the validation function. Args: network: The network instance. batch_size: The batch size. Returns: A function that takes in a batch of images and targets and returns the predicted segmentation mask and the loss. """ input_var = network.input_layers[0].input_var target_var = T.ftensor4() predictions = lasagne.layers.get_output( network.output_layers, deterministic=True)[0] loss = losses.bootstrapped_xentropy( predictions=predictions, targets=target_var, batch_size=batch_size, multiplier=BOOTSTRAP_MULTIPLIER ) pylogging.info("Compile validation function") return theano.function( inputs=[input_var, target_var], outputs=[T.argmax(predictions, axis=1), loss] )
Example #28
Source File: test_dnn.py From attention-lvcsr with MIT License | 5 votes |
def test_conv(self): if not dnn.dnn_available(test_ctx_name): raise SkipTest(dnn.dnn_available.msg) img = T.ftensor4('img') kerns = T.ftensor4('kerns') out = T.ftensor4('out') img_val = numpy.asarray( numpy.random.rand(7, 2, 6, 4), dtype='float32' ) kern_vals = numpy.asarray( numpy.random.rand(8, 2, 4, 3), dtype='float32' ) for params in product( ['valid', 'full', 'half'], [(1, 1), (2, 2)], ['conv', 'cross'] ): out_vals = numpy.zeros( dnn.GpuDnnConv.get_out_shape(img_val.shape, kern_vals.shape, border_mode=params[0], subsample=params[1]), dtype='float32') desc = dnn.GpuDnnConvDesc( border_mode=params[0], subsample=params[1], conv_mode=params[2] )(kerns.shape) conv = dnn.GpuDnnConv()(img, kerns, out, desc) self._compile_and_check( [img, kerns, out], [conv], [img_val, kern_vals, out_vals], dnn.GpuDnnConv )
Example #29
Source File: convolutional.py From OpenDeep with Apache License 2.0 | 5 votes |
def testConv2DOutputSize(self): try: x = ftensor4('x') # batch, channels, height, width s = (None, 3, 25, 32) filters = 25 filter_size = 5 padding = 3 stride = 3 conv1 = Conv2D(inputs=(s, x), n_filters=filters, filter_size=filter_size, padding=padding, stride=stride, outdir=None) f1 = function(inputs=[x], outputs=conv1.get_outputs().shape, allow_input_downcast=True) x1 = np.ones((100, 3, 25, 32)) outs = f1(x1) self.compareSizes(outs=outs, output_size=conv1.output_size, in_size=s, batches=100) finally: if 'x' in locals(): del x if 'conv1' in locals(): del conv1 if 'f1' in locals(): del f1 if 'outs' in locals(): del outs if 'x1' in locals(): del x1
Example #30
Source File: test_abstract_conv.py From attention-lvcsr with MIT License | 5 votes |
def setUp(self): self.input = tensor.ftensor4() self.filters = tensor.ftensor4() self.topgrad = tensor.ftensor4()