Python keras.engine.InputSpec() Examples

The following are 30 code examples of keras.engine.InputSpec(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module keras.engine , or try the search function .
Example #1
Source File: ChainCRF.py    From elmo-bilstm-cnn-crf with Apache License 2.0 6 votes vote down vote up
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)] 
Example #2
Source File: ChainCRF.py    From naacl18-multitask_argument_mining with Apache License 2.0 6 votes vote down vote up
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):
        self.supports_masking = True
        self.uses_learning_phase = True
        self.input_spec = [InputSpec(ndim=3)]
        self.init = initializations.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

        super(ChainCRF, self).__init__(**kwargs) 
Example #3
Source File: FixedBatchNormalization.py    From keras-frcnn with Apache License 2.0 6 votes vote down vote up
def build(self, input_shape):
        self.input_spec = [InputSpec(shape=input_shape)]
        shape = (input_shape[self.axis],)

        self.gamma = self.add_weight(shape,
                                     initializer=self.gamma_init,
                                     regularizer=self.gamma_regularizer,
                                     name='{}_gamma'.format(self.name),
                                     trainable=False)
        self.beta = self.add_weight(shape,
                                    initializer=self.beta_init,
                                    regularizer=self.beta_regularizer,
                                    name='{}_beta'.format(self.name),
                                    trainable=False)
        self.running_mean = self.add_weight(shape, initializer='zero',
                                            name='{}_running_mean'.format(self.name),
                                            trainable=False)
        self.running_std = self.add_weight(shape, initializer='one',
                                           name='{}_running_std'.format(self.name),
                                           trainable=False)

        if self.initial_weights is not None:
            self.set_weights(self.initial_weights)
            del self.initial_weights
        self.built = True 
Example #4
Source File: keras2_emitter.py    From MMdnn with MIT License 6 votes vote down vote up
def _layer_Affine(self):
        self.add_body(0, '''
from keras.engine import Layer, InputSpec
from keras import initializers
from keras  import backend as K

class Affine(Layer):
    def __init__(self, scale, bias=None, **kwargs):
        super(Affine, self).__init__(**kwargs)
        self.gamma = scale
        self.beta = bias

    def call(self, inputs, training=None):
        input_shape = K.int_shape(inputs)
        # Prepare broadcasting shape.
        return self.gamma * inputs + self.beta

    def compute_output_shape(self, input_shape):
        return input_shape
        ''') 
Example #5
Source File: coord_conv.py    From costar_plan with Apache License 2.0 6 votes vote down vote up
def __init__(self, rank,
                 use_radius=False,
                 data_format=None,
                 **kwargs):
        super(_CoordinateChannel, self).__init__(**kwargs)

        if data_format not in [None, 'channels_first', 'channels_last']:
            raise ValueError('`data_format` must be either "channels_last", "channels_first" '
                             'or None.')

        self.rank = rank
        self.use_radius = use_radius
        self.data_format = K.image_data_format() if data_format is None else data_format
        self.axis = 1 if K.image_data_format() == 'channels_first' else -1

        self.input_spec = InputSpec(min_ndim=2)
        self.supports_masking = True 
Example #6
Source File: layer_utils.py    From deep_learning with MIT License 6 votes vote down vote up
def __init__(self, padding=(1, 1), data_format=None, **kwargs):
        super(ReflectionPadding2D, self).__init__(**kwargs)
        self.data_format = conv_utils.normalize_data_format(data_format)
        if isinstance(padding, int):
            self.padding = ((padding, padding), (padding, padding))
        elif hasattr(padding,"__len__"):
            if len(padding) != 2:
                 raise ValueError('`padding` should have two elements. '
                                  'Found: ' + str(padding))
            height_padding = conv_utils.normalize_tuple(padding[0], 2, "1st entry of padding")
            width_padding = conv_utils.normalize_tuple(padding[1], 2, "2nd entry of padding")
            self.padding = (height_padding, width_padding)
        else:
            raise ValueError('`padding` should be either an int, '
                             'a tuple of 2 ints '
                             '(symmetric_height_pad, symmetric_width_pad), '
                             'or a tuple of 2 tuples of 2 ints '
                             '((top_pad, bottom_pad), (left_pad, right_pad)). '
                             'Found: ' + str(padding))
            self.input_spec = InputSpec(ndim=4) 
