def __init__(self, input_dim, EmbedMatrix, input_length=None,
                 init='uniform',
                 W_regularizer=None, activity_regularizer=None,
                 W_constraint=None,
                 mask_zero=False,
                 weights=None, **kwargs):
        '''

        :param input_dim: 字典大小
        :param input_length: max_seq_len最大序列长度
        :param EmbedMatrix: 已经计算好的 word2vec 矩阵,单词 * 嵌入
        '''
        self.input_dim = input_dim
        self.EmbedMatrix = theano.shared(value=EmbedMatrix)
        self.output_dim = EmbedMatrix.shape[1]
        self.init = initializations.get(init)
        self.input_length = input_length
        self.mask_zero = mask_zero

        self.W_constraint = constraints.get(W_constraint)
        self.constraints = [self.W_constraint]

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

        self.initial_weights = weights
        kwargs['input_shape'] = (self.input_dim,)
        super(LookUpEmbeddingLayer, self).__init__(**kwargs) # 在初始化里input_shape = [None, self.input_dim]
Exemplo n.º 2
0
    def __init__(self, nb_filter, nb_row, nb_col,
                 init='glorot_uniform', activation='linear', weights=None,
                 border_mode='valid', subsample=(1, 1),
                 W_regularizer=None, b_regularizer=None, activity_regularizer=None,
                 W_constraint=None, b_constraint=None, **kwargs):

        if border_mode not in {'valid', 'full', 'same'}:
            raise Exception('Invalid border mode for Convolution2D:', border_mode)
        self.nb_filter = nb_filter
        self.nb_row = nb_row
        self.nb_col = nb_col
        self.init = initializations.get(init)
        self.activation = activations.get(activation)
        self.border_mode = border_mode
        self.subsample = tuple(subsample)

        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.constraints = [self.W_constraint, self.b_constraint]

        self.initial_weights = weights
        super(Convolution2D, self).__init__(**kwargs)
Exemplo n.º 3
0
    def __init__(self, nb_filter, nb_row, nb_col,
                 init='glorot_uniform', weights=None,
                 border_mode='same', subsample=(1, 1), dim_ordering='th',
                 W_regularizer=None, activity_regularizer=None,
                 W_constraint=None, **kwargs):

        if border_mode not in {'valid', 'same'}:
            raise Exception('Invalid border mode for Deconvolution2D:', 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)
        assert border_mode in {'same'}, 'border_mode must be same'
        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.activity_regularizer = regularizers.get(activity_regularizer)

        self.W_constraint = constraints.get(W_constraint)

        self.input_spec = [InputSpec(ndim=4)]
        self.initial_weights = weights
        super(Deconvolution2D, self).__init__(**kwargs)
Exemplo n.º 4
0
 def __init__(self, offset_creator_class, weight_basis,
              axis=-1,
              momentum=0.99,
              epsilon=1e-3,
              center=True,
              scale=True,
              beta_initializer='zeros',
              gamma_initializer='ones',
              moving_mean_initializer='zeros',
              moving_variance_initializer='ones',
              beta_regularizer=None,
              gamma_regularizer=None,
              beta_constraint=None,
              gamma_constraint=None,
              **kwargs):
     super(RProjBatchNormalization, self).__init__(offset_creator_class, weight_basis, **kwargs)
     self.supports_masking = True
     self.axis = axis
     self.momentum = momentum
     self.epsilon = epsilon
     self.center = center
     self.scale = scale
     self.beta_initializer = initializers.get(beta_initializer)
     self.gamma_initializer = initializers.get(gamma_initializer)
     self.moving_mean_initializer = initializers.get(moving_mean_initializer)
     self.moving_variance_initializer = initializers.get(moving_variance_initializer)
     self.beta_regularizer = regularizers.get(beta_regularizer)
     self.gamma_regularizer = regularizers.get(gamma_regularizer)
     self.beta_constraint = constraints.get(beta_constraint)
     self.gamma_constraint = constraints.get(gamma_constraint)
Exemplo n.º 5
0
 def __init__(self, offset_creator_class, weight_basis,
              units,
              activation=None,
              use_bias=True,
              kernel_initializer='glorot_uniform',
              bias_initializer='zeros',
              kernel_regularizer=None,
              bias_regularizer=None,
              activity_regularizer=None,
              kernel_constraint=None,
              bias_constraint=None,
              **kwargs):
     if 'input_shape' not in kwargs and 'input_dim' in kwargs:
         kwargs['input_shape'] = (kwargs.pop('input_dim'),)
     super(RProjDense, self).__init__(offset_creator_class, weight_basis, **kwargs)
     self.units = units
     self.activation = activations.get(activation)
     self.use_bias = use_bias
     self.kernel_initializer = initializers.get(kernel_initializer)
     self.bias_initializer = initializers.get(bias_initializer)
     self.kernel_regularizer = regularizers.get(kernel_regularizer)
     self.bias_regularizer = regularizers.get(bias_regularizer)
     self.activity_regularizer = regularizers.get(activity_regularizer)
     self.kernel_constraint = constraints.get(kernel_constraint)
     self.bias_constraint = constraints.get(bias_constraint)
     self.input_spec = InputSpec(min_ndim=2)
     self.supports_masking = True
Exemplo n.º 6
0
    def __init__(self, output_dim, nb_hsm_classes, batch_size,
            init='glorot_uniform',
            W1_weights=None, W1_regularizer=None, W1_constraint=None,
            W2_weights=None, W2_regularizer=None, W2_constraint=None,
            b1_regularizer=None, b1_constraint=None,
            b2_regularizer=None, b2_constraint=None,
            input_dim=None, **kwargs):

        self.__dict__.update(locals())
        del self.self

        self.init = initializations.get(init)
        #self.output_dim = nb_classes * nb_outputs_per_class
        self.nb_outputs_per_class = int(np.ceil(output_dim / float(nb_hsm_classes)))

        self.W1_regularizer = regularizers.get(W1_regularizer)
        self.b1_regularizer = regularizers.get(b1_regularizer)
        self.W2_regularizer = regularizers.get(W2_regularizer)
        self.b2_regularizer = regularizers.get(b2_regularizer)

        self.W1_constraint = constraints.get(W1_constraint)
        self.b1_constraint = constraints.get(b1_constraint)
        self.W2_constraint = constraints.get(W2_constraint)
        self.b2_constraint = constraints.get(b2_constraint)

        self.constraints = [self.W1_constraint, self.b1_constraint,
                self.W2_constraint, self.b2_constraint]

        #self.initial_weights = weights
        self.input_dim = input_dim
        if self.input_dim:
            kwargs['input_shape'] = (self.input_dim,)
        self.input = T.matrix()
        super(HierarchicalSoftmax, self).__init__(**kwargs)
Exemplo n.º 7
0
 def __init__(self, offset_creator_class, weight_basis,
              filters,
              kernel_size,
              strides=(1, 1),
              padding='valid',
              data_format=None,
              activation=None,
              use_bias=True,
              kernel_initializer='glorot_uniform',
              bias_initializer='zeros',
              kernel_regularizer=None,
              bias_regularizer=None,
              activity_regularizer=None,
              kernel_constraint=None,
              bias_constraint=None,
              **kwargs):
     super(RProjLocallyConnected2D, self).__init__(offset_creator_class, weight_basis, **kwargs)
     self.filters = filters
     self.kernel_size = conv_utils.normalize_tuple(kernel_size, 2, 'kernel_size')
     self.strides = conv_utils.normalize_tuple(strides, 2, 'strides')
     self.padding = conv_utils.normalize_padding(padding)
     if self.padding != 'valid':
         raise ValueError('Invalid border mode for LocallyConnected2D '
                          '(only "valid" is supported): ' + padding)
     self.data_format = conv_utils.normalize_data_format(data_format)
     self.activation = activations.get(activation)
     self.use_bias = use_bias
     self.kernel_initializer = initializers.get(kernel_initializer)
     self.bias_initializer = initializers.get(bias_initializer)
     self.kernel_regularizer = regularizers.get(kernel_regularizer)
     self.bias_regularizer = regularizers.get(bias_regularizer)
     self.activity_regularizer = regularizers.get(activity_regularizer)
     self.kernel_constraint = constraints.get(kernel_constraint)
     self.bias_constraint = constraints.get(bias_constraint)
     self.input_spec = InputSpec(ndim=4)
