def __init__(self, inputs_hook=None, params_hook=None, outdir='outputs/conv1d', input_size=None, filter_shape=None, stride=None, border_mode='valid', weights_init='uniform', weights_interval='montreal', weights_mean=0, weights_std=5e-3, bias_init=0, activation='rectifier', convolution='mc0', mrg=RNG_MRG.MRG_RandomStreams(1)): """ Initialize a 1-D convolutional layer. Parameters ---------- inputs_hook : Tuple of (shape, variable) Routing information for the model to accept inputs from elsewhere. This is used for linking different models together. For now, it needs to include the shape information. params_hook : List(theano shared variable) A list of model parameters (shared theano variables) that you should use when constructing this model (instead of initializing your own shared variables). outdir : str The directory you want outputs (parameters, images, etc.) to save to. If None, nothing will be saved. input_size : tuple Shape of the incoming data: (batch_size, num_channels, data_dimensionality). Most likely, your channels will be 1. For example, batches of text will be of the form (N, 1, D) where N=examples in minibatch and D=dimensionality (chars, words, etc.) filter_shape : tuple (num_filters, num_channels, filter_length). This is also the shape of the weights matrix. stride : int The distance between the receptive field centers of neighboring units. This is the 'stride' of the convolution operation. border_mode : str, one of 'valid', 'full', 'same' A string indicating the convolution border mode. If 'valid', the convolution is only computed where the input and the filter fully overlap. If 'full', the convolution is computed wherever the input and the filter overlap by at least one position. If 'same', the convolution is computed wherever the input and the filter overlap by at least half the filter size, when the filter size is odd. In practice, the input is zero-padded with half the filter size at the beginning and half at the end (or one less than half in the case of an even filter size). This results in an output length that is the same as the input length (for both odd and even filter sizes). weights_init : str Determines the method for initializing model weights. See opendeep.utils.nnet for options. weights_interval : str or float If Uniform `weights_init`, the +- interval to use. See opendeep.utils.nnet for options. weights_mean : float If Gaussian `weights_init`, the mean value to use. weights_std : float If Gaussian `weights_init`, the standard deviation to use. bias_init : float The initial value to use for the bias parameter. Most often, the default of 0.0 is preferred. activation : str or Callable The activation function to apply to the layer. See opendeep.utils.activation for options. convolution : str or Callable The 1-dimensional convolution implementation to use. The default of 'mc0' is normally fine. See opendeep.utils.conv1d_implementations for alternatives. (This is necessary because Theano only supports 2D convolutions at the moment). mrg : random A random number generator that is used when adding noise. I recommend using Theano's sandbox.rng_mrg.MRG_RandomStreams. Notes ----- Theano's default convolution function (`theano.tensor.nnet.conv.conv2d`) does not support the 'same' border mode by default. This layer emulates it by performing a 'full' convolution and then cropping the result, which may negatively affect performance. """ super(Conv1D, self).__init__( ** {arg: val for (arg, val) in locals().items() if arg is not 'self'}) ################## # specifications # ################## # grab info from the inputs_hook, or from parameters # expect input to be in the form (B, C, I) (batch, channel, input data) # inputs_hook is a tuple of (Shape, Input) if self.inputs_hook is not None: # make sure inputs_hook is a tuple assert len( self.inputs_hook ) == 2, "expecting inputs_hook to be tuple of (shape, input)" self.input = inputs_hook[1] else: # make the input a symbolic matrix self.input = T.ftensor3('X') # activation function! activation_func = get_activation_function(activation) # convolution function! convolution_func = get_conv1d_function(convolution) # filter shape should be in the form (num_filters, num_channels, filter_length) num_filters = filter_shape[0] filter_length = filter_shape[2] ################################################ # Params - make sure to deal with params_hook! # ################################################ if self.params_hook: # make sure the params_hook has W and b assert len(self.params_hook) == 2, \ "Expected 2 params (W and b) for Conv1D, found {0!s}!".format(len(self.params_hook)) W, b = self.params_hook else: W = get_weights( weights_init=weights_init, shape=filter_shape, name="W", rng=mrg, # if gaussian mean=weights_mean, std=weights_std, # if uniform interval=weights_interval) b = get_bias(shape=(num_filters, ), name="b", init_values=bias_init) # Finally have the two parameters! self.params = [W, b] ######################## # Computational Graph! # ######################## if border_mode in ['valid', 'full']: conved = convolution_func(self.input, W, subsample=(stride, ), image_shape=self.input_size, filter_shape=filter_shape, border_mode=border_mode) elif border_mode == 'same': conved = convolution_func(self.input, W, subsample=(stride, ), image_shape=self.input_size, filter_shape=filter_shape, border_mode='full') shift = (filter_length - 1) // 2 conved = conved[:, :, shift:self.input_size[2] + shift] else: log.error("Invalid border mode: '%s'" % border_mode) raise RuntimeError("Invalid border mode: '%s'" % border_mode) self.output = activation_func(conved + b.dimshuffle('x', 0, 'x'))
def __init__(self, inputs=None, params=None, outdir='outputs/conv1d', n_filters=None, filter_size=None, stride=None, border_mode='valid', weights_init='uniform', weights_interval='montreal', weights_mean=0, weights_std=5e-3, bias_init=0, activation='rectifier', convolution='mc0', mrg=RNG_MRG.MRG_RandomStreams(1), **kwargs): """ Initialize a 1-D convolutional layer. Parameters ---------- inputs : tuple(shape, `Theano.TensorType`) The dimensionality of the inputs for this model, and the routing information for the model to accept inputs from elsewhere. `shape` will be a monad tuple representing known sizes for each dimension in the `Theano.TensorType`. Shape of the incoming data: (batch_size, num_channels, data_dimensionality). Most likely, your channels will be 1. For example, batches of text will be of the form (N, 1, D) where N=examples in minibatch and D=dimensionality (chars, words, etc.) params : Dict(string_name: theano SharedVariable), optional A dictionary of model parameters (shared theano variables) that you should use when constructing this model (instead of initializing your own shared variables). This parameter is useful when you want to have two versions of the model that use the same parameters - such as siamese networks or pretraining some weights. outdir : str The directory you want outputs (parameters, images, etc.) to save to. If None, nothing will be saved. n_filters : int The number of filters to use (convolution kernels). filter_size : int The size of the convolution filter. stride : int The distance between the receptive field centers of neighboring units. This is the 'stride' of the convolution operation. border_mode : str, one of 'valid', 'full', 'same' A string indicating the convolution border mode. If 'valid', the convolution is only computed where the input and the filter fully overlap. If 'full', the convolution is computed wherever the input and the filter overlap by at least one position. weights_init : str Determines the method for initializing model weights. See opendeep.utils.nnet for options. weights_interval : str or float If Uniform `weights_init`, the +- interval to use. See opendeep.utils.nnet for options. weights_mean : float If Gaussian `weights_init`, the mean value to use. weights_std : float If Gaussian `weights_init`, the standard deviation to use. bias_init : float The initial value to use for the bias parameter. Most often, the default of 0.0 is preferred. activation : str or Callable The activation function to apply to the layer. See opendeep.utils.activation for options. convolution : str or Callable The 1-dimensional convolution implementation to use. The default of 'mc0' is normally fine. See opendeep.utils.conv1d_implementations for alternatives. (This is necessary because Theano only supports 2D convolutions at the moment). mrg : random A random number generator that is used when adding noise. I recommend using Theano's sandbox.rng_mrg.MRG_RandomStreams. Notes ----- Theano's default convolution function (`theano.tensor.nnet.conv.conv2d`) does not support the 'same' border mode by default. This layer emulates it by performing a 'full' convolution and then cropping the result, which may negatively affect performance. """ initial_parameters = locals().copy() initial_parameters.pop('self') super(Conv1D, self).__init__(**initial_parameters) if self.inputs is None: return ################## # specifications # ################## # grab info from the inputs_hook, or from parameters # expect input to be in the form (B, C, I) (batch, channel, input data) # inputs_hook is a tuple of (Shape, Input) # self.inputs is a list of all the input expressions (we enforce only 1, so self.inputs[0] is the input) input_shape, self.input = self.inputs[0] assert self.input.ndim == 3, "Expected 3D input variable with form (batch, channel, input_data)" assert len(input_shape) == 3, "Expected 3D input shape with form (batch, channel, input_data)" n_channels = input_shape[1] filter_shape = (n_filters, n_channels, filter_size) # activation function! activation_func = get_activation_function(activation) # convolution function! convolution_func = get_conv1d_function(convolution) outshape = ConvOp.getOutputShape( inshp=(input_shape[-1],), kshp=(filter_size,), stride=(stride,), mode=border_mode ) self.output_size = (input_shape[0], n_filters) + outshape ########## # Params # ########## W = self.params.get( "W", get_weights(weights_init=weights_init, shape=filter_shape, name="W", rng=mrg, # if gaussian mean=weights_mean, std=weights_std, # if uniform interval=weights_interval) ) b = self.params.get( "b", get_bias(shape=(n_filters,), name="b", init_values=bias_init) ) # Finally have the two parameters! self.params = OrderedDict([("W", W), ("b", b)]) ######################## # Computational Graph! # ######################## if border_mode in ['valid', 'full']: conved = convolution_func(self.input, W, subsample=(stride,), image_shape=input_shape, filter_shape=filter_shape, border_mode=border_mode) else: log.error("Invalid border mode: '%s'" % border_mode) raise RuntimeError("Invalid border mode: '%s'" % border_mode) self.output = activation_func(conved + b.dimshuffle('x', 0, 'x'))
def __init__(self, inputs_hook=None, params_hook=None, outdir='outputs/conv1d', input_size=None, filter_shape=None, stride=None, border_mode='valid', weights_init='uniform', weights_interval='montreal', weights_mean=0, weights_std=5e-3, bias_init=0, activation='rectifier', convolution='mc0', mrg=RNG_MRG.MRG_RandomStreams(1)): """ Initialize a 1-D convolutional layer. Parameters ---------- inputs_hook : Tuple of (shape, variable) Routing information for the model to accept inputs from elsewhere. This is used for linking different models together. For now, it needs to include the shape information. params_hook : List(theano shared variable) A list of model parameters (shared theano variables) that you should use when constructing this model (instead of initializing your own shared variables). outdir : str The directory you want outputs (parameters, images, etc.) to save to. If None, nothing will be saved. input_size : tuple Shape of the incoming data: (batch_size, num_channels, data_dimensionality). Most likely, your channels will be 1. For example, batches of text will be of the form (N, 1, D) where N=examples in minibatch and D=dimensionality (chars, words, etc.) filter_shape : tuple (num_filters, num_channels, filter_length). This is also the shape of the weights matrix. stride : int The distance between the receptive field centers of neighboring units. This is the 'stride' of the convolution operation. border_mode : str, one of 'valid', 'full', 'same' A string indicating the convolution border mode. If 'valid', the convolution is only computed where the input and the filter fully overlap. If 'full', the convolution is computed wherever the input and the filter overlap by at least one position. If 'same', the convolution is computed wherever the input and the filter overlap by at least half the filter size, when the filter size is odd. In practice, the input is zero-padded with half the filter size at the beginning and half at the end (or one less than half in the case of an even filter size). This results in an output length that is the same as the input length (for both odd and even filter sizes). weights_init : str Determines the method for initializing model weights. See opendeep.utils.nnet for options. weights_interval : str or float If Uniform `weights_init`, the +- interval to use. See opendeep.utils.nnet for options. weights_mean : float If Gaussian `weights_init`, the mean value to use. weights_std : float If Gaussian `weights_init`, the standard deviation to use. bias_init : float The initial value to use for the bias parameter. Most often, the default of 0.0 is preferred. activation : str or Callable The activation function to apply to the layer. See opendeep.utils.activation for options. convolution : str or Callable The 1-dimensional convolution implementation to use. The default of 'mc0' is normally fine. See opendeep.utils.conv1d_implementations for alternatives. (This is necessary because Theano only supports 2D convolutions at the moment). mrg : random A random number generator that is used when adding noise. I recommend using Theano's sandbox.rng_mrg.MRG_RandomStreams. Notes ----- Theano's default convolution function (`theano.tensor.nnet.conv.conv2d`) does not support the 'same' border mode by default. This layer emulates it by performing a 'full' convolution and then cropping the result, which may negatively affect performance. """ super(Conv1D, self).__init__(**{arg: val for (arg, val) in locals().items() if arg is not 'self'}) ################## # specifications # ################## # grab info from the inputs_hook, or from parameters # expect input to be in the form (B, C, I) (batch, channel, input data) # inputs_hook is a tuple of (Shape, Input) if self.inputs_hook is not None: # make sure inputs_hook is a tuple assert len(self.inputs_hook) == 2, "expecting inputs_hook to be tuple of (shape, input)" self.input = inputs_hook[1] else: # make the input a symbolic matrix self.input = T.ftensor3('X') # activation function! activation_func = get_activation_function(activation) # convolution function! convolution_func = get_conv1d_function(convolution) # filter shape should be in the form (num_filters, num_channels, filter_length) num_filters = filter_shape[0] filter_length = filter_shape[2] ################################################ # Params - make sure to deal with params_hook! # ################################################ if self.params_hook: # make sure the params_hook has W and b assert len(self.params_hook) == 2, \ "Expected 2 params (W and b) for Conv1D, found {0!s}!".format(len(self.params_hook)) W, b = self.params_hook else: W = get_weights(weights_init=weights_init, shape=filter_shape, name="W", rng=mrg, # if gaussian mean=weights_mean, std=weights_std, # if uniform interval=weights_interval) b = get_bias(shape=(num_filters,), name="b", init_values=bias_init) # Finally have the two parameters! self.params = [W, b] ######################## # Computational Graph! # ######################## if border_mode in ['valid', 'full']: conved = convolution_func(self.input, W, subsample=(stride,), image_shape=self.input_size, filter_shape=filter_shape, border_mode=border_mode) elif border_mode == 'same': conved = convolution_func(self.input, W, subsample=(stride,), image_shape=self.input_size, filter_shape=filter_shape, border_mode='full') shift = (filter_length - 1) // 2 conved = conved[:, :, shift:self.input_size[2] + shift] else: log.error("Invalid border mode: '%s'" % border_mode) raise RuntimeError("Invalid border mode: '%s'" % border_mode) self.output = activation_func(conved + b.dimshuffle('x', 0, 'x'))