Example #7
Source File: recurrent.py    From keras_bn_library with MIT License 6 votes vote down vote up
def build(self, input_shape):
		self.input_spec = [InputSpec(shape=input_shape)]
		self.input_dim = input_shape[2]

		self.W = self.init((self.output_dim, 4 * self.input_dim),
		                   name='{}_W'.format(self.name))
		self.U = self.inner_init((self.input_dim, 4 * self.input_dim),
		                         name='{}_U'.format(self.name))
		self.b = K.variable(np.hstack((np.zeros(self.input_dim),
		                               K.get_value(self.forget_bias_init((self.input_dim,))),
		                               np.zeros(self.input_dim),
		                               np.zeros(self.input_dim))),
		                    name='{}_b'.format(self.name))

		self.A = self.init((self.input_dim, self.output_dim),
		                    name='{}_A'.format(self.name))
		self.ba = K.zeros((self.output_dim,), name='{}_ba'.format(self.name))


		self.trainable_weights = [self.W, self.U, self.b, self.A, self.ba]

		if self.initial_weights is not None:
			self.set_weights(self.initial_weights)
			del self.initial_weights 
Example #8
Source File: ChainCRF.py    From emnlp2017-bilstm-cnn-crf with Apache License 2.0 6 votes vote down vote up
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)] 
Example #9
Source File: layers.py    From keras-fcn with MIT License 6 votes vote down vote up
def __init__(self, target_shape, offset=None, data_format=None,
                 **kwargs):
        """Crop to target.

        If only one `offset` is set, then all dimensions are offset by this amount.

        """
        super(CroppingLike2D, self).__init__(**kwargs)
        self.data_format = conv_utils.normalize_data_format(data_format)
        self.target_shape = target_shape
        if offset is None or offset == 'centered':
            self.offset = 'centered'
        elif isinstance(offset, int):
            self.offset = (offset, offset)
        elif hasattr(offset, '__len__'):
            if len(offset) != 2:
                raise ValueError('`offset` should have two elements. '
                                 'Found: ' + str(offset))
            self.offset = offset
        self.input_spec = InputSpec(ndim=4) 
Example #10
Source File: pspnet.py    From keras-image-segmentation with MIT License 6 votes vote down vote up
def __init__(self, target_shape=None,factor=None, data_format=None, **kwargs):
        # conmpute dataformat
        if data_format is None:
            data_format = K.image_data_format()
        assert data_format in {
            'channels_last', 'channels_first'}

        self.data_format = data_format
        self.input_spec = [InputSpec(ndim=4)]
        self.target_shape = target_shape
        self.factor = factor
        if self.data_format == 'channels_first':
            self.target_size = (target_shape[2], target_shape[3])
        elif self.data_format == 'channels_last':
            self.target_size = (target_shape[1], target_shape[2])
        super(BilinearUpSampling2D, self).__init__(**kwargs) 
Example #11
Source File: layers.py    From delft with Apache License 2.0 6 votes vote down vote up
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)] 
Example #12
Source File: keras_bert_layer.py    From nlp_xiaojiang with MIT License 5 votes vote down vote up
def build(self, input_shape):
        self.input_spec = [InputSpec(ndim=3)]
        assert len(input_shape) == 3

        self.W = self.add_weight(shape=(input_shape[2], 1),
                                 name='{}_w'.format(self.name),
                                 initializer=self.init)
        self.trainable_weights = [self.W]
        super(AttentionWeightedAverage, self).build(input_shape) 
Example #13
Source File: layers.py    From research with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def build(self, input_shape):
    self.input_spec = [InputSpec(shape=input_shape)]
    shape = input_shape

    self.gamma = self.gamma_init(shape, name='{}/gamma'.format(self.name))
    self.beta = self.beta_init(shape, name='{}/beta'.format(self.name))
    self.trainable_weights = [self.gamma, self.beta]

    if self.initial_weights is not None:
        self.set_weights(self.initial_weights)
        del self.initial_weights
    self.built = True
    self.called_with = None 
