Example #1
0
    def __init__(self,
                 output_dim,
                 init='glorot_uniform',
                 inner_init='orthogonal',
                 activation='tanh',
                 inner_activation='hard_sigmoid',
                 W_regularizer=None,
                 U_regularizer=None,
                 b_regularizer=None,
                 dropout_W=0.,
                 dropout_U=0.,
                 **kwargs):
        self.output_dim = output_dim
        self.init = initializations.get(init)
        self.inner_init = initializations.get(inner_init)
        self.activation = activations.get(activation)
        self.inner_activation = activations.get(inner_activation)
        self.W_regularizer = regularizers.get(W_regularizer)
        self.U_regularizer = regularizers.get(U_regularizer)
        self.b_regularizer = regularizers.get(b_regularizer)
        self.dropout_W = dropout_W
        self.dropout_U = dropout_U

        if self.dropout_W or self.dropout_U:
            self.uses_learning_phase = True
        super(MGU, self).__init__(**kwargs)
Example #2
0
 def __init__(self, output_dim,
              init='glorot_uniform', inner_init='orthogonal',
              activation='tanh', inner_activation='hard_sigmoid', **kwargs):
   self.output_dim       = output_dim
   self.init             = initializations.get(init)
   self.inner_init       = initializations.get(inner_init)
   self.activation       = activations.get(activation)
   self.inner_activation = activations.get(inner_activation)
   super(DecoderGRU, self).__init__(**kwargs)
Example #3
0
    def __init__(self, nb_filter, filter_length,
                 init='glorot_uniform', activation=None, weights=None,
                 border_mode='valid', subsample_length=1,
                 W_regularizer=None, b_regularizer=None, activity_regularizer=None,
                 W_constraint=None, b_constraint=None,
                 bias=True, input_dim=None, input_length=None, **kwargs):
        if border_mode != 'valid':
            raise ValueError('Invalid border mode for LocallyConnected1D '
                             '(only "valid" is supported):', border_mode)
        self.nb_filter = nb_filter
        self.filter_length = filter_length
        self.init = initializations.get(init, dim_ordering='th')
        self.activation = activations.get(activation)

        self.border_mode = border_mode
        self.subsample_length = subsample_length

        self.W_regularizer = regularizers.get(W_regularizer)
        self.b_regularizer = regularizers.get(b_regularizer)
        self.activity_regularizer = regularizers.get(activity_regularizer)

        self.W_constraint = constraints.get(W_constraint)
        self.b_constraint = constraints.get(b_constraint)

        self.bias = bias
        self.input_spec = [InputSpec(ndim=3)]
        self.initial_weights = weights
        self.input_dim = input_dim
        self.input_length = input_length
        if self.input_dim:
            kwargs['input_shape'] = (self.input_length, self.input_dim)
        super(LocallyConnected1D, self).__init__(**kwargs)
Example #4
0
    def __init__(self, nb_filter, nb_row, nb_col,
                 init='glorot_uniform', activation=None, weights=None,
                 border_mode='valid', subsample=(1, 1),
                 dim_ordering='default',
                 W_regularizer=None, b_regularizer=None, activity_regularizer=None,
                 W_constraint=None, b_constraint=None,
                 bias=True, **kwargs):
        if dim_ordering == 'default':
            dim_ordering = K.image_dim_ordering()
        if border_mode != 'valid':
            raise ValueError('Invalid border mode for LocallyConnected2D '
                             '(only "valid" is supported):', border_mode)
        self.nb_filter = nb_filter
        self.nb_row = nb_row
        self.nb_col = nb_col
        self.init = initializations.get(init, dim_ordering=dim_ordering)
        self.activation = activations.get(activation)

        self.border_mode = border_mode
        self.subsample = tuple(subsample)
        if dim_ordering not in {'tf', 'th'}:
            raise ValueError('`dim_ordering` must be in {tf, th}.')
        self.dim_ordering = dim_ordering

        self.W_regularizer = regularizers.get(W_regularizer)
        self.b_regularizer = regularizers.get(b_regularizer)
        self.activity_regularizer = regularizers.get(activity_regularizer)

        self.W_constraint = constraints.get(W_constraint)
        self.b_constraint = constraints.get(b_constraint)

        self.bias = bias
        self.input_spec = [InputSpec(ndim=4)]
        self.initial_weights = weights
        super(LocallyConnected2D, self).__init__(**kwargs)
Example #5
0
 def __init__(self,
              output_dim,
              init='glorot_uniform',
              attn_activation='tanh',
              **kwargs):
     self.output_dim = output_dim
     self.init = initializations.get(init)
     self.attn_activation = activations.get(attn_activation)
     super(AttentionLayer, self).__init__(**kwargs)
