本文整理汇总了Python中keras.layers.InputSpec方法的典型用法代码示例。如果您正苦于以下问题:Python layers.InputSpec方法的具体用法?Python layers.InputSpec怎么用?Python layers.InputSpec使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.layers
的用法示例。
在下文中一共展示了layers.InputSpec方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import InputSpec [as 别名]
def build(self, input_shape):
self._validate_input_shape(input_shape)
self.input_spec = InputSpec(shape=input_shape)
if not self.layer.built:
self.layer.build(input_shape)
self.layer.built = True
input_dim = input_shape[-1]
if self.layer.return_sequences:
output_dim = self.layer.compute_output_shape(input_shape)[0][-1]
else:
output_dim = self.layer.compute_output_shape(input_shape)[-1]
self._W1 = self.add_weight(shape=(input_dim, input_dim), name="{}_W1".format(self.name), initializer=self.weight_initializer)
self._W2 = self.add_weight(shape=(output_dim, input_dim), name="{}_W2".format(self.name), initializer=self.weight_initializer)
self._W3 = self.add_weight(shape=(2*input_dim, input_dim), name="{}_W3".format(self.name), initializer=self.weight_initializer)
self._b2 = self.add_weight(shape=(input_dim,), name="{}_b2".format(self.name), initializer=self.weight_initializer)
self._b3 = self.add_weight(shape=(input_dim,), name="{}_b3".format(self.name), initializer=self.weight_initializer)
self._V = self.add_weight(shape=(input_dim,1), name="{}_V".format(self.name), initializer=self.weight_initializer)
super(AttentionRNNWrapper, self).build()
示例2: __init__
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import InputSpec [as 别名]
def __init__(self, nb_filters_in, nb_filters_out, nb_filters_att, nb_rows, nb_cols,
init='normal', inner_init='orthogonal', attentive_init='zero',
activation='tanh', inner_activation='sigmoid',
W_regularizer=None, U_regularizer=None,
weights=None, go_backwards=False,
**kwargs):
self.nb_filters_in = nb_filters_in
self.nb_filters_out = nb_filters_out
self.nb_filters_att = nb_filters_att
self.nb_rows = nb_rows
self.nb_cols = nb_cols
self.init = initializations.get(init)
self.inner_init = initializations.get(inner_init)
self.attentive_init = initializations.get(attentive_init)
self.activation = activations.get(activation)
self.inner_activation = activations.get(inner_activation)
self.initial_weights = weights
self.go_backwards = go_backwards
self.W_regularizer = W_regularizer
self.U_regularizer = U_regularizer
self.input_spec = [InputSpec(ndim=5)]
super(AttentiveConvLSTM, self).__init__(**kwargs)
示例3: __init__
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import InputSpec [as 别名]
def __init__(self, output_dim, init='glorot_uniform', activation='relu',weights=None,
W_regularizer=None, b_regularizer=None, activity_regularizer=None,
W_constraint=None, b_constraint=None, input_dim=None, **kwargs):
self.W_initializer = initializers.get(init)
self.b_initializer = initializers.get('zeros')
self.activation = activations.get(activation)
self.output_dim = output_dim
self.input_dim = input_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.initial_weights = weights
self.input_spec = InputSpec(ndim=2)
if self.input_dim:
kwargs['input_shape'] = (self.input_dim,)
super(SparseFullyConnectedLayer, self).__init__(**kwargs)
示例4: build
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import InputSpec [as 别名]
def build(self, input_shape):
assert len(input_shape) == 2
input_dim = input_shape[1]
#self.input_spec = InputSpec(dtype=K.floatx(), shape=(None, input_dim))
self.input_spec = InputSpec(ndim=2, axes={1: input_dim})
self.W = self.add_weight(
shape=(input_dim, self.output_dim),
initializer=self.W_initializer,
name='SparseFullyConnected_W',
regularizer=self.W_regularizer,
constraint=self.W_constraint)
self.b = self.add_weight(
shape=(self.output_dim,),
initializer=self.b_initializer,
name='SparseFullyConnected_b',
regularizer=self.b_regularizer,
constraint=self.b_constraint)
if self.initial_weights is not None:
self.set_weights(self.initial_weights)
del self.initial_weights
#self.built = True
#super(SparseFullyConnectedLayer, self).build(input_shape)
示例5: build
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import InputSpec [as 别名]
def build(self, input_shape):
input_dim = input_shape[2]
self.input_dim = input_dim
self.input_spec = [InputSpec(shape=input_shape)]
self.kernel_shape = (self.window_size, 1, input_dim, self.output_dim * 2)
self.kernel = self.add_weight(self.kernel_shape,
initializer=self.kernel_initializer,
name='kernel',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
if self.use_bias:
self.bias = self.add_weight((self.output_dim * 2,),
initializer=self.bias_initializer,
name='b',
regularizer=self.bias_regularizer,
constraint=self.bias_constraint)
self.built = True
示例6: __init__
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import InputSpec [as 别名]
def __init__(self, ch_j, n_j,
r_num=1,
b_alphas=[8, 8, 8],
kernel_initializer='glorot_uniform',
kernel_regularizer=None,
activity_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(DenseCaps, self).__init__(**kwargs)
self.ch_j = ch_j # number of capsules in layer J
self.n_j = n_j # number of neurons in a capsule in J
self.r_num = r_num
self.b_alphas = b_alphas
self.kernel_initializer = initializers.get(kernel_initializer)
self.kernel_regularizer = regularizers.get(kernel_regularizer)
self.activity_regularizer = regularizers.get(activity_regularizer)
self.kernel_constraint = constraints.get(kernel_constraint)
self.input_spec = InputSpec(min_ndim=3)
self.supports_masking = True
示例7: call
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import InputSpec [as 别名]
def call(self, x, mask=None, **kwargs):
input_shape = K.int_shape(x)
res = super(ShareableGRU, self).call(x, mask, **kwargs)
self.input_spec = [InputSpec(shape=(self.input_spec[0].shape[0],
None,
self.input_spec[0].shape[2]))]
if K.ndim(x) == K.ndim(res):
# A recent change in Keras
# (https://github.com/fchollet/keras/commit/a9b6bef0624c67d6df1618ca63d8e8141b0df4d0)
# made it so that K.rnn with a tensorflow backend does not retain shape information for
# the sequence length, even if it's present in the input. We need to fix that here so
# that our models have the right shape information. A simple K.reshape is good enough
# to fix this.
result_shape = K.int_shape(res)
if input_shape[1] is not None and result_shape[1] is None:
shape = (input_shape[0] if input_shape[0] is not None else -1,
input_shape[1], result_shape[2])
res = K.reshape(res, shape=shape)
return res
示例8: build
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import InputSpec [as 别名]
def build(self, input_shape):
if isinstance(input_shape, tuple):
input_shape = [input_shape]
assert all(len(shape) >= 3 for shape in input_shape), "Need 3 dims to TimeDistribute"
all_timesteps = [i[1] for i in input_shape]
assert len(set(all_timesteps)) == 1, "Tensors must have same number of timesteps"
self.input_spec = [InputSpec(shape=shape) for shape in input_shape]
if not self.layer.built:
child_input_shape = [(shape[0],) + shape[2:] for shape in input_shape]
if len(input_shape) == 1:
child_input_shape = child_input_shape[0]
self.layer.build(child_input_shape)
self.layer.built = True
self.built = True
# It's important that we call Wrapper.build() here, because it sets some important member
# variables. But we can't call KerasTimeDistributed.build(), because it assumes only one
# input, which we're trying to fix. So we use super(KerasTimeDistributed, self).build()
# here on purpose - this is not a copy-paste bug.
super(KerasTimeDistributed, self).build(input_shape) # pylint: disable=bad-super-call
示例9: __init__
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import InputSpec [as 别名]
def __init__(self, layer, weight_initializer="glorot_uniform", return_attention=False, **kwargs):
assert isinstance(layer, RNN)
self.layer = layer
self.supports_masking = True
self.weight_initializer = weight_initializer
self.return_attention = return_attention
self._num_constants = None
super(ExternalAttentionRNNWrapper, self).__init__(layer, **kwargs)
self.input_spec = [InputSpec(ndim=3), InputSpec(ndim=3)]
示例10: __init__
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import InputSpec [as 别名]
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
示例11: build
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import InputSpec [as 别名]
def build(self, input_shape):
self.input_spec = InputSpec(ndim=len(input_shape),
axes={self.axis: input_shape[self.axis]})
shape = (input_shape[self.axis],)
self.gamma = self.add_weight(shape,
initializer=self.gamma_init,
regularizer=self.gamma_regularizer,
name='{}_gamma'.format(self.name))
self.beta = self.add_weight(shape,
initializer=self.beta_init,
regularizer=self.beta_regularizer,
name='{}_beta'.format(self.name))
self.built = True
示例12: build
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import InputSpec [as 别名]
def build(self, input_shape):
kernel_shape_f_g = (1, 1) + (self.channels, self.filters_f_g)
kernel_shape_h = (1, 1) + (self.channels, self.filters_h)
# Create a trainable weight variable for this layer:
self.gamma = self.add_weight(name='gamma', shape=[1], initializer='zeros', trainable=True)
self.kernel_f = self.add_weight(shape=kernel_shape_f_g,
initializer='glorot_uniform',
name='kernel_f')
self.kernel_g = self.add_weight(shape=kernel_shape_f_g,
initializer='glorot_uniform',
name='kernel_g')
self.kernel_h = self.add_weight(shape=kernel_shape_h,
initializer='glorot_uniform',
name='kernel_h')
self.bias_f = self.add_weight(shape=(self.filters_f_g,),
initializer='zeros',
name='bias_F')
self.bias_g = self.add_weight(shape=(self.filters_f_g,),
initializer='zeros',
name='bias_g')
self.bias_h = self.add_weight(shape=(self.filters_h,),
initializer='zeros',
name='bias_h')
super(Attention, self).build(input_shape)
# Set input spec.
self.input_spec = InputSpec(ndim=4,
axes={3: input_shape[-1]})
self.built = True
示例13: build
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import InputSpec [as 别名]
def build(self, input_shape):
# This currently only works for 4D inputs: assuming (B, H, W, C)
self.input_spec = [InputSpec(shape=input_shape)]
shape = (1, 1, 1, input_shape[-1])
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]
self.built = True
示例14: __init__
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import InputSpec [as 别名]
def __init__(self, units,
activation=None,
use_bias=True,
init_criterion='he',
kernel_initializer='quaternion',
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(QuaternionDense, self).__init__(**kwargs)
self.units = units
self.q_units = units // 4
self.activation = activations.get(activation)
self.use_bias = use_bias
self.init_criterion = init_criterion
self.kernel_initializer = 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
开发者ID:Orkis-Research,项目名称:Quaternion-Convolutional-Neural-Networks-for-End-to-End-Automatic-Speech-Recognition,代码行数:36,代码来源:dense.py
示例15: build
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import InputSpec [as 别名]
def build(self, input_shape):
assert len(input_shape) == 2
assert input_shape[-1] % 2 == 0
input_dim = input_shape[-1] // 4
data_format = K.image_data_format()
kernel_shape = (input_dim, self.units)
init_shape = (input_dim, self.q_units)
self.kernel_init = qdense_init(init_shape, self.init_criterion)
self.kernel = self.add_weight(
shape=kernel_shape,
initializer=self.kernel_init,
name='r',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint
)
if self.use_bias:
self.bias = self.add_weight(
shape=(self.units,),
initializer='zeros',
name='bias',
regularizer=self.bias_regularizer,
constraint=self.bias_constraint
)
else:
self.bias = None
self.input_spec = InputSpec(ndim=2, axes={-1: 4 * input_dim})
self.built = True
开发者ID:Orkis-Research,项目名称:Quaternion-Convolutional-Neural-Networks-for-End-to-End-Automatic-Speech-Recognition,代码行数:34,代码来源:dense.py