本文整理匯總了Python中keras.engine.Layer方法的典型用法代碼示例。如果您正苦於以下問題:Python engine.Layer方法的具體用法?Python engine.Layer怎麽用?Python engine.Layer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類keras.engine
的用法示例。
在下文中一共展示了engine.Layer方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: reset_states
# 需要導入模塊: from keras import engine [as 別名]
# 或者: from keras.engine import Layer [as 別名]
def reset_states(self):
assert self.stateful, 'Layer must be stateful.'
input_shape = self.input_spec[0].shape
if not input_shape[0]:
raise Exception('If a RNN is stateful, a complete ' +
'input_shape must be provided (including batch size).')
if hasattr(self, 'states'):
K.set_value(self.states[0],
np.zeros((input_shape[0], self.hidden_recurrent_dim)))
K.set_value(self.states[1],
np.zeros((input_shape[0], self.input_dim)))
K.set_value(self.states[2],
np.zeros((input_shape[0], self.hidden_dim)))
else:
self.states = [K.zeros((input_shape[0], self.hidden_recurrent_dim)),
K.zeros((input_shape[0], self.input_dim)),
K.zeros((input_shape[0], self.hidden_dim))]
示例2: reset_states
# 需要導入模塊: from keras import engine [as 別名]
# 或者: from keras.engine import Layer [as 別名]
def reset_states(self):
assert self.stateful, 'Layer must be stateful.'
input_shape = self.input_spec[0].shape
if not input_shape[0]:
raise ValueError('If a RNN is stateful, it needs to know '
'its batch size. Specify the batch size '
'of your input tensors: \n'
'- If using a Sequential model, '
'specify the batch size by passing '
'a `batch_input_shape` '
'argument to your first layer.\n'
'- If using the functional API, specify '
'the time dimension by passing a '
'`batch_shape` argument to your Input layer.')
if hasattr(self, 'states'):
K.set_value(self.states[0],
np.zeros((input_shape[0], self.input_dim)))
K.set_value(self.states[1],
np.zeros((input_shape[0], self.output_dim)))
else:
self.states = [K.zeros((input_shape[0], self.input_dim)),
K.zeros((input_shape[0], self.output_dim))]
示例3: emit_Slice
# 需要導入模塊: from keras import engine [as 別名]
# 或者: from keras.engine import Layer [as 別名]
def emit_Slice(self, IR_node, in_scope=False):
# It arouses some problems:
# it can be implemented by Lambda Layer
# https://github.com/keras-team/keras/issues/890
self.used_layers.add(IR_node.type)
extra_str = ""
if IR_node.get_attr('strides'):
extra_str += "strides={}".format(IR_node.get_attr('strides'))
if IR_node.get_attr('begin_mask'):
extra_str += ", begin_mask={}".format(IR_node.get_attr('begin_mask'))
if IR_node.get_attr('end_mask'):
extra_str += ", end_mask={}".format(IR_node.get_attr('end_mask'))
if IR_node.get_attr('shrink_axis_mask'):
extra_str += ", shrink_axis_mask={}".format(IR_node.get_attr('shrink_axis_mask'))
code = "{:<15} = __slice({}, {}, {}, {})".format(
IR_node.variable_name,
self.parent_variable_name(IR_node),
IR_node.get_attr('starts'),
IR_node.get_attr('ends'),
extra_str)
return code
示例4: _emit_h_zero
# 需要導入模塊: from keras import engine [as 別名]
# 或者: from keras.engine import Layer [as 別名]
def _emit_h_zero(self, IR_node):
if not self.layers_codes.get(IR_node.pattern, None):
class_code = '''
class my_h_zero(keras.layers.Layer):
def __init__(self, **kwargs):
super(my_h_zero, self).__init__(**kwargs)
def call(self, dummy):
{:<15} = K.constant(np.full((1, {}), {}))
return {}
'''.format(IR_node.variable_name,
IR_node.get_attr('fill_size'),
IR_node.get_attr('fill_value'),
IR_node.variable_name)
self.layers_codes[IR_node.pattern] = class_code
code = "{:<15} = my_h_zero()({})".format(IR_node.variable_name, self.parent_variable_name(IR_node))
return code
示例5: _layer_Affine
# 需要導入模塊: from keras import engine [as 別名]
# 或者: from keras.engine import Layer [as 別名]
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
''')
示例6: _layer_Shape
# 需要導入模塊: from keras import engine [as 別名]
# 或者: from keras.engine import Layer [as 別名]
def _layer_Shape(self):
self.add_body(0, '''
def __shape(input):
return Lambda(lambda x: tf.shape(x))(input)
''')
# def _layer_Constant(self):
# self.add_body(0, '''
# class my_constant(keras.layers.Layer):
# def __init__(self, value, **kwargs):
# super(my_constant, self).__init__(**kwargs)
# self._value = value
# # the input is dummy, just for creating keras graph.
# def call(self, dummy):
# res = K.constant(self._value)
# self.output_shapes = K.int_shape(res)
# return res
# def compute_output_shape(self, input_shape):
# return self.output_shapes
# ''')
示例7: build
# 需要導入模塊: from keras import engine [as 別名]
# 或者: from keras.engine import Layer [as 別名]
def build(self, input_shape: list):
"""
Build the layer.
:param input_shape: the shapes of the input tensors,
for MatchingLayer we need tow input tensors.
"""
# Used purely for shape validation.
if not isinstance(input_shape, list) or len(input_shape) != 2:
raise ValueError('A `MatchingLayer` layer should be called '
'on a list of 2 inputs.')
self._shape1 = input_shape[0]
self._shape2 = input_shape[1]
for idx in 0, 2:
if self._shape1[idx] != self._shape2[idx]:
raise ValueError(
'Incompatible dimensions: '
f'{self._shape1[idx]} != {self._shape2[idx]}.'
f'Layer shapes: {self._shape1}, {self._shape2}.'
)
示例8: build
# 需要導入模塊: from keras import engine [as 別名]
# 或者: from keras.engine import Layer [as 別名]
def build(self, input_shape):
# Used purely for shape validation.
if not isinstance(input_shape, list) or len(input_shape) != 2:
raise ValueError('A `Match` layer should be called '
'on a list of 2 inputs.')
self.shape1 = input_shape[0]
self.shape2 = input_shape[1]
if self.shape1[0] != self.shape2[0]:
raise ValueError(
'Dimension incompatibility '
'%s != %s. ' % (self.shape1[0], self.shape2[0]) +
'Layer shapes: %s, %s' % (self.shape1, self.shape2))
if self.shape1[2] != self.shape2[2]:
raise ValueError(
'Dimension incompatibility '
'%s != %s. ' % (self.shape1[2], self.shape2[2]) +
'Layer shapes: %s, %s' % (self.shape1, self.shape2))
示例9: build
# 需要導入模塊: from keras import engine [as 別名]
# 或者: from keras.engine import Layer [as 別名]
def build(self,input_shape):
# Used purely for shape validation.
if not isinstance(input_shape, list) or len(input_shape) != 2:
raise ValueError('A `Match` layer should be called '
'on a list of 2 inputs.')
self.shape1 = input_shape[0]
self.shape2 = input_shape[1]
if self.shape1[0] != self.shape2[0]:
raise ValueError(
'Dimension incompatibility '
'%s != %s. ' % (self.shape1[0], self.shape2[0]) +
'Layer shapes: %s, %s' % (self.shape1, self.shape2)) # batch_size should equal
if self.shape1[2] != self.shape2[2]:
raise ValueError(
'Dimension incompatibility '
'%s != %s. ' % (self.shape1[2], self.shape2[2]) + # embeding_size should equal
'Layer shapes: %s, %s' % (self.shape1, self.shape2))
self.A = self.add_weight( name='A',
shape=(self.shape1[2], self.shape2[2]),
dtype=K.tf.float32,
initializer=K.tf.contrib.layers.xavier_initializer(),
trainable=True )
super(MatchBilinear, self).build(input_shape) # Be sure to call this somewhere!
示例10: call
# 需要導入模塊: from keras import engine [as 別名]
# 或者: from keras.engine import Layer [as 別名]
def call(self, x, mask=None):
assert self.built, 'Layer must be built before being called'
input_shape = K.int_shape(x)
reduction_axes = list(range(len(input_shape)))
del reduction_axes[self.axis]
broadcast_shape = [1] * len(input_shape)
broadcast_shape[self.axis] = input_shape[self.axis]
if sorted(reduction_axes) == range(K.ndim(x))[:-1]:
x_normed = K.batch_normalization(
x, self.running_mean, self.running_std,
self.beta, self.gamma,
epsilon=self.epsilon)
else:
# need broadcasting
broadcast_running_mean = K.reshape(self.running_mean, broadcast_shape)
broadcast_running_std = K.reshape(self.running_std, broadcast_shape)
broadcast_beta = K.reshape(self.beta, broadcast_shape)
broadcast_gamma = K.reshape(self.gamma, broadcast_shape)
x_normed = K.batch_normalization(
x, broadcast_running_mean, broadcast_running_std,
broadcast_beta, broadcast_gamma,
epsilon=self.epsilon)
return x_normed
示例11: resnet_graph
# 需要導入模塊: from keras import engine [as 別名]
# 或者: from keras.engine import Layer [as 別名]
def resnet_graph(input_image, architecture, stage5=False, train_bn=True):
"""Build a ResNet graph.
architecture: Can be resnet50 or resnet101
stage5: Boolean. If False, stage5 of the network is not created
train_bn: Boolean. Train or freeze Batch Norm layers
"""
assert architecture in ["resnet50", "resnet101"]
# Stage 1
x = KL.ZeroPadding2D((3, 3))(input_image)
x = KL.Conv2D(64, (7, 7), strides=(2, 2), name='conv1', use_bias=True)(x)
x = BatchNorm(name='bn_conv1')(x, training=train_bn)
x = KL.Activation('relu')(x)
C1 = x = KL.MaxPooling2D((3, 3), strides=(2, 2), padding="same")(x)
# Stage 2
x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1), train_bn=train_bn)
x = identity_block(x, 3, [64, 64, 256], stage=2, block='b', train_bn=train_bn)
C2 = x = identity_block(x, 3, [64, 64, 256], stage=2, block='c', train_bn=train_bn)
# Stage 3
x = conv_block(x, 3, [128, 128, 512], stage=3, block='a', train_bn=train_bn)
x = identity_block(x, 3, [128, 128, 512], stage=3, block='b', train_bn=train_bn)
x = identity_block(x, 3, [128, 128, 512], stage=3, block='c', train_bn=train_bn)
C3 = x = identity_block(x, 3, [128, 128, 512], stage=3, block='d', train_bn=train_bn)
# Stage 4
x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a', train_bn=train_bn)
block_count = {"resnet50": 5, "resnet101": 22}[architecture]
for i in range(block_count):
x = identity_block(x, 3, [256, 256, 1024], stage=4, block=chr(98 + i), train_bn=train_bn)
C4 = x
# Stage 5
if stage5:
x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a', train_bn=train_bn)
x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b', train_bn=train_bn)
C5 = x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c', train_bn=train_bn)
else:
C5 = None
return [C1, C2, C3, C4, C5]
############################################################
# Proposal Layer
############################################################
示例12: compute_output_shape
# 需要導入模塊: from keras import engine [as 別名]
# 或者: from keras.engine import Layer [as 別名]
def compute_output_shape(self, input_shape):
return (None, self.proposal_count, 4)
############################################################
# ROIAlign Layer
############################################################
示例13: compute_mask
# 需要導入模塊: from keras import engine [as 別名]
# 或者: from keras.engine import Layer [as 別名]
def compute_mask(self, inputs, mask=None):
return [None, None, None, None]
############################################################
# Detection Layer
############################################################
示例14: resnet_graph
# 需要導入模塊: from keras import engine [as 別名]
# 或者: from keras.engine import Layer [as 別名]
def resnet_graph(input_image, architecture, stage5=False):
assert architecture in ["resnet50", "resnet101"]
# Stage 1
x = KL.ZeroPadding2D((3, 3))(input_image)
x = KL.Conv2D(64, (7, 7), strides=(2, 2), name='conv1', use_bias=True)(x)
x = BatchNorm(axis=3, name='bn_conv1')(x)
x = KL.Activation('relu')(x)
C1 = x = KL.MaxPooling2D((3, 3), strides=(2, 2), padding="same")(x)
# Stage 2
x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
C2 = x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')
# Stage 3
x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
C3 = x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')
# Stage 4
x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
block_count = {"resnet50": 5, "resnet101": 22}[architecture]
for i in range(block_count):
x = identity_block(x, 3, [256, 256, 1024], stage=4, block=chr(98 + i))
C4 = x
# Stage 5
if stage5:
x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
C5 = x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')
else:
C5 = None
return [C1, C2, C3, C4, C5]
############################################################
# Proposal Layer
############################################################
示例15: reset_states
# 需要導入模塊: from keras import engine [as 別名]
# 或者: from keras.engine import Layer [as 別名]
def reset_states(self):
assert self.stateful, 'Layer must be stateful.'
input_shape = self.input_spec[0].shape
if not input_shape[0]:
raise Exception('If a RNN is stateful, a complete ' +
'input_shape must be provided (including batch size).')
if hasattr(self, 'states'):
K.set_value(self.states[0],
np.zeros((input_shape[0], self.output_dim)))
K.set_value(self.states[1],
np.zeros((input_shape[0], self.output_dim)))
else:
self.states = [K.zeros((input_shape[0], self.output_dim)),
K.zeros((input_shape[0], self.output_dim))]