Example #6
0
    def build(self, input_shape):
        input_shape = input_shape[1:]

        t_left_init = initializations.get(self.t_left_init)
        a_left_init = initializations.get(self.a_left_init)
        t_right_init = initializations.get(self.t_right_init)
        a_right_init = initializations.get(self.a_right_init)

        self.t_left = t_left_init(input_shape,
                                  name='{}_t_left'.format(self.name))
        self.a_left = a_left_init(input_shape,
                                  name='{}_a_left'.format(self.name))
        self.t_right = t_right_init(input_shape,
                                    name='{}_t_right'.format(self.name))
        self.a_right = a_right_init(input_shape,
                                    name='{}_a_right'.format(self.name))
        # ensure the the right part is always to the right of the left
        self.t_right_actual = self.t_left + abs(self.t_right)
        self.trainable_weights = [
            self.t_left, self.a_left, self.t_right, self.a_right
        ]
Example #7
0
    def __init__(self,
                 nb_filter,
                 shared_pool,
                 nb_row=1,
                 nb_col=1,
                 init='glorot_uniform',
                 activation='linear',
                 weights=None,
                 border_mode='valid',
                 subsample=(1, 1),
                 dim_ordering=K.image_dim_ordering(),
                 W_regularizer=None,
                 b_regularizer=None,
                 activity_regularizer=None,
                 W_constraint=None,
                 b_constraint=None,
                 bias=True,
                 **kwargs):
        if border_mode != 'valid':
            raise Exception(
                'Invalid border mode for Convolution2D '
                '(only "valid" is supported):', border_mode)
        if tuple(subsample) != (
                nb_row, nb_col
        ):  #model.to_json saves subsample as list and not as tuple
            raise Exception(
                'Local layer only works with equal filter dimensions and strides'
            )
        self.nb_filter = nb_filter
        self.shared_pool = shared_pool
        self.nb_row = nb_row
        self.nb_col = nb_col
        self.init = initializations.get(init, dim_ordering=dim_ordering)
        self.activation = activations.get(activation)

        self.border_mode = border_mode
        self.subsample = tuple(subsample)
        assert dim_ordering in {'tf', 'th'}, 'dim_ordering must be in {tf, th}'
        self.dim_ordering = dim_ordering

        self.W_regularizer = regularizers.get(W_regularizer)
        self.b_regularizer = regularizers.get(b_regularizer)
        self.activity_regularizer = regularizers.get(activity_regularizer)

        self.W_constraint = constraints.get(W_constraint)
        self.b_constraint = constraints.get(b_constraint)

        self.bias = bias
        self.input_spec = [InputSpec(ndim=4)]
        self.initial_weights = weights
        super(SemiShared, self).__init__(**kwargs)
Example #8
0
    def __init__(self, output_dim, gamma_init=1., **kwargs):
        if 'consume_less' in kwargs:
            assert kwargs['consume_less'] == 'gpu'
        else:
            kwargs = kwargs.copy()
            kwargs['consume_less'] = 'gpu'
        super(GRULN, self).__init__(output_dim, **kwargs)

        def gamma_init_func(shape, c=gamma_init, **kwargs):
            if c == 1.:
                return initializations.get('one')(shape, **kwargs)
            return K.variable(np.ones(shape) * c, **kwargs)

        self.gamma_init = gamma_init_func
        self.beta_init = initializations.get('zero')
        self.epsilon = 1e-5
Example #9
0
    def __init__(self,
                 nb_filter,
                 nb_row,
                 nb_col,
                 init='glorot_uniform',
                 activation=None,
                 weights=None,
                 border_mode='valid',
                 subsample=(1, 1),
                 dim_ordering='default',
                 W_regularizer=None,
                 b_regularizer=None,
                 activity_regularizer=None,
                 W_constraint=None,
                 b_constraint=None,
                 bias=True,
                 **kwargs):
        if dim_ordering == 'default':
            dim_ordering = K.image_dim_ordering()
        if border_mode != 'valid':
            raise ValueError(
                'Invalid border mode for LocallyConnected2D '
                '(only "valid" is supported):', border_mode)
        self.nb_filter = nb_filter
        self.nb_row = nb_row
        self.nb_col = nb_col
        self.init = initializations.get(init, dim_ordering=dim_ordering)
        self.activation = activations.get(activation)

        self.border_mode = border_mode
        self.subsample = tuple(subsample)
        if dim_ordering not in {'tf', 'th'}:
            raise ValueError('`dim_ordering` must be in {tf, th}.')
        self.dim_ordering = dim_ordering

        self.W_regularizer = regularizers.get(W_regularizer)
        self.b_regularizer = regularizers.get(b_regularizer)
        self.activity_regularizer = regularizers.get(activity_regularizer)

        self.W_constraint = constraints.get(W_constraint)
        self.b_constraint = constraints.get(b_constraint)

        self.bias = bias
        self.input_spec = [InputSpec(ndim=4)]
        self.initial_weights = weights
        super(LocallyConnected2D, self).__init__(**kwargs)