Exemplo n.º 8
0
    def __init__(self,
                 init='glorot_uniform',
                 activation=None,
                 weights=None,
                 W_regularizer=None,
                 b_regularizer=None,
                 activity_regularizer=None,
                 W_constraint=None,
                 b_constraint=None,
                 bias=True,
                 input_dim=None,
                 **kwargs):

        self.init = initializers.get(init)
        self.activation = activations.get(activation)

        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.initial_weights = weights
        self.input_spec = InputSpec(ndim=2)

        self.input_dim = input_dim
        if self.input_dim:
            kwargs['input_shape'] = (self.input_dim,)
        super(Highway, self).__init__(**kwargs)
Exemplo n.º 9
0
    def __init__(self, nb_filter, nb_row, nb_col, mask_type=None, direction='Down',
                 init='glorot_uniform', activation='linear', weights=None,
                 border_mode='valid', subsample=(1, 1), dim_ordering='th',
                 W_regularizer=None, b_regularizer=None, activity_regularizer=None,
                 W_constraint=None, b_constraint=None,
                 bias=True, **kwargs):
        self.mask_type = mask_type
        self.direction = direction
        if border_mode not in {'valid', 'same'}:
            raise Exception('Invalid border mode for Convolution2D:', 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)
        assert border_mode in {'valid', 'same'}, 'border_mode must be in {valid, same}'
        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(MaskedConvolution2D, self).__init__(**kwargs)
