Python theano.tensor.shape() Examples
The following are 30
code examples of theano.tensor.shape().
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: basic.py From D-VAE with MIT License | 6 votes |
def perform(self, node, inputs, outputs): (x_data, x_indices, x_indptr, x_shape, g_data, g_indices, g_indptr, g_shape) = inputs (g_out,) = outputs if len(x_indptr) - 1 == x_shape[0]: sp_dim = x_shape[1] else: sp_dim = x_shape[0] g_row = numpy.zeros(sp_dim, dtype=g_data.dtype) gout_data = numpy.zeros(x_data.shape, dtype=node.outputs[0].dtype) for i in range(len(x_indptr) - 1): for j_ptr in range(g_indptr[i], g_indptr[i + 1]): g_row[g_indices[j_ptr]] += g_data[j_ptr] for j_ptr in range(x_indptr[i], x_indptr[i + 1]): gout_data[j_ptr] = g_row[x_indices[j_ptr]] for j_ptr in range(g_indptr[i], g_indptr[i + 1]): g_row[g_indices[j_ptr]] = 0 g_out[0] = gout_data
Example #2
Source File: basic.py From attention-lvcsr with MIT License | 6 votes |
def structured_monoid(tensor_op): # Generic operation to perform many kinds of monoid element-wise # operations on the non-zeros of a sparse matrix. # The first parameter must always be a sparse matrix. The other parameters # must be scalars which will be passed as argument to the tensor_op. def decorator(f): def wrapper(*args): x = as_sparse_variable(args[0]) assert x.format in ["csr", "csc"] xs = [scalar.as_scalar(arg) for arg in args[1:]] data, ind, ptr, shape = csm_properties(x) data = tensor_op(data, *xs) return CSM(x.format)(data, ind, ptr, shape) wrapper.__name__ = str(tensor_op.scalar_op) return wrapper return decorator
Example #3
Source File: basic.py From attention-lvcsr with MIT License | 6 votes |
def perform(self, node, inputs, outputs): (a_indices, a_indptr, b, g_ab) = inputs (out,) = outputs g_a_data = numpy.zeros(a_indices.shape, dtype=g_ab.dtype) for j in xrange(len(a_indptr) - 1): ind0 = a_indptr[j] ind1 = a_indptr[j + 1] for i_idx in xrange(ind0, ind1): i = a_indices[i_idx] # Depending on the type of g_ab and b (sparse or dense), # the following dot product can result in a scalar or # a (1, 1) sparse matrix. dot_val = numpy.dot(g_ab[i], b[j].T) if isinstance(dot_val, scipy.sparse.spmatrix): dot_val = dot_val[0, 0] g_a_data[i_idx] = dot_val out[0] = g_a_data
Example #4
Source File: basic.py From attention-lvcsr with MIT License | 6 votes |
def perform(self, node, inputs, outputs): (a_indices, a_indptr, b, g_ab) = inputs (out,) = outputs g_a_data = numpy.zeros(a_indices.shape, dtype=g_ab.dtype) for i in xrange(len(a_indptr) - 1): # loop over rows ind0 = a_indptr[i] ind1 = a_indptr[i + 1] # loop over values in that row (columns) for j_idx in xrange(ind0, ind1): j = a_indices[j_idx] # grad is dot product of i-th row of gradient with j-th row of b # Depending on the type of g_ab and b (sparse or dense), # the following dot product can result in a scalar or # a (1, 1) sparse matrix. dot_val = numpy.dot(g_ab[i], b[j].T) if isinstance(dot_val, scipy.sparse.spmatrix): dot_val = dot_val[0, 0] g_a_data[j_idx] = dot_val out[0] = g_a_data
Example #5
Source File: model.py From gogh-figure with GNU Affero General Public License v3.0 | 6 votes |
def setup_transform_net(self, input_var=None): transform_net = InputLayer(shape=self.shape, input_var=input_var) transform_net = style_conv_block(transform_net, self.num_styles, 32, 9, 1) transform_net = style_conv_block(transform_net, self.num_styles, 64, 3, 2) transform_net = style_conv_block(transform_net, self.num_styles, 128, 3, 2) for _ in range(5): transform_net = residual_block(transform_net, self.num_styles) transform_net = nn_upsample(transform_net, self.num_styles) transform_net = nn_upsample(transform_net, self.num_styles) if self.net_type == 0: transform_net = style_conv_block(transform_net, self.num_styles, 3, 9, 1, tanh) transform_net = ExpressionLayer(transform_net, lambda X: 150.*X, output_shape=None) elif self.net_type == 1: transform_net = style_conv_block(transform_net, self.num_styles, 3, 9, 1, sigmoid) self.network['transform_net'] = transform_net
Example #6
Source File: model.py From gogh-figure with GNU Affero General Public License v3.0 | 6 votes |
def __init__(self, input_var=None, num_styles=None, shape=(None, 3, 256, 256), net_type=1, **kwargs): """ net_type: 0 (fast neural style- fns) or 1 (conditional instance norm- cin) """ assert net_type in [0, 1] self.net_type = net_type self.network = {} if len(shape) == 2: shape=(None, 3, shape[0], shape[1]) elif len(shape) == 3: shape=(None, shape[0], shape[1], shape[2]) self.shape = shape self.num_styles = num_styles self.network['loss_net'] = {} self.setup_loss_net() self.load_loss_net_weights() self.network['transform_net'] = {} self.setup_transform_net(input_var)
Example #7
Source File: basic.py From D-VAE with MIT License | 6 votes |
def grad(self, inputs, gout): (gz,) = gout is_continuous = [(inputs[i].dtype in tensor.continuous_dtypes) for i in range(len(inputs))] if _is_sparse_variable(gz): gz = dense_from_sparse(gz) split = tensor.Split(len(inputs))(gz, 0, tensor.stack( [x.shape[0] for x in inputs])) if not isinstance(split, list): split = [split] derivative = [SparseFromDense(self.format)(s) for s in split] def choose(continuous, derivative): if continuous: return derivative else: return None return [choose(c, d) for c, d in zip(is_continuous, derivative)]
Example #8
Source File: basic.py From attention-lvcsr with MIT License | 6 votes |
def grad(self, inputs, gout): (gz,) = gout is_continuous = [(inputs[i].dtype in tensor.continuous_dtypes) for i in range(len(inputs))] if _is_sparse_variable(gz): gz = dense_from_sparse(gz) split = tensor.Split(len(inputs))(gz, 0, tensor.stack( [x.shape[0] for x in inputs])) if not isinstance(split, list): split = [split] derivative = [SparseFromDense(self.format)(s) for s in split] def choose(continuous, derivative): if continuous: return derivative else: return None return [choose(c, d) for c, d in zip(is_continuous, derivative)]
Example #9
Source File: basic.py From D-VAE with MIT License | 6 votes |
def perform(self, node, inputs, outputs): (x, s) = inputs (z,) = outputs M, N = x.shape assert x.format == 'csc' assert s.shape == (M,) indices = x.indices indptr = x.indptr y_data = x.data.copy() for j in xrange(0, N): for i_idx in xrange(indptr[j], indptr[j + 1]): y_data[i_idx] *= s[indices[i_idx]] z[0] = scipy.sparse.csc_matrix((y_data, indices, indptr), (M, N))
Example #10
Source File: basic.py From attention-lvcsr with MIT License | 6 votes |
def perform(self, node, inputs, outputs): (x, s) = inputs (z,) = outputs M, N = x.shape assert x.format == 'csc' assert s.shape == (M,) indices = x.indices indptr = x.indptr y_data = x.data.copy() for j in xrange(0, N): for i_idx in xrange(indptr[j], indptr[j + 1]): y_data[i_idx] *= s[indices[i_idx]] z[0] = scipy.sparse.csc_matrix((y_data, indices, indptr), (M, N))
Example #11
Source File: basic.py From D-VAE with MIT License | 6 votes |
def structured_monoid(tensor_op): # Generic operation to perform many kinds of monoid element-wise # operations on the non-zeros of a sparse matrix. # The first parameter must always be a sparse matrix. The other parameters # must be scalars which will be passed as argument to the tensor_op. def decorator(f): def wrapper(*args): x = as_sparse_variable(args[0]) assert x.format in ["csr", "csc"] xs = [scalar.as_scalar(arg) for arg in args[1:]] data, ind, ptr, shape = csm_properties(x) data = tensor_op(data, *xs) return CSM(x.format)(data, ind, ptr, shape) wrapper.__name__ = str(tensor_op.scalar_op) return wrapper return decorator
Example #12
Source File: basic.py From D-VAE with MIT License | 6 votes |
def perform(self, node, inputs, outputs): # for efficiency, if remap does nothing, then do not apply it (data, indices, indptr, shape) = inputs (out,) = outputs if len(shape) != 2: raise ValueError('Shape should be an array of length 2') if data.shape != indices.shape: errmsg = ('Data (shape ' + repr(data.shape) + ' must have the same number of elements ' + 'as indices (shape' + repr(indices.shape) + ')') raise ValueError(errmsg) if self.format == 'csc': out[0] = scipy.sparse.csc_matrix((data, indices.copy(), indptr.copy()), numpy.asarray(shape), copy=False) else: assert self.format == 'csr' out[0] = scipy.sparse.csr_matrix((data, indices.copy(), indptr.copy()), shape.copy(), copy=False)
Example #13
Source File: basic.py From attention-lvcsr with MIT License | 6 votes |
def sp_ones_like(x): """ Construct a sparse matrix of ones with the same sparsity pattern. Parameters ---------- x Sparse matrix to take the sparsity pattern. Returns ------- A sparse matrix The same as `x` with data changed for ones. """ # TODO: don't restrict to CSM formats data, indices, indptr, shape = csm_properties(x) return CSM(format=x.format)(tensor.ones_like(data), indices, indptr, shape)
Example #14
Source File: basic.py From attention-lvcsr with MIT License | 6 votes |
def sp_zeros_like(x): """ Construct a sparse matrix of zeros. Parameters ---------- x Sparse matrix to take the shape. Returns ------- A sparse matrix The same as `x` with zero entries for all element. """ # TODO: don't restrict to CSM formats _, _, indptr, shape = csm_properties(x) return CSM(format=x.format)(data=numpy.array([], dtype=x.type.dtype), indices=numpy.array([], dtype='int32'), indptr=tensor.zeros_like(indptr), shape=shape)
Example #15
Source File: basic.py From D-VAE with MIT License | 6 votes |
def perform(self, node, inputs, outputs): (a_indices, a_indptr, b, g_ab) = inputs (out,) = outputs g_a_data = numpy.zeros(a_indices.shape, dtype=g_ab.dtype) for j in xrange(len(a_indptr) - 1): ind0 = a_indptr[j] ind1 = a_indptr[j + 1] for i_idx in xrange(ind0, ind1): i = a_indices[i_idx] # Depending on the type of g_ab and b (sparse or dense), # the following dot product can result in a scalar or # a (1, 1) sparse matrix. dot_val = numpy.dot(g_ab[i], b[j].T) if isinstance(dot_val, scipy.sparse.spmatrix): dot_val = dot_val[0, 0] g_a_data[i_idx] = dot_val out[0] = g_a_data
Example #16
Source File: basic.py From D-VAE with MIT License | 6 votes |
def perform(self, node, inputs, outputs): (a_indices, a_indptr, b, g_ab) = inputs (out,) = outputs g_a_data = numpy.zeros(a_indices.shape, dtype=g_ab.dtype) for i in xrange(len(a_indptr) - 1): # loop over rows ind0 = a_indptr[i] ind1 = a_indptr[i + 1] # loop over values in that row (columns) for j_idx in xrange(ind0, ind1): j = a_indices[j_idx] # grad is dot product of i-th row of gradient with j-th row of b # Depending on the type of g_ab and b (sparse or dense), # the following dot product can result in a scalar or # a (1, 1) sparse matrix. dot_val = numpy.dot(g_ab[i], b[j].T) if isinstance(dot_val, scipy.sparse.spmatrix): dot_val = dot_val[0, 0] g_a_data[j_idx] = dot_val out[0] = g_a_data
Example #17
Source File: basic.py From D-VAE with MIT License | 6 votes |
def sp_zeros_like(x): """ Construct a sparse matrix of zeros. Parameters ---------- x Sparse matrix to take the shape. Returns ------- A sparse matrix The same as `x` with zero entries for all element. """ # TODO: don't restrict to CSM formats _, _, indptr, shape = csm_properties(x) return CSM(format=x.format)(data=numpy.array([], dtype=x.type.dtype), indices=numpy.array([], dtype='int32'), indptr=tensor.zeros_like(indptr), shape=shape)
Example #18
Source File: basic.py From D-VAE with MIT License | 6 votes |
def sp_ones_like(x): """ Construct a sparse matrix of ones with the same sparsity pattern. Parameters ---------- x Sparse matrix to take the sparsity pattern. Returns ------- A sparse matrix The same as `x` with data changed for ones. """ # TODO: don't restrict to CSM formats data, indices, indptr, shape = csm_properties(x) return CSM(format=x.format)(tensor.ones_like(data), indices, indptr, shape)
Example #19
Source File: basic.py From attention-lvcsr with MIT License | 5 votes |
def perform(self, node, inp, outputs): (out,) = outputs x = inp[0] indices = inp[1] gz = inp[2] if x.format in ["csr"]: y = scipy.sparse.csr_matrix((x.shape[0], x.shape[1])) else: y = scipy.sparse.csc_matrix((x.shape[0], x.shape[1])) for a in range(0, len(indices)): y[indices[a]] = gz[a] out[0] = y
Example #20
Source File: basic.py From attention-lvcsr with MIT License | 5 votes |
def make_node(self, data, indices, indptr, shape): data = tensor.as_tensor_variable(data) if not isinstance(indices, gof.Variable): indices_ = numpy.asarray(indices) indices_32 = theano._asarray(indices, dtype='int32') assert (indices_ == indices_32).all() indices = indices_32 if not isinstance(indptr, gof.Variable): indptr_ = numpy.asarray(indptr) indptr_32 = theano._asarray(indptr, dtype='int32') assert (indptr_ == indptr_32).all() indptr = indptr_32 if not isinstance(shape, gof.Variable): shape_ = numpy.asarray(shape) shape_32 = theano._asarray(shape, dtype='int32') assert (shape_ == shape_32).all() shape = shape_32 indices = tensor.as_tensor_variable(indices) indptr = tensor.as_tensor_variable(indptr) shape = tensor.as_tensor_variable(shape) if data.type.ndim != 1: raise TypeError('data argument must be a vector', data.type, data.type.ndim) if indices.type.ndim != 1 or indices.type.dtype not in discrete_dtypes: raise TypeError('indices must be vector of integers', indices, indices.type) if indptr.type.ndim != 1 or indptr.type.dtype not in discrete_dtypes: raise TypeError('indices must be vector of integers', indptr, indptr.type) if shape.type.ndim != 1 or shape.type.dtype not in discrete_dtypes: raise TypeError('n_rows must be integer type', shape, shape.type) return gof.Apply(self, [data, indices, indptr, shape], [SparseType(dtype=data.type.dtype, format=self.format)()])
Example #21
Source File: basic.py From attention-lvcsr with MIT License | 5 votes |
def perform(self, node, inputs, outputs): (x,) = inputs (z,) = outputs N, M = x.shape if N != M: raise ValueError('Diag only apply on square matrix') z[0] = x.diagonal()
Example #22
Source File: basic.py From attention-lvcsr with MIT License | 5 votes |
def perform(self, node, inputs, outputs): (x, y) = inputs (out,) = outputs assert _is_sparse(x) and _is_sparse(y) assert x.shape == y.shape assert x.data.shape == y.data.shape out[0] = x.copy() out[0].data += y.data
Example #23
Source File: basic.py From attention-lvcsr with MIT License | 5 votes |
def perform(self, node, inputs, outputs): (x, y) = inputs (out,) = outputs assert _is_sparse(x) and _is_sparse(y) assert len(x.shape) == 2 assert y.shape == x.shape # This calls the element-wise multiple # x * y calls dot... out[0] = x.multiply(y)
Example #24
Source File: basic.py From attention-lvcsr with MIT License | 5 votes |
def perform(self, node, inputs, outputs): (x, y) = inputs (out,) = outputs assert _is_sparse(x) and _is_sparse(y) assert x.shape == y.shape out[0] = x + y
Example #25
Source File: basic.py From attention-lvcsr with MIT License | 5 votes |
def perform(self, node, inputs, outputs): (x_data, x_indices, x_indptr, x_shape, g_data, g_indices, g_indptr, g_shape) = inputs (g_out,) = outputs if len(x_indptr) - 1 == x_shape[0]: sp_dim = x_shape[1] else: sp_dim = x_shape[0] g_row = numpy.zeros(sp_dim, dtype=g_data.dtype) gout_data = numpy.zeros(x_data.shape, dtype=node.outputs[0].dtype) for i in range(len(x_indptr) - 1): for j_ptr in range(g_indptr[i], g_indptr[i + 1]): g_row[g_indices[j_ptr]] += g_data[j_ptr] for j_ptr in range(x_indptr[i], x_indptr[i + 1]): gout_data[j_ptr] = g_row[x_indices[j_ptr]] for j_ptr in range(g_indptr[i], g_indptr[i + 1]): g_row[g_indices[j_ptr]] = 0 if self.kmap is None: g_out[0] = gout_data else: grad = numpy.zeros_like(x_data) grad[self.kmap] = gout_data g_out[0] = grad
Example #26
Source File: basic.py From attention-lvcsr with MIT License | 5 votes |
def perform(self, node, inputs, outputs): # for efficiency, if remap does nothing, then do not apply it (data, indices, indptr, shape) = inputs (out,) = outputs if self.kmap is not None: data = data[self.kmap] if len(shape) != 2: raise ValueError('Shape should be an array of length 2') if (data.shape != indices.shape and numpy.size(data) != numpy.size(self.kmap)): errmsg = ('Data (shape ' + repr(data.shape) + ' must have the same number of elements ' + 'as indices (shape' + repr(indices.shape) + ') or elements as kmap (' + repr(numpy.size(self.kmap)) + ')') raise ValueError(errmsg) if self.format == 'csc': out[0] = scipy.sparse.csc_matrix((data, indices.copy(), indptr.copy()), numpy.asarray(shape), copy=False) else: assert self.format == 'csr' out[0] = scipy.sparse.csr_matrix((data, indices.copy(), indptr.copy()), shape.copy(), copy=False)
Example #27
Source File: conv_sup_mll.py From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License | 5 votes |
def load_data(mu, sigma, classn): X_test = np.empty(shape=(0, 3, 32, 32)); X_val = np.empty(shape=(0, 3, 32, 32)); X_train = np.empty(shape=(0, 3, 32, 32)); y_test = np.empty(shape=(0, classn)); y_val = np.empty(shape=(0, classn)); y_train = np.empty(shape=(0, classn)); lines = [line.rstrip('\n') for line in open('./data/image/label.txt')]; linen = 0; for line in lines: linen += 1; img = line.split('\t')[0]; lab = [int(x) for x in line.split('\t')[1].split()]; png = misc.imread('./data/' + img).transpose()[0 : 3, 9 : 41, 9 : 41]; png = np.expand_dims(png, axis=0).astype(np.float32) / 255; if linen % 100 <= 19: X_test = np.concatenate((X_test, png)); y_test = np.concatenate((y_test, np.expand_dims(np.array(lab), axis=0))); elif linen % 100 >= 20 and linen % 100 <= 24: X_val = np.concatenate((X_val, png)); y_val = np.concatenate((y_val, np.expand_dims(np.array(lab), axis=0))); elif linen % 100 >= 25: X_train = np.concatenate((X_train, png)); y_train = np.concatenate((y_train, np.expand_dims(np.array(lab), axis=0))); X_train = X_train.astype(np.float32); X_val = X_val.astype(np.float32); X_test = X_test.astype(np.float32); y_train = y_train.astype(np.uint8); y_val = y_val.astype(np.uint8); y_test = y_test.astype(np.uint8); X_train = (X_train - mu) / sigma; X_val = (X_val - mu) / sigma; X_test = (X_test - mu) / sigma; print "Data Loaded", X_train.shape, y_train.shape, X_val.shape, y_val.shape, X_test.shape, y_test.shape; sys.stdout.flush(); return X_train, y_train, X_val, y_val, X_test, y_test;
Example #28
Source File: basic.py From attention-lvcsr with MIT License | 5 votes |
def perform(self, node, inputs, outputs): (x, s) = inputs (z,) = outputs M, N = x.shape assert x.format == 'csc' assert s.shape == (N, ) y = x.copy() for j in xrange(0, N): y.data[y.indptr[j]: y.indptr[j + 1]] *= s[j] z[0] = y
Example #29
Source File: basic.py From attention-lvcsr with MIT License | 5 votes |
def perform(self, node, inputs, outputs): (x, y) = inputs (out,) = outputs assert _is_sparse(x) and not _is_sparse(y) assert x.shape[1] == y.shape[0] out[0] = x.__class__(x.toarray() * y)
Example #30
Source File: basic.py From attention-lvcsr with MIT License | 5 votes |
def perform(self, node, inputs, outputs): (x, y) = inputs (out,) = outputs assert _is_sparse(x) and _is_sparse(y) assert x.shape == y.shape out[0] = self.comparison(x, y).astype('uint8')