本文整理汇总了Python中keras.initializers.constant方法的典型用法代码示例。如果您正苦于以下问题:Python initializers.constant方法的具体用法?Python initializers.constant怎么用?Python initializers.constant使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.initializers
的用法示例。
在下文中一共展示了initializers.constant方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: nn_model
# 需要导入模块: from keras import initializers [as 别名]
# 或者: from keras.initializers import constant [as 别名]
def nn_model():
(x_train, y_train), _ = mnist.load_data()
# 归一化
x_train = x_train.reshape(x_train.shape[0], -1) / 255.
# one-hot
y_train = np_utils.to_categorical(y=y_train, num_classes=10)
# constant(value=1.)自定义常数,constant(value=1.)===one()
# 创建模型:输入784个神经元,输出10个神经元
model = Sequential([
Dense(units=200, input_dim=784, bias_initializer=constant(value=1.), activation=tanh),
Dense(units=100, bias_initializer=one(), activation=tanh),
Dense(units=10, bias_initializer=one(), activation=softmax),
])
opt = SGD(lr=0.2, clipnorm=1.) # 优化器
model.compile(optimizer=opt, loss=categorical_crossentropy, metrics=['acc', 'mae']) # 编译
model.fit(x_train, y_train, batch_size=64, epochs=20, callbacks=[RemoteMonitor()])
model_save(model, './model.h5')
示例2: build
# 需要导入模块: from keras import initializers [as 别名]
# 或者: from keras.initializers import constant [as 别名]
def build(self, input_shape):
assert len(input_shape) == 5, "The input Tensor should have shape=[None, input_height, input_width," \
" input_num_capsule, input_num_atoms]"
self.input_height = input_shape[1]
self.input_width = input_shape[2]
self.input_num_capsule = input_shape[3]
self.input_num_atoms = input_shape[4]
# Transform matrix
self.W = self.add_weight(shape=[self.input_num_atoms, self.kernel_size, self.kernel_size, 1, self.num_capsule * self.num_atoms],
initializer=self.kernel_initializer,
name='W')
self.b = self.add_weight(shape=[self.num_capsule, self.num_atoms, 1, 1],
initializer=initializers.constant(0.1),
name='b')
self.built = True
示例3: build
# 需要导入模块: from keras import initializers [as 别名]
# 或者: from keras.initializers import constant [as 别名]
def build(self, input_shape):
assert len(input_shape) == 5, "The input Tensor should have shape=[None, input_height, input_width," \
" input_num_capsule, input_num_atoms]"
self.input_height = input_shape[1]
self.input_width = input_shape[2]
self.input_num_capsule = input_shape[3]
self.input_num_atoms = input_shape[4]
# Transform matrix
self.W = self.add_weight(shape=[self.kernel_size, self.kernel_size,
self.input_num_atoms, self.num_capsule * self.num_atoms],
initializer=self.kernel_initializer,
name='W')
self.b = self.add_weight(shape=[1, 1, self.num_capsule, self.num_atoms],
initializer=initializers.constant(0.1),
name='b')
self.built = True
示例4: build
# 需要导入模块: from keras import initializers [as 别名]
# 或者: from keras.initializers import constant [as 别名]
def build(self, input_shape: list):
"""
Build the layer.
:param input_shape: the shapes of the input tensors,
for MatchingTensorLayer we need two input tensors.
"""
# Used purely for shape validation.
if not isinstance(input_shape, list) or len(input_shape) != 2:
raise ValueError('A `MatchingTensorLayer` 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}.'
)
if self._init_diag:
interaction_matrix = np.float32(
np.random.uniform(
-0.05, 0.05,
[self._channels, self._shape1[2], self._shape2[2]]
)
)
for channel_index in range(self._channels):
np.fill_diagonal(interaction_matrix[channel_index], 0.1)
self.interaction_matrix = self.add_weight(
name='interaction_matrix',
shape=(self._channels, self._shape1[2], self._shape2[2]),
initializer=constant(interaction_matrix),
trainable=True
)
else:
self.interaction_matrix = self.add_weight(
name='interaction_matrix',
shape=(self._channels, self._shape1[2], self._shape2[2]),
initializer='uniform',
trainable=True
)
super(MatchingTensorLayer, self).build(input_shape)
示例5: update_routing
# 需要导入模块: from keras import initializers [as 别名]
# 或者: from keras.initializers import constant [as 别名]
def update_routing(votes, biases, logit_shape, num_dims, input_dim, output_dim,
num_routing):
if num_dims == 6:
votes_t_shape = [3, 0, 1, 2, 4, 5]
r_t_shape = [1, 2, 3, 0, 4, 5]
elif num_dims == 4:
votes_t_shape = [3, 0, 1, 2]
r_t_shape = [1, 2, 3, 0]
else:
raise NotImplementedError('Not implemented')
votes_trans = tf.transpose(votes, votes_t_shape)
_, _, _, height, width, caps = votes_trans.get_shape()
def _body(i, logits, activations):
"""Routing while loop."""
# route: [batch, input_dim, output_dim, ...]
a,b,c,d,e = logits.get_shape()
a = logit_shape[0]
b = logit_shape[1]
c = logit_shape[2]
d = logit_shape[3]
e = logit_shape[4]
print(logit_shape)
logit_temp = tf.reshape(logits, [a,b,-1])
route_temp = tf.nn.softmax(logit_temp, dim=-1)
route = tf.reshape(route_temp, [a, b, c, d, e])
preactivate_unrolled = route * votes_trans
preact_trans = tf.transpose(preactivate_unrolled, r_t_shape)
preactivate = tf.reduce_sum(preact_trans, axis=1) + biases
# activation = _squash(preactivate)
activation = squash(preactivate, axis=[-1, -2, -3])
activations = activations.write(i, activation)
act_3d = K.expand_dims(activation, 1)
tile_shape = np.ones(num_dims, dtype=np.int32).tolist()
tile_shape[1] = input_dim
act_replicated = tf.tile(act_3d, tile_shape)
distances = tf.reduce_sum(votes * act_replicated, axis=3)
logits += distances
return (i + 1, logits, activations)
activations = tf.TensorArray(
dtype=tf.float32, size=num_routing, clear_after_read=False)
logits = tf.fill(logit_shape, 0.0)
i = tf.constant(0, dtype=tf.int32)
_, logits, activations = tf.while_loop(
lambda i, logits, activations: i < num_routing,
_body,
loop_vars=[i, logits, activations],
swap_memory=True)
a = K.cast(activations.read(num_routing - 1), dtype='float32')
return K.cast(activations.read(num_routing - 1), dtype='float32')
示例6: update_routing
# 需要导入模块: from keras import initializers [as 别名]
# 或者: from keras.initializers import constant [as 别名]
def update_routing(votes, biases, logit_shape, num_dims, input_dim, output_dim,
num_routing):
if num_dims == 6:
votes_t_shape = [5, 0, 1, 2, 3, 4]
r_t_shape = [1, 2, 3, 4, 5, 0]
elif num_dims == 4:
votes_t_shape = [3, 0, 1, 2]
r_t_shape = [1, 2, 3, 0]
else:
raise NotImplementedError('Not implemented')
votes_trans = tf.transpose(votes, votes_t_shape)
_, _, _, height, width, caps = votes_trans.get_shape()
def _body(i, logits, activations):
"""Routing while loop."""
# route: [batch, input_dim, output_dim, ...]
route = tf.nn.softmax(logits, dim=-1)
preactivate_unrolled = route * votes_trans
preact_trans = tf.transpose(preactivate_unrolled, r_t_shape)
preactivate = tf.reduce_sum(preact_trans, axis=1) + biases
activation = _squash(preactivate)
activations = activations.write(i, activation)
act_3d = K.expand_dims(activation, 1)
tile_shape = np.ones(num_dims, dtype=np.int32).tolist()
tile_shape[1] = input_dim
act_replicated = tf.tile(act_3d, tile_shape)
distances = tf.reduce_sum(votes * act_replicated, axis=-1)
logits += distances
return (i + 1, logits, activations)
activations = tf.TensorArray(
dtype=tf.float32, size=num_routing, clear_after_read=False)
logits = tf.fill(logit_shape, 0.0)
i = tf.constant(0, dtype=tf.int32)
_, logits, activations = tf.while_loop(
lambda i, logits, activations: i < num_routing,
_body,
loop_vars=[i, logits, activations],
swap_memory=True)
return K.cast(activations.read(num_routing - 1), dtype='float32')