本文整理汇总了Python中keras.initializers.Zeros方法的典型用法代码示例。如果您正苦于以下问题:Python initializers.Zeros方法的具体用法?Python initializers.Zeros怎么用?Python initializers.Zeros使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.initializers
的用法示例。
在下文中一共展示了initializers.Zeros方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build
# 需要导入模块: from keras import initializers [as 别名]
# 或者: from keras.initializers import Zeros [as 别名]
def build(self, input_shape):
self._g = self.add_weight(
name='gain',
shape=(input_shape[-1],),
initializer=Ones(),
trainable=True
)
self._b = self.add_weight(
name='bias',
shape=(input_shape[-1],),
initializer=Zeros(),
trainable=True
)
示例2: build
# 需要导入模块: from keras import initializers [as 别名]
# 或者: from keras.initializers import Zeros [as 别名]
def build(self, input_shape):
self.gamma = self.add_weight(name='gamma', shape=input_shape[-1:], initializer=Ones(), trainable=True)
self.beta = self.add_weight(name='beta', shape=input_shape[-1:], initializer=Zeros(), trainable=True)
super().build(input_shape)
示例3: build
# 需要导入模块: from keras import initializers [as 别名]
# 或者: from keras.initializers import Zeros [as 别名]
def build(self, input_shape):
assert len(input_shape) >= 3
self.input_spec = [InputSpec(shape=input_shape)]
if not self.layer.built:
self.layer.build(input_shape)
self.layer.built = True
super(AttentionWrapper, self).build()
if hasattr(self.attention_vec, '_keras_shape'):
attention_dim = self.attention_vec._keras_shape[1]
else:
raise Exception(
'Layer could not be build: No information about expected input shape.')
kernel_initializer = self.layer.kernel_initializer
self.U_a = self.layer.add_weight((self.layer.units, self.layer.units), name='{}_U_a'.format(
self.name), initializer=kernel_initializer)
self.b_a = self.layer.add_weight(
(self.layer.units,), name='{}_b_a'.format(self.name), initializer=Zeros())
self.U_m = self.layer.add_weight((attention_dim, self.layer.units), name='{}_U_m'.format(
self.name), initializer=kernel_initializer)
self.b_m = self.layer.add_weight(
(self.layer.units,), name='{}_b_m'.format(self.name), initializer=Zeros())
if self.single_attention_param:
self.U_s = self.layer.add_weight((self.layer.units, 1), name='{}_U_s'.format(
self.name), initializer=kernel_initializer)
self.b_s = self.layer.add_weight(
(1,), name='{}_b_s'.format(self.name), initializer=Zeros())
else:
self.U_s = self.layer.add_weight((self.layer.units, self.layer.units), name='{}_U_s'.format(
self.name), initializer=kernel_initializer)
self.b_s = self.layer.add_weight(
(self.layer.units,), name='{}_b_s'.format(self.name), initializer=Zeros())
示例4: reset_states
# 需要导入模块: from keras import initializers [as 别名]
# 或者: from keras.initializers import Zeros [as 别名]
def reset_states(self, states_value=None):
if len(self.states) == 0:
return
if not self.stateful:
raise AttributeError('Layer must be stateful.')
if not hasattr(self, 'states') or self.states[0] is None:
state_shapes = list(map(K.int_shape, self.model.input[1:]))
self.states = list(map(K.zeros, state_shapes))
if states_value is not None:
if type(states_value) not in (list, tuple):
states_value = [states_value] * len(self.states)
assert len(states_value) == len(self.states), 'Your RNN has ' + str(len(self.states)) + ' states, but was provided ' + str(len(states_value)) + ' state values.'
if 'numpy' not in type(states_value[0]):
states_value = list(map(np.array, states_value))
if states_value[0].shape == tuple():
for state, val in zip(self.states, states_value):
K.set_value(state, K.get_value(state) * 0. + val)
else:
for state, val in zip(self.states, states_value):
K.set_value(state, val)
else:
if self.state_initializer:
for state, init in zip(self.states, self.state_initializer):
if isinstance(init, initializers.Zeros):
K.set_value(state, 0 * K.get_value(state))
else:
K.set_value(state, K.eval(init(K.get_value(state).shape)))
else:
for state in self.states:
K.set_value(state, 0 * K.get_value(state))
# EXECUTION
示例5: build
# 需要导入模块: from keras import initializers [as 别名]
# 或者: from keras.initializers import Zeros [as 别名]
def build(self, input_shape):
self.gamma = self.add_weight(name='gamma', shape=input_shape[-1:],
initializer=Ones(), trainable=True)
self.beta = self.add_weight(name='beta', shape=input_shape[-1:],
initializer=Zeros(), trainable=True)
super(LayerNormalization, self).build(input_shape)
示例6: get_initial_state
# 需要导入模块: from keras import initializers [as 别名]
# 或者: from keras.initializers import Zeros [as 别名]
def get_initial_state(self, inputs):
if type(self.model.input) is not list:
return []
try:
batch_size = K.int_shape(inputs)[0]
except:
batch_size = None
state_shapes = list(map(K.int_shape, self.model.input[1:]))
states = []
if self.readout:
state_shapes.pop()
# default value for initial_readout is handled in call()
for shape in state_shapes:
if None in shape[1:]:
raise Exception('Only the batch dimension of a state can be left unspecified. Got state with shape ' + str(shape))
if shape[0] is None:
ndim = K.ndim(inputs)
z = K.zeros_like(inputs)
slices = [slice(None)] + [0] * (ndim - 1)
z = z[slices] # (batch_size,)
state_ndim = len(shape)
z = K.reshape(z, (-1,) + (1,) * (state_ndim - 1))
z = K.tile(z, (1,) + tuple(shape[1:]))
states.append(z)
else:
states.append(K.zeros(shape))
state_initializer = self.state_initializer
if state_initializer:
# some initializers don't accept symbolic shapes
for i in range(len(state_shapes)):
if state_shapes[i][0] is None:
if hasattr(self, 'batch_size'):
state_shapes[i] = (self.batch_size,) + state_shapes[i][1:]
if None in state_shapes[i]:
state_shapes[i] = K.shape(states[i])
num_state_init = len(state_initializer)
num_state = self.num_states
assert num_state_init == num_state, 'RNN has ' + str(num_state) + ' states, but was provided ' + str(num_state_init) + ' state initializers.'
for i in range(len(states)):
init = state_initializer[i]
shape = state_shapes[i]
try:
if not isinstance(init, initializers.Zeros):
states[i] = init(shape)
except:
raise Exception('Seems the initializer ' + init.__class__.__name__ + ' does not support symbolic shapes(' + str(shape) + '). Try providing the full input shape (include batch dimension) for you RecurrentModel.')
return states
示例7: tp1_node_update
# 需要导入模块: from keras import initializers [as 别名]
# 或者: from keras.initializers import Zeros [as 别名]
def tp1_node_update(graph_node_embs, node_rel, node_rel_weight, max_nodes, max_bi_relations, embed_dim, label):
"""
graph_node_embs has shape (batch_size, max_nodes per graph, embed_dim feats).
"""
dense_dim = embed_dim
x = gather_layer([graph_node_embs, node_rel])
logging.debug('After gather3 shape: {0}'.format(x.shape))
x = Reshape((max_nodes * max_bi_relations, 2 * embed_dim))(x)
x = TimeDistributed(
Dense(
dense_dim,
kernel_initializer=initializers.Ones(),
bias_initializer=initializers.Zeros(),
name=label + '_dense1'))(x)
# TODO: re-enable the batch normalization.
# x = BatchNormalization(axis=2, name=label + '_bn1')(x)
x = Activation('relu')(x)
x = TimeDistributed(
Dense(
dense_dim,
kernel_initializer=initializers.Ones(),
bias_initializer=initializers.Zeros(),
name=label + '_dense2'))(x)
# x = BatchNormalization(axis=2, name=label + '_bn2')(x)
x = Activation('relu')(x)
normalizer = Reshape((max_nodes * max_bi_relations,))(node_rel_weight)
normalizer = RepeatVector(dense_dim)(normalizer)
normalizer = Permute((2, 1))(normalizer)
x = Multiply()([x, normalizer])
x = Reshape((max_nodes, max_bi_relations, dense_dim))(x)
x = Lambda(
lambda xin: K.sum(xin, axis=2),
output_shape=(None, max_nodes * max_bi_relations, dense_dim),
name=label + '_integrate')(x)
return x
# TODO: Dense use_bias=True