def batched_dot(x, y): """Batchwise dot product. This function computes the dot product between the two tensors, by iterating over the first dimension. """ shapeX = get_shape(x) shapeY = get_shape(y) ndimX = x.get_shape().ndims ndimY = y.get_shape().ndims # same as dot but one more batch dimension if ndimX > 2 + 1: x = tf.reshape(x, (-1, np.prod(shapeX[1:-1]), shapeX[-1])) if ndimY > 2 + 1: y_dims = list(range(ndimY)) y_dims = [y_dims.pop(0), y_dims.pop(-2)] + y_dims y = tf.transpose(y, perm=y_dims) outshapeY = tuple([shapeY[i] for i in y_dims[2:]]) y = tf.reshape(y, (-1, shapeY[-2], np.prod(outshapeY))) else: outshapeY = (shapeY[-1], ) # calculate dot product and desire shape output_shape = shapeX[:-1] + outshapeY output = tf.reshape(tf.matmul(x, y), [i if i is not None else -1 for i in output_shape]) return output
def dot(x, y): '''Multiplies 2 tensors. When attempting to multiply a ND tensor with a ND tensor, reproduces the Theano behavior e.g. (2, 3).(4, 3, 5) = (2, 4, 5) (2, 3, 4).(4, 5) = (2, 3, 5) ''' shapeX = get_shape(x) shapeY = get_shape(y) ndimX = x.get_shape().ndims ndimY = y.get_shape().ndims if ndimX > 2: x = tf.reshape(x, (-1, shapeX[-1])) if ndimY > 2: y_dims = list(range(ndimY)) y_dims = [y_dims.pop(-2)] + y_dims y = tf.transpose(y, perm=y_dims) y = tf.reshape(y, (shapeY[-2], -1)) outshapeY = tuple([shapeY[i] for i in y_dims[1:]]) else: outshapeY = (shapeY[-1], ) # calculate dot product and desire shape output_shape = [-1 if i is None else i for i in shapeX[:-1] + outshapeY] output = tf.reshape(tf.matmul(x, y), output_shape) return output
def bayes_crossentropy(y_pred, y_true, nb_classes=None): shape = get_shape(y_pred) if ndim(y_pred) == 1: y_pred = expand_dims(y_pred, -1) # add shape for y_pred0 so we can auto_infer the shape after concat y_pred0 = 1. - y_pred add_shape(y_pred0, shape) y_pred = concatenate([y_pred0, y_pred], axis=-1) elif isinstance(shape[-1], Number) and shape[-1] == 1: # add shape for y_pred0 so we can auto_infer the shape after concat y_pred0 = 1. - y_pred add_shape(y_pred0, shape) y_pred = concatenate([y_pred0, y_pred], axis=-1) if ndim(y_true) == 1: if nb_classes is None: raise Exception('y_pred and y_true must be one_hot encoded, ' 'otherwise you have to provide nb_classes.') y_true = one_hot(y_true, nb_classes) # avoid numerical instability with _EPSILON clipping y_pred = clip(y_pred, EPSILON, 1.0 - EPSILON) if nb_classes is None: nb_classes = get_shape(y_true)[1] # ====== check distribution ====== # distribution = sum(y_true, axis=0) # ====== init confusion info loss ====== # # weighted by y_true loss = y_true * log(y_pred) # probability distribution of each class prob_distribution = dimshuffle(distribution / sum(distribution), ('x', 0)) # we need to clip the prior probability distribution also prob_distribution = clip(prob_distribution, EPSILON, 1.0 - EPSILON) return -1 / nb_classes * sum(loss / prob_distribution, axis=1)
def conv2d(x, kernel, strides=(1, 1), border_mode='valid', filter_dilation=(1, 1)): """ Dimension is ordered by TH input shape: (samples, input_depth, rows, cols) TH kernel shape: (depth, input_depth, rows, cols) Parameters ---------- border_mode: string "same", "valid" or "full". Note ---- dim_ordering : tf-tensorflow (defaults), th-theano TH input shape: (samples, input_depth, conv_dim1, conv_dim2, conv_dim3) TF input shape: (samples, conv_dim1, conv_dim2, conv_dim3, input_depth) --- TH kernel shape: (out_depth, input_depth, kernel_dim1, kernel_dim2, kernel_dim3) TF kernel shape: (kernel_dim1, kernel_dim2, kernel_dim3, input_depth, out_depth) Only support float32 on CPU """ # store original information for calculating output_shape image_shape = get_shape(x) kernel_shape = get_shape(kernel) strides, border_mode, filter_dilation = __validate_strides_padding_dilation( strides, border_mode, filter_dilation, ndim=2) # convert to TF order is_float64 = False if 'float64' in x.dtype.name: # only conv in float32 x = tf.cast(x, 'float32') is_float64 = True if 'float64' in kernel.dtype.name: kernel = tf.cast(kernel, 'float32') if filter_dilation == (1, 1): x = tf.nn.conv2d(x, kernel, strides=(1, ) + strides + (1, ), padding=border_mode) else: assert filter_dilation[0] == filter_dilation[1] assert strides == (1, 1), 'Invalid strides for dilated convolution' x = tf.nn.atrous_conv2d(x, kernel, filter_dilation[0], padding=border_mode) # ====== estimate output shape ====== # if is_float64: x = tf.cast(x, 'float64') add_shape( x, get_conv_output_shape(image_shape, kernel_shape, border_mode, strides, filter_dilation)) return x
def conv2d(x, kernel, strides=(1, 1), border_mode='valid', filter_dilation=(1, 1)): """ Dimension is ordered by TH input shape: (samples, input_depth, rows, cols) TH kernel shape: (depth, input_depth, rows, cols) Parameters ---------- border_mode: string "same", "valid" or "full". Note ---- dim_ordering : tf-tensorflow (defaults), th-theano TH input shape: (samples, input_depth, conv_dim1, conv_dim2, conv_dim3) TF input shape: (samples, conv_dim1, conv_dim2, conv_dim3, input_depth) --- TH kernel shape: (out_depth, input_depth, kernel_dim1, kernel_dim2, kernel_dim3) TF kernel shape: (kernel_dim1, kernel_dim2, kernel_dim3, input_depth, out_depth) Warning ------- For "same" or "half" border_mode, the shape of output only equal to input if kernel shape is odd. """ image_shape = get_shape(x) kernel_shape = get_shape(kernel) if _any(i % 2 == 0 for i in kernel_shape[:-2]): print('[WARNING] Kernel shape %s contains even values, the output shape is ' 'different from the input shape in "same"-border_mode.' % str(kernel_shape[:-2])) strides, border_mode, filter_dilation = __validate_strides_padding_dilation( strides, border_mode, filter_dilation, ndim=2) # ====== convert input to theano format ====== # x = __img_theano_format(x) kernel = __ker_theano_format(kernel) conv_out = T.nnet.conv2d(x, kernel, border_mode=border_mode, subsample=strides, input_shape=(image_shape[0], image_shape[-1]) + image_shape[1:-1], filter_shape=(kernel_shape[-1], kernel_shape[-2]) + kernel_shape[:-2], filter_dilation=filter_dilation) # if border_mode == 'half': # if kernel_shape[2] % 2 == 0: # conv_out = conv_out[:, :, :(x.shape[2] + strides[0] - 1) // strides[0], :] # if kernel_shape[3] % 2 == 0: # conv_out = conv_out[:, :, :, :(x.shape[3] + strides[1] - 1) // strides[1]] # ====== estimate output shape ====== # conv_out = __img_tensorflow_format(conv_out) add_shape(conv_out, get_conv_output_shape(image_shape, kernel_shape, border_mode, strides, filter_dilation)) return conv_out
def categorical_crossentropy(output, target): """ NOTE: the crossentropy is different between tensorflow and theano If the `output` and `target` are mistaken the position, the gradients of cross-entropy cost w.r.t to all variables may be None in tensorflow. """ input_shape = get_shape(output) # scale preds so that the class of each sample sum to 1 output /= sum(output, axis=-1, keepdims=True) output = clip(output, EPSILON, 1.0 - EPSILON) if ndim(target) == 1: target = one_hot(target, get_shape(output)[-1]) x = backend_ops_categorical_crossentropy(output, target) add_shape(x, (input_shape[0], )) return x
def apply_noise(x, level=0.075, noise_dims=None, noise_type='gaussian'): """ Parameters ---------- x: A tensor. level : float or tensor scalar Standard deviation of added Gaussian noise noise_dims: int or list(int) these dimensions will be setted to 1 in noise_shape, and used to broadcast the dropout mask. noise_type: 'gaussian' (or 'normal'), 'uniform' distribution used for generating noise seed: random seed or `tensor.rng` random generator from tensor class Note ---- This function only apply noise on Variable when training is enable """ input_shape = get_shape(x) noise_type = noise_type.lower() # ====== not a training variable NO dropout ====== # if not is_training(): return x # ====== applying noise ====== # shape = get_shape(x, native=True) noise_shape = (shape if noise_dims is None else _process_noise_dim( shape, noise_dims, ndim(x))) if 'normal' in noise_type or 'gaussian' in noise_type: noise = random_normal(shape=noise_shape, mean=0.0, std=level, dtype=x.dtype) elif 'uniform' in noise_type: noise = random_uniform(shape=noise_shape, low=-level, high=level, dtype=x.dtype) # no idea why uniform does not give any broadcastable dimensions if noise_dims is not None: broadcastable = [i for i, j in enumerate(noise_shape) if j == 1] if len(broadcastable) > 0: noise = addbroadcast(noise, *broadcastable) else: raise ValueError('No support for noise_type=' + noise_type) x = x + noise if isinstance(input_shape, (tuple, list)): add_shape(x, input_shape) return x
def conv3d(x, kernel, strides=(1, 1, 1), border_mode='valid', filter_dilation=(1, 1, 1)): """ Run on cuDNN if available. border_mode: string, "same" or "valid". Note ---- dim_ordering : tf-tensorflow (__img_theano_format(x) TH input shape: (samples, input_depth, conv_dim1, conv_dim2, conv_dim3) TF input shape: (samples, conv_dim1, conv_dim2, conv_dim3, input_depth) --- TH kernel shape: (out_depth, input_depth, kernel_dim1, kernel_dim2, kernel_dim3) TF kernel shape: (kernel_dim1, kernel_dim2, kernel_dim3, input_depth, out_depth) """ # get and convert volume_shape to theano format volume_shape = get_shape(x) # get and convert filter_shape to theano format kernel_shape = get_shape(kernel) if _any(i % 2 == 0 for i in kernel_shape[:-2]): print('[WARNING] Kernel shape %s contains even values, the output shape is ' 'different from the input shape in "same"-border_mode.' % str(kernel_shape[:-2])) strides, border_mode, filter_dilation = __validate_strides_padding_dilation( strides, border_mode, filter_dilation, ndim=3) # ====== convert input to theano format ====== # x = __img_theano_format(x) kernel = __ker_theano_format(kernel) # call convolution conv_out = T.nnet.conv3d(x, kernel, border_mode=border_mode, subsample=strides, input_shape=(volume_shape[0], volume_shape[-1]) + volume_shape[1:-1], filter_shape=(kernel_shape[-1], kernel_shape[-2]) + kernel_shape[:-2], filter_dilation=filter_dilation) # if border_mode == 'half': # if kernel_shape[2] % 2 == 0: # conv_out = conv_out[:, :, :(x.shape[2] + strides[0] - 1) // strides[0], :, :] # if kernel_shape[3] % 2 == 0: # conv_out = conv_out[:, :, :, :(x.shape[3] + strides[1] - 1) // strides[1], :] # if kernel_shape[4] % 2 == 0: # conv_out = conv_out[:, :, :, :, :(x.shape[4] + strides[2] - 1) // strides[2]] # back to theano form # convert back to tensorflow shape conv_out = __img_tensorflow_format(conv_out) # infer output shape output_shape = get_conv_output_shape(volume_shape, kernel_shape, border_mode, strides, filter_dilation) add_shape(conv_out, output_shape) return conv_out
def dot(x, y): """ 2 special cases: (2, 3).(4, 3, 5) = (2, 4, 5) (2, 3, 4).(4, 5) = (2, 3, 5) """ output = T.dot(x, y) shapeX = get_shape(x) shapeY = get_shape(y) if isinstance(shapeX, (tuple, list)) and isinstance(shapeY, (tuple, list)): if y.ndim > 2: outshapeY = tuple([shapeY[i] for i in range(y.ndim) if i != y.ndim - 2]) else: outshapeY = (shapeY[-1],) add_shape(output, shapeX[:-1] + outshapeY) return output
def cast(x, dtype): if 'theano.' in str(x.__class__): input_shape = get_shape(x) x = T.cast(x, dtype) add_shape(x, input_shape) return x return np.cast[dtype](x)
def zeros_like(x, dtype=None): if dtype is None: dtype = x.dtype input_shape = get_shape(x) x = T.zeros_like(x, dtype=dtype, opt=True) add_shape(x, input_shape) return x
def batched_dot(x, y): """Batchwise dot product. This function computes the dot product between the two tensors, by iterating over the first dimension. """ output = T.batched_dot(x, y) shapeX = get_shape(x) shapeY = get_shape(y) if isinstance(shapeX, (tuple, list)) and isinstance(shapeY, (tuple, list)): if y.ndim > 2: outshapeY = tuple([shapeY[i] for i in range(1, y.ndim) if i != y.ndim - 2]) else: outshapeY = (shapeY[-1],) add_shape(output, shapeX[:-1] + outshapeY) return output
def antirectify(x): """ This is the combination of a sample-wise L2 normalization with the concatenation of: - the positive part of the input - the negative part of the input The result is a tensor of samples that are twice as large as the input samples. It can be used in place of a ReLU. - Input shape: 2D tensor of shape (samples, n) - Output shape: 2D tensor of shape (samples, 2*n) Notes ----- When applying ReLU, assuming that the distribution of the previous output is approximately centered around 0., you are discarding half of your input. This is inefficient. Antirectifier allows to return all-positive outputs like ReLU, without discarding any data. Tests on MNIST show that Antirectifier allows to train networks with twice less parameters yet with comparable classification accuracy as an equivalent ReLU-based network. """ if ndim(x) != 2: raise Exception('This Ops only support 2D input.') input_shape = get_shape(x) x -= mean(x, axis=1, keepdims=True) # l2 normalization x /= sqrt(sum(square(x), axis=1, keepdims=True)) x = concatenate([relu(x, 0), relu(-x, 0)], axis=1) if isinstance(input_shape, (tuple, list)): add_shape(x, (input_shape[0], input_shape[1] * 2)) return x
def any(x, axis=None, keepdims=False): """Bitwise reduction (logical OR). """ y = T.any(x, axis=axis, keepdims=keepdims) if isinstance(get_shape(x), (tuple, list)): output_shape = auto_infer_shape(T.any, x, axis=axis, keepdims=keepdims) add_shape(y, output_shape) return y
def prod(x, axis=None, keepdims=False): """Multiply the values in a tensor, alongside the specified axis. """ y = T.prod(x, axis=axis, keepdims=keepdims) if isinstance(get_shape(x), (tuple, list)): output_shape = auto_infer_shape(T.prod, x, axis=axis, keepdims=keepdims) add_shape(y, output_shape) return y
def poolWTA(x, pool_size=(2, 2), axis=1): """ This function is adpated from Lasagne Original work Copyright (c) 2014-2015 lasagne contributors All rights reserved. LICENSE: https://github.com/Lasagne/Lasagne/blob/master/LICENSE 'Winner Take All' layer This layer performs 'Winner Take All' (WTA) across feature maps: zero out all but the maximal activation value within a region. Parameters ---------- pool_size : integer the number of feature maps per region. axis : integer the axis along which the regions are formed. **kwargs Any additional keyword arguments are passed to the :class:`Layer` superclass. Notes ----- This layer requires that the size of the axis along which it groups units is a multiple of the pool size. """ input_shape = get_shape(x) num_feature_maps = input_shape[axis] num_pools = num_feature_maps // pool_size if input_shape[axis] % pool_size != 0: raise ValueError("Number of input feature maps (%d) is not a " "multiple of the region size (pool_size=%d)" % (num_feature_maps, pool_size)) pool_shape = () arange_shuffle_pattern = () for k in range(axis): pool_shape += (input_shape[k], ) arange_shuffle_pattern += ('x', ) pool_shape += (num_pools, pool_size) arange_shuffle_pattern += ('x', 0) for k in range(axis + 1, x.ndim): pool_shape += (input_shape[k], ) arange_shuffle_pattern += ('x', ) input_reshaped = reshape(x, pool_shape) max_indices = argmax(input_reshaped, axis=axis + 1, keepdims=True) arange = T.arange(pool_size).dimshuffle(*arange_shuffle_pattern) mask = reshape(T.eq(max_indices, arange), input_shape) output = x * mask add_shape(output, input_shape) return output
def mean(x, axis=None, keepdims=False): dtype = x.dtype if 'int' in str(dtype) or 'bool' in str(dtype): dtype = FLOATX y = T.mean(x, axis=axis, keepdims=keepdims, dtype=dtype) if isinstance(get_shape(x), (tuple, list)): output_shape = auto_infer_shape(T.mean, x, axis=axis, keepdims=keepdims) add_shape(y, output_shape) return y
def squeeze(x, axis): """Remove a 1-dimension from the tensor at index "axis". """ input_shape = get_shape(x) axis = axis % x.ndim x = T.addbroadcast(x, axis) x = T.squeeze(x) if isinstance(input_shape, (tuple, list)): add_shape(x, tuple([j for i, j in enumerate(input_shape) if i != axis])) return x
def check_init_states(s0, nb_layers, batch_size): if s0 is None: return None if s0.get_shape().ndims < 3: s0 = expand_dims(s0, dim=0) s0shape = get_shape(s0) if s0shape[0] == 1 and s0shape[0] != nb_layers: s0 = repeat(s0, n=nb_layers, axes=0) if s0shape[1] == 1: s0 = repeat(s0, n=batch_size, axes=1) return s0
def binary_crossentropy(output, target): input_shape = get_shape(output) if ndim(output) > 1: output = flatten(output, outdim=1) if ndim(target) > 1: target = flatten(target, outdim=1) target = cast(target, output.dtype) # avoid numerical instability with _EPSILON clipping output = clip(output, EPSILON, 1.0 - EPSILON) x = backend_ops_binary_crossentropy(output, target) add_shape(x, input_shape[0]) return x
def transpose(x, axes=None): output_shape = get_shape(x) x = T.transpose(x, axes=axes) if isinstance(output_shape, (tuple, list)): if axes is None: output_shape = output_shape[::-1] else: output_shape = [output_shape[i] for i in axes] add_shape(x, tuple(output_shape)) return x
def pool2d(x, pool_size=(2, 2), strides=None, border_mode='valid', ignore_border=True, mode='max'): """ Parameters ---------- x : N-D theano tensor of input images Input images. Max pooling will be done over the 2 last dimensions. pool_size : tuple of length 2 Factor by which to downscale (vertical ds, horizontal ds). (2,2) will halve the image in each dimension. strides : tuple of two ints Stride size, which is the number of shifts over rows/cols to get the next pool region. If st is None, it is considered equal to ds (no overlap on pooling regions). ignore_border : bool (default None, will print a warning and set to False) When True, (5,5) input with ds=(2,2) will generate a (2,2) output. (3,3) otherwise. padding : tuple of two ints (pad_h, pad_w), pad zeros to extend beyond four borders of the images, pad_h is the size of the top and bottom margins, and pad_w is the size of the left and right margins. mode : {'max', 'avg'} Operation executed on each window. `max` or `average` Note ---- This pooling algorithm has non-deterministic behaviour on cuDNN """ input_shape = get_shape(x) pool_size, strides, border_mode, mode = __validate_pool_stride_border( pool_size, strides, border_mode, mode, ndim=2) ndim = len(input_shape) - 3 # ====== pooling ====== # if mode == 'max': x = tf.nn.max_pool(x, ksize=(1, ) * ndim + pool_size + (1, ), strides=(1, ) * ndim + strides + (1, ), padding=border_mode) elif mode == 'avg': x = tf.nn.avg_pool(x, ksize=(1, ) * ndim + pool_size + (1, ), strides=(1, ) * ndim + strides + (1, ), padding=border_mode) output_shape = get_pool_output_shape(input_shape, pool_size, ignore_border=ignore_border, strides=strides, pad=border_mode) add_shape(x, tuple(output_shape)) return x
def argsort(x): """ The indices in -1 axis will be sorted by the values in descending order. """ axis = -1 _ = [slice(None) for i in range(x.ndim - 1)] + [slice(None, None, -1)] y = T.argsort(x, axis)[_] if isinstance(get_shape(x), (tuple, list)): output_shape = auto_infer_shape(T.argsort, x, axis=axis) add_shape(y, output_shape) return y
def reverse(x, axes=-1): """Apply [::-1] to appropriate axis""" if not isinstance(axes, (tuple, list)): axes = (axes,) axes = [i % x.ndim for i in axes] input_shape = get_shape(x) x = x[tuple([slice(None, None, -1) if i in axes else slice(None) for i in range(x.ndim)])] if isinstance(input_shape, (tuple, list)): add_shape(x, input_shape) return x
def dimshuffle(x, pattern): """Transpose dimensions. pattern should be a tuple or list of dimension indices, e.g. [0, 2, 1]. """ pattern = tuple(pattern) input_shape = get_shape(x) new_shape = tuple([1 if i == 'x' else input_shape[i] for i in pattern]) x = x.dimshuffle(pattern) if isinstance(input_shape, (tuple, list)): add_shape(x, new_shape) return x
def __validate_pool_stride_border(x, pool_size, strides, border_mode, mode, ndim): # pool_size if pool_size is None: pool_size = (2,) * ndim else: pool_size = as_tuple(pool_size, ndim, int) # strides if strides is None: strides = pool_size else: strides = as_tuple(strides, ndim, int) # border_mode if border_mode is None or border_mode == 'valid': border_mode = (0,) * ndim elif border_mode == 'same': # pad x by hand input_shape = get_shape(x)[-ndim - 1:-1] native_shape = get_shape(x, native=True) output_shape = [math.ceil(float(i) / float(j)) for i, j in zip(input_shape, strides)] pad_size = [int((out - 1) * st + ps - ins) for ins, out, ps, st in zip(input_shape, output_shape, pool_size, strides)] # pad if necessary if _sum(pad_size) > 0: padded_x = T.zeros(tuple([native_shape[i] for i in range(x.ndim - ndim - 1)]) + tuple([i + j for i, j in zip(input_shape, pad_size)]) + (native_shape[-1],)) indices = [slice(None) for i in range(x.ndim - ndim - 1)] + \ [slice(i // 2, i // 2 + j) for i, j in zip(pad_size, input_shape)] + \ [slice(None)] x = T.set_subtensor(padded_x[indices], x) else: border_mode = as_tuple(border_mode, ndim, int) # pooling mode if 'max' in mode.lower(): mode = 'max' elif 'avg' in mode.lower(): mode = 'average_exc_pad' return x, pool_size, strides, border_mode, mode
def conv3d(x, kernel, strides=(1, 1, 1), border_mode='valid', filter_dilation=(1, 1, 1)): """ Note ---- dim_ordering : tf-tensorflow (defaults), th-theano TH input shape: (samples, input_depth, conv_dim1, conv_dim2, conv_dim3) TF input shape: (samples, conv_dim1, conv_dim2, conv_dim3, input_depth) --- TH kernel shape: (out_depth, input_depth, kernel_dim1, kernel_dim2, kernel_dim3) TF kernel shape: (kernel_dim1, kernel_dim2, kernel_dim3, input_depth, out_depth) """ volume_shape = get_shape(x) kernel_shape = get_shape(kernel) strides, border_mode, filter_dilation = __validate_strides_padding_dilation( strides, border_mode, filter_dilation, ndim=3) # no dilation for tensorflow if filter_dilation != (1, 1, 1): raise Exception("tensorflow has not supported 3D-dilation yet.") # convert to TF order is_float64 = False if 'float64' in x.dtype.name: # only conv in float32 x = tf.cast(x, 'float32') is_float64 = True if 'float64' in kernel.dtype.name: kernel = tf.cast(kernel, 'float32') # ====== estimate output shape ====== # if is_float64: x = tf.cast(x, 'float64') x = tf.nn.conv3d(x, kernel, (1, ) + strides + (1, ), border_mode) add_shape( x, get_conv_output_shape(volume_shape, kernel_shape, border_mode, strides, filter_dilation)) return x
def variable(value, dtype=FLOATX, name=None, target=None): """Instantiate a tensor variable. """ # ensure unique name if name is None: global _VAR_ID name = 'VAR_%d' % _VAR_ID _VAR_ID += 1 # ====== get the right scope for variable ====== # if len(_CURRENT_VARIABLE_SCOPE) > 0: name = _CURRENT_VARIABLE_SCOPE + "/" + name # ====== check loaded variable ====== # if name in _CREATED_VARIABLE: var = _CREATED_VARIABLE[name] if get_shape(var) != value.shape: raise Exception( 'Found pre-defined variable with shape="%s" but new' ' value has shape="%s"' % (get_shape(var), value.shape)) else: warnings.warn("Load value of new variable to old variable, " "var's name:" + name) var.set_value(value.astype(var.dtype), borrow=False) return var # ====== validate inputs ====== # value = np.asarray(value, dtype=dtype) target = _check_target(target) kwargs = {} if target is not None: kwargs['target'] = target # something wrong with SharedVariable constructor for numpy boolean array if value.dtype == np.bool: value = value.astype('uint8') variable = theano.shared(value=value, name=name, strict=False, **kwargs) add_shape(variable, tuple(variable.shape.eval())) # ====== save all created variable ====== # _CREATED_VARIABLE[name] = variable # save original shared variables return variable
def flatten(x, outdim=1): """ (None, 25, 12, 8) with flatten(outdim=2) => (None, 2400) """ input_shape = get_shape(x) x = T.flatten(x, outdim) if isinstance(input_shape, (tuple, list)): remove_dims = input_shape[(outdim - 1):] remain_dims = input_shape[:(outdim - 1)] n = None if all(i is not None for i in remove_dims): n = np.prod(remove_dims) output_shape = remain_dims + (n,) add_shape(x, output_shape) return x
def apply_mask(x, mask): """ x : 3D tensor mask : 2D tensor Example ------- >>> Input: [128, 500, 120] >>> Mask: [1, 1, 0] >>> Output: [128, 500, 0] """ input_shape = get_shape(x) x = T.mul(x, expand_dims(mask, -1)) add_shape(x, input_shape) return x