Example #14
Source File: layers.py    From research with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def build(self, input_shape):
      self.input_spec = [InputSpec(shape=input_shape)]
      if self.stateful:
          self.reset_states()
      else:
          # initial states: all-zero tensor of shape (output_dim)
          self.states = [None]
      input_dim = input_shape[2]
      self.input_dim = input_dim

      self.V = self.init((self.output_dim, input_dim),
                         name='{}_V'.format(self.name))
      self.W = self.init((input_dim, self.output_dim),
                         name='{}_W'.format(self.name))
      self.U = self.inner_init((self.output_dim, self.output_dim),
                               name='{}_U'.format(self.name))
      self.b = K.zeros((self.output_dim,), name='{}_b'.format(self.name))
      self.ext_b = K.zeros((input_dim,), name='{}_ext_b'.format(self.name))

      self.regularizers = []
      if self.W_regularizer:
          self.W_regularizer.set_param(self.W)
          self.regularizers.append(self.W_regularizer)
      if self.U_regularizer:
          self.U_regularizer.set_param(self.U)
          self.regularizers.append(self.U_regularizer)
      if self.b_regularizer:
          self.b_regularizer.set_param(self.b)
          self.regularizers.append(self.b_regularizer)

      self.trainable_weights = [self.W, self.U, self.b, self.V, self.ext_b]

      if self.initial_weights is not None:
          self.set_weights(self.initial_weights)
          del self.initial_weights 
Example #15
Source File: layers.py    From research with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def build(self, input_shape):
      self.input_spec = [InputSpec(shape=input_shape)]
      if self.stateful:
          self.reset_states()
      else:
          # initial states: all-zero tensor of shape (output_dim)
          self.states = [None]
      input_dim = input_shape[2]
      self.input_dim = input_dim

      self.V = self.init((self.output_dim, input_dim-self.control_dim),
                         name='{}_V'.format(self.name))
      self.W = self.init((input_dim, self.output_dim),
                         name='{}_W'.format(self.name))
      self.U = self.inner_init((self.output_dim, self.output_dim),
                               name='{}_U'.format(self.name))
      self.b = K.zeros((self.output_dim,), name='{}_b'.format(self.name))
      self.ext_b = K.zeros((input_dim-self.control_dim,), name='{}_ext_b'.format(self.name))

      self.regularizers = []
      if self.W_regularizer:
          self.W_regularizer.set_param(self.W)
          self.regularizers.append(self.W_regularizer)
      if self.U_regularizer:
          self.U_regularizer.set_param(self.U)
          self.regularizers.append(self.U_regularizer)
      if self.b_regularizer:
          self.b_regularizer.set_param(self.b)
          self.regularizers.append(self.b_regularizer)

      self.trainable_weights = [self.W, self.U, self.b, self.V, self.ext_b]

      if self.initial_weights is not None:
          self.set_weights(self.initial_weights)
          del self.initial_weights 
Example #16
Source File: layers.py    From fast-neural-style-keras with Apache License 2.0 5 votes vote down vote up
def __init__(self, padding=(1, 1), dim_ordering='default', **kwargs):
        super(ReflectionPadding2D, self).__init__(**kwargs)

        if dim_ordering == 'default':
            dim_ordering = K.image_dim_ordering()

        self.padding = padding
        if isinstance(padding, dict):
            if set(padding.keys()) <= {'top_pad', 'bottom_pad', 'left_pad', 'right_pad'}:
                self.top_pad = padding.get('top_pad', 0)
                self.bottom_pad = padding.get('bottom_pad', 0)
                self.left_pad = padding.get('left_pad', 0)
                self.right_pad = padding.get('right_pad', 0)
            else:
                raise ValueError('Unexpected key found in `padding` dictionary. '
                                 'Keys have to be in {"top_pad", "bottom_pad", '
                                 '"left_pad", "right_pad"}.'
                                 'Found: ' + str(padding.keys()))
        else:
            padding = tuple(padding)
            if len(padding) == 2:
                self.top_pad = padding[0]
                self.bottom_pad = padding[0]
                self.left_pad = padding[1]
                self.right_pad = padding[1]
            elif len(padding) == 4:
                self.top_pad = padding[0]
                self.bottom_pad = padding[1]
                self.left_pad = padding[2]
                self.right_pad = padding[3]
            else:
                raise TypeError('`padding` should be tuple of int '
                                'of length 2 or 4, or dict. '
                                'Found: ' + str(padding))

        if dim_ordering not in {'tf'}:
            raise ValueError('dim_ordering must be in {tf}.')
        self.dim_ordering = dim_ordering
        self.input_spec = [InputSpec(ndim=4)] 