Example #10
0
    def __init__(self,
                 nb_filter,
                 filter_length,
                 init='glorot_uniform',
                 activation=None,
                 weights=None,
                 border_mode='valid',
                 subsample_length=1,
                 W_regularizer=None,
                 b_regularizer=None,
                 activity_regularizer=None,
                 W_constraint=None,
                 b_constraint=None,
                 bias=True,
                 input_dim=None,
                 input_length=None,
                 **kwargs):
        if border_mode != 'valid':
            raise Exception(
                'Invalid border mode for LocallyConnected1D '
                '(only "valid" is supported):', border_mode)
        self.nb_filter = nb_filter
        self.filter_length = filter_length
        self.init = initializations.get(init, dim_ordering='th')
        self.activation = activations.get(activation)

        self.border_mode = border_mode
        self.subsample_length = subsample_length

        self.W_regularizer = regularizers.get(W_regularizer)
        self.b_regularizer = regularizers.get(b_regularizer)
        self.activity_regularizer = regularizers.get(activity_regularizer)

        self.W_constraint = constraints.get(W_constraint)
        self.b_constraint = constraints.get(b_constraint)

        self.bias = bias
        self.input_spec = [InputSpec(ndim=3)]
        self.initial_weights = weights
        self.input_dim = input_dim
        self.input_length = input_length
        if self.input_dim:
            kwargs['input_shape'] = (self.input_length, self.input_dim)
        super(LocallyConnected1D, self).__init__(**kwargs)
Example #11
0
    def __init__(self, nb_kernels, kernel_dim, init='glorot_uniform', weights=None,
                 W_regularizer=None, activity_regularizer=None,
                 W_constraint=None, input_dim=None, **kwargs):
        self.init = initializations.get(init)
        self.nb_kernels = nb_kernels
        self.kernel_dim = kernel_dim
        self.input_dim = input_dim

        self.W_regularizer = regularizers.get(W_regularizer)
        self.activity_regularizer = regularizers.get(activity_regularizer)

        self.W_constraint = constraints.get(W_constraint)

        self.initial_weights = weights
        self.input_spec = [InputSpec(ndim=2)]

        if self.input_dim:
            kwargs['input_shape'] = (self.input_dim,)
        super(MinibatchDiscrimination, self).__init__(**kwargs)
Example #12
0
    def __init__(self,
                 output_dim,
                 nb_feature=4,
                 init='glorot_uniform',
                 bias=True,
                 input_dim=None,
                 **kwargs):
        self.output_dim = output_dim
        self.nb_feature = nb_feature
        self.init = initializations.get(init)

        self.bias = bias
        self.input_spec = [
            InputSpec(ndim=3),
            InputSpec(ndim=3),
            InputSpec(ndim=3)
        ]

        self.input_dim = input_dim
        if self.input_dim:
            kwargs['input_shape'] = (self.input_dim, )
        super(Maxout, self).__init__(**kwargs)
Example #13
0
    def build(self, input_shape):
        bias = self.bias
        user_count = self.user_count
        item_count = self.item_count
        init = self.init
        ubias_regularizer = self.ubias_regularizer
        ubias_constraint = self.ubias_constraint
        cbias_regularizer = self.cbias_regularizer
        cbias_constraint = self.cbias_constraint

        init = initializations.get(init)
        if bias == 'user' or bias == 'both':
            self.ubias = self.add_weight((user_count, 1), initializer=init,
                                         name='{}_ubias'.format(self.name),
                                         regularizer=ubias_regularizer,
                                         constraint=ubias_constraint)
        if bias == 'item' or bias == 'both':
            self.cbias = self.add_weight((item_count, 1), initializer=init,
                                         name='{}_cbias'.format(self.name),
                                         regularizer=cbias_regularizer,
                                         constraint=cbias_constraint)
        self.built = True
Example #14
0
 def __init__(self, init='zero', weights=None, **kwargs):
     self.supports_masking = True
     self.init = initializations.get(init)
     self.initial_weights = weights
     super(PReLU, self).__init__(**kwargs)
Example #15
0
 def gamma_init_func(shape, c=gamma_init, **kwargs):
     if c == 1.:
         return initializations.get('one')(shape, **kwargs)
     return K.variable(np.ones(shape) * c, **kwargs)