Exemplo n.º 10
0
    def __init__(self, nb_filter, filter_length, direction='Down',
                 init='glorot_uniform', inner_init='orthogonal',
                 forget_bias_init='one', activation='tanh',
                 inner_activation='hard_sigmoid',
                 border_mode="same", sub_sample=(1, 1),
                 W_regularizer=None, U_regularizer=None, b_regularizer=None,
                 dropout_W=0., dropout_U=0., **kwargs):

        self.nb_filter = nb_filter
        self.filter_length = filter_length
        self.border_mode = border_mode
        self.subsample = sub_sample
        self.direction = direction

        self.init = initializations.get(init)
        self.inner_init = initializations.get(inner_init)
        self.forget_bias_init = initializations.get(forget_bias_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, self.dropout_U = dropout_W, dropout_U

        kwargs["nb_filter"] = nb_filter
        kwargs["filter_length"] = filter_length

        if self.dropout_W or self.dropout_U:
            self.uses_learning_phase = True
        super(DiagLSTM, self).__init__(**kwargs)
Exemplo n.º 11
0
    def __init__(self, output_dim,
                 init='glorot_uniform', activation='linear', weights=None,
                 W_regularizer=None, b_regularizer=None, activity_regularizer=None,
                 W_constraint=None, b_constraint=None,
                 input_dim=None, input_length1=None, input_length2=None, **kwargs):
        self.output_dim = output_dim
        self.init = initializations.get(init)
        self.activation = activations.get(activation)

        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.constraints = [self.W_constraint, self.b_constraint]

        self.initial_weights = weights

        self.input_dim = input_dim
        self.input_length1 = input_length1
        self.input_length2 = input_length2
        if self.input_dim:
            kwargs['input_shape'] = (self.input_length1, self.input_length2, self.input_dim)
        self.input = K.placeholder(ndim=4)
        super(HigherOrderTimeDistributedDense, self).__init__(**kwargs)
Exemplo n.º 12
0
    def __init__(self, output_dim, init='glorot_uniform', activation='linear',
                 reconstruction_activation='linear', weights=None,
                 W_regularizer=None, b_regularizer=None, activity_regularizer=None,
                 output_reconstruction=False,
                 W_constraint=None, b_constraint=None, input_dim=None, **kwargs):
        self.init = initializations.get(init)
        self.activation = activations.get(activation)
        self.reconstruction_activation = activations.get(reconstruction_activation)
        self.output_reconstruction = output_reconstruction
        self.output_dim = output_dim
        self.pretrain = True

        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.constraints = [self.W_constraint, self.b_constraint]

        self.initial_weights = weights

        self.input_dim = input_dim
        if self.input_dim:
            kwargs['input_shape'] = (self.input_dim,)
        self.input = K.placeholder(ndim=2)
        super(SymmetricAutoencoder, self).__init__(**kwargs)
Exemplo n.º 13
0
    def __init__(self, step_dim,
                 W_regularizer=None, b_regularizer=None,
                 W_constraint=None, b_constraint=None,
                 bias=True, **kwargs):
        """
        Keras Layer that implements an Attention mechanism for temporal data.
        Supports Masking.
        Follows the work of Raffel et al. [https://arxiv.org/abs/1512.08756]
        # Input shape
            3D tensor with shape: `(samples, steps, features)`.
        # Output shape
            2D tensor with shape: `(samples, features)`.
        :param kwargs:
        Just put it on top of an RNN Layer (GRU/LSTM/SimpleRNN) with return_sequences=True.
        The dimensions are inferred based on the output shape of the RNN.
        Example:
            model.add(LSTM(64, return_sequences=True))
            model.add(Attention())
        """
        self.supports_masking = True
        # self.init = initializations.get('glorot_uniform')
        self.init = initializers.get('glorot_uniform')

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

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

        self.bias = bias
        self.step_dim = step_dim
        self.features_dim = 0
        super(Attention, self).__init__(**kwargs)
Exemplo n.º 14
0
 def __init__(self,
              axis=None,
              epsilon=1e-3,
              center=True,
              scale=True,
              beta_initializer='zeros',
              gamma_initializer='ones',
              beta_regularizer=None,
              gamma_regularizer=None,
              beta_constraint=None,
              gamma_constraint=None,
              **kwargs):
     self.beta = None
     self.gamma = None
     super(InstanceNormalization, self).__init__(**kwargs)
     self.supports_masking = True
     self.axis = axis
     self.epsilon = epsilon
     self.center = center
     self.scale = scale
     self.beta_initializer = initializers.get(beta_initializer)
     self.gamma_initializer = initializers.get(gamma_initializer)
     self.beta_regularizer = regularizers.get(beta_regularizer)
     self.gamma_regularizer = regularizers.get(gamma_regularizer)
     self.beta_constraint = constraints.get(beta_constraint)
     self.gamma_constraint = constraints.get(gamma_constraint)
Exemplo n.º 15
0
    def __init__(self, units,
                 projection_units=None,
                 activation='tanh',
                 recurrent_activation='sigmoid',
                 projection_activation='linear',
                 use_bias=True,
                 kernel_initializer='glorot_uniform',
                 recurrent_initializer='orthogonal',
                 projection_initializer='glorot_uniform',
                 bias_initializer='zeros',
                 unit_forget_bias=False,
                 kernel_regularizer=None,
                 recurrent_regularizer=None,
                 projection_regularizer=None,
                 bias_regularizer=None,
                 kernel_constraint=None,
                 recurrent_constraint=None,
                 projection_constraint=None,
                 bias_constraint=None,
                 dropout=0.,
                 recurrent_dropout=0.,
                 implementation=2,
                 **kwargs):
        super(NASCell, self).__init__(**kwargs)
        self.units = units
        self.projection_units = projection_units
        self.activation = activations.get(activation)
        self.recurrent_activation = activations.get(recurrent_activation)
        self.projection_activation = activations.get(projection_activation)
        self.cell_activation = activations.get('relu')
        self.use_bias = use_bias

        self.kernel_initializer = initializers.get(kernel_initializer)
        self.recurrent_initializer = initializers.get(recurrent_initializer)
        self.projection_initializer = initializers.get(projection_initializer)
        self.bias_initializer = initializers.get(bias_initializer)
        self.unit_forget_bias = unit_forget_bias

        self.kernel_regularizer = regularizers.get(kernel_regularizer)
        self.recurrent_regularizer = regularizers.get(recurrent_regularizer)
        self.projection_regularizer = regularizers.get(projection_regularizer)
        self.bias_regularizer = regularizers.get(bias_regularizer)

        self.kernel_constraint = constraints.get(kernel_constraint)
        self.recurrent_constraint = constraints.get(recurrent_constraint)
        self.projection_constraint = constraints.get(projection_constraint)
        self.bias_constraint = constraints.get(bias_constraint)

        self.dropout = min(1., max(0., dropout))
        self.recurrent_dropout = min(1., max(0., recurrent_dropout))
        self.implementation = implementation

        if self.projection_units is not None:
            self.state_size = (self.projection_units, self.units)
        else:
            self.state_size = (self.units, self.units)

        self._dropout_mask = None
        self._recurrent_dropout_mask = None
Exemplo n.º 16
0
    def __init__(self, input_dim, output_dim, causes_dim,
                 hid2output,
                 init='glorot_uniform',
                 W_regularizer=None,
                 W_constraint=None,
                 b_regularizer=None,
                 b_constraint=None,
                 activation=lambda X: T.minimum(20, T.maximum(0, X)),
                 activity_regularizer=None,
                 truncate_gradient=-1,
                 weights=None, name=None,
                 return_mode='both',
                 return_sequences=True):
        super(GAE, self).__init__()
        self.input_dim = input_dim
        self.output_dim = output_dim
        self.causes_dim = causes_dim
        self.activation = activations.get(activation)
        self.init = initializations.get(init)
        self.truncate_gradient = truncate_gradient
        self.input = T.tensor3()
        self.return_mode = return_mode
        self.return_sequences = return_sequences

        self.V = self.init((input_dim, output_dim))
        self.U = self.init((input_dim, output_dim))
        self.W = self.init((output_dim, causes_dim))
        self.bo = shared_zeros((self.output_dim))
        self.bc = shared_zeros((self.causes_dim))

        self.params = [self.V, self.U, self.W]

        self.regularizers = []
        self.W_regularizer = regularizers.get(W_regularizer)
        if self.W_regularizer:
            self.W_regularizer.set_param(self.W)
            self.regularizers.append(self.W_regularizer)

        self.b_regularizer = regularizers.get(b_regularizer)
        if self.b_regularizer:
            self.b_regularizer.set_param(self.b)
            self.regularizers.append(self.b_regularizer)

        self.activity_regularizer = regularizers.get(activity_regularizer)
        if self.activity_regularizer:
            self.activity_regularizer.set_layer(self)
            self.regularizers.append(self.activity_regularizer)

        self.W_constraint = constraints.get(W_constraint)
        self.b_constraint = constraints.get(b_constraint)
        self.constraints = [self.W_constraint, self.b_constraint]

        if weights is not None:
            self.set_weights(weights)

        if name is not None:
            self.set_name(name)
    def __init__(self, epsilon=1e-3, axis=-1,
                 weights=None, beta_init='zero', gamma_init='one',
                 gamma_regularizer=None, beta_regularizer=None, **kwargs):

        self.supports_masking = True
        self.beta_init = initializers.get(beta_init)
        self.gamma_init = initializers.get(gamma_init)
        self.epsilon = epsilon
        self.axis = axis
        self.gamma_regularizer = regularizers.get(gamma_regularizer)
        self.beta_regularizer = regularizers.get(beta_regularizer)
        self.initial_weights = weights
        super(FixedBatchNormalization, self).__init__(**kwargs)
Exemplo n.º 18
0
    def __init__(self, filters, kernel_size,strides=1,padding='same',dilation_rate=1,
                 bias_initializer='zeros',kernel_initializer='glorot_uniform', activation='linear', weights=None,
                 border_mode='valid', subsample_length=1,
                 kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None,
                 kernel_constraint=None, bias_constraint=None,
                 use_bias=True, input_dim=None, input_length=None, tied_to=None,data_format='channels_last',rank=1,learnedKernel=None,layer_inner=None,
                 **kwargs):
        if border_mode not in {'valid', 'same'}:
            raise Exception('Invalid border mode for Convolution1D:', border_mode)
        self.input_spec = [InputSpec(ndim=3)]
        self.input_dim = input_dim
        self.input_length = input_length
        self.tied_to = tied_to
        self.learnedKernel = np.array(learnedKernel)
        #self.tied_to.set_weights([weights, bias])
        self.strides = conv_utils.normalize_tuple(strides, rank, 'strides')
        self.padding = conv_utils.normalize_padding(padding)
        self.dilation_rate = conv_utils.normalize_tuple(dilation_rate, rank, 'dilation_rate')
        self.data_format = data_format
        self.filters = filters
        if self.tied_to is not None:
            self.kernel_size = self.tied_to.kernel_size
        else:
            self.kernel_size = kernel_size
        self.kernel_initializer = initializers.get(kernel_initializer)
        self.bias_initializer = initializers.get(bias_initializer)
        self.activation = activations.get(activation)
        assert border_mode in {'valid', 'same'}, 'border_mode must be in {valid, same}'
        self.border_mode = border_mode
        self.subsample_length = subsample_length
        self.subsample = (subsample_length, 1)

        self.kernel_regularizer = regularizers.get(kernel_regularizer)
        self.bias_regularizer = regularizers.get(bias_regularizer)
        self.activity_regularizer = regularizers.get(activity_regularizer)

        self.kernel_constraint = constraints.get(kernel_constraint)
        self.bias_constraint = constraints.get(bias_constraint)

        self.use_bias = use_bias

        self.rank = 1

        self.layer_inner = layer_inner

        if self.input_dim:
            kwargs['input_shape'] = (self.input_length, self.input_dim)

        #self.layer_inner = kwargs.pop('layer_inner')

        super(Convolution1D_tied, self).__init__(**kwargs)
    def __init__(
        self,
        input_dim,
        output_dim,
        init="glorot_uniform",
        activation="linear",
        weights=None,
        name=None,
        W_regularizer=None,
        activity_regularizer=None,
        W_constraint=None,
    ):

        super(DenseNoBias, self).__init__()
        self.init = initializations.get(init)
        self.activation = activations.get(activation)
        self.input_dim = input_dim
        self.output_dim = output_dim

        self.input = T.matrix()
        self.W = self.init((self.input_dim, self.output_dim))

        # self.params = [self.W, self.b]
        self.params = [self.W]

        self.regularizers = []
        self.W_regularizer = regularizers.get(W_regularizer)
        if self.W_regularizer:
            self.W_regularizer.set_param(self.W)
            self.regularizers.append(self.W_regularizer)

        # self.b_regularizer = regularizers.get(b_regularizer)
        # if self.b_regularizer:
        #    self.b_regularizer.set_param(self.b)
        #    self.regularizers.append(self.b_regularizer)

        self.activity_regularizer = regularizers.get(activity_regularizer)
        if self.activity_regularizer:
            self.activity_regularizer.set_layer(self)
            self.regularizers.append(self.activity_regularizer)

        self.W_constraint = constraints.get(W_constraint)
        # self.b_constraint = constraints.get(b_constraint)
        # self.constraints = [self.W_constraint, self.b_constraint]
        self.constraints = [self.W_constraint]

        if weights is not None:
            self.set_weights(weights)

        if name is not None:
            self.set_name(name)
Exemplo n.º 20
0
    def __init__(self, input_dim, hidden_dim, init='glorot_uniform', weights=None, name=None,
        W_regularizer=None, bx_regularizer=None, bh_regularizer=None, #activity_regularizer=None,
        W_constraint=None, bx_constraint=None, bh_constraint=None):

        super(RBM, self).__init__()
        self.init = initializations.get(init)
        self.input_dim = input_dim
        self.hidden_dim = hidden_dim

        self.input = T.matrix()
        self.W = self.init((self.input_dim, self.hidden_dim))
        self.bx = shared_zeros((self.input_dim))
        self.bh = shared_zeros((self.hidden_dim))

        self.params = [self.W, self.bx, self.bh]

        self.regularizers = []

        self.W_regularizer = regularizers.get(W_regularizer)
        if self.W_regularizer:
            self.W_regularizer.set_param(self.W)
            self.regularizers.append(self.W_regularizer)

        self.bx_regularizer = regularizers.get(bx_regularizer)
        if self.bx_regularizer:
            self.bx_regularizer.set_param(self.bx)
            self.regularizers.append(self.bx_regularizer)

        self.bh_regularizer = regularizers.get(bh_regularizer)
        if self.bh_regularizer:
            self.bh_regularizer.set_param(self.bh)
            self.regularizers.append(self.bh_regularizer)

        #self.activity_regularizer = regularizers.get(activity_regularizer)
        #if self.activity_regularizer:
        #    self.activity_regularizer.set_layer(self)
        #    self.regularizers.append(self.activity_regularizer)

        self.W_constraint = constraints.get(W_constraint)
        self.bx_constraint = constraints.get(bx_constraint)
        self.bh_constraint = constraints.get(bh_constraint)
        self.constraints = [self.W_constraint, self.bx_constraint, self.bh_constraint]

        if weights is not None:
            self.set_weights(weights)

        if name is not None:
            self.set_name(name)

        self.srng = RandomStreams(seed=np.random.randint(10e6))
    def __init__(self, timesteps, bias=True, simple=False,
                 W_regularizer=None, W_constraint=None,
                 V_regularizer=None, V_constraint=None,
                 **kwargs):
        self.supports_masking = True
        self.init = initializers.get('glorot_uniform')
        self.bias = bias
        self.timesteps = timesteps
        self.simple = simple
        self.W_regularizer = regularizers.get(W_regularizer)
        self.W_constraint = constraints.get(W_constraint)
        self.V_regularizer = regularizers.get(V_regularizer)
        self.V_constraint = constraints.get(V_constraint)

        super(Attention, self).__init__(**kwargs)
    def __init__(self, timesteps, attention_size, bias=True,
                 W_regularizer=regularizers.l1(0.01), W_constraint=None,
                 U_regularizer=regularizers.l1(0.01), U_constraint=None,
                 **kwargs):
        self.supports_masking = True
        self.init = initializers.get('glorot_uniform')
        self.bias = bias
        self.timesteps = timesteps
        self.attention_size = attention_size
        self.W_regularizer = regularizers.get(W_regularizer)
        self.W_constraint = constraints.get(W_constraint)
        self.U_regularizer = regularizers.get(U_regularizer)
        self.U_constraint = constraints.get(U_constraint)

        super(Attention, self).__init__(**kwargs)
Exemplo n.º 23
0
    def __init__(self, nb_filter, filter_length,
                 init='glorot_uniform', activation='linear', weights=None,
                 padding='valid', strides=[1,1,1],
                 W_regularizer=None, b_regularizer=None, activity_regularizer=None,
                 W_constraint=None, b_constraint=None, input_dim=None, input_length=None, **kwargs):

        if padding not in {'valid','same'}:
            raise Exception('Invalid border mode for Convolution1D:', padding)
        #self.deconv_shape = deconv_shape

        # transform 1 D in 2D
        #deconv_shape = [batch_size, output_size_y, output_size_x, number_of_filters]
        # self.deconv_shape = [deconv_shape[0],1,deconv_shape[1],deconv_shape[2]]

        self.nb_filter = nb_filter
        self.filter_length = filter_length
        self.init = initializations.get(init)
        self.activation = activations.get(activation)
        assert padding in {'valid', 'same'}, 'border_mode must be in {valid, same}'
        self.padding = padding
        # necessary for loading, since a 4 dim. stride will be saved
        if len(strides) == 3:
            self.strides = [strides[0], 1, strides[1], strides[2]]
        else:
            self.strides = strides

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

        # self.W_shape = [1, W_shape[0], W_shape[1], W_shape[2]]
        # self.b_shape = b_shape

        self.W_constraint = constraints.get(W_constraint)
        self.b_constraint = constraints.get(b_constraint)
        self.constraints = [self.W_constraint, self.b_constraint]

        self.initial_weights = weights
        #self.input = K.placeholder(ndim=4) # old keras 0.3.x

        # Keras 1.0:
        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(Convolution1D_Transpose_Arbitrary, self).__init__(**kwargs)
Exemplo n.º 24
0
 def __init__(self,
              kernel_size,
              strides=(1, 1),
              padding='valid',
              depth_multiplier=1,
              data_format=None,
              activation=None,
              use_bias=True,
              depthwise_initializer='glorot_uniform',
              bias_initializer='zeros',
              depthwise_regularizer=None,
              bias_regularizer=None,
              activity_regularizer=None,
              depthwise_constraint=None,
              bias_constraint=None,
              **kwargs):
     super(DepthwiseConv2D, self).__init__(
         filters=None,
         kernel_size=kernel_size,
         strides=strides,
         padding=padding,
         data_format=data_format,
         activation=activation,
         use_bias=use_bias,
         bias_regularizer=bias_regularizer,
         activity_regularizer=activity_regularizer,
         bias_constraint=bias_constraint,
         **kwargs)
     self.depth_multiplier = depth_multiplier
     self.depthwise_initializer = initializers.get(depthwise_initializer)
     self.depthwise_regularizer = regularizers.get(depthwise_regularizer)
     self.depthwise_constraint = constraints.get(depthwise_constraint)
     self.bias_initializer = initializers.get(bias_initializer)
Exemplo n.º 25
0
    def __init__(self,
                 W_regularizer=None, b_regularizer=None,
                 W_constraint=None, b_constraint=None,
                 bias=True, **kwargs):

        self.supports_masking = True
        self.init = initializers.get('glorot_uniform')

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

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

        self.bias = bias
        super(Attention_layer, self).__init__(**kwargs)
Exemplo n.º 26
0
    def __init__(self, input_dim, hidden_dim, init='glorot_uniform', weights=None, name=None,
                 W_regularizer=None, bx_regularizer=None, bh_regularizer=None, #activity_regularizer=None,
                 W_constraint=None, bx_constraint=None, bh_constraint=None):

        super(RBM, self).__init__()
        self.init = initializations.get(init)
        self.input_dim = input_dim
        self.hidden_dim = hidden_dim

        self.input = K.placeholder(ndim = 2)
        self.W = self.init((self.input_dim, self.hidden_dim))
        self.bx = K.zeros((self.input_dim))
        self.bh = K.zeros((self.hidden_dim))

        self.params = [self.W, self.bx, self.bh]

        self.regularizers = []
        self.W_regularizer = regularizers.get(W_regularizer)
        if self.W_regularizer:
            self.W_regularizer.set_param(self.W)
            self.regularizers.append(self.W_regularizer)

        self.bx_regularizer = regularizers.get(bx_regularizer)
        if self.bx_regularizer:
            self.bx_regularizer.set_param(self.bx)
            self.regularizers.append(self.bx_regularizer)

        self.bh_regularizer = regularizers.get(bh_regularizer)
        if self.bh_regularizer:
            self.bh_regularizer.set_param(self.bh)
            self.regularizers.append(self.bh_regularizer)

        #self.activity_regularizer = regularizers.get(activity_regularizer)
        #if self.activity_regularizer:
        #    self.activity_regularizer.set_layer(self)
        #    self.regularizers.append(self.activity_regularizer)

        self.W_constraint = constraints.get(W_constraint)
        self.bx_constraint = constraints.get(bx_constraint)
        self.bh_constraint = constraints.get(bh_constraint)
        self.constraints = [self.W_constraint, self.bx_constraint, self.bh_constraint]

        if weights is not None:
            self.set_weights(weights)

        if name is not None:
            self.set_name(name)
Exemplo n.º 27
0
    def __init__(self,
                 units = 1,
                 output_dim = 1,
                 W_regularizer=None, b_regularizer=None,
                 W_constraint=None, b_constraint=None,
                 bias=True,
                 return_sequence = True,
                 return_attention=False,
                 return_probabilities = False,
                 **kwargs):
        """
        Keras Layer that implements an Attention mechanism for temporal data.
        Supports Masking.
        Follows the work of Raffel et al. [https://arxiv.org/abs/1512.08756]
        # Input shape
            3D tensor with shape: `(samples, steps, features)`.
        # Output shape
            2D tensor with shape: `(samples, features)`.
        :param kwargs:
        Just put it on top of an RNN Layer (GRU/LSTM/SimpleRNN) with return_sequences=True.
        The dimensions are inferred based on the output shape of the RNN.
        Note: The layer has been tested with Keras 1.x
        Example:
        
            # 1
            model.add(LSTM(64, return_sequences=True))
            model.add(Attention())
            # next add a Dense layer (for classification/regression) or whatever...
            # 2 - Get the attention scores
            hidden = LSTM(64, return_sequences=True)(words)
            sentence, word_scores = Attention(return_attention=True)(hidden)
        """
        self.supports_masking = True
        self.return_attention = return_attention
        self.init = initializers.get('glorot_uniform')
        self.attention_dim = units
        self.output_dim = output_dim
        self.W_regularizer = regularizers.get(W_regularizer)
        self.b_regularizer = regularizers.get(b_regularizer)

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

        self.bias = bias
        super(AttentionDecoder, self).__init__(**kwargs)
Exemplo n.º 28
0
  def __init__(self, output_dim, output_length,
               init='glorot_uniform', inner_init='orthogonal',
               activation='tanh',
               W_regularizer=None, U_regularizer=None, b_regularizer=None,
               dropout_W=0., dropout_U=0., **kwargs):
      self.output_dim = output_dim
      self.output_length = output_length
      self.init = initializations.get(init)
      self.inner_init = initializations.get(inner_init)
      self.activation = activations.get(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, self.dropout_U = dropout_W, dropout_U

      if self.dropout_W or self.dropout_U:
          self.uses_learning_phase = True
      super(DreamyRNN, self).__init__(**kwargs)
    def __init__(self, input_dim, output_dim, init='uniform', input_length=None,
                 W_regularizer=None, activity_regularizer=None, W_constraint=None,
                 mask_zero=False, weights=None, **kwargs):
        self.input_dim = input_dim
        self.output_dim = output_dim
        self.init = initializations.get(init)
        self.input_length = input_length
        self.mask_zero = mask_zero

        self.W_constraint = constraints.get(W_constraint)
        self.constraints = [self.W_constraint]

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

        self.initial_weights = weights
        kwargs['input_shape'] = (self.input_dim,)
        super(FixedEmbedding, self).__init__(**kwargs)
Exemplo n.º 30
0
    def __init__(self, step_dim,
                 W_regularizer=None, b_regularizer=None,
                 W_constraint=None, b_constraint=None,
                 bias=True, **kwargs):
        self.supports_masking = True
        # self.init = initializations.get('glorot_uniform')
        self.init = initializers.get('glorot_uniform')

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

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

        self.bias = bias
        self.step_dim = step_dim
        self.features_dim = 0
        super(Attention, self).__init__(**kwargs)
Exemplo n.º 31
0
	def __init__(self, units,
				s = 5.,
				kernel_initializer='glorot_uniform',
				kernel_regularizer=None,
				kernel_constraint=None,
				**kwargs):
		if 'input_shape' not in kwargs and 'input_dim' in kwargs:
			kwargs['input_shape'] = (kwargs.pop('input_dim'),)
		super(Dense, self).__init__(**kwargs)
		self.units = units
		self.s = s
		
		self.kernel_initializer = initializers.get(kernel_initializer)
		self.kernel_regularizer = regularizers.get(kernel_regularizer)
		self.kernel_constraint = constraints.get(kernel_constraint)
Exemplo n.º 32
0
 def __init__(self,
              momentum=0.99,
              epsilon=1e-3,
              center=True,
              scale=True,
              alpha_init='one',
              beta_init='zero',
              alpha_regularizer=None,
              beta_regularizer=None,
              alpha_constraint=None,
              beta_constraint=None,
              **kwargs):
     super(SmoothErrorCounter, self).__init__(**kwargs)
     # self.supports_masking = True
     self.momentum = momentum
     self.epsilon = epsilon
     self.center = center
     self.scale = scale
     self.alpha_initializer = initializers.get(alpha_init)
     self.beta_initializer = initializers.get(beta_init)
     self.alpha_regularizer = regularizers.get(alpha_regularizer)
     self.beta_regularizer = regularizers.get(beta_regularizer)
     self.alpha_constraint = constraints.get(alpha_constraint)
     self.beta_constraint = constraints.get(beta_constraint)
    def __init__(self,
                 step_dim,
                 W_regularizer=None,
                 b_regularizer=None,
                 W_constraint=None,
                 b_constraint=None,
                 bias=True,
                 **kwargs):
        """
        Keras Layer that implements an Attention mechanism for temporal data.
        Supports Masking.
        Follows the work of Raffel et al. [https://arxiv.org/abs/1512.08756]
        # Input shape
            3D tensor with shape: `(samples, steps, features)`.
        # Output shape
            2D tensor with shape: `(samples, features)`.
        :param kwargs:
        Just put it on top of an RNN Layer (GRU/LSTM/SimpleRNN) with return_sequences=True.
        The dimensions are inferred based on the output shape of the RNN.
        Example:
            model.add(LSTM(64, return_sequences=True))
            model.add(Attention())
        """
        self.supports_masking = True
        self.init = initializers.get('glorot_uniform')

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

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

        self.bias = bias
        self.step_dim = step_dim
        self.features_dim = 0
        super(Attention, self).__init__(**kwargs)
def test_custom_objects_scope():
    def custom_fn():
        pass

    class CustomClass(object):
        pass

    with custom_object_scope({
            'CustomClass': CustomClass,
            'custom_fn': custom_fn
    }):
        act = activations.get('custom_fn')
        assert act == custom_fn
        cl = regularizers.get('CustomClass')
        assert cl.__class__ == CustomClass
Exemplo n.º 35
0
    def __init__(self,
                 nb_classes,
                 frequency_table=None,
                 mode=0,
                 init='glorot_uniform',
                 weights=None,
                 W_regularizer=None,
                 b_regularizer=None,
                 activity_regularizer=None,
                 W_constraint=None,
                 b_constraint=None,
                 bias=True,
                 verbose=False,
                 **kwargs):
        '''
		# Arguments:
		nb_classes: Number of classes.
		frequency_table: list. Frequency of each class. More frequent classes will have shorter huffman codes.
		mode: integer. One of [0, 1]
		verbose: boolean. Set to true to see the progress of building huffman tree. 
		'''
        self.nb_classes = nb_classes
        if frequency_table is None:
            frequency_table = [1] * nb_classes
        self.frequency_table = frequency_table
        self.mode = mode
        self.init = initializations.get(init)
        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.initial_weights = weights
        self.verbose = verbose
        super(Huffmax, self).__init__(**kwargs)
Exemplo n.º 36
0
    def __init__(self,
                 axis=-1,
                 epsilon=1e-3,
                 center=True,
                 scale=True,
                 beta_initializer="zeros",
                 gamma_initializer="ones",
                 beta_regularizer=None,
                 gamma_regularizer=None,
                 beta_constraint=None,
                 gamma_constraint=None,
                 **kwargs):
        super().__init__(**kwargs)
        if isinstance(axis, (list, tuple)):
            self.axis = list(axis)
        elif isinstance(axis, int):
            self.axis = axis
        else:
            raise TypeError("Expected an int or a list/tuple of ints for the "
                            "argument 'axis', but received: %r" % axis)

        self.epsilon = epsilon
        self.center = center
        self.scale = scale
        self.beta_initializer = initializers.get(beta_initializer)
        self.gamma_initializer = initializers.get(gamma_initializer)
        self.beta_regularizer = regularizers.get(beta_regularizer)
        self.gamma_regularizer = regularizers.get(gamma_regularizer)
        self.beta_constraint = constraints.get(beta_constraint)
        self.gamma_constraint = constraints.get(gamma_constraint)

        self.supports_masking = True

        # Indicates whether a faster fused implementation can be used. This will
        # be set to True or False in build()"
        self._fused = None
Exemplo n.º 37
0
    def __init__(self,
                 init='glorot_uniform',
                 U_regularizer=None,
                 b_start_regularizer=None,
                 b_end_regularizer=None,
                 U_constraint=None,
                 b_start_constraint=None,
                 b_end_constraint=None,
                 weights=None,
                 **kwargs):
        super(ChainCRF, self).__init__(**kwargs)
        self.init = initializers.get(init)
        self.U_regularizer = regularizers.get(U_regularizer)
        self.b_start_regularizer = regularizers.get(b_start_regularizer)
        self.b_end_regularizer = regularizers.get(b_end_regularizer)
        self.U_constraint = constraints.get(U_constraint)
        self.b_start_constraint = constraints.get(b_start_constraint)
        self.b_end_constraint = constraints.get(b_end_constraint)

        self.initial_weights = weights

        self.supports_masking = True
        self.uses_learning_phase = True
        self.input_spec = [InputSpec(ndim=3)]
Exemplo n.º 38
0
 def __init__(self,
              num_heads,
              key_dim,
              value_dim=None,
              dropout=0.0,
              use_bias=True,
              output_shape=None,
              attention_axes=None,
              kernel_initializer="glorot_uniform",
              bias_initializer="zeros",
              kernel_regularizer=None,
              bias_regularizer=None,
              activity_regularizer=None,
              kernel_constraint=None,
              bias_constraint=None,
              **kwargs):
     super(MultiHeadAttention, self).__init__(**kwargs)
     self._num_heads = num_heads
     self._key_dim = key_dim
     self._value_dim = value_dim if value_dim else key_dim
     self._dropout = dropout
     self._use_bias = use_bias
     self._output_shape = output_shape
     self._kernel_initializer = initializers.get(kernel_initializer)
     self._bias_initializer = initializers.get(bias_initializer)
     self._kernel_regularizer = regularizers.get(kernel_regularizer)
     self._bias_regularizer = regularizers.get(bias_regularizer)
     self._kernel_constraint = constraints.get(kernel_constraint)
     self._bias_constraint = constraints.get(bias_constraint)
     if attention_axes is not None and not isinstance(
             attention_axes, collections.abc.Sized):
         self._attention_axes = (attention_axes, )
     else:
         self._attention_axes = attention_axes
     self._built_from_signature = False
     self._query_shape, self._key_shape, self._value_shape = None, None, None
Exemplo n.º 39
0
 def __init__(self, units,
              activation=None,
              use_bias=True,
              init_criterion='he',
              kernel_initializer='complex',
              bias_initializer='zeros',
              kernel_regularizer=None,
              bias_regularizer=None,
              activity_regularizer=None,
              kernel_constraint=None,
              bias_constraint=None,
              seed=None,
              **kwargs):
     if 'input_shape' not in kwargs and 'input_dim' in kwargs:
         kwargs['input_shape'] = (kwargs.pop('input_dim'),)
     super(ComplexDense, self).__init__(**kwargs)
     self.units = units
     self.activation = activations.get(activation)
     self.use_bias = use_bias
     self.init_criterion = init_criterion
     if kernel_initializer in {'complex'}:
         self.kernel_initializer = kernel_initializer
     else:
         self.kernel_initializer = initializers.get(kernel_initializer)
     self.bias_initializer = initializers.get(bias_initializer)
     self.kernel_regularizer = regularizers.get(kernel_regularizer)
     self.bias_regularizer = regularizers.get(bias_regularizer)
     self.activity_regularizer = regularizers.get(activity_regularizer)
     self.kernel_constraint = constraints.get(kernel_constraint)
     self.bias_constraint = constraints.get(bias_constraint)
     if seed is None:
         self.seed = np.random.randint(1, 10e6)
     else:
         self.seed = seed
     self.input_spec = InputSpec(ndim=2)
     self.supports_masking = True
Exemplo n.º 40
0
    def __init__(self,
                 units,
                 output_dim,
                 activation='tanh',
                 return_probabilities=False,
                 name='AttentionDecoder',
                 kernel_initializer='glorot_uniform',
                 recurrent_initializer='orthogonal',
                 bias_initializer='zeros',
                 kernel_regularizer=None,
                 bias_regularizer=None,
                 activity_regularizer=None,
                 kernel_constraint=None,
                 bias_constraint=None,
                 **kwargs):

        self.units = units
        self.output_dim = output_dim
        self.return_probabilities = return_probabilities
        self.activation = activations.get(activation)
        self.kernel_initializer = initializers.get(kernel_initializer)
        self.recurrent_initializer = initializers.get(recurrent_initializer)
        self.bias_initializer = initializers.get(bias_initializer)

        self.kernel_regularizer = regularizers.get(kernel_regularizer)
        self.recurrent_regularizer = regularizers.get(kernel_regularizer)
        self.bias_regularizer = regularizers.get(bias_regularizer)
        self.activity_regularizer = regularizers.get(activity_regularizer)

        self.kernel_constraint = constraints.get(kernel_constraint)
        self.recurrent_constraint = constraints.get(kernel_constraint)
        self.bias_constraint = constraints.get(bias_constraint)

        super(AttentionDecoder, self).__init__(**kwargs)
        self.name = name
        self.return_sequences = True
Exemplo n.º 41
0
    def __init__(self,
                 init='glorot_uniform',
                 transform_bias=-2,
                 n_rel=5,
                 mean=1,
                 activation='linear',
                 weights=None,
                 W_regularizer=None,
                 b_regularizer=None,
                 activity_regularizer=None,
                 W_constraint=None,
                 b_constraint=None,
                 bias=True,
                 input_dim=None,
                 **kwargs):
        self.init = initializations.get(init)
        self.transform_bias = transform_bias
        self.activation = activations.get(activation)
        self.n_rel = n_rel
        self.mean = mean

        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.initial_weights = weights
        self.input_spec = [InputSpec(ndim=2)]

        self.input_dim = input_dim
        if self.input_dim:
            kwargs['input_shape'] = (self.input_dim, )
        super(GraphHighway, self).__init__(**kwargs)
Exemplo n.º 42
0
    def __init__(self,
                 W_regularizer=None,
                 u_regularizer=None,
                 b_regularizer=None,
                 W_constraint=None,
                 u_constraint=None,
                 b_constraint=None,
                 bias=True,
                 **kwargs):

        self.supports_masking = True
        #self.init = initializations.get('glorot_uniform')
        self.init = glorot_uniform()

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

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

        self.bias = bias
        super(AttentionWithContext, self).__init__(**kwargs)
Exemplo n.º 43
0
    def __init__(self,
                 output_dim,
                 k,
                 activation=None,
                 kernel_regularizer=None,
                 **kwargs):
        if 'input_shape' not in kwargs and 'input_dim' in kwargs:
            kwargs['input_shape'] = (kwargs.pop('input_dim'), )
        super(FMLayer, self).__init__(**kwargs)

        self.output_dim = output_dim
        self.k = k
        self.activation = activations.get(activation)
        self.kernel_regularizer = regularizers.get(kernel_regularizer)
        self.input_spec = InputSpec(ndim=2)
Exemplo n.º 44
0
 def __init__(self,
              num_filters,
              activation='relu',
              use_bias=True,
              kernel_initializer='glorot_uniform',
              bias_initalizer='zeros',
              kernel_regularizer=None,
              bias_regularizer=None,
              kernel_constraint=None,
              bias_constraint=None,
              include_back_hops=False,
              **kwargs):
     super(LocalGraphLayer, self).__init__(**kwargs)
     self.num_filters = num_filters
     self.activation = activations.get(activation)
     self.use_bias = use_bias
     self.kernel_initializer = initializers.get(kernel_initializer)
     self.bias_initalizer = initializers.get(bias_initalizer)
     self.kernel_regularizer = regularizers.get(kernel_regularizer)
     self.bias_regularizer = regularizers.get(bias_regularizer)
     self.kernel_constraint = constraints.get(kernel_constraint)
     self.bias_constraint = constraints.get(bias_constraint)
     self.include_back_hops = include_back_hops
     self.input_spec = InputSpec(ndim=4)
Exemplo n.º 45
0
    def __init__(self,
                 first_dim,
                 last_dim,
                 init='glorot_uniform',
                 activation=None,
                 weights=None,
                 W_regularizer=None,
                 b_regularizer=None,
                 activity_regularizer=None,
                 W_constraint=None,
                 b_constraint=None,
                 bias=True,
                 input_dim=None,
                 **kwargs):

        self.init = initializations.get(init)
        self.activation = activations.get(activation)

        self.input_dim = input_dim
        self.first_dim = first_dim
        self.last_dim = last_dim

        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.initial_weights = weights
        self.input_spec = [InputSpec(ndim=2)]

        if self.input_dim:
            kwargs['input_shape'] = (self.input_dim, )
        super(Dense3D, self).__init__(**kwargs)
Exemplo n.º 46
0
    def __init__(self, input_dim, output_dim,
                 init='uniform', input_length=None,
                 W_regularizer=None, activity_regularizer=None,
                 W_constraint=None,
                 mask_zero=False,
                 weights=None, dropout=0., **kwargs):
        self.input_dim = input_dim
        self.output_dim = output_dim
        self.init = initializations.get(init)
        self.input_length = input_length
        self.mask_zero = mask_zero
        self.dropout = dropout

        self.W_constraint = constraints.get(W_constraint)

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

        if 0. < self.dropout < 1.:
            self.uses_learning_phase = True
        self.initial_weights = weights
        kwargs['input_shape'] = (self.input_length,)
        kwargs['input_dtype'] = 'int32'
        super(Embedding, self).__init__(**kwargs)
Exemplo n.º 47
0
 def __init__(self,
              output_dim,
              activation=None,
              use_bias=True,
              kernel_initializer='glorot_uniform',
              bias_initializer='zeros',
              kernel_regularizer=None,
              **kwargs):
     super(DiagonalReal, self).__init__(**kwargs)
     self.output_dim = output_dim
     self.activation = activations.get(activation)
     self.use_bias = use_bias
     self.kernel_initializer = initializers.get(kernel_initializer)
     self.bias_initializer = initializers.get(bias_initializer)
     self.kernel_regularizer = regularizers.get(kernel_regularizer)
Exemplo n.º 48
0
 def __init__(self,
              axis=None,
              epsilon=1e-3,
              center=True,
              scale=True,
              beta_initializer='zeros',
              gamma_initializer='ones',
              beta_regularizer=None,
              gamma_regularizer=None,
              beta_constraint=None,
              gamma_constraint=None,
              **kwargs):
     super(InstanceNormalization, self).__init__(**kwargs)
     self.supports_masking = True
     self.axis = axis
     self.epsilon = epsilon
     self.center = center
     self.scale = scale
     self.beta_initializer = initializers.get(beta_initializer)
     self.gamma_initializer = initializers.get(gamma_initializer)
     self.beta_regularizer = regularizers.get(beta_regularizer)
     self.gamma_regularizer = regularizers.get(gamma_regularizer)
     self.beta_constraint = constraints.get(beta_constraint)
     self.gamma_constraint = constraints.get(gamma_constraint)
Exemplo n.º 49
0
  def test_revived_sequential(self):
    model = keras.models.Sequential()
    model.add(keras.layers.Dense(5, input_shape=(3,),
                                 kernel_regularizer=regularizers.get('l2')))
    model.add(keras.layers.Dense(2, kernel_regularizer=regularizers.get('l2')))

    self.evaluate(tf.compat.v1.variables_initializer(model.variables))

    saved_model_dir = self._save_model_dir()
    model.save(saved_model_dir, save_format='tf')
    loaded = keras_load.load(saved_model_dir)

    self.assertLen(loaded.layers, 2)
    self.assertLen(loaded.losses, 2)

    loaded.pop()

    self.assertLen(loaded.layers, 1)
    self.assertLen(loaded.losses, 1)

    loaded.add(keras.layers.Dense(2, kernel_regularizer=regularizers.get('l2')))

    self.assertLen(loaded.layers, 2)
    self.assertLen(loaded.losses, 2)
Exemplo n.º 50
0
    def __init__(self,
                 x_k,
                 hidden_dim,
                 stochastic=True,
                 kernel_initializer='glorot_uniform',
                 bias_initializer='zeros',
                 kernel_regularizer=None,
                 bias_regularizer=None,
                 kernel_constraint=None,
                 bias_constraint=None,
                 **kwargs):
        self.x_k = x_k
        self.hidden_dim = hidden_dim
        self.stochastic = stochastic

        self.kernel_initializer = initializers.get(kernel_initializer)
        self.bias_initializer = initializers.get(bias_initializer)
        self.kernel_regularizer = regularizers.get(kernel_regularizer)
        self.bias_regularizer = regularizers.get(bias_regularizer)

        self.kernel_constraint = constraints.get(kernel_constraint)
        self.bias_constraint = constraints.get(bias_constraint)
        self.units = x_k + 1
        Recurrent.__init__(self, return_sequences=True, **kwargs)
Exemplo n.º 51
0
    def __init__(self,
                 kernel_size,
                 strides=(1, 1),
                 padding='valid',
                 depth_multiplier=1,
                 data_format=None,
                 activation=None,
                 use_bias=True,
                 depthwise_initializer='glorot_uniform',
                 bias_initializer='zeros',
                 depthwise_regularizer=None,
                 bias_regularizer=None,
                 activity_regularizer=None,
                 depthwise_constraint=None,
                 bias_constraint=None,
                 **kwargs):
        super(DepthwiseConv2D,
              self).__init__(filters=None,
                             kernel_size=kernel_size,
                             strides=strides,
                             padding=padding,
                             data_format=data_format,
                             activation=activation,
                             use_bias=use_bias,
                             bias_regularizer=bias_regularizer,
                             activity_regularizer=activity_regularizer,
                             bias_constraint=bias_constraint,
                             **kwargs)
        self.depth_multiplier = depth_multiplier
        self.depthwise_initializer = initializers.get(depthwise_initializer)
        self.depthwise_regularizer = regularizers.get(depthwise_regularizer)
        self.depthwise_constraint = constraints.get(depthwise_constraint)
        self.bias_initializer = initializers.get(bias_initializer)

        self._padding = padding.upper()

        if K.image_data_format() == 'channels_last':
            self._strides = (1, ) + strides + (1, )
        else:
            self._strides = (
                1,
                1,
            ) + strides

        if self.data_format == 'channels_last':
            self._data_format = "NHWC"
        else:
            self._data_format = "NCHW"
Exemplo n.º 52
0
 def __init__(self,
              kernel_initializer='glorot_uniform',
              kernel_regularizer=None,
              kernel_constraint=None,
              use_bias=True,
              mid_units=None,
              alpha=1.,
              keepdims=False,
              **kwargs):
     self.kernel_initializer = initializers.get(kernel_initializer)
     self.kernel_regularizer = regularizers.get(kernel_regularizer)
     self.kernel_constraint = constraints.get(kernel_constraint)
     self.use_bias = use_bias
     self.mid_units = mid_units
     self.supports_masking = True
     super(PairAttention, self).__init__(alpha, keepdims, **kwargs)
Exemplo n.º 53
0
    def __init__(self,
                 alpha_initializer=Constant(3.0),
                 alpha_regularizer=None,
                 alpha_constraint=MinMaxValue(),
                 **kwargs):
        if 'input_shape' not in kwargs and 'input_dim' in kwargs:
            kwargs['input_shape'] = (kwargs.pop('input_dim'), )

        super(PLSEU, self).__init__(**kwargs)
        self.alpha_initializer = initializers.get(alpha_initializer)
        self.alpha_regularizer = regularizers.get(alpha_regularizer)
        self.alpha_constraint = constraints.get(alpha_constraint)
        self.alpha = None

        self.input_spec = InputSpec(min_ndim=2)
        self.supports_masking = True
Exemplo n.º 54
0
 def __init__(self,
              layer_to_tie,
              activation=None,
              add_biases=False,
              projection_regularizer=None,
              projection_dropout: float = 0.0,
              scaled_attention=False,
              **kwargs):
     super(TiedOutputLayer, self).__init__(**kwargs)
     self.layer_to_tie = layer_to_tie
     self.activation = activations.get(activation)
     self.add_biases = add_biases
     self.projection_regularizer = regularizers.get(projection_regularizer)
     self.projection_dropout = projection_dropout
     self.scaled_attention = scaled_attention
     self.supports_masking = True
Exemplo n.º 55
0
 def get_config(self):
     config = {
         'x_imputation': self.x_imputation,
         'input_decay': serialize_keras_object(self.input_decay),
         'hidden_decay': serialize_keras_object(self.hidden_decay),
         'use_decay_bias': self.use_decay_bias,
         'feed_masking': self.feed_masking,
         'masking_decay': serialize_keras_object(self.masking_decay),
         'decay_initializer': initializers.get(self.decay_initializer),
         'decay_regularizer': regularizers.get(self.decay_regularizer),
         'decay_constraint': constraints.get(self.decay_constraint)
     }
     base_config = super(GRUD, self).get_config()
     for c in ['implementation', 'reset_after']:
         del base_config[c]
     return dict(list(base_config.items()) + list(config.items()))
Exemplo n.º 56
0
    def __init__(self,
                 filters,
                 pooling='sum',
                 kernel_initializer='glorot_uniform',
                 kernel_regularizer=None,
                 bias_initializer='zeros',
                 activation=None,
                 **kwargs):
        self.activation = activations.get(activation)
        self.kernel_initializer = initializers.get(kernel_initializer)
        self.bias_initializer = initializers.get(bias_initializer)
        self.kernel_regularizer = regularizers.get(kernel_regularizer)
        self.filters = filters
        self.pooling = pooling

        super(GraphConvV, self).__init__(**kwargs)
Exemplo n.º 57
0
 def __init__(self,
              r=None,
              kernel_initializer='glorot_uniform',
              kernel_regularizer=None,
              kernel_constraint=None,
              **kwargs):
     super(CCMProjection, self).__init__(**kwargs)
     self.radius = r
     self.kernel_initializer = initializers.get(kernel_initializer)
     self.kernel_regularizer = regularizers.get(kernel_regularizer)
     if self.radius == 'spherical':
         self.kernel_constraint = self.Pos()
     elif self.radius == 'hyperbolic':
         self.kernel_constraint = self.Neg()
     else:
         self.kernel_constraint = constraints.get(kernel_constraint)
Exemplo n.º 58
0
 def __init__(
     self,
     gamma_initializer="ones",
     gamma_regularizer=None,
     gamma_constraint=None,
     epsilon=1e-07,
     **kwargs,
 ):
     super(WeightNorm_Conv, self).__init__(**kwargs)
     if self.rank == 1:
         self.data_format = "channels_last"
     self.gamma_initializer = sanitizedInitGet(gamma_initializer)
     self.gamma_regularizer = regularizers.get(gamma_regularizer)
     self.gamma_constraint = constraints.get(gamma_constraint)
     self.epsilon = epsilon
     self.gamma = None
Exemplo n.º 59
0
 def __init__(self,
              input_dim,
              max_sent1,
              max_sent2,
              kernel_initializer='glorot_uniform',
              kernel_regularizer=None,
              **kwargs):
     self.input_dim = input_dim
     if self.input_dim:
         kwargs['input_shape'] = (self.input_dim, )
     super(BiAttentionLayer, self).__init__(**kwargs)
     self.max_sent1 = max_sent1
     self.max_sent2 = max_sent2
     self.kernel_initializer = initializers.get(kernel_initializer)
     #self.bias_initializer = initializers.get(bias_initializer)
     self.kernel_regularizer = regularizers.get(kernel_regularizer)
Exemplo n.º 60
0
    def __init__(self,
                 units,
                 inner_init='orthogonal',
                 activation='tanh',
                 recurrent_regularizer=None,
                 recurrent_dropout=0.,
                 **kwargs):
        self.units = units
        self.recurrent_initializer = initializers.get(inner_init)
        self.activation = activations.get(activation)
        self.recurrent_regularizer = regularizers.get(recurrent_regularizer)
        self.recurrent_dropout = recurrent_dropout

        if self.recurrent_dropout:
            self.uses_learning_phase = True
        super(SimpleURNN, self).__init__(**kwargs)