Example #17
Source File: resnet101.py    From FashionAI_KeyPoint_Detection_Challenge_Keras with MIT License 5 votes vote down vote up
def build(self, input_shape):
        self.input_spec = [InputSpec(shape=input_shape)]
        shape = (int(input_shape[self.axis]),)

        self.gamma = K.variable(
            self.gamma_init(shape),
            name='{}_gamma'.format(self.name))
        self.beta = K.variable(
            self.beta_init(shape),
            name='{}_beta'.format(self.name))
        self.trainable_weights = [self.gamma, self.beta]

        if self.initial_weights is not None:
            self.set_weights(self.initial_weights)
            del self.initial_weights 
Example #18
Source File: ChainCRF.py    From emnlp2017-bilstm-cnn-crf with Apache License 2.0 5 votes vote down vote up
def build(self, input_shape):
        assert len(input_shape) == 3
        n_classes = input_shape[2]
        n_steps = input_shape[1]
        assert n_steps is None or n_steps >= 2
        self.input_spec = [InputSpec(dtype=K.floatx(),
                                     shape=(None, n_steps, n_classes))]

        self.U = self.add_weight((n_classes, n_classes),
                                 initializer=self.init,
                                 name='U',
                                 regularizer=self.U_regularizer,
                                 constraint=self.U_constraint)

        self.b_start = self.add_weight((n_classes, ),
                                       initializer='zero',
                                       name='b_start',
                                       regularizer=self.b_start_regularizer,
                                       constraint=self.b_start_constraint)

        self.b_end = self.add_weight((n_classes, ),
                                     initializer='zero',
                                     name='b_end',
                                     regularizer=self.b_end_regularizer,
                                     constraint=self.b_end_constraint)

        if self.initial_weights is not None:
            self.set_weights(self.initial_weights)
            del self.initial_weights

        self.built = True 
Example #19
Source File: keras_bert_layer.py    From nlp_xiaojiang with MIT License 5 votes vote down vote up
def build(self, input_shape):
        self.input_spec = [InputSpec(ndim=3)]
        assert len(input_shape) == 3

        self.W = self.add_weight(shape=(input_shape[2], 1),
                                 name='{}_w'.format(self.name),
                                 initializer=self.init)
        self.trainable_weights = [self.W]
        super(AttentionWeightedAverage, self).build(input_shape) 
Example #20
Source File: resnet152.py    From landmark-recognition-challenge with GNU General Public License v3.0 5 votes vote down vote up
def build(self, input_shape):
        self.input_spec = [InputSpec(shape=input_shape)]
        shape = (int(input_shape[self.axis]),)

        self.gamma = K.variable(self.gamma_init(shape), name='%s_gamma'%self.name)
        self.beta = K.variable(self.beta_init(shape), name='%s_beta'%self.name)
        self.trainable_weights = [self.gamma, self.beta]

        if self.initial_weights is not None:
            self.set_weights(self.initial_weights)
            del self.initial_weights 
Example #21
Source File: layers.py    From research with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, nb_filter, nb_row, nb_col,
                 init='glorot_uniform', activation='linear', weights=None,
                 border_mode='valid', subsample=(1, 1), dim_ordering='tf',
                 W_regularizer=None, b_regularizer=None, activity_regularizer=None,
                 W_constraint=None, b_constraint=None, **kwargs):

        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.input_spec = [InputSpec(ndim=4)]
        self.initial_weights = weights
        super(Deconv2D, self).__init__(**kwargs) 
Example #22
Source File: FixedBatchNormalization.py    From keras-faster-rcnn with Apache License 2.0 5 votes vote down vote up
def build(self, input_shape):
        self.input_spec = [InputSpec(shape=input_shape)]
        shape = (input_shape[self.axis],)

        self.gamma = self.add_weight(shape,
                                     initializer=self.gamma_init,
                                     regularizer=self.gamma_regularizer,
                                     name='{}_gamma'.format(self.name),
                                     trainable=False)
        self.beta = self.add_weight(shape,
                                    initializer=self.beta_init,
                                    regularizer=self.beta_regularizer,
                                    name='{}_beta'.format(self.name),
                                    trainable=False)
        self.running_mean = self.add_weight(shape, initializer='zero',
                                            name='{}_running_mean'.format(self.name),
                                            trainable=False)
        self.running_std = self.add_weight(shape, initializer='one',
                                           name='{}_running_std'.format(self.name),
                                           trainable=False)

        if self.initial_weights is not None:
            self.set_weights(self.initial_weights)
            del self.initial_weights

        self.built = True 
