Python theano.tensor() Examples
The following are 30
code examples of theano.tensor().
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
, or try the search function
.
Example #1
Source File: layers.py From DL4MT with BSD 3-Clause "New" or "Revised" License | 6 votes |
def shared_dropout_layer(shape, use_noise, trng, value, scaled=True): #re-scale dropout at training time, so we don't need to at test time if scaled: proj = tensor.switch( use_noise, trng.binomial(shape, p=value, n=1, dtype='float32')/value, theano.shared(numpy.float32(1.))) else: proj = tensor.switch( use_noise, trng.binomial(shape, p=value, n=1, dtype='float32'), theano.shared(numpy.float32(value))) return proj # feedforward layer: affine transformation + point-wise nonlinearity
Example #2
Source File: gen_graph.py From D-VAE with MIT License | 6 votes |
def timeit_2vector_theano(init, nb_element=1e6, nb_repeat=3, nb_call=int(1e2), expr="a**2 + b**2 + 2*a*b"): t3 = timeit.Timer("tf(av,bv)", """ import theano import theano.tensor as T import numexpr as ne from theano.tensor import exp %(init)s av=a bv=b a=T.dvector() b=T.dvector() tf= theano.function([a,b],%(expr)s) """%locals() ) ret=t3.repeat(nb_repeat,nb_call) return np.asarray(ret)
Example #3
Source File: opt.py From D-VAE with MIT License | 6 votes |
def local_csm_properties_csm(node): """ If we find csm_properties(CSM(*args)), then we can replace that with the *args directly. """ if node.op == csm_properties: csm, = node.inputs if csm.owner and (csm.owner.op == CSC or csm.owner.op == CSR): # csm.owner.inputs could be broadcastable. In that case, we have # to adjust the broadcasting flag here. ret_var = [theano.tensor.patternbroadcast(i, o.broadcastable) for i, o in izip(csm.owner.inputs, node.outputs)] return ret_var return False
Example #4
Source File: util.py From gated-graph-transformer-network with MIT License | 6 votes |
def broadcast_concat(tensors, axis): """ Broadcast tensors together, then concatenate along axis """ ndim = tensors[0].ndim assert all(t.ndim == ndim for t in tensors), "ndims don't match for broadcast_concat: {}".format(tensors) broadcast_shapes = [] for i in range(ndim): if i == axis: broadcast_shapes.append(1) else: dim_size = next((t.shape[i] for t in tensors if not t.broadcastable[i]), 1) broadcast_shapes.append(dim_size) broadcasted_tensors = [] for t in tensors: tile_reps = [bshape if t.broadcastable[i] else 1 for i,bshape in enumerate(broadcast_shapes)] if all(rep is 1 for rep in tile_reps): # Don't need to broadcast this tensor broadcasted_tensors.append(t) else: broadcasted_tensors.append(T.tile(t, tile_reps)) return T.concatenate(broadcasted_tensors, axis)
Example #5
Source File: opt.py From D-VAE with MIT License | 6 votes |
def make_node(self, x, y): x, y = sparse.as_sparse_variable(x), tensor.as_tensor_variable(y) out_dtype = scalar.upcast(x.type.dtype, y.type.dtype) if self.inplace: assert out_dtype == y.dtype indices, indptr, data = csm_indices(x), csm_indptr(x), csm_data(x) # We either use CSC or CSR depending on the format of input assert self.format == x.type.format # The magic number two here arises because L{scipy.sparse} # objects must be matrices (have dimension 2) assert y.type.ndim == 2 out = tensor.TensorType(dtype=out_dtype, broadcastable=y.type.broadcastable)() return gof.Apply(self, [data, indices, indptr, y], [out])
Example #6
Source File: opt.py From D-VAE with MIT License | 6 votes |
def make_node(self, x, y, p_data, p_ind, p_ptr, p_ncols): x = tensor.as_tensor_variable(x) y = tensor.as_tensor_variable(y) p_data = tensor.as_tensor_variable(p_data) p_ind = tensor.as_tensor_variable(p_ind) p_ptr = tensor.as_tensor_variable(p_ptr) p_ncols = tensor.as_tensor_variable(p_ncols) assert p_ncols.dtype == 'int32' dtype_out = scalar.upcast(x.type.dtype, y.type.dtype, p_data.type.dtype) dot_out = scalar.upcast(x.type.dtype, y.type.dtype) # We call blas ?dot function that take only param of the same type x = tensor.cast(x, dot_out) y = tensor.cast(y, dot_out) return gof.Apply(self, [x, y, p_data, p_ind, p_ptr, p_ncols], [ tensor.tensor(dtype=dtype_out, broadcastable=(False,)), tensor.tensor(dtype=p_ind.type.dtype, broadcastable=(False,)), tensor.tensor(dtype=p_ptr.type.dtype, broadcastable=(False,)) ])
Example #7
Source File: util.py From gated-graph-transformer-network with MIT License | 6 votes |
def reduce_log_sum(tensor, axis=None, guaranteed_finite=False): """ Sum probabilities in the log domain, i.e return log(e^vec[0] + e^vec[1] + ...) = log(e^x e^(vec[0]-x) + e^x e^(vec[1]-x) + ...) = log(e^x [e^(vec[0]-x) + e^(vec[1]-x) + ...]) = log(e^x) + log(e^(vec[0]-x) + e^(vec[1]-x) + ...) = x + log(e^(vec[0]-x) + e^(vec[1]-x) + ...) For numerical stability, we choose x = max(vec) Note that if x is -inf, that means all values are -inf, so the answer should be -inf. In this case, choose x = 0 """ maxval = T.max(tensor, axis) maxval_full = T.max(tensor, axis, keepdims=True) if not guaranteed_finite: maxval = T.switch(T.isfinite(maxval), maxval, T.zeros_like(maxval)) maxval_full = T.switch(T.isfinite(maxval_full), maxval_full, T.zeros_like(maxval_full)) reduced_sum = T.sum(T.exp(tensor - maxval_full), axis) logsum = maxval + T.log(reduced_sum) return logsum
Example #8
Source File: nmt.py From nmt with BSD 3-Clause "New" or "Revised" License | 6 votes |
def debugging_adadelta(lr, tparams, grads, inp, cost): zipped_grads = [theano.shared(p.get_value() * numpy.float32(0.), name='%s_grad'%k) for k, p in tparams.iteritems()] running_up2 = [theano.shared(p.get_value() * numpy.float32(0.), name='%s_rup2'%k) for k, p in tparams.iteritems()] running_grads2 = [theano.shared(p.get_value() * numpy.float32(0.), name='%s_rgrad2'%k) for k, p in tparams.iteritems()] zgup = [(zg, g) for zg, g in zip(zipped_grads, grads)] rg2up = [(rg2, 0.95 * rg2 + 0.05 * (g ** 2)) for rg2, g in zip(running_grads2, grads)] f_grad_shared = theano.function(inp, cost, updates=zgup+rg2up, profile=profile) updir = [-tensor.sqrt(ru2 + 1e-6) / tensor.sqrt(rg2 + 1e-6) * zg for zg, ru2, rg2 in zip(zipped_grads, running_up2, running_grads2)] ru2up = [(ru2, 0.95 * ru2 + 0.05 * (ud ** 2)) for ru2, ud in zip(running_up2, updir)] param_up = [(p, p + ud) for p, ud in zip(itemlist(tparams), updir)] f_update = theano.function([lr], [], updates=ru2up+param_up, on_unused_input='ignore', profile=profile) return f_grad_shared, f_update
Example #9
Source File: nmt.py From nmt with BSD 3-Clause "New" or "Revised" License | 6 votes |
def adadelta(lr, tparams, grads, inp, cost): running_up2 = [theano.shared(p.get_value() * numpy.float32(0.), name='%s_rup2'%k) for k, p in tparams.iteritems()] running_grads2 = [theano.shared(p.get_value() * numpy.float32(0.), name='%s_rgrad2'%k) for k, p in tparams.iteritems()] rg2_new = [0.95 * rg2 + 0.05 * (g ** 2) for rg2, g in zip(running_grads2, grads)] rg2up = [(rg2, r_n) for rg2, r_n in zip(running_grads2, rg2_new)] updir = [-tensor.sqrt(ru2 + 1e-6) / tensor.sqrt(rg2 + 1e-6) * zg for zg, ru2, rg2 in zip(grads, running_up2, rg2_new)] ru2up = [(ru2, 0.95 * ru2 + 0.05 * (ud ** 2)) for ru2, ud in zip(running_up2, updir)] param_up = [(p, p + ud) for p, ud in zip(itemlist(tparams), updir)] inp += [lr] f_update = theano.function(inp, cost, updates=rg2up+ru2up+param_up, on_unused_input='ignore', profile=profile) return f_update
Example #10
Source File: logistic_sgd.py From deeplearn_hsi with BSD 2-Clause "Simplified" License | 6 votes |
def errors(self, y): """Return a float representing the number of errors in the minibatch over the total number of examples of the minibatch ; zero one loss over the size of the minibatch :type y: theano.tensor.TensorType :param y: corresponds to a vector that gives for each example the correct label """ # check if y has same dimension of y_pred if y.ndim != self.y_pred.ndim: raise TypeError( 'y should have the same shape as self.y_pred', ('y', y.type, 'y_pred', self.y_pred.type) ) # check if y is of the correct datatype if y.dtype.startswith('int'): # the T.neq operator returns a vector of 0s and 1s, where 1 # represents a mistake in prediction return T.mean(T.neq(self.y_pred, y)) else: raise NotImplementedError()
Example #11
Source File: skipthoughts.py From text-to-image with MIT License | 6 votes |
def build_encoder_bi(tparams, options): """ build bidirectional encoder, given pre-computed word embeddings """ # word embedding (source) embedding = tensor.tensor3('embedding', dtype='float32') embeddingr = embedding[::-1] x_mask = tensor.matrix('x_mask', dtype='float32') xr_mask = x_mask[::-1] # encoder proj = get_layer(options['encoder'])[1](tparams, embedding, options, prefix='encoder', mask=x_mask) projr = get_layer(options['encoder'])[1](tparams, embeddingr, options, prefix='encoder_r', mask=xr_mask) ctx = tensor.concatenate([proj[0][-1], projr[0][-1]], axis=1) return embedding, x_mask, ctx # some utilities
Example #12
Source File: layers.py From 3D-R2N2 with MIT License | 6 votes |
def set_output(self): output_shape = self._output_shape padding = self._padding unpool_size = self._unpool_size unpooled_output = tensor.alloc(0.0, # Value to fill the tensor output_shape[0], output_shape[1] + 2 * padding[0], output_shape[2], output_shape[3] + 2 * padding[1], output_shape[4] + 2 * padding[2]) unpooled_output = tensor.set_subtensor(unpooled_output[:, padding[0]:output_shape[ 1] + padding[0]:unpool_size[0], :, padding[1]:output_shape[3] + padding[1]:unpool_size[ 1], padding[2]:output_shape[4] + padding[2]:unpool_size[2]], self._prev_layer.output) self._output = unpooled_output
Example #13
Source File: layers.py From 3D-R2N2 with MIT License | 6 votes |
def set_output(self): padding = self._padding input_shape = self._input_shape if np.sum(self._padding) > 0: padded_input = tensor.alloc(0.0, # Value to fill the tensor input_shape[0], input_shape[1] + 2 * padding[1], input_shape[2], input_shape[3] + 2 * padding[3], input_shape[4] + 2 * padding[4]) padded_input = tensor.set_subtensor( padded_input[:, padding[1]:padding[1] + input_shape[1], :, padding[3]:padding[3] + input_shape[3], padding[4]:padding[4] + input_shape[4]], self._prev_layer.output) else: padded_input = self._prev_layer.output self._output = conv3d2d.conv3d(padded_input, self.W.val) + \ self.b.val.dimshuffle('x', 'x', 0, 'x', 'x')
Example #14
Source File: layers.py From 3D-R2N2 with MIT License | 6 votes |
def set_output(self): padding = self._padding input_shape = self._input_shape padded_input = tensor.alloc(0.0, # Value to fill the tensor input_shape[0], input_shape[1] + 2 * padding[1], input_shape[2], input_shape[3] + 2 * padding[3], input_shape[4] + 2 * padding[4]) padded_input = tensor.set_subtensor(padded_input[:, padding[1]:padding[1] + input_shape[ 1], :, padding[3]:padding[3] + input_shape[3], padding[4]:padding[4] + input_shape[4]], self._prev_layer.output) fc_output = tensor.reshape( tensor.dot(self._fc_layer.output, self.Wx.val), self._output_shape) self._output = conv3d2d.conv3d(padded_input, self.Wh.val) + \ fc_output + self.b.val.dimshuffle('x', 'x', 0, 'x', 'x')
Example #15
Source File: layers.py From 3D-R2N2 with MIT License | 6 votes |
def set_output(self): padding = self._padding input_shape = self._input_shape padded_input = tensor.alloc(0.0, # Value to fill the tensor input_shape[0], input_shape[1] + 2 * padding[1], input_shape[2], input_shape[3] + 2 * padding[3], input_shape[4] + 2 * padding[4]) padded_input = tensor.set_subtensor(padded_input[:, padding[1]:padding[1] + input_shape[ 1], :, padding[3]:padding[3] + input_shape[3], padding[4]:padding[4] + input_shape[4]], self._prev_layer.output) self._output = conv3d2d.conv3d(padded_input, self.W.val) + \ self.b.val.dimshuffle('x', 'x', 0, 'x', 'x')
Example #16
Source File: core.py From CAPTCHA-breaking with MIT License | 6 votes |
def __init__(self, layers, mode='sum'): ''' Merge the output of a list of layers or containers into a single tensor. mode: {'sum', 'concat'} ''' if len(layers) < 2: raise Exception("Please specify two or more input layers (or containers) to merge") self.mode = mode self.layers = layers self.params = [] self.regularizers = [] self.constraints = [] for l in self.layers: params, regs, consts = l.get_params() self.regularizers += regs # params and constraints have the same size for p, c in zip(params, consts): if p not in self.params: self.params.append(p) self.constraints.append(c)
Example #17
Source File: skipthoughts.py From StackGAN with MIT License | 6 votes |
def build_encoder_bi(tparams, options): """ build bidirectional encoder, given pre-computed word embeddings """ # word embedding (source) embedding = tensor.tensor3('embedding', dtype='float32') embeddingr = embedding[::-1] x_mask = tensor.matrix('x_mask', dtype='float32') xr_mask = x_mask[::-1] # encoder proj = get_layer(options['encoder'])[1](tparams, embedding, options, prefix='encoder', mask=x_mask) projr = get_layer(options['encoder'])[1](tparams, embeddingr, options, prefix='encoder_r', mask=xr_mask) ctx = tensor.concatenate([proj[0][-1], projr[0][-1]], axis=1) return embedding, x_mask, ctx # some utilities
Example #18
Source File: skipthoughts.py From text-to-image with MIT License | 6 votes |
def build_encoder_bi(tparams, options): """ build bidirectional encoder, given pre-computed word embeddings """ # word embedding (source) embedding = tensor.tensor3('embedding', dtype='float32') embeddingr = embedding[::-1] x_mask = tensor.matrix('x_mask', dtype='float32') xr_mask = x_mask[::-1] # encoder proj = get_layer(options['encoder'])[1](tparams, embedding, options, prefix='encoder', mask=x_mask) projr = get_layer(options['encoder'])[1](tparams, embeddingr, options, prefix='encoder_r', mask=xr_mask) ctx = tensor.concatenate([proj[0][-1], projr[0][-1]], axis=1) return embedding, x_mask, ctx # some utilities
Example #19
Source File: net.py From 3D-R2N2 with MIT License | 6 votes |
def __init__(self, random_seed=dt.datetime.now().microsecond, compute_grad=True): self.rng = np.random.RandomState(random_seed) self.batch_size = cfg.CONST.BATCH_SIZE self.img_w = cfg.CONST.IMG_W self.img_h = cfg.CONST.IMG_H self.n_vox = cfg.CONST.N_VOX self.compute_grad = compute_grad # (self.batch_size, 3, self.img_h, self.img_w), # override x and is_x_tensor4 when using multi-view network self.x = tensor.tensor4() self.is_x_tensor4 = True # (self.batch_size, self.n_vox, 2, self.n_vox, self.n_vox), self.y = tensor5() self.activations = [] # list of all intermediate activations self.loss = [] # final loss self.output = [] # final output self.error = [] # final output error self.params = [] # all learnable params self.grads = [] # will be filled out automatically self.setup()
Example #20
Source File: optimizers.py From DL4MT with BSD 3-Clause "New" or "Revised" License | 5 votes |
def rmsprop(lr, tparams, grads, inp, cost, profile=False): zipped_grads = [theano.shared(p.get_value() * numpy.float32(0.), name='%s_grad' % k) for k, p in tparams.iteritems()] running_grads = [theano.shared(p.get_value() * numpy.float32(0.), name='%s_rgrad' % k) for k, p in tparams.iteritems()] running_grads2 = [theano.shared(p.get_value() * numpy.float32(0.), name='%s_rgrad2' % k) for k, p in tparams.iteritems()] zgup = [(zg, g) for zg, g in zip(zipped_grads, grads)] rgup = [(rg, 0.95 * rg + 0.05 * g) for rg, g in zip(running_grads, grads)] rg2up = [(rg2, 0.95 * rg2 + 0.05 * (g ** 2)) for rg2, g in zip(running_grads2, grads)] f_grad_shared = theano.function(inp, cost, updates=zgup+rgup+rg2up, profile=profile) updir = [theano.shared(p.get_value() * numpy.float32(0.), name='%s_updir' % k) for k, p in tparams.iteritems()] updir_new = [(ud, 0.9 * ud - 1e-4 * zg / tensor.sqrt(rg2 - rg ** 2 + 1e-4)) for ud, zg, rg, rg2 in zip(updir, zipped_grads, running_grads, running_grads2)] param_up = [(p, p + udn[1]) for p, udn in zip(itemlist(tparams), updir_new)] f_update = theano.function([lr], [], updates=updir_new+param_up, on_unused_input='ignore', profile=profile) return f_grad_shared, f_update
Example #21
Source File: skipthoughts.py From StackGAN with MIT License | 5 votes |
def build_encoder(tparams, options): """ build an encoder, given pre-computed word embeddings """ # word embedding (source) embedding = tensor.tensor3('embedding', dtype='float32') x_mask = tensor.matrix('x_mask', dtype='float32') # encoder proj = get_layer(options['encoder'])[1](tparams, embedding, options, prefix='encoder', mask=x_mask) ctx = proj[0][-1] return embedding, x_mask, ctx
Example #22
Source File: optimizers.py From DL4MT with BSD 3-Clause "New" or "Revised" License | 5 votes |
def adam(lr, tparams, grads, inp, cost, beta1=0.9, beta2=0.999, e=1e-8, profile=False): gshared = [theano.shared(p.get_value() * 0., name='%s_grad' % k) for k, p in tparams.iteritems()] gsup = [(gs, g) for gs, g in zip(gshared, grads)] f_grad_shared = theano.function(inp, cost, updates=gsup, profile=profile) updates = [] t_prev = theano.shared(numpy.float32(0.)) t = t_prev + 1. lr_t = lr * tensor.sqrt(1. - beta2**t) / (1. - beta1**t) for p, g in zip(tparams.values(), gshared): m = theano.shared(p.get_value() * 0., p.name + '_mean') v = theano.shared(p.get_value() * 0., p.name + '_variance') m_t = beta1 * m + (1. - beta1) * g v_t = beta2 * v + (1. - beta2) * g**2 step = lr_t * m_t / (tensor.sqrt(v_t) + e) p_t = p - step updates.append((m, m_t)) updates.append((v, v_t)) updates.append((p, p_t)) updates.append((t_prev, t)) f_update = theano.function([lr], [], updates=updates, on_unused_input='ignore', profile=profile) return f_grad_shared, f_update
Example #23
Source File: layers.py From DL4MT with BSD 3-Clause "New" or "Revised" License | 5 votes |
def fflayer(tparams, state_below, options, prefix='rconv', activ='lambda x: tensor.tanh(x)', **kwargs): return eval(activ)( tensor.dot(state_below, tparams[pp(prefix, 'W')]) + tparams[pp(prefix, 'b')]) # GRU layer
Example #24
Source File: optimizers.py From DL4MT with BSD 3-Clause "New" or "Revised" License | 5 votes |
def adadelta(lr, tparams, grads, inp, cost, profile=False): zipped_grads = [theano.shared(p.get_value() * numpy.float32(0.), name='%s_grad' % k) for k, p in tparams.iteritems()] running_up2 = [theano.shared(p.get_value() * numpy.float32(0.), name='%s_rup2' % k) for k, p in tparams.iteritems()] running_grads2 = [theano.shared(p.get_value() * numpy.float32(0.), name='%s_rgrad2' % k) for k, p in tparams.iteritems()] zgup = [(zg, g) for zg, g in zip(zipped_grads, grads)] rg2up = [(rg2, 0.95 * rg2 + 0.05 * (g ** 2)) for rg2, g in zip(running_grads2, grads)] f_grad_shared = theano.function(inp, cost, updates=zgup+rg2up, profile=profile) updir = [-tensor.sqrt(ru2 + 1e-6) / tensor.sqrt(rg2 + 1e-6) * zg for zg, ru2, rg2 in zip(zipped_grads, running_up2, running_grads2)] ru2up = [(ru2, 0.95 * ru2 + 0.05 * (ud ** 2)) for ru2, ud in zip(running_up2, updir)] param_up = [(p, p + ud) for p, ud in zip(itemlist(tparams), updir)] f_update = theano.function([lr], [], updates=ru2up+param_up, on_unused_input='ignore', profile=profile) return f_grad_shared, f_update
Example #25
Source File: opt.py From D-VAE with MIT License | 5 votes |
def make_node(self, a_data, a_indices, a_indptr, b): b = tensor.as_tensor_variable(b) a_data = tensor.as_tensor_variable(a_data) a_indices = tensor.as_tensor_variable(a_indices) a_indptr = tensor.as_tensor_variable(a_indptr) assert a_data.type.ndim == 1 assert a_indices.type.ndim == 1 assert a_indptr.type.ndim == 1 assert b.type.ndim == 1 return gof.Apply(self, [a_data, a_indices, a_indptr, b], [tensor.tensor(b.dtype, (False,))])
Example #26
Source File: opt.py From D-VAE with MIT License | 5 votes |
def make_node(self, a_data, a_indices, a_indptr, b): assert b.type.ndim == 1 return gof.Apply(self, [a_data, a_indices, a_indptr, b], [tensor.tensor(b.dtype, (False,))])
Example #27
Source File: core.py From CAPTCHA-breaking with MIT License | 5 votes |
def get_output(self, train=False): X = self.get_input(train) nshape = make_tuple(X.shape[0], *self.dims) return theano.tensor.reshape(X, nshape)
Example #28
Source File: core.py From CAPTCHA-breaking with MIT License | 5 votes |
def get_output(self, train=False): X = self.get_input(train) size = theano.tensor.prod(X.shape) // X.shape[0] nshape = (X.shape[0], size) return theano.tensor.reshape(X, nshape)
Example #29
Source File: embeddings.py From CAPTCHA-breaking with MIT License | 5 votes |
def get_output(self, train=False): X = self.get_input(train) w = self.W_w[X[:, 0]] # nb_samples, proj_dim c = self.W_c[X[:, 1]] # nb_samples, proj_dim dot = T.sum(w * c, axis=1) dot = theano.tensor.reshape(dot, (X.shape[0], 1)) return self.activation(dot)
Example #30
Source File: core.py From CAPTCHA-breaking with MIT License | 5 votes |
def get_output(self, train=False): X = self.get_input(train) tensors = [X]*self.n stacked = theano.tensor.stack(*tensors) return stacked.dimshuffle((1, 0, 2))