Example #23
Source File: layers.py    From keras-gp with MIT License 5 votes vote down vote up
def build(self, input_shape):
        """Create the internal variables for communication with GP backend.

        Arguments:
        ----------
            input_shape: Keras tensor (future input to layer)
                or list/tuple of Keras tensors to reference
                for weight shape computations.
        """
        assert len(input_shape) == 2
        input_dim = input_shape[-1]
        self.input_spec = InputSpec(dtype=K.floatx(), shape=(None, input_dim))

        # Configure GP backend
        self.backend.configure(input_dim, self.hyp, **self.backend_config)

        # Internal shared variables
        self._dlik_dh = K.zeros((self.nb_train_samples, input_dim))
        self._batch_ids = K.variable(np.zeros(self.batch_size), dtype='int32')
        self._batch_sz = K.variable(self.batch_size, dtype='int32')

        # Internal metrics
        self._nlml = K.variable(0.)
        self._mse = K.variable(0.)

        self.built = True 
Example #24
Source File: bayesian_dense.py    From bayesian_dense with MIT License 5 votes vote down vote up
def build(self, input_shape):
        assert len(input_shape) == 2
        self.input_dim = input_shape[1]
        input_dim=self.input_dim
        self.input_spec = [InputSpec(dtype=K.floatx(),
                                     shape=(None, input_dim))]

        self.W_mu = self.init((input_dim, self.output_dim),
                           name='{}_W_mu'.format(self.name))
        self.W_log_sigma = self.init_sigma((input_dim, self.output_dim),
                           name='{}_W_log_sigma'.format(self.name))
        if self.bias:
            self.b_mu = self.init((self.output_dim,),
                             name='{}_b_mu'.format(self.name))
            self.b_log_sigma = self.init_sigma((self.output_dim,),
                             name='{}_b_log_sigma'.format(self.name))
            self.trainable_weights = [self.W_mu, self.W_log_sigma, self.b_mu, self.b_log_sigma]
        else:
            self.trainable_weights = [self.W_mu, self.W_log_sigma]

        self.regularizers = []
        if self.W_regularizer:
            self.W_regularizer.set_param(self.W_mu)
            self.regularizers.append(self.W_regularizer)
        if self.b_regularizer:
            self.b_regularizer.set_param(self.b_mu)
            self.regularizers.append(self.b_regularizer)
        if self.W_sigma_regularizer:
            self.W_sigma_regularizer.set_param(self.W_log_sigma)
            self.regularizers.append(self.W_sigma_regularizer)
        if self.b_sigma_regularizer:
            self.b_sigma_regularizer.set_param(self.b_log_sigma)
            self.regularizers.append(self.b_sigma_regularizer)
        if self.activity_regularizer:
            self.activity_regularizer.set_layer(self)
            self.regularizers.append(self.activity_regularizer)
        if self.initial_weights is not None:
            self.set_weights(self.initial_weights)
            del self.initial_weights 
Example #25
Source File: bayesian_dense.py    From bayesian_dense with MIT License 5 votes vote down vote up
def __init__(self, output_dim, init='glorot_uniform',
                 init_sigma=lambda shape, name:init_uniform(shape, -10, -5, name=name),
                 activation='linear', weights=None,
                 W_regularizer=None, b_regularizer=None, W_sigma_regularizer=None, b_sigma_regularizer=None,
                 activity_regularizer=None,
                 bias=True, input_dim=None, **kwargs):
        self.init = initializations.get(init)
        self.init_sigma = init_sigma
        self.activation = activations.get(activation)
        self.output_dim = output_dim
        self.input_dim = input_dim
        self.uses_learning_phase = True

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

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

        if self.input_dim:
            kwargs['input_shape'] = (self.input_dim,)
        super(BayesianDense, self).__init__(**kwargs) 
Example #26
Source File: models.py    From pOSAL with MIT License 5 votes vote down vote up
def __init__(self, upsampling=(2, 2), output_size=None, data_format=None, **kwargs):

        super(BilinearUpsampling, self).__init__(**kwargs)

        self.data_format = conv_utils.normalize_data_format(data_format)
        self.input_spec = InputSpec(ndim=4)
        if output_size:
            self.output_size = conv_utils.normalize_tuple(
                output_size, 2, 'output_size')
            self.upsampling = None
        else:
            self.output_size = None
            self.upsampling = conv_utils.normalize_tuple(
                upsampling, 2, 'upsampling') 
Example #27
Source File: resnet152.py    From keras-transfer-learning-for-oxford102 with MIT License 5 votes vote down vote up
def build(self, input_shape):
        self.input_spec = [InputSpec(shape=input_shape)]
        shape = (int(input_shape[self.axis]),)

        self.gamma = self.gamma_init(shape, name='{}_gamma'.format(self.name))
        self.beta = self.beta_init(shape, name='{}_beta'.format(self.name))
        self.trainable_weights = [self.gamma, self.beta]

        if self.initial_weights is not None:
            self.set_weights(self.initial_weights)
            del self.initial_weights 
Example #28
Source File: FixedBatchNormalization.py    From Keras_object_detection with Apache License 2.0 5 votes vote down vote up
def build(self, input_shape):
        self.input_spec = [InputSpec(shape=input_shape)]
        shape = (input_shape[self.axis],)

        self.gamma = self.add_weight(shape,
                                     initializer=self.gamma_init,
                                     regularizer=self.gamma_regularizer,
                                     name='{}_gamma'.format(self.name),
                                     trainable=False)
        self.beta = self.add_weight(shape,
                                    initializer=self.beta_init,
                                    regularizer=self.beta_regularizer,
                                    name='{}_beta'.format(self.name),
                                    trainable=False)
        self.running_mean = self.add_weight(shape, initializer='zero',
                                            name='{}_running_mean'.format(self.name),
                                            trainable=False)
        self.running_std = self.add_weight(shape, initializer='one',
                                           name='{}_running_std'.format(self.name),
                                           trainable=False)

        if self.initial_weights is not None:
            self.set_weights(self.initial_weights)
            del self.initial_weights

        self.built = True 
Example #29
Source File: ChainCRF.py    From naacl18-multitask_argument_mining with Apache License 2.0 5 votes vote down vote up
def build(self, input_shape):
        assert len(input_shape) == 3
        n_classes = input_shape[2]
        n_steps = input_shape[1]
        assert n_classes >= 2
        assert n_steps is None or n_steps >= 2
        self.input_spec = [InputSpec(dtype=K.floatx(),
                                     shape=(None, n_steps, n_classes))]

        self.U = self.add_weight((n_classes, n_classes),
                                 initializer=self.init,
                                 name='{}_U'.format(self.name),
                                 regularizer=self.U_regularizer,
                                 constraint=self.U_constraint)

        self.b_start = self.add_weight((n_classes, ),
                                       initializer='zero',
                                       name='{}_b_start'.format(self.name),
                                       regularizer=self.b_start_regularizer,
                                       constraint=self.b_start_constraint)

        self.b_end = self.add_weight((n_classes, ),
                                     initializer='zero',
                                     name='{}_b_end'.format(self.name),
                                     regularizer=self.b_end_regularizer,
                                     constraint=self.b_end_constraint)

        if self.initial_weights is not None:
            self.set_weights(self.initial_weights)
            del self.initial_weights

        self.built = True 
Example #30
Source File: mobilenets-checkpoint.py    From CBAM-keras with MIT License 5 votes vote down vote up
def build(self, input_shape):
        if len(input_shape) < 4:
            raise ValueError('Inputs to `DepthwiseConv2D` should have rank 4. '
                             'Received input shape:', str(input_shape))
        if self.data_format == 'channels_first':
            channel_axis = 1
        else:
            channel_axis = 3
        if input_shape[channel_axis] is None:
            raise ValueError('The channel dimension of the inputs to '
                             '`DepthwiseConv2D` '
                             'should be defined. Found `None`.')
        input_dim = int(input_shape[channel_axis])
        depthwise_kernel_shape = (self.kernel_size[0],
                                  self.kernel_size[1],
                                  input_dim,
                                  self.depth_multiplier)

        self.depthwise_kernel = self.add_weight(
            shape=depthwise_kernel_shape,
            initializer=self.depthwise_initializer,
            name='depthwise_kernel',
            regularizer=self.depthwise_regularizer,
            constraint=self.depthwise_constraint)

        if self.use_bias:
            self.bias = self.add_weight(shape=(input_dim * self.depth_multiplier,),
                                        initializer=self.bias_initializer,
                                        name='bias',
                                        regularizer=self.bias_regularizer,
                                        constraint=self.bias_constraint)
        else:
            self.bias = None
        # Set input spec.
        self.input_spec = InputSpec(ndim=4, axes={channel_axis: input_dim})
        self.built = True